Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rock Paper Scissors - get winner mathematically

I just finished my rock paper scissors game in python. Right now, the user has to chose between three buttons, and a string user1 is set to either "Rock", "Paper", or "Scissors". And when I evaluate the game, the code simply runs a few if statements and for example checks,

if computer == "Paper"

But if I want to expand the game a little bit with my own items, that would be a lot of "if/else". Sow I thought I'd give each item a unique number, so that I just can use this number and don't have to mess around with the strings.

My teacher gave me the hint to divide the two ints in order to get who wins.

But after I thought about this for a while, I didn't found any solution on how I could do this. It's just about the logic behind this, I don't want any code.

How can I find the winner just with a little math, using the two ints as the items? Thank you!

like image 400
dv02 Avatar asked Oct 18 '25 18:10

dv02


2 Answers

Let's say we are implementing normal Rock Paper Scissors, as you say in your comment on your question. Let's assign:

Rock:     0
Paper:    1
Scissors: 2

This answer assumes you know about the modulo operator (%), so if not, please look that up first to understand what it means. I use it in this answer so that when we add 1 to scissors (which is 2), you get 0 instead of 3, since rock is 0 and there are no items for number 3.

With this assignment of numbers to choices, we want a choice to win if it comes right after the other person's choice, lose if it comes right before it, and tie if they're equal. For example, 2 comes right after 1, so scissors beats paper. We will use the modulo operator to make sure that our numbers stay between 1 and 3 (including 1 and 3).

So if you want to determine if player 1 wins, you would check if their move is 1 bigger than player 2's move. To tell if they tie, see if they have the same move. And if neither of those is true, then player 2 must have won. Here's an example implementation with some tests:

>>> def winner(p1, p2):
...   if (p1+1) % 3 == p2:
...     return "Player 2 won because their move is one greater than player 1"
...   elif p1 == p2:
...     return "It's a draw because both players played the same move"
...   else:
...     return "Player 1 wins because we know that it's not a draw and that player 2 didn't win"
...
>>>
>>>
>>> rock = 0
>>> paper = 1
>>> scissors = 2
>>> winner(rock, paper)
'Player 2 won because their move is one greater than player 1'
>>> winner(paper, scissors)
'Player 2 won because their move is one greater than player 1'
>>> winner(scissors, rock)
'Player 2 won because their move is one greater than player 1'
>>> winner(rock, scissors)
"Player 1 wins because we know that it's not a draw and that player 2 didn't win"
>>> winner(paper, paper)
"It's a draw because both players played the same move"

Now in this game, the mathematical rule was that an item beats the item with the number 1 less than it (modulo 3). If you add more items, you will need to come up with a mathematical rule to govern how the game works. One example (that wouldn't be very fun) would be to keep the rule that an item beats the item 1 less than it (and therefore loses to the item 1 more than it), and has a tie with any other item, though this would be a rather boring game.

Hope that answer helps!! Good luck!

like image 142
Christopher Shroba Avatar answered Oct 20 '25 08:10

Christopher Shroba


An easier way to solve this is to make two dictionaries for win and lose conditions, then assign user input as the keys and computer input as the values in the dictionary.

rps_win={'rock':'scissor','paper':'rock','scissor':'paper'}
rps_lose={'rock':'paper','paper':'scissor','scissor':'rock'}

Then check if that user-computer pair is from the win or lose dictionary and accordingly add the scores.

if rps_win[b]==a:
    player=player+1 #incrementing player points
    print('win point!')
elif rps_lose[b]==a:
    comp=comp+1 #incrementing computer points
    print('lose point!')
else:
    print('tie!')
like image 30
Anujeet Swain Avatar answered Oct 20 '25 06:10

Anujeet Swain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!