1. NIM is a game in which two players take turns removing objects from a collection of objects. A player can only take 1, 2, or 3 object(s) per turn.. The game is commonly played with things like toothpicks. The player taking the last object loses.
assignment due tmrw i hope if someone could help me , i am not sure if b,c,d are correct
#include<iostream>
using namespace std;
int main ()
{
int objects,remove;
do
{ cout<<"Welcome to NIM game!\nEnter the initial number of objects to play: "<<endl;
cin>>objects; // here we input the initial number (a)
while(objects<1 &&objects!=-1 ) //the total of objects can't be less than 1
{ cout<<"This is not a valid value.\nEnter a number greater than 1"<<endl;
cin>>objects;}
while(objects>1 && objects!=2) //
{
{cout<<"There is "<<objects<<" object(s).\nEnter the amount(between 1 and 3) that you would like to remove"<<endl;
cin>>remove;}
c. rule: only 1, 2, or 3 object(s) taken per turn (only allow 1 or 2 or 3 objects to be taken not more).
if(remove>=1 && remove<=3)
{ cout<<"You have removed "<<remove<<" objects"<<endl;
objects-=remove;} b. The game should display how many objects remain in the collection after each players’turn.
else
{ cout<<"This is not an acceptable value.\nEnter a value between 1 and 3"<<endl;
cin>>remove;}
{objects-=remove;
}
d. Make sure that there is enough objects at each turn (a player cannot take 3 objects when the number of remaining objects is only 2)
while(objects==2)
{ remove<=2;
cout<<"There is "<<objects<<" objects.\nEnter the amount(between 1 and 3) that you would like to remove"<<endl;
cin>>remove;
objects-=remove;}
e. rule: player taking last object loses (the game should stop when it is over)
if((objects<2 &&objects!=0) ||objects==1)
{cout<<"There is "<<objects<<" objects.\nOne object left...Next player lost\nGame Over\nPress -1 to quit or enter the number of objects to play again "<<endl;
cin>>objects;}
else if(objects==0)
{cout<<"You tooked the last object ... You lost\nGame over\nPress -1 to quit or enter the number of objects to play again\n";
cin>>objects;}}}
while(objects!= -1);
f. Provide a Play again feature
if(objects==-1)
{
cout<<"Thank u for playing\n";}
first thank u
second , i am not sure if c and d are correct when trying to execute there is some logic error for exemple there is 9 objects
nb i wish to remove is 3
you have removed 3 objects
there is 3 objects left
#include<iostream>
usingnamespace std;
int main ()
{
int objects,remove;
do
{
cout<<"Welcome to NIM game!\nEnter the initial number of objects to play: "<<endl;
cin>>objects; // here we input the initial number (a)
while(objects<1 &&objects!=-1 ) //the total of objects can't be less than 1
{
cout<<"This is not a valid value.\nEnter a number greater than 1"<<endl;
cin>>objects;
}
while(objects>1 && objects!=2) //
{
{
cout<<"There is "<<objects<<" object(s).\nEnter the amount(between 1 and 3) that you would like to remove"<<endl;
cin>>remove;
}
//c. rule:
// only 1, 2, or 3 object(s) taken per turn (only allow 1 or 2 or 3 objects to be taken not more).
if(remove>=1 && remove<=3)
{
cout<<"You have removed "<<remove<<" objects"<<endl;
objects-=remove;
}
// b. The game should display how many objects remain in the collection after each players’turn.
else
{
cout<<"This is not an acceptable value.\nEnter a value between 1 and 3"<<endl;
cin>>remove;
}
{
objects-=remove;
}
// d. Make sure that there is enough objects at each turn (a player cannot take 3 objects when the number of remaining objects is only 2)
while(objects==2)
{
remove<=2;
cout<<"There is "<<objects<<" objects.\nEnter the amount(between 1 and 3) that you would like to remove"<<endl;
cin>>remove;
objects-=remove;
}
//e. rule:
// player taking last object loses (the game should stop when it is over)
if((objects<2 &&objects!=0) ||objects==1)
{
cout<<"There is "<<objects<<" objects.\nOne object left...Next player lost\nGame Over\nPress -1 to quit or enter the number of objects to play again "<<endl;
cin>>objects;
}
elseif(objects==0)
{
cout<<"You tooked the last object ... You lost\nGame over\nPress -1 to quit or enter the number of objects to play again\n";
cin>>objects;
}
}
}
while(objects!= -1);
// f. Provide a Play again feature
if(objects==-1)
{
cout<<"Thank u for playing\n";
}
system("pause");
return 0;
}