diff --git a/TP01/Nim.ipynb b/TP01/Nim.ipynb index ca01e40..18aba0b 100644 --- a/TP01/Nim.ipynb +++ b/TP01/Nim.ipynb @@ -470,6 +470,72 @@ " # Position avantageuse (gagnante)\n", " return sum(etat_jeu) # Heuristique standard" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Bonus" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# création d'un système de sauvegarde des parties jouées\n", + "\n", + "def sauvegarder_partie(historique):\n", + " \"\"\"\n", + " Sauvegarde l'historique de la partie dans un fichier.\n", + " \n", + " :param historique: Liste des coups effectués pendant la partie.\n", + " :param fichier: Nom du fichier où sauvegarder l'historique.\n", + " \"\"\"\n", + " fichier = \"historique.txt\"\n", + " with open(fichier, \"a\") as f:\n", + " f.write(\"Nouvelle partie :\")\n", + " for coup in historique:\n", + " f.write(f\"{coup} \")\n", + " f.write(\"\\n\")\n", + "\n", + "\n", + "# modification de la fonction jeu_nim pour sauvegarder l'historique des coups\n", + "def jeu_nim(etat_initial):\n", + " etat = etat_initial[:]\n", + " historique = []\n", + " while not est_etat_final(etat):\n", + " afficher_etat(etat)\n", + " try:\n", + " pile = int(input(\"Entrez le numéro de la pile (1, 2, 3, ...): \")) - 1\n", + " nb_objets = int(input(\"Entrez le nombre d'objets à retirer: \"))\n", + " historique.append(f\"Joueur: Pile {pile + 1}, Objets retirés: {nb_objets}\")\n", + " appliquer_mouvement(etat, pile, nb_objets)\n", + " historique.append(f\"Player: {etat}\")\n", + " afficher_etat(etat)\n", + " except ValueError:\n", + " print(\"Entrée invalide. Veuillez entrer des nombres entiers.\")\n", + " continue\n", + "\n", + " if est_etat_final(etat):\n", + " print(\"\\nFélicitations ! Vous avez gagné !\")\n", + " break\n", + "\n", + " # Tour de l'IA\n", + " chemin_optimal = algorithme_a_star(etat)\n", + " if chemin_optimal and len(chemin_optimal) > 1:\n", + " etat_ia = chemin_optimal[1]\n", + " historique.append(f\"IA: Pile {pile + 1}, Objets retirés: {nb_objets}\")\n", + " print(\"\\nL'IA joue...\")\n", + " etat = etat_ia\n", + " historique.append(f\"AI: {etat}\")\n", + "\n", + " if est_etat_final(etat):\n", + " print(\"\\n L'IA a gagné !\")\n", + " break\n", + " sauvegarder_partie(historique)" + ] } ], "metadata": { diff --git a/TP01/code.py b/TP01/code.py index 556bc06..664e692 100644 --- a/TP01/code.py +++ b/TP01/code.py @@ -91,6 +91,7 @@ def algorithme_a_star(etat_initial): return None +#affichade d'un chemin def reconstruire_chemin(noeud_final): """ Reconstruit la séquence de mouvements depuis l’état initial jusqu’à l’état final. @@ -105,20 +106,27 @@ def reconstruire_chemin(noeud_final): noeud_courant = noeud_courant.parent return chemin[::-1] -def sauvegarder_partie(historique, fichier="historique_parties.txt"): + + +#sauvegarde de partie +def sauvegarder_partie(historique): """ Sauvegarde l'historique de la partie dans un fichier. :param historique: Liste des coups effectués pendant la partie. :param fichier: Nom du fichier où sauvegarder l'historique. """ + fichier = "historique.txt" with open(fichier, "a") as f: - f.write("Nouvelle partie\n") + f.write("Nouvelle partie :") for coup in historique: - f.write(f"{coup}\n") + f.write(f"{coup} ") f.write("\n") + + +#jeu de nim version joueur vs IA def jeu_nim(etat_initial): etat = etat_initial[:] historique = [] @@ -129,6 +137,7 @@ def jeu_nim(etat_initial): nb_objets = int(input("Entrez le nombre d'objets à retirer: ")) historique.append(f"Joueur: Pile {pile + 1}, Objets retirés: {nb_objets}") appliquer_mouvement(etat, pile, nb_objets) + historique.append(f"Player: {etat}") afficher_etat(etat) except ValueError: print("Entrée invalide. Veuillez entrer des nombres entiers.") @@ -145,21 +154,28 @@ def jeu_nim(etat_initial): historique.append(f"IA: Pile {pile + 1}, Objets retirés: {nb_objets}") print("\nL'IA joue...") etat = etat_ia + historique.append(f"AI: {etat}") if est_etat_final(etat): print("\n L'IA a gagné !") break sauvegarder_partie(historique) + + +#pile aléatoire def pile_random(etat) : piles_non_vides = [i for i, pile in enumerate(etat) if pile > 0] return random.choice(piles_non_vides) - +# nombre d'objets aléatoire selon la pile def nb_objets_random(etat, pile): return random.randint(1, etat[pile]) + + +## jeu de nim version aléatoire vs IA def jeu_nim_random(etat_initial): etat = etat_initial[:] historique = [] @@ -168,7 +184,7 @@ def jeu_nim_random(etat_initial): try: pile = pile_random(etat) nb_objets = nb_objets_random(etat, pile) - historique.append(f"Joueur: Pile {pile + 1}, Objets retirés: {nb_objets}") + historique.append(f"Player : {etat}") appliquer_mouvement(etat, pile, nb_objets) afficher_etat(etat) except ValueError: @@ -183,9 +199,9 @@ def jeu_nim_random(etat_initial): chemin_optimal = algorithme_a_star(etat) if chemin_optimal and len(chemin_optimal) > 1: etat_ia = chemin_optimal[1] - historique.append(f"IA: Pile {pile + 1}, Objets retirés: {nb_objets}") print("\nL'IA joue...") etat = etat_ia + historique.append(f"AI: {etat}") if est_etat_final(etat): print("\n L'IA a gagné !") @@ -193,5 +209,9 @@ def jeu_nim_random(etat_initial): sauvegarder_partie(historique) + + + +#main etat_initial = [3, 4, 5] jeu_nim_random(etat_initial) \ No newline at end of file