Rock Paper Scissors

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

enum weapon {rock = 0, paper = 1,scissor = 2};

class Player
{
public:
    void Set(int n){
        Weapon = n;
    }

    int Get(){
    return Weapon;
    }
    
    
    
private:
    
    int Weapon;
};

int main()
{
    Player ChoiceRock;
    Player ChoicePaper;
    Player ChoiceScissors;
    
    ChoiceRock.Set(0);
    ChoicePaper.Set(1);
    ChoiceScissors.Set(2);
    
    cout << "Enter number of your Weapon choice Rock = " << ChoiceRock.Get() << "Paper = " << ChoicePaper.Get()<< "Scissors = " << ChoiceScissors.Get() <<endl;
    
    
}



Hi. The code above is a simple game I am working on using Set and Get methods. I have completed the rest of the codes to the game for example, instructions and gameplay. But I am stuck on the user input, for example user entering the number "0" for Rock.

From my understanding, Set and Get methods are for setting values and retrieving those values, so in this method can I use the Set and Get methods for user input or I can't?

I would like to thank you in advance for the help.

You code seems very confused. You have created three player objects and you're using them to tell the player what number to pick for weapon choice.

This makes much more sense for that:
cout << "Enter number of your Weapon choice Rock = 0 , Paper = 1, Scissors = 2" <<endl;

The logic should be something like:

Create ONE player object.
Output instructions to screen (i.e. my code above).
Get user input
1
2
3
int userInput;
cin >> userInput;
playerObject.set(userInput);



Thank you for the help. But how would one go about and create an object? I have read instructional files on the internet, which is confusing me.

Thank you in advance for the help.
1
2
3
4
5
6
7
8
9
10
11
int x; // This creates an object of type int, named x

double y; // This creates an object of type double, named y

string z; // This creates an object of type string, named z

string z("eggs"); // This creates an object of type string, named z, AND gives it an initial value of "eggs"

double y = 4.13; // This creates an object of type double, named y, AND gives it an initial value of 4.13

Player PlayerObject; // This creates an object of type player, named PlayerObject 
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
class Player 
{
private:
int rock,paper,scissors;
int number;
int score;

public:
void setWeapon() 
{
	if(number == 1)
	cout << "Rock" << rock << endl;
	if(number == 2)
	cout << "Paper" << paper << endl;
	if(number == 3)
	cout << "Scissors" << scissors << endl;
	else{
		cout<<"ERROR Invaild number"<<endl;
	}
}
	int getWeapon() {return number;}
};
int main()
{
	Player pWeapon;
	int input;
	cout<<"Enter number of your weapon choice Rock = 1, Paper = 2, Scissors = 3:"<<endl;
	cin >> input;
	pWeapon.setWeapon();
	cout<<pWeapon.getWeapon()<<endl;
	return 0;
}


Thank you so much, that really help. As you can see from the code above I managed to get it working. It does compile, however, if I type in 2 for Paper it outputs an error.
Your number value is not valid when setWeapon is called. You need to assign the right value to number. You can pass through setWeapon(input).
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
#include <iostream>
using namespace std;
class Player 
{
private:
int rock,paper,scissors;
int number;
int score;
public:
void setWeapon() 
{
	if(number >= 1 || number <= 3)
	cout<<number;
	else if(number == 1)
	cout<<"Rock"<<rock<<endl;
	else if(number == 2)
	cout << "Paper"<<paper<<endl;
	else if(number == 3)
	cout << "Scissors"<<scissors<<endl;
	else{
		cout<<"ERROR Invaild number"<<endl;
	}
}
	int getWeapon() {return number;}
};
int main()
{
	Player pWeapon;
	int input;
	cout<<"Enter number of your weapon choice Rock = 1, Paper = 2, Scissors = 3:"<<endl;
	cin >> input;
	pWeapon.setWeapon();
	cout<<pWeapon.getWeapon()<<endl;
	return 0;
}


Hi. So I have tried to assign the right value to number, but after compiling and typing in a number, for example 2, it returns 00.

Where am I wrong?? I am finding it difficult to assign the right value to number?
Standard setter function:

void setValue(type id);

You're not sending the user input to the class at all. How is it going to have any idea what it wants?

1
2
3
cout<<"Enter number of your weapon choice Rock = 1, Paper = 2, Scissors = 3:"<<endl;
cin >> input;
pWeapon.setWeapon(input);
Let's take a look at your code.

Player pWeapon;
Create an object of type Player, named pWeapon

int input;
Create an object of type int, named input

cout<<"Enter number of your weapon choice Rock = 1, Paper = 2, Scissors = 3:"<<endl;
Output some words to the screen.

cin >> input;
Get a value from the keyboard and store it in the variable named input.

pWeapon.setWeapon();

Now call the class function setWeapon() on the Player object. This function takes no parameters, so we are passing it no information.

Let's follow to that function:
if(number >= 1 || number <= 3)
So now we compare the variable number. What value does number have? It is a class variable and you never gave it a value, so it will be some random number. It could be anything.


 
I am finding it difficult to assign the right value to number? 

Here is how to assign a value to number.
number = 7;
This assigns the value 7 to number.

Here is another example:
number = x;
This assigns whatever the value of x is (in this example) to number.

Your code nowhere attempts to set the value of number. I think that right now classes are too advanced for you. You need to go back and learn:

1) Assigning values to things
2) Functions


Last edited on
I have successfully completed this game. I would like to thank you for the help and advice.
Topic archived. No new replies allowed.