Writing a Rock-Paper-Scissors game using different functions.

[C++]
Hi! I'm very new at programming and I need your help with my homework. I've had a hard time correcting mistakes but whatever I changed, it just seems to get worse or get a little bit better.

A little note that we were given specific functions to use and we can add or modify the functions! I'll list the functions here:
1. int random();
2. string input();
3. string compChoice(int);
4. void displayCompChoice(string);
5. bool compare(string, string);

Here's my code by the way! Thank you for helping me! <3

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int random();
int input();
string compChoice(string);
string inputChange(string);
void displayCompChoice(string);
void displayInput(string);
bool compare(string, string);

int main(){
	
	string h, d, c, k, w;
	
 	displayInput(k);
 	displayCompChoice(h);

 	
 	if (compare(c, d)==true) {
 		cout<<"You Win!"<<endl;
 		cout<<" wins over "<<w;
 		return 0;
	 }
	else if (compare(c, d)==false) {
	 	cout<<"Computer Wins!"<<endl;
	 	cout<<w<<" wins over ";
	 	return 0;
	}
	else if (inputChange==compChoice){
		//repeat the code until both are different from each other
	}

	 
 
 	

 	
 	return 0;
}

int random() {
	srand(time(NULL));
 	int x;
 	x = rand()%3+1; //generate random number from 1-3
	 return x;
}

int input(){
	
	int f;
	cout<<"=== MENU ==="<<endl<<"Choose your fighter: "<<endl;
	cout<<"1.   Rock"<<endl<<"2.   Paper"<<endl<<"3.   Scissors"<<endl;
	cout<<endl<<"Enter your choice: ";
	cin>>f;
	return f;
}

string inputChange(string v) {
	switch (input()) {
		case 1: {
			v="rock";
			return v;
			break;
		}
		case 2: {
			v="paper";
			return v;
			break;
		}
		case 3: {
			v="scissors";
			return v;
			break;
		}
	}
}

void displayInput(string k) {
	string v;
	cout<<endl<<"You have chosen: "<<inputChange(v);

}

string compChoice(string g) {
	switch (random()) {
		case 1: {
			g ="rock";
			return g;
			break;
		}
		case 2: {
			g ="paper";
			return g;
			break;
		}
		case 3: {
			g ="scissors";
			return g;
			break;
		}
	}
}

void displayCompChoice(string h) {
	string g;
	cout<<endl<<"Computer choice: "<<compChoice(g);
}

bool compare(string a, string b) {
	string g, v;
	a=inputChange(v);
	b=compChoice(g);
	
	if (a=="rock" && b=="scissors") {
		return true;
	}
	else if (a=="paper" && b=="rock") {
		return true;
	}
	else if (a=="scissors" && b=="paper") {
		return true;
	}
	else if (a=="rock" && b=="paper") {
		return false;
	}
	else if (a=="paper" && b=="scissors") {
		return false;
	}
	else if (a=="scissors" && b=="rock") {
		return false;
	}
	else {
		return 0;
	}
	}


The output should be:

=== MENU ===
Choose your fighter:
1. Rock
2. Paper
3. Scissors

Enter your choice: 1

You have chosen: rock
Computer choice: scissors

You win!
rock wins over scissors
Last edited on
My first suggestion would be to write functions that match the assignment suggestions.

1. int random();
int random();
OK so far

2. string input();
int input();
You return an int, but the suggestion was string.

3. string compChoice(int);
string compChoice(string);
The suggestion was to pass an int, not a string.
At a guess, you're meant to pass the result of random() into the function, not call random() within the function.

> string inputChange(string);
Huh?
If input() did what was supposed to, this wouldn't be necessary.

4. void displayCompChoice(string);
void displayCompChoice(string);
Looks good, but it calls compChoice(). Again, this is a parameter that gets passed in.

5. bool compare(string, string);
bool compare(string, string);
Seems OK, if a little strangely named.


>string h, d, c, k, w;
Pick meaningful names.

Your main should be something like this
1
2
3
4
5
6
7
8
9
10
string human = input();
string computer = compChoice(random());
// call the two display functions
if ( compare(human,computer) ) {
  cout<<"You Win!"<<endl;
  cout<<human << " wins over "<< computer << endl;
} else {
  cout<<"Computer Wins!"<<endl;
  cout<<computer<<" wins over " << human << endl;
}

Today I learned that Linux systems have the "random" name already defined (polluting stdlib), which prevents your code from compiling on cpp.sh
https://linux.die.net/man/3/random

You might want to change the name of your random function be something else, or to put it in its own namespace. Your code will still compile on Windows, however.

______________________

But as far as your logic goes:

(1) You claim your function is string compChoice(int); But you are creating it as string compChoice(string).
Which is it? Notice the places where you have a function parameter, but don't actually use it.

(2) The logic within your displayInput and displayCompChoice functions do not connect back to the logic in your main function.

Instead of receiving user input within your 'display' functions, you should call your 'input' or 'inputChange' function directly from main, and make sure you save the return value if that function in a variable. (Also, the name 'inputChange' is misleading, I would go with the structure salem c mentioned.)

Your display___ functions should only be for displaying. You shouldn't call inputChoice/compChoice from here.
Last edited on
Consider 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int random();
string input();
string compChoice(int);
void displayCompChoice(const string&);
bool compare(const string&, const string&);

void displayInput(const string&);
string name(int);

int main() {
	srand(static_cast<unsigned int>(time(nullptr)));

	const string human {input()};
	const string computer {compChoice(random())};

	displayInput(human);
	displayCompChoice(computer);

	if (human == computer)
		cout << "\nDraw\n";
	else if (compare(human, computer)) {
		cout << "\nYou Win!\n";
		cout << human << " wins over " << computer << '\n';
	} else {
		cout << "\nComputer Wins!\n";
		cout << computer << " wins over " << human << '\n';
	}
}

int random() {
	return rand() % 3 + 1;
}

string input() {
	int f {};

	do {
		cout << "=== MENU ===\nChoose your fighter:\n";
		cout << "\n1.   Rock\n2.   Paper\n3.   Scissors\n";
		cout << endl << "Enter your choice: ";

		cin >> f;
	} while ((f < 1 || f > 3) && (std::cout << "Invalid choice\n"));

	return name(f);
}

string name (int choice) {
	switch (choice) {
		case 1:
			return "rock";

		case 2:
			return "paper";

		case 3:
			return "scissors";
	}
}

void displayInput(const string& choice) {
	cout << "\nYou have chosen: " << choice << '\n';
}

string compChoice(int comp) {
	return name(comp);
}

void displayCompChoice(const string& choice) {
	cout << "Computer choice: " << choice << '\n';
}

bool compare(const string& hum, const string& com) {
	if (hum == "rock" && com == "scissors")
		return true;

	if (hum == "paper" && com == "rock")
		return true;

	if (hum == "scissors" && com == "paper")
		return true;

	return false;
}

Thank you all for the tips! The code's working now by the way! (after some few revisions) :))
Topic archived. No new replies allowed.