Created
October 15, 2023 19:45
-
-
Save abderrazak-bouadma/48e28fe4c05e9582744c48d92cbb0e54 to your computer and use it in GitHub Desktop.
allumettes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def take_matches(matches, max_take): | |
# L'ordinateur prend des allumettes au hasard, mais en garantissant qu'il ne prend pas la dernière. | |
computer_choice = random.randint(1, max_take) | |
if matches - computer_choice == 1: | |
computer_choice = 1 | |
return computer_choice | |
def main(): | |
matches = 21 # Nombre initial d'allumettes | |
max_take = 3 # Maximum d'allumettes que l'on peut prendre par tour | |
print("Bienvenue dans le jeu des allumettes !") | |
print("Règles : Vous pouvez prendre entre 1 et 3 allumettes par tour. L'ordinateur joue également.") | |
print("Le premier à prendre la dernière allumette perd.") | |
while matches > 0: | |
player_choice = int(input(f"Combien d'allumettes souhaitez-vous prendre (1-{max_take}) ? ")) | |
# Valider l'entrée du joueur | |
if 1 <= player_choice <= max_take: | |
matches -= player_choice | |
if matches <= 0: | |
print("Félicitations, vous avez pris la dernière allumette. Vous avez gagné !") | |
break | |
else: | |
print(f"Il reste {matches} allumettes.") | |
computer_choice = take_matches(matches, max_take) | |
print(f"L'ordinateur prend {computer_choice} allumettes.") | |
matches -= computer_choice | |
if matches <= 0: | |
print("L'ordinateur a pris la dernière allumette. L'ordinateur gagne !") | |
else: | |
print(f"Il reste {matches} allumettes.") | |
else: | |
print("Veuillez choisir un nombre d'allumettes valide (entre 1 et 3).") | |
if __name__ == "__main": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment