This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes General Computing and the fly likes need help with C++ problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » General Computing
Bookmark "need help with C++ problem" Watch "need help with C++ problem" New topic
Author

need help with C++ problem

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i hope someone here is a C++ programmer. I love/hate C++
this program should only let players roll 3 times, it used to do that, but it doesn't now.

#include "ShipCaptainMateCrew.h"
ShipCaptainMateCrew::ShipCaptainMateCrew()
{
wins = 0;
losses = 0;
}
void ShipCaptainMateCrew: lay()
{
int i; //loop counter
int myScore;
int yourScore;
int rolls;
int dice ;
int roll[5];
char response;
bool quit = false;
bool ship;
bool captain;
bool mate;
do
{
myScore = 0;
yourScore = 0;
//players turn...parts of this should be in another function maybe
rolls = 3;
dice = 5;
ship = false;
captain = false;
mate = false;
do
{
cout << "Press ENTER to roll the dice." << endl;
getch();
if (ship == true)
cout << "You have your ship\n";
if (captain == true)
cout << "You have your captain\n";
if (mate == true)
cout << "You have your mate\n";
cout << "You rolled ";
rolls = player.roll(roll, dice);
//is there a six?
for (i = 0; i < dice; i++)
{
if ((roll[i] == 6) && (ship == false))
{
cout << "You got your ship.\n";
cout << "Do you wish to keep it? y/n";
cin >> response;
if (response == 'y' || response == 'Y')
{
ship = true;
dice--;
}
}
}
if ((ship == true) && (captain == false))
{
//is there a five?
for (i = 0; i < dice; i++)
{
if ((roll[i] == 5) && (captain == false))
{
cout << "You got your captain.\n";
cout << "Do you wish to keep him? y/n";
cin >> response;
if (response == 'y' || response == 'Y')
{
captain = true;
dice--;
}
}
}
}
if ((captain == true) && (mate == false))
{
//is there a four?
for (i = 0; i < dice; i++)
{
if ((roll[i] == 4) && (mate == false))
{
cout << "You got your mate.\n";
cout << "Do you wish to keep him? y/n";
cin >> response;
if (response == 'y' || response == 'Y')
{
mate = true;
dice--;
}
}
}
}
if (mate == true)
{
if (rolls != 0)
{
if ( dice > 1)
{
cout << "The second die is a " << roll[1]
<< ". Do you want to keep it? y/n";
cin >> response;
if (response == 'y' || response == 'Y')
dice--;
}
if (dice > 0)
{
cout << "The first die is a " << roll[0]
<< ". Do you want to keep it? y/n";
cin >> response;
if (response == 'y' || response == 'Y')
dice--;
}
if (dice == 0)
{
yourScore = roll[0] + roll[1];
cout << "Your score is " << yourScore << endl;
}
}
else
{
yourScore = roll[0] + roll[1];
cout << "Your score is " << yourScore << endl;
}
}
}
while ((rolls != 0) || (dice != 0));
//computers turn
rolls = 3;
dice = 5;
ship = false;
captain = false;
mate = false;
do
{
if (ship == true)
cout << "I have my ship\n";
if (captain == true)
cout << "I have my captain\n";
if (mate == true)
cout << "I have my mate\n";
cout << "I rolled ";
rolls = computer.roll(roll, dice);
//is there a six?
for (i = 0; i < dice; i++)
{
if ((roll[i] == 6) && (ship == false))
{
cout << "I got my ship.\n";
ship = true;
dice--;
}
}
if ((ship == true) && (captain == false))
{
//is there a five?
for (i = 0; i < dice; i++)
{
if ((roll[i] == 5) && (captain == false))
{
cout << "I got my captain.\n";
captain = true;
dice--;
}
}
}
if ((captain == true) && (mate == false))
{
//is there a five?
for (i = 0; i < dice; i++)
{
if ((roll[i] == 4) && (mate == false))
{
cout << "I got my mate.\n";
mate = true;
dice--;
}
}
}
if (mate == true)
{
if (rolls != 0)
{
if ( dice > 1)
if (roll[1] > 3)
dice--;
if ( dice > 0)
if (roll[0] > 3)
dice--;
if (dice == 0)
{
myScore = roll[0] + roll[1];
cout << "My score is " << myScore << endl;
}
}
else
{
myScore = roll[0] + roll[1];
cout << "My score is " << myScore << endl;
}
}
}
while ((rolls != 0) || (dice != 0));
//game over
cout << "Your score was " << yourScore << endl;
cout << "My score was " << myScore << endl;
if (myScore > yourScore)
{
cout << "I win!" << endl;
losses++;
}
else if (yourScore > myScore)
{
cout << "You win!" << endl;
wins++;
}
else
{
cout << "We tied!" << endl;
}
if (wins == 1)
cout << "You have won " << wins << " time" << endl;
else
cout << "You have won " << wins << " times" << endl;
if (losses == 1)
cout << "I have won " << losses << " time" << endl;
else
cout << "I have won " << wins << " times" << endl;
cout << "Would you like to play again? y/n" << endl;
cin >> response;
if (response == 'n' || response == 'N')
quit = true;
}
while (quit == false);
cout << "You won " << wins << " times." << endl;
cout << "You lost " << losses << " times." << endl;
//why insult the player's intelligence?
//cout << "I won " << losses << " times." << endl;
//cout << "I lost " << wins << " times." << endl;
}

the call to roll() decrements rolls. at least it is supposed to.

int Player::roll(int roll[], int elements)
{
for (int i = 0; i < elements; i++)
roll[i] = die[i].getDiePoint();
stuff.sort(roll, elements);
stuff.printArray(roll, elements);
rolls--;
return rolls;
}


SCJP
Visit my download page
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

How come a "bartender" isn't using *code* tags when posting code?


Please ignore post, I have no idea what I am talking about.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I am looking at it now to see what the problem may be. You might want to post this question at http://forums.embeddedthought.com.
We have forums specifically for C/C++.


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Is there any way you could post the HEADER file that you included? That way I can do some testing to find out what your problem is.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i dont like how the code tag makes the text small....i think i found the problem
while ((rolls != 0) || (dice != 0));
should be
while ((rolls != 0) && (dice != 0));
ill try it when i get home, i shouldnt write code at 2 AM .............
actually it is hard to read without the indentation, i will use code tag next time
[ May 14, 2003: Message edited by: Randall Twede ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Glad you might have figured it out. Let me know if that worked out for you.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

greg,
that was part of the problem. i also got rid of the rolls variable in the Player class. i should have been suspicious when the call to the function returned the value of Player.rolls and asssigned it to the rolls variable of the calling class.
there were a few other minor problems to iron out as well.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, I am glad you got it ironed out. Way to go!!
 
GeeCON Prague 2014
 
subject: need help with C++ problem