The assignment was as follows: \" Create a program for the Nim Game This will be
ID: 3631672 • Letter: T
Question
The assignment was as follows:" Create a program for the Nim Game
This will be a game between you and your computer.
0. Display little information about game and its rules on screen.
1. At the beginning of the game there will be stack of 30 stones.
2. Each player has to pick 1 or 2 or 3 stones in their respective turn.
The game will continue till the number of stones left has become zero.
3. The one who pick the last stone is looser.
4. Display the winner at the end.
Give the source code name as "nim.cpp".
Example:
Welcome to the Nim Game, its is played between user(Player-1) and computer(Player-2).
You can pick atleast 1 and atmost 3 from the stone stack.
player1 turn, pick the number of stone
2
the number of stone left is :28
player2 turn, pick the number of stone :2
the number of stone left is :26
player1 turn, pick the number of stone
1
the number of stone left is :25
player2 turn, pick the number of stone :1
the number of stone left is :24
player1 turn, pick the number of stone
3
the number of stone left is :21
player2 turn, pick the number of stone :2
the number of stone left is :19
player1 turn, pick the number of stone
3
......
the number of stone left is :2
player2 turn, pick the number of stone :1
the number of stone left is :1
player1 turn, pick the number of stone
1
the number of stone left is :0
player 2 is winner"
I wrote the following code below, but now I have to fix it so that it doesnt ask whether or not to continue after every turn...Please help, I will rate lifesaver if it works!
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
char option='A';
do
{
char option2='Z';
int a,stone;
stone=30;
do
{
cout<<"Welcome to the Nim Game. There is a stack of 30 stones, each player will pick up 1, 2 or 3 stones per turn until all of the stones are gone. Whoever picks up the last stone loses. You are player 1, and the computer is player two. Player 1 will pick first."<<endl;
do
{
cout<<"Player 1, please type 1, 2 or 3, to choose the number of stones you wish to pick up:"<<endl;
cin>>a;
if (a>3 || a<1)
{
cout<<"Please enter a number between 1 and 3."<<endl;
}
else if(a>=1&&a<=3)
{
stone=stone-a;
cout<<"There are now only "<<stone<<" stones available"<<endl;
if (stone<=0)
{
cout<<"There are no more stones, Player 2 wins!"<<endl;
}
}
cout<<"Player 2 will now pick up 1, 2 or 3 stones."<<endl;
{
srand(time(NULL));
int num =rand()%3+1;
stone=stone-num;
cout<<"Player 2 picked "<<num<<" stones. There are now only "<<stone<<" stones available."<<endl;
if (stone<=0)
{
cout<<"There are no more stones, Player 1 wins!"<<endl;
}
}
cout<<"press Y if you would like to pick again or N if the game is over."<<endl;
cin>>option2;
}
while (option2=='Y'||option2=='y');
}
cout<<"Press P to play again or press X to Exit the game."<<endl;
cin>>option;
}
while (option2=='P'||option2=='p');
}
Explanation / Answer
Hi, I couldn't get your code to work on my machine thanks to copy-pasting and cramster's crappy formatting, so I thought it would be easier to just re-write it using the same general logic you need.
Here is the code using srand() as well. If you need any alterations to it or you have any questions, send me a message to inbox or comment here.
I hope this helps - if so then pelase rate :)
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
cout << "Welcome to the Nim Game" << endl;
cout << "***********************" << endl;
cout << "- It is played between user (Player-1) and computer (Player-2)." <<endl;
cout << "- There is a stack of 30 stones available to you."<< endl;
cout << "- Player-1 and Player-2 take turns taking stones from the stak."<<endl;
cout << "- The one to pick up the last stone loses the game." << endl;
cout << "- You can pick at least 1 and at most 3 from the stone stack" << endl;
cout << endl;
int stack = 30;
int player = 0; // 0 for user and 1 for computer
int pick = 0;
srand ( time(NULL) );
do
{
// it's user's turn
if(player == 0)
{
pick = 0;
do {
cout << "player 1 turn, pick the number of stones: ";
cin >> pick;
if((pick < 1) || (pick > 3))
cout << "Cannot pick less than 1 or more than 3 stones!" << endl;
if(pick > stack)
cout << "Cannot pick more than the remaining " << stack << " stones!" << endl;
} while( (pick < 1) || (pick > 3) || ( pick > stack) );
}
//it's computer's turn
else
{
pick = 0;
cout << "player 2 turn, pick the number of stones: ";
do {
pick = rand() % 4;
} while( (pick < 1) || (pick > 3) || ( pick > stack));
cout << pick << endl;
}
//update the stack
stack = stack - pick;
cout << "The number of stones left is " << stack << endl;
// switch users for next turn now
if(player == 0)
player = 1;
else
player =0;
}while(stack > 0);
cout << endl;
cout << "Game Over!" << endl;
if(player ==0)
cout << "Player 1 is winner" << endl;
else
cout << "Player 2 is winner" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.