Code:
#include<iostream>
#include<ctime>
using namespace std;
int pile = 20;
int humanMarbles;
void gameStart();
bool evaluate(int x);
int main()
{
gameStart();
cout << "Pile is set to " << pile << endl;
cout << "Your turn";
cin >> humanMarbles;
if (evaluate(humanMarbles) == true);
{
pile = pile - humanMarbles;
cout << "Pile is set to " << pile;
}
//system("pause");
return 0;
}
void gameStart()
{
//system("cls");
cout << "This is a Nim Game implementation \n"
"--------------------------------- \n"
"you play against the computer taking turns removing marbles from a marble pile \n"
"you may take 1 up to pile/2. The one who takes the last marble loses \n";
cout << endl << endl;
//srand(static_cast<unsigned int>(time(0)));
//pile = rand() % 100 + 1;
return;
}
bool evaluate(int x)
{
bool check;
if (x <= pile/2 && x>=1)
{
check = true;
}
else
{
cout << "Cannot take marbles";
check = false;
}
return check;
}
I am trying to implement a nim game where the computer and a player take turns in removing marbles from a pile, the last one to take a marble loses. I came across this problem. I am using an evaluate fuction to determine whether the user inputs a number inside the desired interval(1,pile/2). I does not work correctly though. The program will subtract the marble inputed by the user whether it is inside the interval or not.
Also the lines highlighted as comment cause a compiler error in a platform different than dev c++. Implicit declaration of fiction...What is that about?!