Reputation: 9
Is there was a way I could do a rock paper scissors game with greater than and less than signs?
I know there are different ways of doing RPS, but I want to know specifically about greater and less than.
Here is my code:
"Rock" > "Scissors" and 'Rock' < 'Paper'
"Paper" > "Rock" and 'Scissors' < 'Rock'
"Sissors" > "Paper" and 'Paper' < 'Scissors'
choose1 = input("Player One, Enter your answer ")
choose2 = input("Player Two, Enter your answer ")
if choose1 == "Paper":
"Paper" > "Rock"
if choose1 == "Scissor":
"Scissor" > "Rock"
if choose1 != "Rock" and choose1 != "Paper" and choose1 != "Scissors":
print("Player one please chose Rock, Paper, or Scissors")
if choose2 != "Rock" and choose2 != "Paper" and choose2 != "Scissors":
print("Player two please chose Rock, Paper, or Scissors")
if choose1 > choose2:
print('Player1 ({}) beats ({})'.format (choose1, choose2))
else:
print('Player2 ({}) beats ({})'.format (choose2, choose1))
The game works, however, it sees rock as beating everything, paper beating only scissors, and scissors beating nothing.
How do I fix this code so it performs correctly?
Upvotes: 0
Views: 780
Reputation: 5587
A pure "mathematical" form using the operators lt and gt, without hacking the operators can be achieved.
#ask the choices and map to P=3,R=2,S=1, using a dictionary for instance
#any difference between options will work as long as simmetric
#and the rest is adapted with the step different from one
result = abs(choose1 - choose2)
if not result:
print "Tie"
if result == 1:
#choose max
if choose1 > choose2:
print "Player 1 wins"
else:
print "Player 2 wins"
else:
#choose min:
if choose1 < choose2:
print "Player 1 wins"
else:
print "Player 2 wins"
Upvotes: 1
Reputation: 8972
If you really want to use <
and >
, make classes, simple string will not comply your needs.
class Rock:
def __gt__(self, other):
return isinstance(other, Scissors)
class Paper:
def __gt__(self, other):
return isinstance(other, Rock)
class Scissors:
def __gt__(self, other):
return isinstance(other, Paper)
CHOICES = {
"rock": Rock(),
"paper": Paper(),
"scissors": Scissors()
}
a = CHOICES["rock"]
b = CHOICES["scissors"]
print("player a wins:", a > b)
EDIT: or maybe better with only one class
class RPS:
table = {
"rock": "scissors",
"paper": "rock",
"scissors": "paper"
}
def __init__(self, what):
self.what = what
self.winsover = self.table[what]
def __eq__(self, other):
return self.what == other.what
def __gt__(self, other):
return self.winsover == other.what
a = RPS("rock")
b = RPS("scissors")
print("player a wins:", a > b)
Upvotes: 3
Reputation: 53623
Create a dictionary of choices, each k/v pair represents the choice, and the item which that choice will "beat" in the RPS game, then you can test the two players' choices like so:
def rps():
"""
choices defines the choice, and opposing choice which it will beat
"""
choices = {'rock':'scissors', 'paper':'rock', 'scissors':'paper'}
c1 = raw_input("Player One, Enter your answer ")
c2 = raw_input("Player Two, Enter your answer ")
if choices[c1] == c2:
print 'player 1 wins, {} beats {}'.format(c1,c2)
elif choices[c2] == c1:
print 'player 2 wins, {} beats {}'.format(c2,c1)
else:
print 'both players choose {}'.format(c1)
Upvotes: 2