rock paper scissors problem!

hi everyone...
i know there were a lot of RPS related topics, but non of them seems to be having the problems i'm having. my problems are :
1- the program completely ignores the "cout"s in the PlayersChoice function.
(somebody told me to add cin.ignore for this matter, but the problem is still there.)
2- the program always says that it's a tie!

any help?? thank you in advance...

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;

    
    
    void player (int& a)
  
    {
    
        char r, p, s;
        char playerschoice;
        
        cout << "Welcome... This is a minimal Rock-Paper-Scissors game." << endl;
        cout << "Let the game begin... " << endl;
        cout << "Enter R for Rock, P for Paper and S for Scissors: ";
        

        cin >> playerschoice;
        cin.ignore();
    
    if ( playerschoice == r ) 
       {
           cout << "You are going with: Rock..." << endl;
           a = 0;
       }
    
    else if ( playerschoice == p )
       {   
       
        cout << "You are going with: Paper..." << endl;
           a = 1; 
       }
  
    
    else if ( playerschoice == s )
       {
        
        cout << "You are going with: Scissors..." << endl;
           a = 2;  
    
       }

    }
    
    
    void computer (int& b)
   
    {
    
        int ComputersChioce;
        
        srand(time(NULL));
        ComputersChioce = rand() % 3;
    
    if ( ComputersChioce == 0 )
        
       {
          cout << "And the Computer's Chioce is: Rock! " << endl;
          b = 0;
       }
    
    else if ( ComputersChioce == 1 )
     
       {
          cout << "And the Computer's Choice is: Paper! " << endl;
          b = 1;
       }
        
    else if ( ComputersChioce == 2 )
    
       {
          cout << "And the Computer's Choice is: Scissors! " << endl;
          b = 2;
       }
    
    }
    
int main ()
   

    {
    
        int x, y;
        
        player(x);
        computer(y);
        
        if (x == 0 || y == 0)
            
            cout << "It's a TIE...";
        
        else if (x == 0 || y == 1)
            
            cout << "You LOSE...";
        
        else if (x == 0 || y == 2)
            
            cout << "You WIN!";
        
        else if (x == 1 || y == 0)
            
            cout << "You WIN!";
        
        else if (x == 1 || y == 1)
            
            cout << "It's a TIE...";
        
        else if (x == 1 || y == 2)
            
            cout << "You LOSE...";
        
        else if (x == 2 || y == 0)
            
            cout << "You LOSE...";
        
        else if (x == 2 || y == 1)
            
            cout << "You WIN!";
        
        else if (x == 2 || y == 2)
            
            cout << "It's a TIE..."; 
            
        
    }
Last edited on
srand() should only be called once, i.e. put it in main, not in a function that might potentially later be called repetitively. (Just a suggestion.)

You want and's not or's (&& not ||.)
Also, a tie happen whenever x == y; you can check just once instead of three times. Also wins happen whenever x == (y + 1) % 3... think about it! (Look at the way you defined Rock, Paper, and Scissors.)
Last edited on
All you need to do to fix your cout problem is put single quotes around the 'r', 'p' and 's'. This returns the ascii value of that letter. Otherwise you are returning the "variables" r, p, and s, which are declared but not initialized so they are returning garbage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    if ( playerschoice == 'r' ) 
       {
           cout << "You are going with: Rock..." << endl;
           a = 0;
       }
    
    else if ( playerschoice == 'p' )
       {   
       
        cout << "You are going with: Paper..." << endl;
           a = 1; 
       }
  
    
    else if ( playerschoice == 's' )
       {
        
        cout << "You are going with: Scissors..." << endl;
           a = 2;  
    
       }
Last edited on
Oh yea, and that *facepalm*.
you don't know how much you've helped me guys... THANK YOU A THOUSAND TIMES! (that might sound gay, but what you did really helped me!)

@mathhead200 : thank you for caring that much... really...
I did what "stewbond" told me to do, and all the program started working perfectly... and later i am going to add a loop which ends the game after one side wins the game three times... do you think what you said : "the srand might potentially later be called repetitively. " would be a problem later on? and also, how would you suggest to end the program that way?? using an "IF" to end it after 3 times or "WHILE"??

