From e2d1cd9933969d23f61d36ae989da9313468a359 Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Fri, 1 Feb 2019 20:09:24 +0100 Subject: [PATCH] Prepare source for 2nd workshop --- sources/04-pendu.py | 52 ---------------- sources/hangman/hangman.py | 79 ++++++++++++++++++++++++ sources/hangman/scores.py | 16 +++++ sources/hangman/scores.txt | 2 + sources/{pendu.txt => hangman/words.txt} | 0 5 files changed, 97 insertions(+), 52 deletions(-) delete mode 100644 sources/04-pendu.py create mode 100644 sources/hangman/hangman.py create mode 100644 sources/hangman/scores.py create mode 100644 sources/hangman/scores.txt rename sources/{pendu.txt => hangman/words.txt} (100%) diff --git a/sources/04-pendu.py b/sources/04-pendu.py deleted file mode 100644 index e48d942..0000000 --- a/sources/04-pendu.py +++ /dev/null @@ -1,52 +0,0 @@ -import random - - -def read_words(): - file = open("pendu.txt") - contents = file.read() - file.close() - words = contents.splitlines() - return words - - -def choose_word(words): - n = len(words) - index = random.randint(0, n-1) - return words[index] - - -def has_won(word, letters): - for letter in word: - if letter not in letters: - return False - return True - - -def display_hint(word, letters): - for letter in word: - if letter in letters: - print(letter, end="") - else: - print("_", end="") - print("") - - -def main(): - words = read_words() - word = choose_word(words) - print(word) - - letters = set() - display_hint(word, letters) - - while True: - new_letter = input() - letters.add(new_letter) - display_hint(word, letters) - if has_won(word, letters): - print("Gagné") - return - - - -main() diff --git a/sources/hangman/hangman.py b/sources/hangman/hangman.py new file mode 100644 index 0000000..64f50af --- /dev/null +++ b/sources/hangman/hangman.py @@ -0,0 +1,79 @@ +import sys +import random + + +def read_words(): + file = open("words.txt") + contents = file.read() + file.close() + words = contents.splitlines() + return words + + +def choose_word(words): + n = len(words) + index = random.randint(0, n - 1) + return words[index] + + +def has_won(word, letters): + for letter in word: + if letter not in letters: + return False + return True + + +def display_hint(word, letters): + for letter in word: + if letter in letters: + print(letter, end="") + else: + print("_", end="") + print("") + + +def main(): + num_tries = 10 + words = read_words() + word = choose_word(words) + + letters = set() + display_hint(word, letters) + + while num_tries: + print(num_tries, "essai(s) restant(s)") + + answer = input() + while answer in letters: + print("Lettre déjà proposée") + answer = input() + + if len(answer) == len(word): + guess = answer + if guess == word: + print("Gagné") + sys.exit(0) + else: + print("Mauvaise réponse") + num_tries -= 1 + + elif len(answer) == 1: + letter = answer + letters.add(letter) + display_hint(word, letters) + if has_won(word, letters): + print("Gagné") + sys.exit(0) + else: + num_tries -= 1 + + else: + print("Veuillez entrer juste une lettre ou le mot entier") + + print("Vous avez dépassé le nombre d'essais autorisés") + print("Le mot à deviner était :", word) + sys.exit(1) + + + +main() diff --git a/sources/hangman/scores.py b/sources/hangman/scores.py new file mode 100644 index 0000000..09f9d38 --- /dev/null +++ b/sources/hangman/scores.py @@ -0,0 +1,16 @@ +def parse_scores(): + res = dict() + with open("scores.txt") as file: + for line in file.readlines(): + name, score = line.split() + res[name] = int(score) + return res + + +def main(): + scores = parse_scores() + for name, score in scores.items(): + print(name, score) + + +main() diff --git a/sources/hangman/scores.txt b/sources/hangman/scores.txt new file mode 100644 index 0000000..9830e87 --- /dev/null +++ b/sources/hangman/scores.txt @@ -0,0 +1,2 @@ +alice 42 +bob 33 diff --git a/sources/pendu.txt b/sources/hangman/words.txt similarity index 100% rename from sources/pendu.txt rename to sources/hangman/words.txt