Bonjour, bonsoir, bon matin,
Je suis actuellement en train d'apprendre à programmer en Python, avec python 3.4 et j'en suis rendu à la notion de classes, d'objets, de méthodes et tout ce qui peut aller avec.
Avant de continuer, j'aimerais savoir si ma façon de gérer les classes est considérable comme "juste" par un programmeur expérimenté, pour éviter de finir par écrire du code que moi seul comprendrait. Si vous avez des remarques à propos de quoi que ce soit d'autre (gestions des fonctions, mon niveau plus que moyen en anglais), faites vous plaisir , je suis là pour écouter ce que je peux apprendre.
L'exercice
Réaliser le jeu du "Plus Ou Moins" dans une version utilisant les notions de POO que vous venez de découvrir.
Conditions de validation :
- Les règles doivent être affichées au départ.
- La saisie doit être sécurisée autant que cela est possible selon vous (si l'utilisateur saisi "\n" ou "3.141592" le jeu lui signale que ce n'est pas un nombre valide), pensez aux instructions try/except/else.
- Il doit y avoir au moins deux niveaux de difficultés, avec un choix pour l'utilisateur avant de jouer.
Voici ce que j'ai écrit, après bien des déboires avec la POO, que je comprends assez mal je pense.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | # !/usr/bin/env python3 # -*-coding:utf-8-* # LessOrMorePython.py """ Less or More game, made by Poliorcetics. First version has been made with the C programming language, but this version is made with Python 3.4.3 because I want learn it. Copyright (c) Poliorcetics, 2015. All rights reserved. """ # Import randrange to use it in random initialization import random as ran ############################################################################### # 'Infos' class ############################################################################### class Infos(object): """ 'Infos' class ============= Contains some informations about the game and the author Variables --------- [String] - author : contains informations about the author - version : contains the version number - copyright : contains informations about the copyright [Tuple] - all_infos : contains all the previous informations in a tuple """ author = "Poliorcetics, alias Alexis B." version = "0.2.1" copyright = "Copyright © Poliorcetics, 2015. All rights reserved." all_infos = (author, version, copyright) ############################################################################### # 'Game' class, main class. ############################################################################### class Game(object): """ 'Game' class ============ Contains all of what is basically needed by the game to run. Variables --------- [String] - player_name : deprecated right now (line is commented) [Tuple] - infos : contains informations about the author, the version and the copyright. See more by looking at the documentation of the Infos class. Functions --------- The ‘self,’ is omitted in all functions, but it's the first argument everywhere (if you use the function outside the class, it doesn't need to be write) - introduceGame() : just here to display the title of the game - displayRules() : displays the rules - chooseDifficulty() : return the choice of the player for the difficulty level - computeSearchedNumber(dif, top): create the mysterious number and return it - defineTopOfTheRange(dif) : return the top of the range, corresponding to the selected difficulty - isLessOrMore(t_nb, s_nb) : displays if the typed number is higher or lower than the searched number - searchNumber(s_nb, top) : let the player searching what is the searched number - ending(inf) : displays some informations about author, version and copyright All functions have their own documentation, look at it for more informations """ # player_name = "Default" infos = Infos.all_infos def introduceGame(self): """Introduces the game with a simple shell display""" print(" \n---------------------------") print(" >>> Less Or More Game ! <<<") print(" ---------------------------\n") def displayRules(self): """ Displays the rules of the Less Or More Game. No arg needed, no return. """ print("-- Rules --"); print("1. The game will create a mysterious number.") print("2. The game will ask you to enter a number.") print("3. This number MUST BE included in the range \ corresponding to the selected difficulty.") print("4. This number MUST BE an integer, like 42 and unlike 3.1415.") print("5. If the number you type is in range and is higher \ than the searched number, the game will say \"That's less !\", else it will \ say \"That's more !\".") print("6. You have to find the mysterious number with a minimum \ number of tests.") def chooseDifficulty(self): """ Displays the difficulty options and returns the player's choice. Return 1, 2 or 3. """ dif = 0 # Displays possible choices for the difficulty print("\n-- Difficulty --"); print("1 : Mysterious number is in range [1;100]") print("2 : Mysterious number is in range [1;1000]") print("3 : Mysterious number is in range [1;10000]\n") print(">> To choose a difficulty, enter 1, 2 or 3.") # Asks the user to know what he want while not(0 < dif < 4): dif = input("Please choose a difficulty : ") try: dif = int(dif) except ValueError: dif = 0 else: dif = int(dif) return dif def computeSearchedNumber(self, dif, top): """ Return 'searched_number' (INT), that the player will have to find to win - 'dif' : INT, represents the choosen difficulty - 'top' : INT, represents the maximum possible value for the searched number """ # Game displays that it is computing for 'searched_number' print("\nCalculation of the mysterious number ...") if dif == 1: # +1 : to make the true range searched_number = ran.randrange(top)+1 elif dif == 2: searched_number = ran.randrange(top)+1 else: # dif == 3 searched_number = ran.randrange(top)+1 # Game lets the user know that it finished to compute searched number print("Finished ! \n") # Uncomment to get the answer before playing (use it only for tests please) print(searched_number, "\n") return searched_number def defineTopOfTheRange(self, dif): """ Return 'top_of_the_range' (INT), the maximum possible value for the searched number - 'dif' : INT, represents the choosen difficulty """ if dif == 1: top_of_the_range = 100 elif dif == 2: top_of_the_range = 1000 else: top_of_the_range = 10000 return top_of_the_range def isLessOrMore(self, t_nb, s_nb): """ Displays if the typed number 't_nb' is lower or higher than the searched number 's_nb' Returns False if t_nb != s_nb, else True """ if t_nb > s_nb: print("\nThat's less !\n") return False elif t_nb < s_nb: print("\nThat's more !\n") return False else: # t_nb == s_nb print("\nCongratulations ! You found the searched number !\n") return True def searchNumber(self, s_nb, top): """ Return the number of attempts 'nb_of_tests' the player made to found the searched number 's_nb' - 's_nb' : INT, the number the player have to find - 'top' : INT, represents the maximum possible value for the searched number (and the typed number) While the player does not enter the searched number, this function will keep going, asking the player again and again. """ nb_of_tests = 0 t_nb = 0 # Begin the real game : the player will have to enter a number and # to find the searched number while t_nb != s_nb: # Player enters an integer that MUST BE included in the choosen range while not(0 < t_nb <= top) or not(position): nb_of_tests += 1 # every attempt is counted t_nb = input("Please, enter an integer in range [1:{}] ".format(top)) # Checks if the typed number is in range and an integer try: t_nb = int(t_nb) except (ValueError, TypeError): pass t_nb = 0 print("An integer in the range please.") else: t_nb = int(t_nb) if (0 < t_nb <= top): break else: print("An integer in the range please.") # Displays to the player if he is lower or higher compare to the searched number position = Game.isLessOrMore(self, t_nb, s_nb) return nb_of_tests def ending(self, inf): """ Displays the informations about the author, the version and the copyright. - 'inf' : TUPLE, contains the informations. See the 'Infos' class to learn more about it. """ print("Made with Python 3.4.3 and <3 by {}.".format(inf[0])) print("Version {}, {cop}\n".format(inf[1], cop=inf[2])) # If this file is lauched directly, the game begin if __name__ == "__main__": game = Game() game.introduceGame() game.displayRules() difficultyLevel = game.chooseDifficulty() top_of_the_range = game.defineTopOfTheRange(difficultyLevel) searched_number = game.computeSearchedNumber(difficultyLevel, top_of_the_range) game.searchNumber(searched_number, top_of_the_range) game.ending(Game.infos) |
Si j'ai oublié de préciser une information, n'hésitez pas à demander .