@stewbond : thanks for caring man... you were a huge help.
1
2
3
4
5
6
7
8
9
10
11
const int WINS = 3;
int myWins = 0,
    comWins = 0;
while( myWins < WINS && comWins < WINS ) {
    //Let's play a games...
    //(Remember to increment either myWins or comWins in here after each game, so the loop ends!)
}
if( myWins > comWins ) //or "if( myWins >= WINS )" works too
    //I win!
else
    //Aww... computer wins 
Something like that. And yes about srand(). You only seed the PRNG once! This is very important as it's one of those logical errors and it can be very hard to track down. Using rand() in a function (over and over) is fine (it's designed to be used tat way in fact,) just remember to use srand() before you invoke that function, and exactly once; (srand() somewhere very near the beginning of the program should be fine for this one.) Same goes for srand() in a loop, no!
Last edited on
Also, you should run a global substitution to replace "ComputersChioce".
thank you so much guys... i didn't know people care so much. again, thank you for your help... i'm gonna post the final program in a few...
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
//  main.cpp
//  RPS
//
//  Created by Amirali Monfared on 11/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;



    void player (int& a)
  
{
    
        char playerschoice;
        
                cout << "Enter R for Rock, P for Paper and S for Scissors: ";
        

        cin >> playerschoice;
        cin.ignore();
    
    if ( playerschoice == 'r' ) 
       {
           cout << "You are going with: Rock..." << endl;
           a = 0;
       }
    
    else if ( playerschoice == 'p' )
       {   
       
        cout << "You are going with: Paper..." << endl;
           a = 1; 
       }
  
    
    else if ( playerschoice == 's' )
       {
        
        cout << "You are going with: Scissors..." << endl;
           a = 2;  
    
       }
   }
    
    
    void computer (int& b)
   
    {
    
        int ComputersChioce;
        
        srand(time(NULL));
        ComputersChioce = rand() % 3;
    
    if ( ComputersChioce == 0 )
        
       {
          cout << "And the Computer's Chioce is: Rock! " << endl;
          b = 0;
       }
    
    else if ( ComputersChioce == 1 )
     
       {
          cout << "And the Computer's Choice is: Paper! " << endl;
          b = 1;
       }
        
    else if ( ComputersChioce == 2 )
    
       {
          cout << "And the Computer's Choice is: Scissors! " << endl;
          b = 2;
       }
    
    }
    
int main ()
   

    {
    
        const int TotalWins = 3;
        int PlayerWins = 0;
        int ComputerWins = 0;
        int x, y;
        int i = 0, j = 0;
        
        cout << "Welcome... This is a minimal Rock-Paper-Scissors game." << endl;
        cout << "Let the game begin... " << endl;

        
        while (PlayerWins < TotalWins && ComputerWins < TotalWins) 
        
        {
                    
            player(x);
            computer(y);
        
        if (x == 0 && y == 0)
            
        {
            cout << "It's a TIE...";
            cout << endl;
            cout << endl;
        }
            
        else if (x == 0 && y == 1)
            
        {
            cout << "You LOSE...";
            ComputerWins ++;
            cout << endl;
            cout << endl;
            j++;
        }
        else if (x == 0 && y == 2)
            
        {   
            cout << "You WIN!";
            PlayerWins ++;
            cout << endl;
            cout << endl;
            i++;
        }
        
        else if (x == 1 && y == 0)
            
        {
            cout << "You WIN!";
            PlayerWins ++;
            cout << endl;
            cout << endl;
            i++;
        }
        
        else if (x == 1 && y == 1)
            
        {
            cout << "It's a TIE...";
            cout << endl;
            cout << endl;
        }
            
        else if (x == 1 && y == 2)
            
        {
            cout << "You LOSE...";
            ComputerWins ++;
            cout << endl;
            cout << endl;
            j++;
        }
        
        else if (x == 2 && y == 0)
            
        {
            cout << "You LOSE...";
            ComputerWins ++;
            cout << endl;
            cout << endl;
            j++;
        }
        
        else if (x == 2 && y == 1)
            
        {
            cout << "You WIN!";
            PlayerWins ++;
            cout << endl;
            cout << endl;
            i++;
        }

        
        else if (x == 2 && y == 2)
            
        {
            cout << "It's a TIE..."; 
            cout << endl;
            cout << endl;
            
        }
            
            if (PlayerWins == 3)
            
                cout << "You Beat The Computer... That Was Unlikely." << endl;
            
            else 
                
                cout << "Computer Won... Haha... " << endl;    
        
            
        } 
    }

