Rock, Paper, Scissors using enums and functions help!!

Feb 17, 2022 at 11:39pm
the requirements for this assignment are to use enums and functions and an option to play again. im pretty confuse on exactly what I am doing wrong. also extremely new to programing
here what I got any help is great

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

using namespace std;
// enums
enum Option {rock = 'R', paper = 'P', scissors = 'S'};


//functions
void displayInstrutions ()
{
cout << "Lets play rock, paper, scissor against the computer" <<endl;
cout << "Rock beats scissors, scissors beats paper and paper beats rock" <<endl;
}

string getMove() {
int move;
cout << "\nChoices:" << endl;
cout << "R - Rock" << endl;
cout << "P - Paper " << endl;
cout << "S - Scissors " << endl;
cout << "Enter your choice: " << endl;
cin >> move;
move = tolower(move);
if (move == rock) {
cout << "you choose rock" << endl;
}
else if (move == scissors)
{
cout << "you choose scissors" << endl;
}
else if (move == paper)
{
cout << "you choose paper" << endl;
}
else
{
cout << "invalid entry" << endl;
}
return {};
}

int getCompMove()
{
srand(time(0));
const int MAX_VALUE = 3;
const int MIN_VALUE = 1;
auto c_move = (rand() % MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;

if (c_move == 1)
{
c_move = rock;
}
if (c_move == 2)
{
c_move = paper;
}
if (c_move == 3)
{
c_move = scissors;
}
return c_move;
}
bool getWinner(int c_move, int userMove)
{
if ((userMove == rock) && (c_move == 3))
{
return true;
}
else if ((userMove == rock) && (c_move == 2))
{
return false;
}
else if ((userMove == scissors) && (c_move == 2))
{
return true;
}
else if ((userMove == scissors) && (c_move == 1))
{
return false;
}
else if ((userMove == paper) && (c_move == 1))
{
return true;
}
else if (userMove == paper && c_move == 3)
{
return false;
}
else if (userMove == c_move)
{
return "its a tie";
}
else
{
cout << "Invalid entry" << endl;
}
return false;
}

bool playAgain (int playAnswer)
{
const char yes = 'Y';
const char no = 'N';
int answer;
do {
cin >> answer;
answer = tolower(answer);
if (answer != yes && answer != no) {
cout << "invalid entry" << endl;
}
answer = playAnswer;

} while (answer != yes && answer != no);
}


int main() {

//variables
int compMove = 0;
int userMove = 0;

cout << "Wanna play a game?" << endl;

displayInstrutions();

//user choice / function
getMove();
getCompMove();

//winner
cout << "The winner is: " << endl;
getWinner

cout << "would you like to play again? Y or N " << endl;
playAgain(bool)
}
Feb 18, 2022 at 2:30am
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

You should call srand ONE TIME ONLY, not every time you enter getCompMove. You are potentially reseeding the C random generator multiple times before time has a chance to tick to the next time interval, effectively resetting the random generator to the beginning of the same sequence.

Move it to the beginning in main.
Feb 18, 2022 at 10:49am
As formatted:

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

using namespace std;

// enums
enum Option { rock = 'R', paper = 'P', scissors = 'S' };

//functions
void displayInstrutions() {
	cout << "Lets play rock, paper, scissor against the computer" << endl;
	cout << "Rock beats scissors, scissors beats paper and paper beats rock" << endl;
}

string getMove() {
	int move;

	cout << "\nChoices:" << endl;
	cout << "R - Rock" << endl;
	cout << "P - Paper " << endl;
	cout << "S - Scissors " << endl;
	cout << "Enter your choice: " << endl;
	cin >> move;

	move = tolower(move);
	if (move == rock) {
		cout << "you choose rock" << endl;
	} else if (move == scissors) {
		cout << "you choose scissors" << endl;
	} else if (move == paper) {
		cout << "you choose paper" << endl;
	} else {
		cout << "invalid entry" << endl;
	}

	return {};
}

int getCompMove() {
	srand(time(0));

	const int MAX_VALUE = 3;
	const int MIN_VALUE = 1;
	auto c_move = (rand() % MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;

	if (c_move == 1) {
		c_move = rock;
	}

	if (c_move == 2) {
		c_move = paper;
	}

	if (c_move == 3) {
		c_move = scissors;
	}

	return c_move;
}

bool getWinner(int c_move, int userMove) {
	if ((userMove == rock) && (c_move == 3)) {
		return true;
	} else if ((userMove == rock) && (c_move == 2)) {
		return false;
	} else if ((userMove == scissors) && (c_move == 2)) {
		return true;
	} else if ((userMove == scissors) && (c_move == 1)) {
		return false;
	} else if ((userMove == paper) && (c_move == 1)) {
		return true;
	} else if (userMove == paper && c_move == 3) {
		return false;
	} else if (userMove == c_move) {
		return "its a tie";
	} else {
		cout << "Invalid entry" << endl;
	}

	return false;
}

bool playAgain(int playAnswer) {
	const char yes = 'Y';
	const char no = 'N';
	int answer;

	do {
		cin >> answer;
		answer = tolower(answer);

		if (answer != yes && answer != no) {
			cout << "invalid entry" << endl;
		}

		answer = playAnswer;
	} while (answer != yes && answer != no);
}


int main() {
	//variables
	int compMove = 0;
	int userMove = 0;

	cout << "Wanna play a game?" << endl;

	displayInstrutions();

	//user choice / function
	getMove();
	getCompMove();

	//winner
	cout << "The winner is: " << endl;
	getWinner

	cout << "would you like to play again? Y or N " << endl;
	playAgain(bool)

}


L37. Shouldn't you return the move choice?
L41 - as stated above, this should be moved to the beginning of main().
getWinner() - there's no need to have an else statement after a return.
playAgain() - the function needs to return a value. Why is a function argument needed?
L112 - the returned value from the function is not used.
L113 - the returned value from the function is not used.
L117 - This needs to be called with 2 parameters - as per the function definition - and a terminating ; is needed.
L120 - This needs to be called as per its definition and a terminating ; is needed.
Last edited on Feb 18, 2022 at 10:50am
Feb 18, 2022 at 2:01pm
Perhaps something like:

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
#include <iostream>
#include <random>
#include <cctype>

enum Option { rock = 'R', paper = 'P', scissors = 'S' };
enum Result {win = 0, loose, draw};

void displayInstrutions() {
	std::cout << "Lets play rock, paper, scissor against the computer\n";
	std::cout << "Rock beats scissors, scissors beats paper and paper beats rock\n";
}

void display(Option opt) {
	switch (opt) {
		case rock:
			std::cout << "rock\n";
			break;

		case paper:
			std::cout << "paper\n";
			break;

		case scissors:
			std::cout << "scissors\n";
			break;
	}
}

Option getMove() {
	char move {};

	std::cout << "\nChoices:\n"
		<< "R - Rock\n"
		<< "P - Paper\n"
		<< "S - Scissors\n";

	while (true) {
		std::cout << "Enter your choice: ";
		std::cin >> move;

		move = static_cast<char>(std::toupper(static_cast<unsigned char>(move)));

		if (move == rock || move == scissors || move == paper) {
			const auto opt {static_cast<Option>(move)};

			std::cout << "\nYou chose ";
			display(opt);
			return opt;
		}

		std::cout << "Invalid entry\n";
	}
}

Option getCompMove() {
	static constexpr int MAX_VALUE {2};
	static constexpr int MIN_VALUE {0};
	static constexpr Option opt[3] {rock, paper, scissors};
	static std::mt19937 rng(std::random_device {}());
	static std::uniform_int_distribution<int> distrib(MIN_VALUE, MAX_VALUE);

	return opt[distrib(rng)];
}

Result getWinner(Option c_move, Option userMove) {
	if (c_move == userMove)
		return draw;

	if ((c_move == rock) && (userMove == scissors))
		return win;

	if ((c_move == scissors) && (userMove == paper))
		return win;

	if ((c_move == paper) && (userMove == rock))
		return win;

	return loose;
}

bool playAgain() {
	static constexpr char yes {'Y'};
	static constexpr char no {'N'};
	char answer {};

	do {
		std::cin >> answer;
		answer = static_cast<char>(std::toupper(static_cast<unsigned char>(answer)));
	} while (answer != yes && answer != no && (std::cout << "Invalid entry. Please enter again: "));

	return answer == yes;
}

int main() {
	std::cout << "Wanna play a game?\n";
	displayInstrutions();

	do {
		const auto userMove {getMove()};
		const auto compMove {getCompMove()};

		std::cout << "Computer chose ";
		display(compMove);

		switch (getWinner(compMove, userMove)) {
			case win:
				std::cout << "Computer wins\n";
				break;

			case loose:
				std::cout << "User wins\n";
				break;

			case draw:
				std::cout << "Its a draw!\n";
				break;
		}

	} while ((std::cout << "\nWould you like to play again? Y or N: ") && playAgain());
}

Topic archived. No new replies allowed.