You are viewing our Forum Archives. To view or take place in current topics click here.
A C++ game of Rock Scissor Paper (need some simple help)
Posted:

A C++ game of Rock Scissor Paper (need some simple help)Posted:

RailDex
  • Powerhouse
Status: Offline
Joined: Jan 30, 201014Year Member
Posts: 479
Reputation Power: 19
Status: Offline
Joined: Jan 30, 201014Year Member
Posts: 479
Reputation Power: 19
Ok so I have this game of RPS, I changed a lot of parts of it and got it all running correctly, but I have came to the following problem:
I built the game, added in some ASCII art to represent each item (Rock, Paper, Scissors)
I got the game to work with no errors, it actually plays but I forgot when I was building it that I needed it to have classes.
I NEVER really use classes because I have only really made really simple small things written in C++ which don't even need them. (I find programming languages very hard to comprehend)
So I was wondering if someone could take a look at it and maybe help be get each object (I think that's what it's called) put into classes, this would be giving Rock, Paper and Scissor their own separate classes.

Here's the code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    srand((unsigned)time(0));
    int choice;
    int 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";
   }
    return main();
}
#2. Posted:
Experiment5X
  • TTG Senior
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Classes are a HUGE part of programming and they make everything a lot more organized and easier to read. So you really do need to learn them if you plan on continuing on in the programming field. From what you said, it sounds like you're making this program for a class, so I'm not going to do the whole thing for you, however I will make the rock class. It'd probably look like this:



class Rock
{
public:
     static void print()
     {
           cout <<"        . -- ~~~ -- .\n";
           cout <<"    .-~               ~-.\n";
           cout <<"   /                     \\n";
           cout <<"  /                       \\n";
           cout <<" |                         |\n";
           cout <<" |                         |\n";
           cout <<" |                         |\n";
           cout <<"  \                       /\n";
           cout <<"   \                     /\n";
           cout <<"    `-.               .-'\n";
           cout <<"        ~- . ___ . -~\n";
     }
};


I'm not really sure what other methods you'd need to have in there, so I just made the one print.

Anyways, like I said above, classes are really important to programming, well at least OOP. So I encourage you to become more familiar with them. Some good places to learn, are thenewboston.org and learncpp.com. Here is the first video tutorial on classes from thenewboston.

#3. Posted:
RailDex
  • Powerhouse
Status: Offline
Joined: Jan 30, 201014Year Member
Posts: 479
Reputation Power: 19
Status: Offline
Joined: Jan 30, 201014Year Member
Posts: 479
Reputation Power: 19
Experiment5X wrote Classes are a HUGE part of programming and they make everything a lot more organized and easier to read. So you really do need to learn them if you plan on continuing on in the programming field. From what you said, it sounds like you're making this program for a class, so I'm not going to do the whole thing for you, however I will make the rock class. It'd probably look like this:



class Rock
{
public:
     static void print()
     {
           cout <<"        . -- ~~~ -- .\n";
           cout <<"    .-~               ~-.\n";
           cout <<"   /                     \\n";
           cout <<"  /                       \\n";
           cout <<" |                         |\n";
           cout <<" |                         |\n";
           cout <<" |                         |\n";
           cout <<"  \                       /\n";
           cout <<"   \                     /\n";
           cout <<"    `-.               .-'\n";
           cout <<"        ~- . ___ . -~\n";
     }
};


I'm not really sure what other methods you'd need to have in there, so I just made the one print.

Anyways, like I said above, classes are really important to programming, well at least OOP. So I encourage you to become more familiar with them. Some good places to learn, are thenewboston.org and learncpp.com. Here is the first video tutorial on classes from thenewboston.



Hmm, wasn't really for a class (as in school/college) just something I wanted to try, as i thought it wouldn't be too hard.
But thanks this will help me quite a bit i'm sure.
#4. Posted:
UnrealEgg
  • Powerhouse
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
You could have a class named "draw" where you had functions to draw scissors, rock and paper for example.

Then just include the class like:
#include "draw.hpp"

Then call the public functions you declared.


Like so:
draw.hpp:
#if !define _DRAW_HPP_
#define _DRAW_HPP_

class Draw{

   public:
      Draw();
      ~Draw();
      
      void drawRock();
      void drawScissors();
      void drawPaper();

};

#ENDIF //_DRAW_HPP_


draw.cpp:
#include "draw.hpp"
#include <iostream>

using namespace std;

Draw::Draw(){

}

Draw::~Draw(){

}

void Draw::drawRock(){
   cout <<"        . -- ~~~ -- .\n";
   cout <<"    .-~               ~-.\n";
   cout <<"   /                     \\n";
   cout <<"  /                       \\n";
   cout <<" |                         |\n";
   cout <<" |                         |\n";
   cout <<" |                         |\n";
   cout <<"  \                       /\n";
   cout <<"   \                     /\n";
   cout <<"    `-.               .-'\n";
   cout <<"        ~- . ___ . -~\n";     
}


void Draw::drawScissors(){
   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";
}

void Draw::drawPaper(){
   cout <<"  ____________\n";
   cout <<" | __________ |\n";
   cout <<" | __________ |\n";
   cout <<" | __________ |\n";
   cout <<" | __________ |\n";
   cout <<" | __________ |\n";
   cout <<" | __________ |\n";
   cout <<" | __________ |\n";
   cout <<" | __________ |\n";
   cout <<" | __________ |\n";
   cout <<" |____________|\n";
}


The new "main.cpp":

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "draw.hpp"

using namespace std;
int main()
{
    srand((unsigned)time(0));
    int choice;
    int 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)
    {
   drawRock();     
              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)
    {
   drawPaper();
               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)
   {
   drawScissors;
              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";
   }
    return main();
}



Just a side note, this may not compile as is but its probably close. You could also pass through compchoice and output the necessary lines using the if statements you created too.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.