Help With Game

I've been stuck on this problem for like an hour and am desperate :(
an its due tomorrow.
I'm working on the game of Nim and this is my code so far.
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
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <cmath>
using namespace std;
bool deadNum(int n)
{
	int i = -1;
	while ((int)pow(2.0, ++i) < n)
	return ((int)pow(2.0, i) == n + 1);
}
int compute(int n)
{
	int i = -1;
	while ((int)pow(2.0, ++i) < n)
	i--;
	return n - ((int)pow(2.0, i) - 1);
}
int main()
{
	/*
		Game of Nim
		Variables
		pile = amount of marbles 
		Turn = Whose turn it is
		compSub & playerSub = Amount taken from pile from either computer or player
	*/
	int pile, turn, compSub, playerSub, mode;
	const int MIN = 10;
	const int MAX = 100;
	// Initializes the random number seed
	srand(time(0));
	cout << "\t\t\t  -----------------\n";
	cout << "\t\t\t  |The Game of Nim|\n";
	cout << "\t\t\t  |  By 1337Hax0r |\n";
	cout << "\t\t\t  -----------------\n";
	cout << "\n\n\n";
	cout << "\t\t\tWelcome to The Game of Nim\n";
	cout << "Instructions:  \n";
	cout << "Take 'marbles from pile' \nYou must take at least one marble \nYou can't take more than half of the remaining pile \nWhomever takes the last marble loses\nGood Luck !\n";
	turn = rand() % 2;
	if (turn == 0)
	{
		cout << "The Computer goes first.\n";
	}
	else
	{
		cout << "You go first.\n";
	}
	pile = MIN + rand() % (MAX - MIN);
	mode = rand() % 2;
	cout << "The initial amount of marbles is " << pile << ".\n";
	if (mode == 0)
	{
		cout << "The computer will play stupid.\n";
		while (pile != 0)
		{
			if (turn == 0)
			{
				compSub = 1 + rand() % (pile/ 2);
				pile -= compSub;
				cout << "The computer took off  " << compSub << " marbles.\n";
				cout << "There are now " << pile << "marbles left.\n\n\n";
			}
			else
			{
				do {
					cout << "\nYou can take away " << pile / 2 << " marbles.\n";
					cout << "How many marbles do you want to take off?";
					cin >> playerSub;
				} while (playerSub <= 0 || playerSub > pile / 2);
				
				pile -= playerSub;
				cout << "There are currently " << pile << "marbles left.\n\n";
			}
			turn = turn == 0 ? 1 : 0;
			}
		}
	compSub = compute(pile);
	pile -= compSub;
	cout << "The computer took " << compSub << " marbles.\n";
	cout << "There are currently " << pile << "marbles left.\n\n";
	else{
		do{
			cout << "How many marbles do you want to take?";
			cin >> playerSub;
		} while (playerSub <= 0 || playerSub > pile / 2);
		pile -= playerSub;
		cout << " There are currently " << pile << " marbles left.\n\n\n";
	}
	turn = turn == 0 ? 1 : 0;
	if (turn == 1)
	{
		cout << "The computer won!";
	}
	else
	{
		cout << "You won!";
	}
	return 0;
}
closed account (j3Rz8vqX)
What in particular do you need help with?
(Your question?)
I was just wondering if you guys saw any bugs?
closed account (j3Rz8vqX)
One too many '}' for your first while loop.
Delete line 77.

Also your function deadNum, seems to be complaining about have a return.
Possibly the while loop may be initiating the return function below it?
Possibly apply {} after the while loop? Unless you intend to apply return during each of your while-loops cycle; if so add a return after your already implemented return.
Last edited on
1
2
while ((int)pow(2.0, ++i) < n)
	return ((int)pow(2.0, i) == n + 1);


You can't return multiple times.
@giblit
how would i fix that?
Well what exactly are you trying to do? If you want to increment i until i2 is greater than or equal to n then you want a semi colon on that line.
Topic archived. No new replies allowed.