Program for a game similar to rock-paper-scissors

In summary, the program is for a game called "proton-neutron-electron" where two players choose between three options (P, N, E). In the event of a tie, player 2 wins. There are nine possible outcomes and the winner is determined based on the combinations of choices made by the players. However, there seems to be an error in the fifth possibility where P2 should be the winner.
  • #1
sandy.bridge
798
1

Homework Statement


I'm writing a program for a game similar to rock-paper-scissors. It is called proton-neutron-electron (P, N, E). There are two players. In the event of a tie, player 2 wins
P1 denotes player 1, P2 denotes player 2, P = proton, N = neutron, E = electron. P1P means player 1 picks proton.
There are 9 possibilities. Here they are:
P1P + P2P = P2 wins
P1P + P2N = P1 wins
P1P + P2E = P1 wins
P1N + P2P = P2 wins
P1N + P2N = P2 wins
P1N + P2E = P2 wins
P1E + P2P = P1 wins
P1E + P2N = P2 wins
P1E + P2E = P2 wins

The Attempt at a Solution


Here is what I have written in the compiler; it does not work. I am not looking for another solution, I would just like to know where I went wrong in my syntax.
PHP:
	char user1, user2;
	bool A, B, C, D, E, F;
	cout << "Player 1, please enter your move: ";
		cin >> user1;
		cout << "Player 2, please enter your move: ";
		cin >> user2;

	if (user2 = 'P')
	{
		A = true;
	}
	else
	{
		A = false;
	}

	if (user2 = 'N')
	{
		B = true;
	}
	else
	{
		B = false;
	}
	if (user2 = 'E')
	{
		C = true;
	}
	else
	{
		C = false;
	}

	if (user1 = 'P')
	{
		D = true;
		}
	else
	{
		D = false;
	}

	if (user1 = 'N')
	{
		E = true;
	}
	else
	{
		E = false;
	}
	if (user1 = 'E')
	{
		F = true;
	}
	else
	{
		F = false;
	}	if ((B&&D)||(C&&D)||(A&&E)||(B&&F))
		{
			cout << "The winner is player 1!";
		}
	else
		{
			cout << "The winner is player 2!";
		}

	return 0;
The way that it is set up now always results in P1 winning and I can't see why. Thanks in advance!
 
Last edited:
Physics news on Phys.org
  • #2


You could start by replacing all = with == and see what happens?
 
  • #3


That didn't work.
 
  • #4


I managed to just do as such:
PHP:
#include <iostream>
using namespace std;

int main() {
	char user1, user2;
			cout << "What move P1? ";
			cin >> user1;
			cout << "What move P2? ";
			cin >> user2;

			if (((user1 == 'P')&&(user2 == 'N'))||((user1 == 'P')&&(user2 == 'E'))||((user1 == 'N')&&(user2 == 'P'))||((user1 == 'E')&&(user2 == 'N')))
				{
					cout << "Player 1 is the winner!";
				}
			else
				{
					cout << "Player 2 is the winner!";
				}


			return 0;
}

I'd still like to know what I was doing wrong for the first part.
 
  • #5


Can you post the changed code that you tried but didn't work?
 
  • #6


PHP:
    char user1, user2;
    bool A, B, C, D, E, F;
    cout << "Player 1, please enter your move: ";
        cin >> user1;
        cout << "Player 2, please enter your move: ";
        cin >> user2;

    if (user2 == 'P')
    {
        A == true;
    }
    else
    {
        A == false;
    }

    if (user2 == 'N')
    {
        B == true;
    }
    else
    {
        B == false;
    }
    if (user2 == 'E')
    {
        C == true;
    }
    else
    {
        C == false;
    }

    if (user1 == 'P')
    {
        D == true;
        }
    else
    {
        D == false;
    }

    if (user1 == 'N')
    {
        E == true;
    }
    else
    {
        E == false;
    }
    if (user1 == 'E')
    {
        F == true;
    }
    else
    {
        F == false;
    }    if ((B&&D)||(C&&D)||(A&&E)||(B&&F))
        {
            cout << "The winner is player 1!";
        }
    else
        {
            cout << "The winner is player 2!";
        }

    return 0;
This didn't work ^^^^
PHP:
#include <iostream>
using namespace std;

int main() {
	char user1, user2;
			cout << "What move P1? ";
			cin >> user1;
			cout << "What move P2? ";
			cin >> user2;

			if (((user1 == 'P')&&(user2 == 'N'))||((user1 == 'P')&&(user2 == 'E'))||((user1 == 'N')&&(user2 == 'P'))||((user1 == 'E')&&(user2 == 'N')))
				{
					cout << "Player 1 is the winner!";
				}
			else
				{
					cout << "Player 2 is the winner!";
				}			return 0;
}
This did work ^^^
 
  • #7


Note: '==' is a logical operator which you use, for example, in an 'if' statement to test if a certain condition is true:

if (user1 == 'P') {'some calculations'}

If you wish to assign a certain value to a variable, then the following statement is used:

