RockPaperScissors

Pages: 12
Hello,

I decided to write a rock,paper and scissors game where the computer and player take turns, and computer makes random moves. I also want to use the boolean literal to know if game is over or not and if input is correct. Now this whole exercise seems too difficult for me, here is what I have so far:

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
#include<algorithm>
#include<iostream>

using namespace std;


int next_play()                       // Is it computers turn?

{

int randint (2) { return randint (0,2) };            // Determine next_play

}


int main()


int rock=0;
int paper=1;
int scissors=2;
string s;

bool draw=false;
bool gameover=false;
bool validstring=true;

while (!gameover) {                   // Player #1 turn

                                       

cout << "enter \"rock\", \"paper\", or \"scissors\"\n"

string s;
s=validstring;
	
while(cin >> s)

if (s=="rock" || "paper" || "scissors")
validstring=true;

while(s! validstring) {

cout<< "sorry: bad operator";

return 0; }



int x = 0;
		if (s=="scissors") {
			
			s = "scissors";
		}
		else if (s=="rock") {
			
			s = "rock";
		}
		else if (s=="paper") {
		
			s = "paper";
		}



int y=next_play();
string ss;                // computers play

switch(y) {              // convert numeric representations to strings

if (s=y)  {

bool draw=true; }

else {
	
         if (y==0 && s==1) {  bool win=true; }               // paper beat rock
	                       
	
			
            else if (y==2 && x==1) {  bool win=true; }         // scissors beat paper
					                    
				
			
	    else if (y==0 && x==2) { bool win=true; }          // rock beat scissors
					                      
				
	}		

 
}


Last edited on
Now this whole exercise seems too difficult for me
You should resist the urge to try to program the entire thing at once. This code isn't close to functional. Start from this point:
1
2
3
4
int main()
{
    return 0;
}
Start modifying it by asking for input from the user, and proving that you've stored it correctly.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main()
{
    std::string userInput;
    std::cout << "Enter \"rock\", \"paper\", or \"scissors\": ";
    std::cin >> userInput;
    std::cout << "You entered " << userInput << std::endl;
    return 0;
}

Now that you know userInput holds the correct information, you could then try to validate it.
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
#include <iostream>
#include <string>

int main()
{
    std::string userInput;
    std::cout << "Enter \"rock\", \"paper\", or \"scissors\": ";
    std::cin >> userInput;
    
    bool validInput;
    if (s == "rock" || s == "paper" || s == "scissors")
    {
        validInput = true;
    }
    else
    {
        validInput = false;
    }

    if (validInput)
    {
        std::cout << "Good input";
    }
    else
    {
        std::cout << "Bad input";
    }
    return 0;
}

Make sure each step along the way works before moving to the next one. It helps to plan out a series of small steps that lead to solving your problem.
1. get input
2. validate input
3. generate computer move (doesn't have to be random, make it always choose paper at first)
4. compare user input and computer move to determine a winner
5. move the code to generate a computer move into its own function
6. make the computer play random

You may think of more discrete steps along the way, but if you try to do everything at once you will get lost quickly.
Something like this for start:

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

#include<algorithm>
#include<iostream>
#include<vector>

using namespace std;


int main()
{
    std::string s;
    std::cout << "Enter \"rock\", \"paper\", or \"scissors\": ";
    std::cin >> s;
    
    bool validInput;
    if (s == "rock" || s == "paper" || s == "scissors")
    {
        validInput = true;
    }
    else
    {
        validInput = false;
    }

 
  while (validInput=true;)

  int rock=0;
  int paper=1;
  int scissors=2;

  vector<int>strings
  for(string temp;cin>>temp;)       // Read whitespace separated words
  strings.push_back(temp);

  for (int i=0 ; i<s; ++i)
  cout<<strings[i]<<;


  else
      {
          std::cout << "Bad input";
      }
      return 0;
}
Here is my improved code, I`m just thinking how to get round of the problem that I can`t compare strings on switch statements.

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

#include<algorithm>
#include<iostream>
#include<vector>

using namespace std;


int main()
{
    string s;
    cout << "Enter \"rock\", \"paper\", or \"scissors\": ";
    cin >> s; 
    
    bool validInput;
    if (s == "rock" || s == "paper" || s == "scissors")
    {
        validInput = true;
    }
    else
    {
        validInput = false;
    }
 
 
  if (validInput) { 

int x = 0;
		if (s=="scissors") {
			x = 0;
			s = "scissors";
		}
		else if (s=="rock") {
			x = 1;
			s = "rock";
		}
		else if (s=="paper") {
			x = 2;
			s = "paper";
		}



  vector<int>x
  for(int temp;cin>>temp;)       
  x.push_back(temp);

  for (int x=0 ; x<2; ++x)

  cout<< x <<;

switch (x) {

  case 0:

  cout<< "scissors";

  break;

  case 1:

  cout<< "rock";

  break;

  case 2:

  cout<< "paper";


  

 }

 while (!validInput) {cout << "Bad input";

      return 0; }

   }

}

Last edited on
If you don't mind requiring the user to enter a single character of type char, you can do whatever you want in a switch/case scenario:

In type char:

'P' = 80 when represented as int
'Q' = 81 when. ..
'R' = 82 etc.
'S' = 83

So there are your four choices.

If you want to get cute, you can use lower case char & 95 to convert to upper case, and subtract 80 to get choices 0, 1, 2, 3 for your switch/case instead. This makes your computer choice rand() %4 !=1:

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
do 
{
// some instructions here, explain the game, etc.
// could include some error message if the loop conditions have forced another iteration

cin>> userchoice; // userchoice is type char
userchoice = userchoice & 95 - 80;
} while (userchoice < 0 || userchoice > 3);

//if the userchoice is not 1, play the game:

do
{ computerchoice = rand() %4; }  //computerchoice is type int
while (computerchoice == 1); // computer not allowed to quit!

//now you have two char (or int) values for your switch:

bool win = false;  //preset this so you only need two lines for each choice

switch (userchoice){
case 0:
win = (computerchoice == 2); // 2 is rock
break;
case 2:
win = (computerchoice == . . .); //etc
}

// if win is true; increment wins 
// else increment losses 
// display results etc. . . 


Last edited on
Yep, thanks for reply. I have not got that far, to compare user/computer inputs.

2) Maps would be one solution

to the problem:

"If you don't mind requiring the user to enter a single character of type char, you can do whatever you want in a switch/case scenario:"

But I don`t want to use maps because

1) I don`t understand them

2) I don`t have enough information to learn them.

Here is my code so far :

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

#include <iostream>
#include <vector>

using namespace std;



int main()
{
    char a;
    cout << "Enter "r", "p", or "s": ";
    cin>>a;

    
    
    bool validInput;
    if (a == "r" || a == "p" || a == "s")
    {
        validInput = true;
    }
    else
    {
        validInput = false;
    }

    if (validInput)
    {

   
vector<int> myvector {0,1,2};

int x=0;


generate (myvector.begin(), myvector.end(), RandomNumber);
  


     switch(x) {

     case "0":
     cout<< "scissors";

     break;
 
     case "1":
     cout<<"rock";

     break;

     case "2":

     cout<<"paper";

    }


 

   
    }
    else
    {
        std::cout << "Bad input";
    }
    return 0;
}

 



EIDT: edited the code, but should be something like this.


Last edited on
cout << "Enter "r", "p", or "s": ";

There's a quotation mark problem here - you'll get errors because cout, when evaluating this line, will be looking for variables r, p and s. You'll need to change all of the double quotes from delimiters to literals except the first and last ones if you want to include them in your instructions.




if (a == "r" || a == "p" || a == "s")

for the expression a == r, the compiler will report:

Error: operand types are incompatible ("char" and "const char *")


should be (a == 'r'. . . etc.)

Also:

A single-letter char input can actually be used in your case without translation:

Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool validInput = true;
switch (a){
case 'R':
case 'r':
// do something;
break;

case 'S':
case 's';
// do something else
break;

case 'P';
case 'p';
//do even more
break;

case etc. . .:
break;

default:
validInput = false;
}
etc. . .
Last edited on
OK.

I am a complete beginner, so Rock, Paper and Scissors game requires two days for me before I get it right.

Now I have this:

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

int main()
{
    char userchoice= 'a';
    cout << "Enter r, p, or s: ";
    cin>>userchoice;

   

    
    
    bool validInput;
    if (a == 'r' || a == 'p' || a == 's')
    {
        validInput = true;
    }
    else
    {
        validInput = false;
    }

    if (validInput)
    {

   switch(a) {

     case 'r':
     userchoice=0; 

     break;
 
     case 'p':
     userchoice=1;

     break;

     case 's':
     userchoice=2;
     

    }

  }
    


if ((userchoice==0 && comp==2 ) || (userchoice==1 && comp==2) || (userchoice==2 && comp==1))

{
cout<<"You win!";
wins++;
}

else if ((choice==comp ){

cout<<"Tie!"; }

else {

cout<<"You lose!";
loses++;

}



How to generate computer move is another issue. My idea was to have a vector of 3 ints and then make the integers come in random order, after that assign them to corresponding string values.


EDIT: Actually I found this: http://www.cplusplus.com/reference/cctype/tolower/
Last edited on
Look at this:

1
2
if ( a == 'b') c = 3;
if (c == 3) { do something};


isn't that the same as:

1
2
if (a == 'b') c = 3;
if (c == 3)  {do something};


That is what you're doing in some places:

1
2
3
4
if (a == 'r'. . . )
 {validInput = true; }
. . .
if (validinput){ . . .}


is the same as:

1
2
3
4
5
if (a == 'r'. . . )
{validInput = true; }
. . .
if (validinput)
{ . . .};


and

1
2
3
4
case 'r':
userchoice=0;
 . . . .
if (userchoice == 0. . .){. . .};


is the same as this:

1
2
3
4
5
case 'r':
userchoice=0;
 . . . .
if (userchoice == 0. . .)
{. . .};


this:

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
bool validinput = true;  //placed here so you don't have to repeat it for each valid case.
int result = 0;   // flag initialization
// have the computer make its choice here. . .

   switch(a) {

     case 'r':
            // compare with computer choice and 
            // establish win/lose/draw with a flag variable
            // could be <0> = tie, <1> = win, <2> = loss
     break;
 
     case 'p':
            // compare . . . 
     break;

     case 's':
            // compare . . .
     break; 

     default:
     validinput = false;
)
// use validiput here to decide whether to play on or ask for valid entry
// if you continue:
// use your flag variable in another switch/case 
//to add to losses, wins or ties as necessary and display the results. 


can simplify the entire program, performing the validity check and computing the results with a smaller number of logical comparisons.

Last edited on
I have played whole day chess so I don`t have much time today anymore but my current code looks like this:

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

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;



