You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

102 lines
2.1 KiB

  1. import random
  2. import json
  3. def lire_scores():
  4. with open("scores.json") as f:
  5. scores = json.load(f)
  6. return scores
  7. def enregistrer_scores(scores):
  8. with open("scores.json", "w") as f:
  9. scores = json.dump(scores, f, indent=2)
  10. return scores
  11. def choisir_mot_au_hasard():
  12. fichier = open("mots.txt")
  13. contenu = fichier.read()
  14. fichier.close()
  15. mots = contenu.splitlines()
  16. n = len(mots)
  17. index = random.randint(0, n - 1)
  18. return mots[index]
  19. def demander_joueur():
  20. joueur = input("donnez votre nom: ")
  21. return joueur
  22. def jeu():
  23. mot = choisir_mot_au_hasard()
  24. # pour débugger:
  25. print(mot)
  26. tentatives = []
  27. while True:
  28. afficher_indice(mot, tentatives)
  29. lettre = demander_lettre()
  30. tentatives += [lettre]
  31. score = len(tentatives)
  32. if a_gagné(mot, tentatives):
  33. print(mot)
  34. print("Gagné: score", score)
  35. return score
  36. def a_gagné(mot, tentatives):
  37. for c in mot:
  38. if c not in tentatives:
  39. return False
  40. return True
  41. def demander_lettre():
  42. print("entrer une lettre")
  43. lettre = input()
  44. return lettre
  45. def afficher_indice(mot, tentatives):
  46. for c in mot:
  47. if c in tentatives:
  48. print(c, end="")
  49. else:
  50. print("_", end="")
  51. print()
  52. def trouve_record(scores):
  53. record = None
  54. for nom in scores:
  55. score = scores[nom]
  56. if record is None:
  57. record = score
  58. else:
  59. if score < record:
  60. record = score
  61. return record
  62. def ajouter_resultat(scores, joueur_courant, score_courant):
  63. if not scores:
  64. print("Vous avez établi le record à", score_courant)
  65. return {joueur_courant: score_courant}
  66. ancien_record = trouve_record(scores)
  67. if score_courant < ancien_record:
  68. print("Vous avez battu le précédent record:", ancien_record)
  69. nouveaux_scores= scores
  70. nouveaux_scores[joueur_courant] = score_courant
  71. return nouveaux_scores
  72. else:
  73. return scores
  74. def main():
  75. # Votre code ici ...
  76. print("bonjour")
  77. main()