i know there are a lot of newbie problems... but at least, it works... thank you guys. any suggestions for polishing it?? i really appreciate what you guys did...
Last edited on
Put a single condition for TIE! TIE occurs when x==y... So no need for 3 different if loops... Also, make it idiot proof... Like, what if the player enters a letter other than r, p or s? or what if he's got CAPSLOCK on? Put the conditions as
1
2
3
4
if (playerschoice=='r'||playerschoice=='R')
{
  //Blah blah
}


...and so on. Also put a check for a letter input other than r, p or s, and assign it a default value. Like, keep a default choice as a random choice b/w r, p or s. So even if the user presses some other letter or number, your program won't crash...
Last edited on
thank you so much caprico... i changed the TIE logic...
i was playing around with the program, and then i sadly opened a can of worms...
the problem is i wanted to give the player the choice to continue the game if they want to... but i don't know where exactly to put the while loop... i put it at the beginning of the main (), and it didn't work... i also tried the end... didn't work either... any idea?
Last edited on
Crazy idea... Put all of line 89-200 into a void function, and put function in do-while loop! :P
Simpler would be to put a do at the beginning, at line 98, and then before you end the do, ask for user choice to continue, close do, and then put while condition (ch=='y' || ch=='Y');... :)

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
       do 
    
    {
            
    
        while (PlayerWins < TotalWins && ComputerWins < TotalWins)

        {
                       
            player(x);
            computer(y);
        
        if (x == y)
            
        {
            cout << "It's a TIE...";
            cout << endl;
            cout << endl;
        }
            
        else if (x == 0 && y == 1)
            
        {
            cout << "You LOSE...";
            ComputerWins ++;
            cout << endl;
            cout << endl;
            j++;
        }
        else if (x == 0 && y == 2)
            
        {   
            cout << "You WIN!";
            PlayerWins ++;
            cout << endl;
            cout << endl;
            i++;
        }
        
        else if (x == 1 && y == 0)
            
        {
            cout << "You WIN!";
            PlayerWins ++;
            cout << endl;
            cout << endl;
            i++;
        }
        
                   
        else if (x == 1 && y == 2)
            
        {
            cout << "You LOSE...";
            ComputerWins ++;
            cout << endl;
            cout << endl;
            j++;
        }
        
        else if (x == 2 && y == 0)
            
        {
            cout << "You LOSE...";
            ComputerWins ++;
            cout << endl;
            cout << endl;
            j++;
        }
        
        else if (x == 2 && y == 1)
            
        {
            cout << "You WIN!";
            PlayerWins ++;
            cout << endl;
            cout << endl;
            i++;
        }

        
                   
            if (PlayerWins == 3)
            {
                
                cout << "You Beat The Computer... That Was Unlikely." << endl << endl;
            
            
            }
            
            
            else if (ComputerWins == 3) 
                
                cout << "Computer Won... Haha... " << endl;
           
             
            } 
       
            
    }
        while (ans == 'y' || ans == 'Y') ;
        
        {
        
            cout << "Do You Want To Conyinue? (Y/N): ";
            cin >> ans;
       
        }
       
                       
       

        
}

it doesn't work... nothing happens... where's the problem?
and... thank you... :)
your syntax is wrong.. Ask the question just BEFORE you close the do loop...
i moved lines 105 and 106 to 99... but now it keeps asking the question... i'm sorry if i'm bothering you with many questions...
try closing the inner while loop before you do this:

1
2
3
4
5
6
7
8
9
10
11
12
if (PlayerWins == 3)
            {
                
                cout << "You Beat The Computer... That Was Unlikely." << endl << endl;
            
            
            }
            
            
            else if (ComputerWins == 3) 
                
                cout << "Computer Won... Haha... " << endl;


then ask question...
still not happening... i did what you said now it is finishing the game every time after playing. i literally did everything that I can think of...
Hmm... just as a wild shot, try the void function?
Topic archived. No new replies allowed.