TP 13 oct 2022
This commit is contained in:
		
							
								
								
									
										58
									
								
								13oct/hangman.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								13oct/hangman.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,58 @@ | ||||
| from hangmantui import hangman, clear | ||||
| from userInput import ask_word_in_dictionary, ask_letter | ||||
|  | ||||
|  | ||||
| def hangman_start(score: int): | ||||
|     """start the hangman """ | ||||
|     life = 10 | ||||
|     choosed_word = ask_word_in_dictionary() | ||||
|     found_letters = '*' * len(choosed_word) | ||||
|     not_in_word = '' | ||||
|     while life > 0: | ||||
|         draw(life, found_letters) | ||||
|         in_letter = ask_letter(not_in_word) | ||||
|         if in_letter in choosed_word: | ||||
|             found_letters = unhide_letters(choosed_word, in_letter, found_letters) | ||||
|         else: | ||||
|             not_in_word += in_letter | ||||
|             life -= 1 | ||||
|  | ||||
|         if found_letters == choosed_word: | ||||
|             clear() | ||||
|             print('CONGRATS!! the word was: ', choosed_word) | ||||
|             score += len(list(dict.fromkeys(choosed_word[:]))) + life - len(not_in_word) | ||||
|             break | ||||
|  | ||||
|     return score | ||||
|  | ||||
|  | ||||
| def unhide_letters(choosed_word, letter, found_letters): | ||||
|     """take a letter and unhide it in the found letters | ||||
|     :choosed_word: the original word | ||||
|     :letter: the letter to unhide | ||||
|     :found_letters: the current found letter | ||||
|     :returns: found letter with unhiden letters | ||||
|     """ | ||||
|     ret = '' | ||||
|     for i in range(len(choosed_word)): | ||||
|         if letter in choosed_word[i]: | ||||
|             ret += letter | ||||
|         else: | ||||
|             ret += found_letters[i] | ||||
|     return ret | ||||
|  | ||||
|  | ||||
| def draw(life, word): | ||||
|     """draw the main screen with hangman, life remaining and word with * on unknown letters | ||||
|     :life: int of life remaining | ||||
|     :word: current word with * on unknown letters and other letters on found | ||||
|     """ | ||||
|     clear() | ||||
|     print(life) | ||||
|     hangman(life) | ||||
|     print(word) | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     print("your score is", hangman_start(0)) | ||||
|  | ||||
							
								
								
									
										66
									
								
								13oct/hangmantui.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								13oct/hangmantui.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,66 @@ | ||||
| # -*- encoding:utf-8 -*- | ||||
| """Terminal User Interface for the hangman.""" | ||||
|  | ||||
|  | ||||
| def clear(): | ||||
|     """Clear the terminal screen.""" | ||||
|     print(chr(27) + "[2J") | ||||
|  | ||||
|  | ||||
| def hangman(lives): | ||||
|     """ | ||||
|     Display a hangman state on terminal with maximum 10 lives. | ||||
|  | ||||
|     lives is the remaining number of lives between 0 and 10. When lives is 0, | ||||
|     the hangman is completed/dead. | ||||
|     """ | ||||
|     # Top | ||||
|     print("  __________") | ||||
|     print("  | /     |") | ||||
|     # Head | ||||
|     if lives <= 5: | ||||
|         print("  |/      O") | ||||
|     else: | ||||
|         print("  |/") | ||||
|     # Arms and torso | ||||
|     if lives <= 2: | ||||
|         print("  |      /|\\") | ||||
|     elif lives <= 3: | ||||
|         print("  |      /|") | ||||
|     elif lives <= 4: | ||||
|         print("  |       |") | ||||
|     else: | ||||
|         print("  |") | ||||
|     # Torso | ||||
|     if lives <= 4: | ||||
|         print("  |       |") | ||||
|     else: | ||||
|         print("  |") | ||||
|     # Legs | ||||
|     if lives <= 0: | ||||
|         print("  |      / \\") | ||||
|     elif lives <= 1: | ||||
|         print("  |      /") | ||||
|     else: | ||||
|         print("  |") | ||||
|     # Bottom / Stool | ||||
|     if lives <= 6: | ||||
|         print("  |       _") | ||||
|     else: | ||||
|         print("  |)") | ||||
|     if lives <= 7: | ||||
|         print(" /|\\     /|\\") | ||||
|     elif lives <= 8: | ||||
|         print(" /|\\     / \\") | ||||
|     elif lives <= 9: | ||||
|         print(" /|\\     /") | ||||
|     else: | ||||
|         print(" /|\\") | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     for i in range(10, -1, -1): | ||||
|         clear() | ||||
|         print("%i lives left:" % i) | ||||
|         hangman(i) | ||||
|         print("") | ||||
							
								
								
									
										
											BIN
										
									
								
								13oct/serie4.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								13oct/serie4.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										58
									
								
								13oct/userInput.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								13oct/userInput.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,58 @@ | ||||
| def belongs_to_dictionary(word): | ||||
|     """check if a word is in dictionary | ||||
|     :word: the word to check | ||||
|     :returns: true if in word.txt else false | ||||
|     """ | ||||
|     with open("words.txt") as wl: | ||||
|         word_list = wl.read().split() | ||||
|         return word in word_list | ||||
|  | ||||
|  | ||||
| def ask_word_in_dictionary(): | ||||
|     """ ask a word, if not in word.txt redo | ||||
|     :returns: word of word.txt | ||||
|     """ | ||||
|     while(True): | ||||
|         in_word = input("insert a word that belongs to the file words.txt: ") | ||||
|         if belongs_to_dictionary(in_word): | ||||
|             return in_word | ||||
|         print(f"{in_word} is not in word.txt") | ||||
|  | ||||
|  | ||||
| def ask_letter(tried_letters): | ||||
|     """ ask for a letter and check if it is not in trie_letters else redo | ||||
|     :tried_letters: list of already tried letters | ||||
|     :returns: the letter that is not in tried letters | ||||
|     """ | ||||
|     while(True): | ||||
|         in_letter = input(f"insert a letter that is not tested yet: {tried_letters.split()}: ") | ||||
|         if in_letter not in tried_letters: | ||||
|             return in_letter | ||||
|         print(f"{in_letter} is already guessed") | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__":  # Only tests | ||||
|     print("Test de la fonction, belongs_to_dictionary") | ||||
|     testing_words = {'banana': True, | ||||
|                      'patatas': False, | ||||
|                      'tonitch': False, | ||||
|                      'other': True, | ||||
|                      'lol': False, | ||||
|                      1: False, | ||||
|                      False: False} | ||||
|     for value, result in testing_words.items(): | ||||
|         if belongs_to_dictionary(value) == result: | ||||
|             print(f"{value} has been tested, returned {result} -- SUCCESS!") | ||||
|         else: | ||||
|             print(f"{value} has been tested, returned {result} -- ERROR!") | ||||
|  | ||||
|     print("Test de la fonction, ask_word_in_dictionary") | ||||
|     value = ask_word_in_dictionary() | ||||
|     print(f"The function returned value {value}") | ||||
|  | ||||
|     print("Test de la fonction, ask_letter") | ||||
|     value = ask_letter("") | ||||
|     print(f"The function returned value {value}") | ||||
|     value = ask_letter("aeiou") | ||||
|     print(f"The function returned value {value}") | ||||
|     print("Test are finished") | ||||
							
								
								
									
										113810
									
								
								13oct/words.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										113810
									
								
								13oct/words.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user