rock/paper/scissors

This is what I have as a start to my R/P/S game. The only parameters are:You must use the rand() function and a switch statement in the random selection of the computer’s action.
If you could just point me in the right direction, that would be helpful.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This program plays you in a game of Rock/Paper/Scissors
#include <iostream>
#include <cstdlib>
#include <limits>
#include <ctime>

using namespace std;

#define win Congratulations, you won!
#define loose Sorry, you lost. Please try again.

int main()
{

    int choice;
    int i;
    int comp;
    char res;
   
    unsigned seed;
    
    cout << "Rock/Paper/scissors is a simple game. The rules are: \n Rock beats scissors. \n Scissors beats Paper. \n Paper beats rock.\n";    
    //The choices
    cout << "Game Choices.\n\n";
    cout << "1. Rock\n";
    cout << "2. Paper\n";
    cout << "3. Scissors\n";
    cout << "4. Quit, exits the game.\n\n";
    cout << "Please enter your choice.";
    cin >> choice;
 
    
     //Now for the IF/Else statements.
    
    if (choice == 1) //Rock
        {   
        cout << "You picked Rock.\n";
        cout << "Now here was my choice.\n";
        }
    
     else if (choice == 2) //Paper
        {   
        cout << "You picked Paper.\n";
        cout << "Now here was my choice.\n";
        }
    
   else if (choice == 3) //Scissors
        {   
        cout << "You picked Scissors.\n";
        cout << "Now here was my choice.\n";
        }
    else if ( choice == 4)
        {
        return 0;
        }
//*************************************************
        seed = time(0);
        srand(seed); //For the random generator.
        
 comp = rand() % 3 + 1; //Computers pick.
  
  if (comp==1) //Computer rock
        {
        res = 1;
        cout << "Rock!\n";
        }
  
  else if (comp==2) //Computer paper
       {
        res = 2;
       cout << "Paper!\n";
        }
  else if (comp==3)  // Computer scissors
        {
        res = 3
        cout << "Scissors!\n";
        }
  
  switch (res)
    {
        case '1': cout << "test";
        case '2': cout << "tests";
        case '3': cout << "test";
    }


 system("pause");
      return 0;
}


This is what I have now, I think I did something wrong with the random function, could some one explain?
Also, I now need it to display how many wins/losses/ties and how many rounds I played. This is what I have so far, I need help getting the switch function for the WIN/Lose/Tie, the counter, and getting it to keep playing until you choose the quit option.
Last edited on
Your rand statements generate numbers 1-3, inclusively. When the player chooses rock, paper, or scissors, you have a chain of if statements to determine whether the player chose rock, paper, or scissors. Just move the code for the computer's choice out of the statements, and do the same thing as you did for the player.

-Albatross
Not quite sure why you have

rand() % 3 + 1 << comp;

Shouldn't it be:

comp = rand() % 3 + 1;
yeah, I fixed that a long time ago, I was just in the zone when I typed that I guess, I don't know.
Than write if (comp==choice)
display win
else loose
Not trying to be pushy or anything, but I've been working on this all night. Could some one give me a little nudge in the right direction with this.
Well, after you've got the input of the choices, the next step in the game is to compare the choices.
I've played around with rock paper scissors concepts in C++ earlier on. A good way to compare values and determine wins,losses or ties is to use logical combinations in your if statements, so that they check for all wins, or losses, and are not dependent upon an individual choice, or require multiple if statements to account for multiple possibilities.
1
2
3
4
5
6
//scans for all winning combinations
if ((choice==1 && comp==3 ) || (choice==2 && comp==1) || (choice==3 && comp==2))
{
cout<<"You win!";
wins++;
}

You can use a similar statement with the numeric values reversed to account for losses, and a simple (choice==comp) based if statement for ties. If the game is embedded in a loop, so that multiple games can be played, you can also use increments to count the number of wins,losses or ties (such as the wins++ in this example).
Last edited on
Topic archived. No new replies allowed.