Nim game. small issue.

So below is my nim game. Basically pick up sticks...

my problem is that when the "pile" gets low, even if you enter less than the last remaining pile size the cout message "That is more sticks than is remain in the pile!" pops up??

if you run and play it in VS command prompt you'll see what I mean.

not sure if the line just needs to be moved or...



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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	// Initial variable declaration.
	int pile = 13;
	int p1Sticks;
	int p2Sticks;
	int curPile = pile;
	string player1;
	string player2;
	
	//Welcome message and rules.
	cout << "\t\t\t~Welcome to Nim!~" << endl << endl;
	cout << "\tThe object of the game is to be the player to pick up the last stick" << endl;
	cout << "\t\tYou may pick up between 1 and 4 sticks per turn." << endl;
	cout << "\tRemember the first to be the last wins so play with a strategy!" << endl << endl;
	
	// Enter Player names.
	cout << "Player one please enter your name: ";
	cin >> player1;
	cout << endl << endl;
	
	cout << "Player two please enter your name: ";
	cin >> player2;
	cout << endl << endl;
	
	cout << "Thank you " << player1 << " and " << player2 << "!" << endl << endl;
	
	//Do-While loop for sticks remaining in pile.
	do
	{


				//Do-While loop for each player one turn.
				do
				{
				
					cout << player1 << " how many sticks would you like to pick up? ";
					cin >> p1Sticks;
						curPile = (curPile - p1Sticks);
						
							//if conditions for game parameters player 1.
							if (p1Sticks < 1){
							cout << "You must opt to pick up at least one stick!" << endl;}
							if (p1Sticks > 4){
							cout << "Four is the maximum amount of sticks per turn!" << endl;}
							if (curPile < p1Sticks){
							cout << "That is more sticks than is remain in the pile!" << endl;}
						
					if (curPile ==0){
					cout << player1 << " You win! Congratulations!" << endl << endl;
					return 103;
					}
				
					else{
					cout << "There are " << curPile << " sticks remaining " << player1 << "!" << endl << endl;
					break;
					}
					
					
				
				}while (curPile != 0);
				
					
			
				//Do-While loop for each player two turn.
				do
				{
					cout << player2 << " how many sticks would you like to pick up? ";
					cin >> p2Sticks;
						curPile = (curPile - p2Sticks);
						
						
							//if conditions for game parameters player 2.
							if (p2Sticks < 1){
							cout << "You must opt to pick up at least one stick!" << endl;}
							if (p2Sticks > 4){
							cout << "Four is the maximum amount of sticks per turn!" << endl;}
							if (curPile < p2Sticks){
							cout << "That is more sticks than is remain in the pile!" << endl;}
						
					if (curPile ==0){
					cout << player2 << " You win! Congratulations!" << endl << endl;
					return 103;
					}
					
					else {
					cout << "There are " << curPile << " sticks remaining " << player2 <<"!" << endl << endl;
					break;
					}
					
				}while (curPile != 0);	
	
	}while (curPile != 0);
	
	cout << "Play Again!";
	
	return 0;
	
}
on line 44 you subtract the number of sticks the player chose... BEFORE verifying to make sure they chose a legal amount.


Same problem with line 75.

In fact, you seem to be duplicating a lot of code. Did you learn about functions yet?
Last edited on
No, the furthest we've gotten is basic arrays.

Thank you for catching that for me.

Topic archived. No new replies allowed.