int main()
{
    unsigned seed;
    int comp;
    char userchoice= 'a';
    cout << "Enter r, p, or s: ";
    cin>>userchoice;

    seed = time(0);
    srand(seed); 
        
    comp = rand() % 3 + 1; 

    
    
    bool validInput;
    if ('a' == 'r' || 'a' == 'p' || 'a' == 's')
    {
        validInput = true;
    }
    else
    {
        validInput = false;
    }

    if (validInput)
    {

   switch('a') {

     case 'r':
     return 0;; 

     break;
 
     case 'p':
     return 1;

     break;

     case 's':
     return 2;
     

    }

  }




    
if (('a'==0 && comp==2 ) || ('a'==1 && comp==2) || ('a'==2 && comp==1))

{
cout<<"You win!";
wins++;
}

else if (('a'==comp ){

cout<<"Tie!"; }

else {

cout<<"You lose!";
loses++;

}

}


 



I should remove valid input?

How to generate computer move is another issue. My idea was to have a vector of 3 ints and then make the integers come in random order, after that assign them to corresponding string values.


is way, way too complicated!

comp = rand % 3

will set comp to 0, 1, 2 randomly.

Which is perfectly fine and simple.

const char choices[3][10] = {"Paper","Rock","Scissors"}

lets you choose which choice has been made using:

choices[comp];

if you do use an array to define the choices, you can assign a value to userchoice in the switch(a) block and use it to display the player choice too.
Do you want to use multidimensional arrays?

The game split up to 6 different cases, consider

rock vs scissors // bool win=true

paper vs scissors // bool win=false

rock vs paper // bool win=false

paper vs paper // int res=draw

scissors vs scissors

rock vs rock


But why [3] [10] ?

can I write

const char [3] [3]

foo [2] [0] = rock vs scissors
The array is simply a container for the descriptions that arise from the computer and/or the player choice. A char array has to have room in each "row" of the three choices[0],choices[1], choices[2] to contain the longest set of characters that describe the choices (Scissors == 8 char + /0), so the array has to be at least choices[3][9]. I just estimated instead of counting characters. The important thing is that the second dimension has to be at least the length of the longest element plus 1. Using an array for this purpose has a lot of advantages. If you display based on a switch/case you end up with something like this:

1
2
3
4
5
6
7
8
9
10
cout << "Computer choses: ";
switch (computerchoice){
case 0: cout << "rock " << endl;
break;
case 1: cout << "paper" << endl;
break;
case 2: cout << "scissors" << endl;
break;
}
// repeat for playerchoice. . . lots of extra code  


Using an array of choices, if the computer selects integer 1,
computerchoice = 1
all you have to is:
1
2
cout << "Player choses " << choices[playerchoice] << endl;
cout << "Computer choses " << choices[computerchoice]<< endl;


The logic for tie, win, or lose is simple too:

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
if (playerchoice == computerchoice)
{ // tie handling routine}
else
{
bool win = false; //so you don' t have to make multiple assignments if you do win,
 // only have to change this if player wins

switch (playerchoice)
{
case 'r':             // rock
case 'R':            // Rock in upper case
   win = (computerchoice == 2) ;  // can only win (computer ==2) or lose (computer == 1)
                         // because tie has already been ruled out by first if statements
  break;
case 'p': // paper 
case 'P': // Paper
   win = (computerchoice == 0);
etc.
} // end of case block so display result

cout << "You chose " choices[playerchoice] << endl;
cout << "Computer chose " choices[computerchoice] << endl  << ". You "
if (win) cout << "lose.";
else cout << "win.";
cout << endl;
} // tie result resumes here, so display the score  



Last edited on
Something like this:

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

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;



int main()
{
    unsigned seed;
    int comp;
    char userchoice= 'a';
    cout << "Enter r, p, or s: ";
    
    while(cin >> 'a') {	// we'll as long as we get "good" input and then stop

    

   

    const char choices[3][9] = {"Paper","Rock","Scissors"}   // Scissors has 8 characters

    seed = time(0);
    srand(seed); 
        
    comp = rand() % 3 + 1;         // have the computer make its choice here. . .

   switch (playerchoice) {

   case 'r': return 0;
   break;
   case 'p': return 1;
   break;
   case 's': return 2;

}
                                                   

   choices[player];

   switch (playerchoice){
   case 0: cout << "rock " << endl;
   break;
   case 1: cout << "paper" << endl;
   break;
   case 2: cout << "scissors" << endl;
   break;
}


    choices[comp];

    cout << "Computer choses: ";
   switch (computerchoice){
   case 0: cout << "rock " << endl;
   break;
   case 1: cout << "paper" << endl;
   break;
   case 2: cout << "scissors" << endl;
   break;
}


cout << "Player choses " << choices[playerchoice] << endl;
cout << "Computer choses " << choices[computerchoice]<< endl;

	

 
int result = 0;   // flag initialization

if (([playerchoice]=0 && [computerchoice]==2 ) || ([playerchoice]==1 && [computerchoice]==2) || ([playerchoice]==2 && [computerchoice]==1))

{
int result=1;
 cout<<"You win!"; ;
++result;
}

else if (([playerchoice]==[computerchoice] )

{
int result=2;
cout<<"Tie!"; ++result }


else 

{ 
int result=3;

cout<<"You lose!"; ++result }

 }

}

}
 
                         
Well, I can see there are some things you still need to work through:

look at the comments, see if you can get a better idea how switch/case can help you replace if (){}, make sure you don't overthink the logic. You only need to compare user/computer once.

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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;



int main()
{
    	unsigned seed;
    	int comp;
    	char userchoice= 'a';
    	cout << "Enter r, p, or s: ";
    
    	while(cin >> 'a') 
	{		// we'll as long as we get "good" input and then stop

    			// cin can't put anything into "const char = 'a'"
			// so you need to input to userchoice here

		// move the next three lines to the beginning - before your while statement
		// you only need to do these tasks once.

    		const char choices[3][9] = {"Paper","Rock","Scissors"}   // Scissors has 8 characters

   		seed = time(0);

    		srand(seed); 
        
    		comp = rand() % 3 + 1;   // have the computer make its choice here. . .

   		switch (playerchoice) // don't get your variables mixed up -- you declared userchoice earlier
		
		{
   		case 'r': return 0; // what does return do in main()?
			// compare to computer choice here and in the next two cases
   		break;
   		case 'p': return 1; // you can put all the win/lose/tie logic in this set of cases
   		break;
   		case 's': return 2;

		} //this ends the switch (playerchoice) routine . . . what does it do?
                                                   

   		choices[player]; // this statement can't do anything 

		// starting here, delete the two switch/case 
 
  		switch (playerchoice) // I'm sorry if you got misled - this switch/case and the next one
					// aren't necessary if you use the array choices[3][10]



		{
   		case 0: cout << "rock " << endl;
   		break;
   		case 1: cout << "paper" << endl;
   		break;
   		case 2: cout << "scissors" << endl;
   		break;
		}


    		choices[comp];

    		cout << "Computer choses: ";
   		switch (computerchoice)
	
		{
   		case 0: cout << "rock " << endl;
   		break;
   		case 1: cout << "paper" << endl;
   		break;
   		case 2: cout << "scissors" << endl;
   		break;
		}

		// this is where you end removing the switches
		// the next two lines replace all of the preceeding two switch/cases

		cout << "Player choses " << choices[playerchoice] << endl;
		cout << "Computer choses " << choices[computerchoice]<< endl;
		
		
 
int result = 0;   // flag initialization needs to go up to the beginning too

// don't need the next statement -- you already have the if for playerchoice in the first switch, right?

if (([playerchoice]=0 && [computerchoice]==2 ) || ([playerchoice]==1 && [computerchoice]==2) || ([playerchoice]==2 && [computerchoice]==1))

// if you need to use a result value, set it in the first switch / case statements in

{
int result=1;
 cout<<"You win!"; ;
++result;
}

else if (([playerchoice]==[computerchoice] )

{
int result=2;
cout<<"Tie!"; ++result }


else 

{ 
int result=3;

cout<<"You lose!"; ++result }

 }

}

}
 
                         
Last edited on
Well, I haven`t had much time to program lately, but I took some advice and now it compiles thanks to you! (I have to admit that there must be some mistakes involved because if I enter a character the program quits).

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

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;



int main()
{
    	unsigned seed;
    	int comp;
        int draw=0;
        int win=0;
        int loss=0;
      
    	char userchoice= 'a';
    	cout << "Enter r, p, or s: ";

        const char choices[3][9] = {"Paper","Rock","Scissors"};   

   		seed = time(0);

    		srand(seed); 
        
    		comp = rand() % 3 + 1;   
    
    	while(cin >> userchoice) {


bool win = false; 


  if (userchoice == comp)

     { int result=2;
     cout<<"Tie!"; ++draw; }

  else 

    {


		switch (userchoice) 
		
		{
   		case 'r': 
			  return 1; win = (comp == 3);
   		break;
   		case 'p': return 2; win = (comp == 1);
   		break;
   		case 's': return 3; win = (comp == 2);

		} 
    }

}                                                          // ends the while statement


 cout << "Player choses " << choices[userchoice] << endl;
 cout << "Computer choses " << choices[comp]<< endl;
		

if (win == true) {cout<<"You won!"; ++win; }

else { cout<<"You lost"; ++loss; }

                                                   
cout << " score: you==" << win << " computer==" << loss << " same==" << draw << "\n";
   		
cout << "Please try again: ";
	
while (!cin >> userchoice) {

cin.clear();

cout << "Please try again\n"; }

}              
		




It remains a mystery for me though what is the meaning of uppercase or lowercase letters.

Maybe one day, I learn C++, but now seems impossible.
Last edited on
One problem I see is that at line 33 you declare win as a bool variable. This variable has the scope of your while loop (lines 30-59). It hides the win variable you declared at line 16. Lines 50-54 set the instance of win declared at line 33. At line 66, you're testing the variable declared at line 16, which you've never modified.

It remains a mystery for me though what is the meaning of uppercase or lowercase letters.

C and C++ are case sensitive languages. Variables or functions named foo and Foo are different.



Last edited on
You're getting closer, but there is one glaring issue:

user entry:

char userchoice
with legal values 'r', 'p','s'

can NEVER equal int comp in range 0 to 2. . .

SO read the following, I think you can rework slightly and should get to the finish line:

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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    	unsigned seed;
    	int comp;
        int draw=0;
        int wins=0;
        int loss=0;
        const char choices[3][9] = {"Paper","Rock","Scissors"}; 

	 char userchoice= 'a';
	 int userint=  0;

    	cout << "Enter r, p, or s: ";

   	    seed = time(0);

   	    srand(seed);
         
    	comp = rand() % 3 + 1;   
    
    	while(cin >> userchoice)
	
    	{
		
		// since we can't recalculate userchoice into an int in range 0-2;
		// let's set int result to 0 = draw, 1 = win, -1 = loss in the switch commands and
		// use the switch/case logic to assign an int value for user choice from the array:
	        int result = 0 so we can cout choice[userint];

        	switch (userchoice)
	        {
	            case 'p': // is choice [0]
	            case 'P': // upper case okay too
		            if (comp == 2) result--;  // result is -1 for loss
		            if (comp == 1) result++;  // result is +1 for win
		            userint = 0;
		            break;
	            case 'r'// :etc.. . (keep going here for R, s and S, using the same type of commands
			// r would be 1, s would be 2 for userint, and you can figure the result using same
			// values for comp (0 = Paper, 1 = Rock, 2 = Scissors.

        	}

             cout << "Player choses " << choices[userint] << endl;
             cout << "Computer choses " << choices[comp]<< endl;
		
	        switch (result) 
        	{
            	case 0:
            		cout << "You Tied!";
            		draw ++;
	            	break;
	        case -1:
	            	cout << "You Lost!";
	            	loss ++;
	            	break;
	        case 1:
		        cout << "You Won!";
		        wins ++;
	        }
    }
}


You'll have to complete some missing cases, and your couts for the scores.

Might want to change the endings, too so you have a graceful exit. . .
Well I´m not sure what to change so I did

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

while(cin >> userchoice)
	
    	{

         switch (userchoice) {

        case 'p': "Paper"=choices[0] break;
        case 'r': "Rock"=choices[1] break;
        case 's': "Scissors"=choices[2] break;

       }
		
 
	 
             int result = 0;                   //so we can cout choice[userint];


        	switch (userchoice)
	        {
	          case 'p': 
		            if (comp == 3) result--;  // result is -1 for loss
		            if (comp == 2) result++;  // result is +1 for win
		            userint = 0;
		            break;

	          case 'r': 
		            if (comp == 1) result--;  
		            if (comp == 3) result++;  
		            userint = 1;
                            break;

                  case 's': 
		            if (comp == 2) result--;  
		            if (comp == 1) result++;  
		            userint = 2;
        	}           break;

             cout << "Player choses " << choices[userint] << endl;
             cout << "Computer choses " << choices[comp]<< endl;
		
Lines 8-10: What are you trying to accomplish? You can't assign something to a character literal.

Line 6: I don't see that you need this switch statement at all. The switch statement at line 19 branches on the same variable.



Pages: 12