CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2012
    Posts
    5

    rock paper scissors game

    i am just about to finish my rock papper sissors game as i added classes to the games. however i dont know how to add a scoring system. any help.

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    class Rock
    {
    int choice;
    int compchoice;
    public:

    void Game();


    };

    void Rock::Game()
    {
    srand((unsigned)time(0));
    compchoice = (rand()%2)+1;
    cout << "Welcome to Rock Paper Scissors.";
    cout << " You will be playing against the computer. Type 1 for";
    cout << " rock, 2 for paper, and 3 for scissors\n";
    cin >> choice;

    if (choice == 1)
    {
    cout <<" . -- ~~~ -- .\n";
    cout <<" .-~ ~-.\n";
    cout <<" / \\n";
    cout <<" / \\n";
    cout <<" | |\n";
    cout <<" | |\n";
    cout <<" | |\n";
    cout <<" \ /\n";
    cout <<" \ /\n";
    cout <<" `-. .-'\n";
    cout <<" ~- . ___ . -~\n";

    if (compchoice == 1)
    cout << "It's a tie!\n\n\n\n";
    else if (compchoice == 2)
    cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
    else if (compchoice == 3)
    cout << "Rock beats scissors! You win!\n\n\n\n";
    }

    if (choice == 2)
    {
    cout <<" ____________\n";
    cout <<" | __________ |\n";
    cout <<" | __________ |\n";
    cout <<" | __________ |\n";
    cout <<" | __________ |\n";
    cout <<" | __________ |\n";
    cout <<" | __________ |\n";
    cout <<" | __________ |\n";
    cout <<" | __________ |\n";
    cout <<" | __________ |\n";
    cout <<" |____________|\n";
    if (compchoice == 1)
    cout << "It's a tie!\n\n\n\n";
    else if (compchoice == 2)
    cout << "Paper beats rock! You win!\n\n\n\n";
    else if (compchoice == 3)
    cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
    }

    if (choice == 3)
    {
    cout <<" ___ ___\n";
    cout <<" / _ \ / _ \\n";
    cout <<" / / \ \ / / \ \\n";
    cout <<" \ \_/ / \ \_/ /\n";
    cout <<" \___/ \___/\n";
    cout <<" \ \ / /\n";
    cout <<" \ O /\n";
    cout <<" // \\\n"; //" << endl;
    cout <<" // \\\n";
    cout <<" // \\\n";
    cout <<" // \\\n";
    cout <<" // \\\n";
    cout <<" // \\\n";
    cout <<" // \\\n";
    cout <<"/ \\n";
    if (compchoice == 1)
    cout << "It's a tie!\n\n\n\n";
    else if (compchoice == 2)
    cout << "Scissors beat paper! You win!\n\n\n\n";
    else if (compchoice == 3)
    cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
    }



    }

    int main()
    {
    Rock Main;

    Main.Game();

    return main();
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: rock paper scissors game

    Quote Originally Posted by sauc3_kid View Post
    i am just about to finish my rock papper sissors game as i added classes to the games. however i dont know how to add a scoring system.
    1. Please, define "scoring system".
    2. If you want someone else look at your code then format it properly using Code tags.
    3. What is this code snippet supposed to do with so called "scoring system"?
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2012
    Posts
    5

    Re: rock paper scissors game

    lol i am a beginner. i want to declare score as integer so that when you win a game you get plus one

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: rock paper scissors game

    Quote Originally Posted by sauc3_kid View Post
    lol i am a beginner. i want to declare score as integer so that when you win a game you get plus one
    For the sake of everyone who is looking at your code -- here is your code properly formatted, as well as all of those superfluous cout statements removed:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    class Rock
    {
        int choice;
        int compchoice;
    public:
    
        void Game();
    };
    
    void Rock::Game()
    {
        srand((unsigned)time(0));
        compchoice = (rand()%2)+1;
    
        if (choice == 1)
        {
            if (compchoice == 1)
                cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 2)
                cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
            else if (compchoice == 3)
                cout << "Rock beats scissors! You win!\n\n\n\n";
        }
    
        if (choice == 2)
        {
            if (compchoice == 1)
                cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 2)
                cout << "Paper beats rock! You win!\n\n\n\n";
            else if (compchoice == 3)
                cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
        }
    
        if (choice == 3)
        {
            if (compchoice == 1)
                cout << "It's a tie!\n\n\n\n";
            else if (compchoice == 2)
                cout << "Scissors beat paper! You win!\n\n\n\n";
            else if (compchoice == 3)
                cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
        }
    }
    
    int main()
    {
        Rock Main;
        Main.Game();
        return main();
    }
    The first thing is this:
    Code:
        return main();
    It is illegal to call main() directly like this in C++. Not only that, even if you were allowed to call main(), this is an infinite recursion. What are you trying to accomplish by recursively calling main()? If you're trying to keep the game "alive", then why not just write an infinite loop?
    Code:
        while (true)
        {
            Rock Main;
            Main.Game();
       }
    Your program will never end (unless you terminate the program), but at least you aren't 1) writing illegal C++ code and 2) ending up in an infinite recursion. The better solution is to ask whether to quit the game or keep going, and set up a while loop depending on this response.

    So this "scoring system" -- Your Rock::Game() function should return a value to denote what the results are. Right now it doesn't return anything, so all the outside world (main()) knows about the Game() is -- nothing.

    If not that, then add the "scoring system" to the Rock class itself so that every time Game() is called, this system updates member variables denoting the outcome. Then add public member functions to Rock that get the current scores so that the outside world can access this information.

    Note that none of this requires fancy output -- I see too many beginners clutter (and waste time) with cout's trying to produce pretty output, and the important things such as the logic and design of the game is ignored or left for last. What good is nice output if the game doesn't work correctly? With the code posted as above, it is much clear as to what to work on and what to change.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 17th, 2012 at 09:05 PM.

  5. #5
    Join Date
    Aug 2012
    Posts
    5

    Re: rock paper scissors game

    you are a star. i wish i had mentors like u around me to help me with my programming struggles

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured