#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <cmath>
usingnamespace 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;
constint MIN = 10;
constint 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;
}
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.