Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create the game of Nim using C++. There will be 3 piles of marbles used for the

ID: 3543083 • Letter: C

Question

Create the game of Nim using C++. There will be 3 piles of marbles used for the game. The first pile will contain 19 marbles, the second pile contain 21 marble, and the third contain 23 marbles. On each turn a player can choose exactly one pile and remove as many as they Want to. The game should include: Display the rules, Display the piles of marbles in groups of 5 displaying one letter 'O' per marble. Alternate players turns until a winner is declared. Ask if the players want to play again and if so play another game

Explanation / Answer

#include<iostream>


using namespace std;


void displayM(int p1,int p2,int p3)

{

int i=0;

cout<<endl<<"Pile 1 = "<<p1<<endl;

for(i=p1/5;i>0;i--)

cout<<"O O O O O"<<endl;

for(i=p1%5;i>0;i--)

cout<<"O ";

cout<<endl<<"Pile 2 = "<<p2<<endl;

for(i=p2/5;i>0;i--)

cout<<"O O O O O"<<endl;

for(i=p2%5;i>0;i--)

cout<<"O ";

cout<<endl<<"Pile 3 = "<<p3<<endl;

for(i=p3/5;i>0;i--)

cout<<"O O O O O"<<endl;

for(i=p3%5;i>0;i--)

cout<<"O ";

}


int main()

{

int pile1=19,pile2=21,pile3=23,pile,n;

char ch='y',c='y';


while(c=='y'||c=='Y') {

pile1=19;

pile2=21;

pile3=23;

/* Display Rules */

cout<<endl<<"1. On each turn a player can choose exactly one pile and remove as many marbles from that pile as they Want to.";

cout<<endl<<"2. The player that takes the last marble(s) from the only remaining pile wins.";


/* Display marbles */

displayM(pile1,pile2,pile3);


while((pile1+pile2+pile3)>0) {

do {

ch='y';

cout<<endl<<"Player 1 choose pile";

cin>>pile;

cout<<endl<<"choose no. of marbles to remove";

cin>>n;

switch(pile) {

case 1:

if(pile1-n>=0&&pile1>0)

pile1 -= n;

else

ch='n';

break;

case 2:

if(pile2-n>=0&&pile2>0)

pile2 -= n;

else

ch='n';

break;

case 3:

if(pile3-n>=0&&pile3>0)

pile3 -= n;

else

ch='n';

break;

default:

ch='n';

}

}while(ch=='n');


displayM(pile1,pile2,pile3);


if(pile1+pile2+pile3==0) {

cout<<endl<<"Player 1 Wins!!";

break;

}

do {

ch='y';

cout<<endl<<"Player 2 choose pile";

cin>>pile;

cout<<endl<<"choose no. of marbles to remove";

cin>>n;

switch(pile) {

case 1:

if(pile1-n>=0&&pile1>0)

pile1 -= n;

else

ch='n';

break;

case 2:

if(pile2-n>=0&&pile2>0)

pile2 -= n;

else

ch='n';

break;

case 3:

if(pile3-n>=0&&pile3>0)

pile3 -= n;

else

ch='n';

break;

default:

ch='n';

}

}while(ch=='n');


displayM(pile1,pile2,pile3);


if(pile1+pile2+pile3==0) {

cout<<endl<<"Player 2 Wins!!";

break;

}

}

cout<<endl<<"want to play again?? (y/n)";

cin>>c;

}


return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote