I am having one issue with my project. We are making a game of Nim code.I'm 99% done with it, i worked hard on it and i feel like i did a good job, however my project is not displaying the matches i want. For example, it display's the inital number of them 23. But once the first player subtracts a number, it doesn't display matches for player 2, just the number of matches remaining. Then as i keep running the program the same thing happens. This is what it's supposed to ouput
Input/Output sample
WELCOME TO NIM
------- -- ---
Enter the starting player's name (no spaces)-->John
Enter the second player's name (no spaces)-->Mary
There are 23 matches.
ooooooooooooooooooooooo
|||||||||||||||||||||||
Player John please enter the number of matches to remove-->2
There are 21 matches.
ooooooooooooooooooooo
|||||||||||||||||||||
Player Mary please enter the number of matches to remove-->3
There are 18 matches.
oooooooooooooooooo
||||||||||||||||||
Player John please enter the number of matches to remove-->1
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
// Holds variables.
string player1;
string player2;
int topMatch = 23;
int bottomMatch = 23;
constint total = 23;
int n, mtotal;
bool winner = false;
mtotal = total;
// Display's Welcome Message to user.
cout<<"WELCOME TO NIM"<<endl;
cout<<"------- -- ---"<<endl;
// Skips a line.
cout<<endl;
// Asks player 1 to enter their name.
cout<<"Enter the starting player's name (no spaces) -->";
cin>>player1;
// Skips a line.
cout<<endl;
// Asks player 2 to enter their name.
cout<<"Enter the second player's name (no spaces) -->";
cin>>player2;
// Skips a line.
cout<<endl;
// Defines the number of matches.
topMatch=23, bottomMatch=23;
// Beginning of the code.
while (!winner)
{
if(mtotal >= 0)
// Display's message with the total number of matches.
cout<<"There are "<<mtotal<< " matches." <<endl;
// Display's matches.
while (topMatch > 0)
{
cout<<"o";
topMatch--;
}
cout<<endl;
while (bottomMatch > 0)
{
cout<<"|";
bottomMatch--;
}
cout<<endl<<endl;
// Ask's player 1 to enter the number of matches he wants to remove.
cout<<"Player "<<player1<<" please enter the number of matches to remove-->";
cin>> n;
cout<<endl;
// Removes only one match if the player enter a number lower than one and higher than 3.
if ( n<1 || n>3)
{
n=1;
}
// Removes 1 to 3 matches depending on the number entered.
if ( n >=1 && n <=3)
{
mtotal -= n;
}
// Equation that removes "matches".
topMatch=topMatch-n;
bottomMatch= bottomMatch -n;
// Removes the number of matches the user inputs.
while (topMatch > 0)
{
cout<<"o";
topMatch--;
}
cout<<endl;
while (bottomMatch > 0)
{
cout<<"|";
bottomMatch--;
}
// Display's Message if player is the winner.
if (mtotal==0 || mtotal<=0)
{winner = true;
cout<<"Game over. Player "<<player2<<" is the winner!"<<endl;
cin.get ( ); cin.get ( );
}
// Beginning of player 2 coding.
if(mtotal >= 0)
// Display's the number of matches remaining after player 1 turn.
cout<<"There are "<<mtotal<<" matches"<<endl<<endl;
// Equation that removes "matches".
topMatch=topMatch-n;
bottomMatch= bottomMatch -n;
// Removes the number of matches the user inputs.
while (topMatch > 0)
{
cout<<"o";
topMatch--;
}
cout<<endl;
while (bottomMatch > 0)
{
cout<<"|";
bottomMatch--;
}
// Asks player 2 to remove matches.
cout<<"Player "<<player2<<" please enter the number of matches to remove-->";
cin>>n;
cout<<endl;
// Removes only one match if the player enter a number lower than one and higher than 3.
if ( 1>n || n>3)
{
n=1;
}
// Equation that removes "matches".
topMatch=topMatch-n;
bottomMatch= bottomMatch-n;
// Removes the number of matches the user inputs.
while (topMatch > 0)
{
cout<<"o";
topMatch--;
}
cout<<endl;
while (bottomMatch > 0)
{
cout<<"|";
bottomMatch--;
}
// Removes number of matches entered as long as it's a number between 1 and 3.
if ( n >=1 && n <=3)
// Display's the number of matches removed from player 2.
{
mtotal -= n;}
// Display's congratulation message if player 2 has won.
if (mtotal==0 || mtotal<=0)
{winner = true;
cout<<"Game over. Player "<<player1<<" is the winner"<<endl;
cin.get ( ); cin.get ( );
}
}
cin.get ( ); cin.get ( );
}