I am working on getting more familiar with python. To do this I am working on a rock, paper, scissors game. I have a class to handle the comparison of the values to determine a winner. The trouble is whenever I do the comparison using an overloaded greater than operator, the result is wrong only when rock is involved. I make a compare method to test if my logic is correct and it is. Is there something about the behavior of overloaded operators in python that is causing this?
My code:
import random rock = "rock" paper = "paper" scissors = "scissors" options = (rock, paper, scissors) class RcpValue: def __init__(self, value = rock): self.__value = value @property def value(self): return self.__value def __eq__(self, other): if self.__value == other.__value: return True return False def compare(self, other): if (self.__value == rock) and (other.__value == paper): return False if (self.__value == rock) and (other.__value == scissors): return True if (self.__value == paper) and (other.__value == scissors): return False if (self.__value == paper) and (other.__value == rock): return True if (self.__value == scissors) and (other.__value == rock): return False if (self.__value == scissors) and (other.__value == paper): return True def __gt__(self, other): if (self.__value == rock) and (other.__value == paper): return False if (self.__value == rock) and (other.__value == scissors): return True if (self.__value == paper) and (other.__value == scissors): return False if (self.__value == paper) and (other.__value == rock): return True if (self.__value == scissors) and (other.__value == rock): return False if (self.__value == scissors) and (other.__value == paper): return True def determine_winner(p1: RcpValue, p2: RcpValue): if p1.value == p2.value: return "Tied!" if p1.value > p2.value: #if p1.compare(p2): return "Player 1 wins" return "Player 2 wins" def test(): for i in options: for j in options: player1 = RcpValue(i) player2 = RcpValue(j) print(f"Player 1 chose: {player1.value}") print(f"Player 2 chose: {player2.value}") print(determine_winner(player1, player2)) print() test() Output using __gt__:
Player 1 chose: rock Player 2 chose: rock Tied! Player 1 chose: rock Player 2 chose: paper Player 1 wins Player 1 chose: rock Player 2 chose: scissors Player 2 wins Player 1 chose: paper Player 2 chose: rock Player 2 wins Player 1 chose: paper Player 2 chose: paper Tied! Player 1 chose: paper Player 2 chose: scissors Player 2 wins Player 1 chose: scissors Player 2 chose: rock Player 1 wins Player 1 chose: scissors Player 2 chose: paper Player 1 wins Player 1 chose: scissors Player 2 chose: scissors Tied! Output using compare():
Player 1 chose: rock Player 2 chose: rock Tied! Player 1 chose: rock Player 2 chose: paper Player 2 wins Player 1 chose: rock Player 2 chose: scissors Player 1 wins Player 1 chose: paper Player 2 chose: rock Player 1 wins Player 1 chose: paper Player 2 chose: paper Tied! Player 1 chose: paper Player 2 chose: scissors Player 2 wins Player 1 chose: scissors Player 2 chose: rock Player 2 wins Player 1 chose: scissors Player 2 chose: paper Player 1 wins Player 1 chose: scissors Player 2 chose: scissors Tied!