{
user1 = 'P';

}
 
  • #8
Ah yes, that was where my issue was. Works now! Thanks! I will obviously just use the other one though because it is much shorter.
 
  • #9
sandy.bridge said:

Homework Statement


I'm writing a program for a game similar to rock-paper-scissors. It is called proton-neutron-electron (P, N, E). There are two players. In the event of a tie, player 2 wins
P1 denotes player 1, P2 denotes player 2, P = proton, N = neutron, E = electron. P1P means player 1 picks proton.
There are 9 possibilities. Here they are:
1. P1P + P2P = P2 wins
2. P1P + P2N = P1 wins
3. P1P + P2E = P1 wins
4. P1N + P2P = P2 wins
5. P1N + P2N = P1 wins << P2 should be the winner here
6. P1N + P2E = P2 wins
7. P1E + P2P = P1 wins
8. P1E + P2N = P2 wins
9. P1E + P2E = P2 wins
I added numbers to the rules for easier identification.

Your rule 5 violates what you said earlier about ties going to player 2.
Can you summarize the rules any better than in the table?

It looks like proton beats neutron (rules 2, 4) and electron beats neutron (rule 6) sometimes, but neutron beats electron sometimes (rule 8). Is that also a mistake?

In rock paper scissors, there is a simple set of rules: rock covers (beats) paper, scissors cut (beat) paper, and paper covers (beats) rock. Is there a similar, simple rule here that you didn't mention? If so, it would make your logic much simpler.
 
  • #10
Yeah, I noticed that as well. There doesn't seem to be any logicality with it, but that is how the table was given in the assignment. At that point I just made sure the algorithm obeyed the table.

PHP:
	         P1 plays P  	P1 plays N	         P1 plays E
P2 plays P	     P2	             P1	              P2
P2 plays N	     P1	             P2	              P1
P2 plays E	     P1	             P2	              P2
 
  • #11


I redid the indentation in your code so that the lines didn't scroll so far to the right.
sandy.bridge said:
Code:
#include <iostream>
using namespace std;

int main() 
{
  char user1, user2;
  cout << "What move P1? ";
  cin >> user1;
  cout << "What move P2? ";
  cin >> user2;

  if (((user1 == 'P')&&(user2 == 'N')) || 
      ((user1 == 'P')&&(user2 == 'E')) || 
      ((user1 == 'N')&&(user2 == 'P')) || 
      ((user1 == 'E')&&(user2 == 'N')) )
  {
    cout << "Player 1 is the winner!";
  }
  else
  {
    cout << "Player 2 is the winner!";
  }

  return 0;
}
This did work ^^^
 
  • #12
sandy.bridge said:
Yeah, I noticed that as well.
Then you have an error in post 1. According to the table below, if both players play a neutron, P2 wins.

sandy.bridge said:
There doesn't seem to be any logicality with it, but that is how the table was given in the assignment. At that point I just made sure the algorithm obeyed the table.

PHP:
	         P1 plays P  	P1 plays N	         P1 plays E
P2 plays P	     P2	             P1	              P2
P2 plays N	     P1	             P2	              P1
P2 plays E	     P1	             P2	              P2
 
  • #13
Oops! I just edited that thanks.
 
  • #14
Instead of one if block with a large condition, I would write three if blocks in which I check what player 1 has. I have implemented the first of these if blocks, and have added comments to suggest what the logic is.

Code:
if (user1 == 'P')
{
  if (user2 == 'P')
  {
    cout << "Player 2 is the winner!";
  }
  else
  {
    cout << "Player 1 is the winner!";
  }
}
else if (user1 == 'N')
{
  // Player 2 wins only if player 1 has 'P'
}
else // user1 == 'E'
{
  // Player 1 wins only if player 2 has 'N'
}
 

1. What is the basic concept of a game similar to rock-paper-scissors?

The basic concept of a game similar to rock-paper-scissors is to have two players make a simultaneous choice between three hand gestures (rock, paper, or scissors) and determine a winner based on the rules of the game.

2. What are the rules of a game similar to rock-paper-scissors?

The rules of a game similar to rock-paper-scissors state that rock beats scissors, scissors beats paper, and paper beats rock. If both players make the same gesture, it is a tie.

3. How can a computer program simulate a game similar to rock-paper-scissors?

A computer program can simulate a game similar to rock-paper-scissors by using conditional statements and random number generators to determine the computer's choice and compare it to the player's choice.

4. Is there a strategy for winning a game similar to rock-paper-scissors?

There is no guaranteed strategy for winning a game similar to rock-paper-scissors since the outcome is based on chance. However, some players may use tactics such as observing patterns in their opponent's gestures or making unpredictable choices.

5. Can a game similar to rock-paper-scissors be modified or expanded?

Yes, a game similar to rock-paper-scissors can be modified or expanded. Some variations include adding more hand gestures, introducing new rules, or creating a tournament-style gameplay.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
3
Views
1K
  • Precalculus Mathematics Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
33
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top