kajumblies
asked on
C++
Hi i realy need help writing this program im not sure how to do it does anyone know how to write the code for this program in microsoft visuall studios C++
Develop a version of the Nim game in C++.
This version of the Nim game consists of a pile of stones, ranging from 10 to 16. The player and the computer will alternate (take turn) to remove 1, 2, or 3 stones from the pile. The player will go first. The one, who takes the last stone, loses. The number of stones in the pile will be randomly generated at the beginning of the game and be displayed. At the end of a game, the player will be prompted to either start another game or to quit.
This program will keep track of the total number of games played and the total number of games the player has won. The number of stones removed by the computer will be randomly decided. This program will perform the necessary error checking: the number of stones to remove cannot be greater than 3 or the number of stones left in the pile. In this program, you will implement a function with the following prototype:
int myRandom(int upperLimit);
This function generates and returns a random number between 1 and upperLimit.
Develop a version of the Nim game in C++.
This version of the Nim game consists of a pile of stones, ranging from 10 to 16. The player and the computer will alternate (take turn) to remove 1, 2, or 3 stones from the pile. The player will go first. The one, who takes the last stone, loses. The number of stones in the pile will be randomly generated at the beginning of the game and be displayed. At the end of a game, the player will be prompted to either start another game or to quit.
This program will keep track of the total number of games played and the total number of games the player has won. The number of stones removed by the computer will be randomly decided. This program will perform the necessary error checking: the number of stones to remove cannot be greater than 3 or the number of stones left in the pile. In this program, you will implement a function with the following prototype:
int myRandom(int upperLimit);
This function generates and returns a random number between 1 and upperLimit.
You would want to start with an Integer value representing the number of stones remaining in the pile set to 10.
You could then prompt the user for the number of stones they wish to take, then display the remaining number of stones.
You could have the computer take a random number of stones using (rand()%3)+1, providing you use basic error checking.
You would loop this until the number of stones remaining is 0.
If you have a basic understanding of c++ you should be able to get this to work
You could then prompt the user for the number of stones they wish to take, then display the remaining number of stones.
You could have the computer take a random number of stones using (rand()%3)+1, providing you use basic error checking.
You would loop this until the number of stones remaining is 0.
If you have a basic understanding of c++ you should be able to get this to work
ASKER
i dont really know what you mean what kind of a loop should i use cause this is my first time coming across C++ and im still struggling with understand looping and stuff like that
You would want to use a while{} or a do{}while loop because these can quit when a variable is equal to a value
do {
//prompt and input
} while (stones != 0);
do {
//prompt and input
} while (stones != 0);
ASKER
what would you put inside the loop i dont really understand the point of this game in the first place. I understand kinda the loop stuff but im not really sure what im supposed to put inside them since im not really clear as to what this game is asking
The game wants you to take turns with the computer taking some stones from the pile
int stones = 10;
bool playerturn = true;
while (stones != 0) {
if (playerturn) {
//input player's move with error checking
}
else {
//generate computer's turn with error checking
}
playerturn = !playerturn;
}
ASKER
what does error cheking mean and what do i put down for the computers turn
ASKER
i dont really understand this assignment its too confusing i got this but im not sure what the heck im doing with it
#include <iostream>
using namespace std;
int main()
{
cout << "Game #1 " << endl;
cout << "There are 10 stones in a pile. You may remove 1, 2 or 3 stones. " << endl;
int stones = 10;
bool playerturn = true;
while (stones != 0) {
if (playerturn) {
cout << "How many stones would you like to remove? " << endl;
cin << stones;
}
else {
//generate computer's turn with error checking
}
playerturn = !playerturn;
}
return 0;
}// main
#include <iostream>
using namespace std;
int main()
{
cout << "Game #1 " << endl;
cout << "There are 10 stones in a pile. You may remove 1, 2 or 3 stones. " << endl;
int stones = 10;
bool playerturn = true;
while (stones != 0) {
if (playerturn) {
cout << "How many stones would you like to remove? " << endl;
cin << stones;
}
else {
//generate computer's turn with error checking
}
playerturn = !playerturn;
}
return 0;
}// main
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much this is i a big help and yes it does help me understand the code i really apreciate your help thank you so much
ASKER
Welcome to Nim game!
Game #1. There are 10 stones in the pile. You may remove 1, 2, or 3 stones at a time.
How many stones would you like to remove? 3
I removed 3 from the pile. There are 4 stone(s) in the pile.
How many stones would you like to remove? 3
I remove 1 from the pile. There are 0 stone(s) in the pile.
Congratulations! You win.
You have won 1 game(s).
Would you like to play again? (Y/N): N
Have a lucky day.
Please help