Skip to content

Instantly share code, notes, and snippets.

@mattrousseau
Created March 14, 2022 14:34
Show Gist options
  • Save mattrousseau/68c29db7b9d36eb5092e1755cc855a7d to your computer and use it in GitHub Desktop.
Save mattrousseau/68c29db7b9d36eb5092e1755cc855a7d to your computer and use it in GitHub Desktop.
import random
class Game():
HANDS = ("PIERRE", "FEUILLE", "CISEAUX")
def __init__(self, max_score=3):
self.max_score = max_score
self.player_score = 0
self.computer_score = 0
def play(self):
while self.game_is_running():
# Demander la main à l'utilisateur
print(f"Mains possibles: {', '.join(self.HANDS)}")
player_hand = input("Quel est votre choix?\n> ")
if player_hand not in self.HANDS:
print("La main choisie n'est pas valide")
continue
# Générer aléatoirement la main de l'ordinateur
computer_hand = random.choice(self.HANDS)
print(f"L'ordinateur joue {computer_hand}")
# Comparer les 2 mains et annoncer le vainqueur de la manche
self.compare_hand(player_hand, computer_hand)
self.print_score()
self.print_game_over()
def game_is_running(self):
return self.player_score < self.max_score and\
self.computer_score < self.max_score
def compare_hand(self, player_hand, computer_hand):
if player_hand == computer_hand:
print("Manche nulle")
elif (player_hand == "FEUILLE" and computer_hand == "PIERRE")\
or (player_hand == "PIERRE" and computer_hand == "CISEAUX")\
or (player_hand == "CISEAUX" and computer_hand == "FEUILLE"):
print("Vous gagnez la manche !")
self.player_score += 1
else:
print("Vous perdez la manche :(")
self.computer_score += 1
def print_score(self):
print(
f"votre score: {self.player_score} - score de l'ordi: {self.computer_score}")
print("-" * 8)
def print_game_over(self):
if self.player_score > self.computer_score:
print("Vous avez gagné !!!!!!")
else:
print("Vous avez perdu la partie :( :( :(")
if __name__ == '__main__':
game = Game()
game.play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment