nim game

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.

thanks, any help would be great.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//Anh Tran

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace 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;
					   }
					   else if (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;
               }
                   
       else if(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;
}

closed account (jwC5fSEw)
That would be because in your function gamenumber() you're setting k to zero, incrementing it and returning it.
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


post the code again pleas...
this is what is displayed sometimes

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
try puting it

winner++
return winner

I'm not quite sure, but it could solve the problem
and u could make a function for the computer stone,

somtihng that will use rand(), but in the limits betwean 1 and 3, (and the curent stones in pile status) that would make the code much shorter...
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.

example: int myRandom(int upperLimit);

my professor explained it but not quite well.
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 ...
Last edited on
where would i put that though? can i do that with my numberofstonesinpile() function prototype?

EDIT:

just read your edited post
Last edited on
than u know the answer :)

hope it works :)
where you recommend me to implement that though?
yes, highli
i mean look

you can change a lot of your main code by just doing this
1
2
3
4
5
6
7
8
9
10
11
12
uplimit==3
            ..
             ... 
              if(currentstonesinpile<3)
              uplimit==currentstonesinpile;
               .
               .
               .
               computerstone=Computerstone(int uplimit)
                .
                .
                 . 


it will shorten the main, and you will not have much repeating code.
(the dots represent some code u will write there)
Last edited on
Topic archived. No new replies allowed.