hi i need help with my nim game. everytime i play another game, the game number does not change. for example, if it is the second game, it does not display "game #2," it still reads "game#1."
also sometimes this occurs. when there are three stones left and i remove one, it removes one but it also removes one again.
//Anh Tran
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
srand ((unsigned)time(0));//random number seeding
char again;
int gamenumber();
int playerstone;
int computerstone;
int numberofstonesinpile();
int winner();
do
{
int currentstonesinpile = numberofstonesinpile();
cout << "Welcome to the Nim Game #" << gamenumber() << endl;
cout << "There are " << currentstonesinpile << "stones in the pile" << endl;
cout << "You only allow to remove 1, 2, or 3 stones at a time." << endl;
while(currentstonesinpile > 0)
{
cout << "How many stones would you like to remove?" << endl;
cin >> playerstone;
currentstonesinpile = currentstonesinpile - playerstone;
if (currentstonesinpile==0)
{
cout << "You Lose" <<endl;
break;
}
if (playerstone > 3)
{ //the game restarts if the player chooses more than 3 stones.
cout << "Please only remove 1, 2 , or 3 stones at a time. " << endl;
break;
}
if (currentstonesinpile == 2)//computer cannot take more than what is left in the pile
{
computerstone = rand() %2 +1;
currentstonesinpile = currentstonesinpile - computerstone;
cout << "I removed " << computerstone << "." << "There are " << currentstonesinpile << "left in the pile" << endl;
if (computerstone==1)
{
//if the computer removes 1, there is one left for the player to take.
cout << "I removed " << computerstone << "." << "There are " << currentstonesinpile << "left in the pile" << endl;
}
elseif (computerstone==2)
{
//currentstonesinpile=0;
//if the computer removes 2 stones, there is no stone left. Therefore, the player wins.
cout << "You Won" << " " << winner() << " " << "game(s)"<< endl;
}
}
if (currentstonesinpile == 1)
{
//if there is only one stone left, the computer must take the last stone and cannot go above one.
computerstone = 1 ;
currentstonesinpile = currentstonesinpile - computerstone;
cout << "I removed " << computerstone << "." << "There are " << currentstonesinpile << "left in the pile" << endl;
cout << "You Won " << " " << winner() << " " << " game(s)"<< endl;
break;
}
elseif(playerstone <= 3 )
{computerstone = rand() % 3 + 1;
currentstonesinpile = currentstonesinpile - computerstone;
if (currentstonesinpile<=0)
{// the number of stones removed shoulbe be displayed as 0 instead of a negative number
currentstonesinpile=0;
cout << "Congratulations. You win!" << endl;
cout << "You Won " << winner() << " game(s)"<< endl;
}
cout << "I removed " << computerstone << " stone(s)." << "There are " << currentstonesinpile << " stone(s) left in the pile" << endl;
}
}//while
cout << "would you like to play again? Y/N " << endl;
cin >> again;
}//do
while ( again !='n' && again!='N');
cout << "Have a nice day!" << endl;
return (0);
}
int gamenumber()
{
int k =0;
return ++k;
}
int numberofstonesinpile()
{
int b = rand() %6 +10;
return b;
}
int winner()
{
int winner =0;
return ++winner;
}
maybe you should deklare k (if that is the nuber of games) befor the main do-while, because every time the program gets to the int gamenuber it will return k to 0, and the same for winner
thanks i have fixed that problem. thank you for both of your help.
i also have this problem where after i removed a stone i get this.
1 2 3 4 5 6 7
I removed 2.There are 0left in the pile
You Won 1 game(s)
Congratulations. You win!
You Won 2 game(s)
I removed 1 stone(s).There are 0 stone(s) left in the pile
would you like to play again? Y/N
I removed 2.There are 0left in the pile
You Won 1 game(s)
Congratulations. You win!
You Won 2 game(s)
I removed 1 stone(s).There are 0 stone(s) left in the pile
would you like to play again? Y/N
no i solved that part. both the part where i had problems with increments are fixed. its just there are some problems within the main part of the coding. i forgot to mention about this part that i needed to include.
i was suppose to have a function prototype, where there is an upperlimit.
pa mogao je misliti na milion stvari, ali evo sta bih ja uradio
int myRandom(int uperlimit){
int c(rand());
while(c>uperlimit)
c=rand();
return c;
}
and u can define the uper limit=3 if the stone pile is >=3, or 2 if the stone pile is 2.
in that way u don't need some special cases u have in your main()
EDIT
what is going on in that fragment:
you create a local variable c and give it a random value, then you chek if it is under the limit if not you take another random number and so on after that you return the value of c ...