NIM GAME

I began taking C++, most of the assignments that I had been given so far were easy for me except this one. This program is a NIM game, but the user plays against the computer and the computer always has to win. So far I understood that what ever the user inputs 4 must be subtracted from it for the computer to make the choice and in the end win. there are 13 stones. This is what I got so far. NOTE: I know how to validate the function. The problem is I don't know where to even start. A guide will be helpful. Thanks.
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
  #include <iostream>
  using namespace std;

  // function protypes
  int Player1Pick(int stones_in_pile, int last_taken_p2);
  int Player2Pick(int stones_in_pile);
  bool ValidPick(int num_of_stones);
  
 int main()
{
	int stones_left = 13, P1Taken, P2Taken;
	
	cout << "Welcome, you have been choosen to play a game with 13 stones,  otherwise known as the NIM game\n."
	     << " You'll be playing against I, the MIGHTY COMPUTER. The object of this game is to take 1,2 or 3\n"
	     << " stones from the said pile. The player that manages to remove the last stone wins this game.\n "
	     << " You're going to need all the luck from the world to win. Because I'll never lose!!!>:)";
	
   do{
       logic to play the game goes here

	
	
	
	
      }while (stones_left>0);
	
	return 0;
}
///////////////////////////////////////////////////////////////////
int Player1Pick(int stones_in_pile, int last_taken_p2)
{
      // the computer picks a stone here and returns to that number	
	
}
///////////////////////////////////////////////////////////////////
int Player2Pick(int stones_in_pile)
{
     //prompt user to pick his stones, call valid pick function, force valid input from the user and return the number of stones the player picked	
	
	
}
/////////////////////////////////////////////////////////////////////
bool ValidPick(int num_of_stones)
{
	// validate that the user inputed 1, 2 , or 3
	// return true if ok, false if not
}

Last edited on
So do you understand the logic behind how the computer would win every time with perfect play? basically whoever picks the 5th stone can win the game (edit: well actually the 1st because then you control who can get to 5 and subsequently 9 and then 13 for the win.) not sure if that was already explained or if you already figured that part out.
an example
make it so:
1. when stonesleft is 13, the computer plays 1. (12 left then)
2. user plays 1, 2 or 3 (stonesleft is between 11 and 9)
3. computer plays stonesleft - 8 (stonesleft is now 8)
4. user plays 1,2 or 3 (stonesleft is now between 7 and 5)
5. computer plays stonesleft - 4 (stonesleft is now 4)
6. user plays 1,2 or 3 again (stonesleft is now between 3 and 1)
7. computer wins with a final stonesleft - 0 aka just stones left

didn't know how much help you wanted with the coding itself, if you wanted to make it more general you could use do a program with 1 + (4 mod (last pick)) and loop that. let us know what you're thinking
Last edited on
I might have it figured it out, though the problem I'm having is with the validation the last part. Can I use if statements or do I have to use something like bool check to make it work.

Topic archived. No new replies allowed.