Help with rock, paper, scissors, lizard, spock

Hey all! I am a newb and need a little guidance. Quick note to let you know where I'm at: I have done some personal study, and tutorials off and on for a few years and have gotten to the point where I want to hit the programming hard. I did a tutorial for javascript a while back, and it taught me rock, paper, scissors. At the time I modified it to rock, paper, scissors, lizard, spock. Now I have attempted to convert the javascript to C++ here is my attempt.
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
  #include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main(void) {
	string playerName;
	string userChoice;
	string computerChoice;
	cout << "What is your name?";
	getline (cin, playerName);
	cout << "Hello, " << playerName<< "!";
	cout << "Do you choose rock, paper, scissors, lizard or Spock?";
	getline (cin, userChoice);
	computerChoice = rand() % 5 + 1;
	if (computerChoice = 1) {
		computerChoice = "rock";
	}
	else if (computerChoice = 2) {
		computerChoice = "paper";
	}
	else if (computerChoice = 3) {
		computerChoice = "scissors";
	}
	else if (computerChoice = 4) {
		computerChoice = "lizard";
	}
	else if (computerChoice = 5) {
		computerChoice = "spock";
	}
	if (computerChoice == userChoice) {
		cout << "It's a draw";
	}
	else if (computerChoice = "rock") {
		if (userChoice = "scissors") {
			cout << "Rock wins";
		}
		else if (userChoice = "lizard") {
			cout << "Rock wins";
		}
		else if (userChoice = "spock") {
			cout << "Spock wins";
		}
		else if (userChoice = "paper") {
			cout << "Paper wins";
		}
	}
	else if (computerChoice = "paper") {
		if (userChoice = "rock") {
			cout << "Paper wins";
		}
		else if (userChoice = "spock") {
			cout << "Paper wins";
		}
		else if (userChoice = "scissors") {
			cout << "Scissors wins";
		}
		else if (userChoice = "lizard") {
			cout << "Lizard wins";
		}
	}
	else if (computerChoice = "scissors") {
		if (userChoice = "paper") {
			cout << "Scissors wins";
		}
		else if (userChoice = "lizard") {
			cout << "Scissors wins";
		}
		else if (userChoice = "rock") {
			cout << "Rock wins";
		}
		else if (userChoice = "spock") {
			cout << "Spock wins";
		}
	}
	else if (computerChoice = "lizard") {
		if (userChoice = "paper") {
			cout << "Lizard wins";
		}
		else if (userChoice = "spock") {
			cout << "Lizard wins";
		}
		else if (userChoice = "rock") {
			cout << "Rock wins";
		}
		else if (userChoice = "scissors") {
			cout << "Scissors";
		}
	}
	else if (computerChoice = "spock") {
		if (userChoice = "rock") {
			cout << "Spock wins";
		}
		else if (userChoice = "scissors") {
			cout << "Spock wins";
		}
		else if (userChoice = "paper") {
			cout << "Paper wins";
		}
		else if (userChoice = "lizard") {
			cout << "Lizard wins";
		}
	}
	system("pause");

	return 0;
}


I have 60 errors most of them being userChoice and computerChoice need to have a bool type or be converted to a bool. The other errors are all my if, else if statements register as "conditional expression of type...is illegal." What do I need to do to get from this to a working script? Also, thank you in advance =D
Last edited on
Your if statements need a double equal sign, not a single one, I can tell you that much, but you are also attempting to use the strings userChoice and computerChoice as integers. I'm not sure where you came up with the idea of lizard and spock, but I get the idea. Use separate integer variables with values 1-5 that determine each string's value, and correct the single equal signs in each if statement, as they will apply that value rather than check for it.
In your code, you've declared computerChoice as a string, but then you try to assign it an integer value:
computerChoice = rand() % 5 + 1;
I don't know about Javascript (never coded in it before), but in C++, variables can't change types.
So, you're probably going to want to declare a new (int) variable to hold the random value, and then assign the proper value to computerChoice based on the value of that.

Also, for comparison, you need to use two == signs:
if (computerChoice == 1)

What your code is doing right now is assigning the value 1 to computerChoice and then checking if the new value isn't 0 (which of course it's not).
I see, the double = brought me down to 10 errors. I think I understand what you are saying about the integer variables I'll have to look into that a little more. Thank you for the help.

BTW the idea, for lizard and spock i took from The Big Bang Theory
Topic archived. No new replies allowed.