Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import choice
- import sys
- from string import ascii_lowercase as alphabet
- LISTWORDS = ('bat baboon badger abbreviate abolish businessman change\
- directions discount ever everyday floor fluor glass')
- ERRORS = {
- 'integer': 'argument must be an integer',
- 'value': 'greater than or equal to 0',
- 'absence': 'absence of a letter',
- 'letter': 'not in alphabet [a-z]',
- 'propos': 'letter already proposed'
- }
- HANGMANPICS = ['''
- +---+
- | |
- |
- |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- | |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- /| |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- /|\ |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- /|\ |
- / |
- |
- =========''', '''
- +---+
- | |
- O |
- /|\ |
- / \ |
- |
- =========''']
- def check(value):
- '''Analyse l'argument de la ligne de commande'''
- if not value.isdigit() or int(value) <= 0:
- print(ERRORS['integer'])
- print(ERRORS['value'])
- sys.exit()
- def askLetter():
- '''Demande une lettre en contrôlant celle-ci'''
- letter = input('Entrer une lettre: ')
- if letter in propositions:
- print(ERRORS['propos'])
- elif letter not in alphabet:
- print(ERRORS['letter'])
- else:
- letter = letter[0] # pour s'assurer de prendre une seule lettre
- propositions.append(letter)
- return letter
- askLetter()
- def play(n):
- '''Le jeu complet'''
- secret = choice(LISTWORDS.split()) # choix d'un mot dans la liste
- word = ['_'] * len(secret) # construction du mot inconnu
- counter = 0
- while secret != word and counter <= n:
- letter = askLetter()
- if letter not in secret: # Si la lettre ne fait pas parti du mot secret
- print(HANGMANPICS[counter])
- print(ERRORS['absence'])
- counter += 1 # on compte une erreur, un coup
- else:
- for index, char in enumerate(secret):
- if char == letter:
- word[index] = char # on place la/les lettre(s) dans le mot inconnu
- print('Mes propositions: {}'.format(''.join(propositions)))
- print(' '.join(word)) # Affichage propre du mot inconnu
- def main(arg):
- '''Ligne de commande et gestion du replay'''
- MAX_MOVE = sys.argv[1] # récupération du nombre de coups maximum
- check(MAX_MOVE) # on vérifie que l'on peut continuer le programme
- replay = True
- while replay:
- propositions[:] = [] # Ne pas oublier de réinitialiser propositions quand on rejoue
- play(int(MAX_MOVE)) # On joue pour MAX_MOVE coups
- replay = False if input('Voulez-vous rejouer (o/n): ') == 'n' \
- else True
- propositions = []
- main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement