Skip to content

Instantly share code, notes, and snippets.

@Arno0x
Created February 28, 2024 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arno0x/2b1aeb09ccd3fd540829ae5f86f15f99 to your computer and use it in GitHub Desktop.
Save Arno0x/2b1aeb09ccd3fd540829ae5f86f15f99 to your computer and use it in GitHub Desktop.
Jeu du pendu en Python et en ligne de commande
#!/usr/bin/python
# -*- coding: utf8 -*-
from sys import stdout
import json
import os
# Import external modules
try:
import requests
except ModuleNotFoundError as e:
print("[Error] Could not import module [%s]. Not installed. Please install using 'pip3 install %s'" %(e.name, e.name))
exit()
#--------------------------------------------------------------
# Finds all indexes of a character in a string
def find(s, ch):
return [i for i, ltr in enumerate(s) if ltr == ch]
#--------------------------------------------------------------
def printWord(arrayOfChar):
for char in arrayOfChar:
stdout.write("\33[1;34m"+char+"\33[0m ")
#--------------------------------------------------------------
pendu = [str] * 8
pendu[0] = ("\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n")
pendu[1] = ("\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
" |\n"
" /|\\\n"
" / | \\\n"
" / | \\\n")
pendu[2] = ("\n"
" |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" /|\\\n"
" / | \\\n"
" / | \\\n")
pendu[3] = (" --------------\n"
" |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" |\n"
" /|\\\n"
" / | \\\n"
" / | \\\n")
pendu[4] = (" --------------\n"
" | |\n"
" | |\n"
" | / \\\n"
" | \\_/\n"
" |\n"
" |\n"
" |\n"
" |\n"
" /|\\\n"
" / | \\\n"
" / | \\\n")
pendu[5] = (" --------------\n"
" | |\n"
" | |\n"
" | / \\\n"
" | \\_/\n"
" | |\n"
" | |\n"
" | |\n"
" |\n"
" /|\\\n"
" / | \\\n"
" / | \\\n")
pendu[6] = (" --------------\n"
" | |\n"
" | |\n"
" | / \\\n"
" | \\_/\n"
" | ----|----\n"
" | |\n"
" | |\n"
" |\n"
" /|\\\n"
" / | \\\n"
" / | \\\n")
pendu[7] = (" --------------\n"
" | |\n"
" | |\n"
" | / \\\n"
" | \\_/\n"
" | ----|----\n"
" | |\n"
" | |\n"
" | / \\\n"
" /|\\ / \\\n"
" / | \\ / \\\n"
" / | \\\n")
#--------------------------------------------------------------
# Download a random word from online API
response = requests.get("https://trouve-mot.fr/api/random")
if response.status_code != 200:
print("[Error] Could not download word from internet")
exit()
# Retrieve word from the JSON payload
word = json.loads(response.text)[0]['name'].upper()
guessed = ['_'] * len(word)
#--------------------------------------------------------------
# Start main loop
wrongLetterCount = 0
proposedLetters = []
while True:
os.system('clear')
print("\33[1;31m",pendu[wrongLetterCount],"\33[0m\n")
printWord(guessed)
print("\n\nLettres déjà tentées: %s" % proposedLetters)
if wrongLetterCount == len(pendu)-1:
print("Perdu ! :-(")
print("Le mot à deviner était: ",word)
exit()
# Ask for a new letter
while True:
letter = input("Entre une lettre: ").upper()
if letter in proposedLetters: print("La lettre '%s' a déjà été proposée, réessaye" % letter)
else: break
# Add the letter to the list of all proposed letters
proposedLetters.append(letter)
if len(find(word, letter)) == 0: wrongLetterCount += 1
else:
for index in find(word, letter):
guessed[index] = letter
# Check if the word was found
if word == ''.join(guessed):
printWord(guessed)
print("Gagne ! :-)")
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment