How to add an option to repeat the code and a win count for every round.

Hi everyone, I'm just a beginner here and would appreciate it if anyone could help me add an option to repeat the game and add a win count after every round.

The game I was trying to code is rock, paper, scissors, and I got everything down except for the part I need help with. Thank you all. Below is my code...

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
 #include <iostream>
#include <time.h>
#include <iomanip>

	const char rock = 'r';
	const char paper = 'p';
	const char scissors = 's';

using namespace std;

char ComputerChoice() {
	srand(time(0));

	int num = rand() % 3+1;

	if(num==1) return 'r';
	if(num==2) return 'p';
	if(num==2) return 's';
}

char MyChoice() {
	char c;
	cout << "Let's play rock,paper,scissors!" <<endl;
	cout << "In order to play,choose one of the following options" <<endl;
	cout << "(r) for rock" " " "(p) for paper" " " "(s) for scissors" <<endl;
	cin >> c;

	while (c!='r' && c!='p' && c!='s')
	{
		cout<< "We can't use this for rock,paper,scissors! Please choose one of the following only."<<endl;
		cout<< "(r) for rock" " " "(p) for paper" " " "(s) for scissors"<<endl;
		cin>> c;
	}

	return c;
}

void SelectedOption (char option) {
	if (option == 'r') cout << "Rock" <<endl;
	if (option == 'p') cout << "Paper" <<endl;
	if (option == 's') cout << "Scissors" <<endl;
}

void chooseWinner(char mChoice, char cChoice) {
	if (mChoice == rock && cChoice == paper) {
		cout<< "Computer Wins! Paper beats rock."<<endl;
	}
	else if (mChoice == paper && cChoice == scissors) {
		cout << "Computer Wins! Scissors beats paper."<<endl;
	}
	else if (mChoice == scissors && cChoice == rock) {
		cout << "Computer Wins! Rock beats scissors."<<endl;
	}
	else if (mChoice == rock && cChoice == scissors) {
		cout<< "You win! Rock beats scissors."<<endl;
	}
	else if (mChoice == paper && cChoice == rock) {
		cout<< "You win! Paper beats rock."<<endl;
	}
	else if (mChoice == scissors && cChoice == paper) {
		cout<< "You win! Scissors beats paper."<< endl;
	}
	else{
		cout << "Tie! Play again!" <<endl;
	}
}

int main()
{

	char mChoice;
	char cChoice;

	mChoice = MyChoice();
	cout<< "Your choice is: " <<endl;
	SelectedOption(mChoice);

	cChoice = ComputerChoice();
	cout << "Computer's choice is: " <<endl;
	SelectedOption(cChoice);

	chooseWinner(mChoice, cChoice);

	return 0;
}
 
A while or do/while loop would work to repeat the game until the user wants to quit.

https://www.learncpp.com/cpp-tutorial/intro-to-loops-and-while-statements/
Its just a loop around what you have, roughly:

do //line 73
{...
if(player won) //do you have a way to know this, or can you get it?
count++ //create this out of the loop initially zero etc
cout do you want to play again? //create variable to deal with this response, cin their answer...
cin >> variable;
}
while(variable != 'N' || variable != 'n');
Last edited on
> and I got everything down except for the part I need help with.
You mean you found the code and now you're stuck.
https://www.mycplus.com/source-code/cplusplus-source-code/rock-paper-scissors-game-in-c/

You can't copy/paste your way through learning.

Topic archived. No new replies allowed.