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.
kajumbliesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kajumbliesAuthor Commented:
the output is supposed to look something like this
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
0
tmiddletCommented:
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
0
kajumbliesAuthor Commented:
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
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

tmiddletCommented:
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);
0
kajumbliesAuthor Commented:
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
0
tmiddletCommented:
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;
}

Open in new window

0
kajumbliesAuthor Commented:
what does error cheking mean and what do i put down for the computers turn
0
kajumbliesAuthor Commented:
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
0
tmiddletCommented:
This should work for you, but I hope that if this is a class that you can get a basic understanding of the code.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
    srand((unsigned)time(0));//Make sure random is random
    char doagain = 'Y';
    int game = 1;
    int wongames = 0;
    cout << "Welcome to Nim game!\n";
    do {
        cout << "Game #" << game << " " << endl;
        cout << "There are 10 stones in a pile. You may remove 1, 2 or 3 stones at a time. " << endl;
        int stones = 10;
        bool playerturn = true;
        do {
            if (playerturn) {
                int stonesToRemove;
                //input player's turn
                do {
                    cout << "How many stones would you like to remove? ";
                    cin >> stonesToRemove;
                } while (stonesToRemove > 3 || stonesToRemove > stones || stonesToRemove < 1); // Make sure selection is within the boundries
                stones -= stonesToRemove;
            }
            else {
                int stonesToRemove;
                //randomly select some stones
                if (stones >= 3) {
                    //there are at least 3 stones
                    stonesToRemove = (rand()%3)+1;
                }
                else {
                    //there are less than 3 stones
                    stonesToRemove = (rand()%stones)+1;
                }
                stones -= stonesToRemove;
                cout << "I removed " << stonesToRemove << " from the pile. There are " << stones << " stone(s) in the pile.\n";
            }
            playerturn = !playerturn;
        } while (stones != 0);
        game++;
        if (playerturn) {
            wongames++;
            cout << "Congradulations! You win.\nYou have won " << wongames << " game(s).\n";
        }
        else {
            cout << "You lose.\nYou have won " << wongames << " game(s).\n";
        }
        cout << "Would you like to play again? (Y/N): ";
        cin >> doagain;
    } while(doagain == 'Y');
    cout << "Have a lucky day.";
    return 0;
}// main

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kajumbliesAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.