{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercices du livre - Chapitre 8 - solutions\n",
    "\n",
    "---\n",
    "\n",
    "<table class=\"appbclp1\">\n",
    "<tr> <td style=\"font-weight: bold!;\">auteurs :</td><td style=\"text-align: left!;\">Laurent Pointal &lt;<a href=\"mailtp:laurent.pointal@limsi.fr\">laurent.pointal@limsi.fr</a>&gt; &amp; Bob Cordeau &lt;<a href=\"pycours@kordeo.eu\">pycours@kordeo.eu</a>&gt; </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">licence :</td><td style=\"text-align: left!;\">Creative Commons Attribution-ShareAlike 4.0 International Public License (CC BY-SA 4.0) </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">source :</td><td style=\"text-align: left!;\"><a href=\"https://github.com/lpointal/appbclp\">https://github.com/lpointal/appbclp</a></td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">description :</td><td style=\"text-align: left!;\">Les programmes dans ce Notebook Jupyter servent de support au livre <span style=\"font-style: italic!;\">« Python 3 - Apprendre à  programmer dans l'écosystème Python»</span>, édition Dunod, dans lequel les différentes sections sont expliquées </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">url :</td><td style=\"text-align: left!;\"><a href=\"https://www.dunod.com/sciences-techniques/python-3\">https://www.dunod.com/EAN/9782100809141</a></td> </tr>\n",
    "</table>\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "# Exercice 1\n",
    "\n",
    "# Import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "import random\n",
    "\n",
    "# Définition de classe ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "class Domino:\n",
    "    def __init__(self, facea, faceb):\n",
    "        self.fa = facea\n",
    "        self.fb = faceb\n",
    "    \n",
    "    def __str__(self):\n",
    "        return \"[{}:{}]\".format(self.fa, self.fb)\n",
    "           \n",
    "    def appariement(self, autre):\n",
    "        \"Retourne la marque du premier côté apparié trouvé, sinon None.\" \n",
    "        for n in (self.fa, self.fb):\n",
    "            if n in (autre.fa, autre.fb):\n",
    "                return n\n",
    "        return None\n",
    "\n",
    "# Programme principal ==========================================================\n",
    "pioche = []\n",
    "for i in range(0, 7):\n",
    "    for j in range(0, 7):\n",
    "        pioche.append(Domino(i, j))\n",
    "random.shuffle(pioche)\n",
    "joueur1 = pioche[0:7]\n",
    "del pioche[0:7]\n",
    "joueur2 = pioche[0:7]\n",
    "del pioche[0:7]\n",
    "\n",
    "print(\"Dominos joueur 1 : \", end='')\n",
    "for d in joueur1:\n",
    "    print(d, end='  ')\n",
    "print()\n",
    "print(\"Dominos joueur 2 : \", end='')\n",
    "for d in joueur2:\n",
    "    print(d, end='  ')\n",
    "\n",
    "d1 = joueur1[0]\n",
    "for d2 in joueur2:\n",
    "    if d1.appariement(d2) is not None:\n",
    "        print(d1, \"<==>\", d2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Noms des deux joueurs :  Max Seb\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "rangée     : (1, 2, 3, 4)\n",
      "allumettes : [1, 3, 5, 7]\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "C'est à Max de jouer :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "\tNuméro de la rangée           :  1\n",
      "\tNombre d'allumettes à enlever :  2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tLa rangée 1 ne comporte que 1 allumette(s) !\n",
      "\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "rangée     : (1, 2, 3, 4)\n",
      "allumettes : [1, 3, 5, 7]\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "C'est à Max de jouer :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "\tNuméro de la rangée           :  1\n",
      "\tNombre d'allumettes à enlever :  1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "rangée     : (1, 2, 3, 4)\n",
      "allumettes : [0, 3, 5, 7]\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "C'est à Seb de jouer :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "\tNuméro de la rangée           :  2\n",
      "\tNombre d'allumettes à enlever :  3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "rangée     : (1, 2, 3, 4)\n",
      "allumettes : [0, 0, 5, 7]\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "C'est à Max de jouer :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "\tNuméro de la rangée           :  3\n",
      "\tNombre d'allumettes à enlever :  5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "rangée     : (1, 2, 3, 4)\n",
      "allumettes : [0, 0, 0, 7]\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "C'est à Seb de jouer :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "\tNuméro de la rangée           :  4\n",
      "\tNombre d'allumettes à enlever :  2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "rangée     : (1, 2, 3, 4)\n",
      "allumettes : [0, 0, 0, 5]\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "C'est à Max de jouer :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "\tNuméro de la rangée           :  4\n",
      "\tNombre d'allumettes à enlever :  4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "rangée     : (1, 2, 3, 4)\n",
      "allumettes : [0, 0, 0, 1]\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "C'est à Seb de jouer :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "\tNuméro de la rangée           :  4\n",
      "\tNombre d'allumettes à enlever :  1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "rangée     : (1, 2, 3, 4)\n",
      "allumettes : [0, 0, 0, 0]\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "C'est à Max de jouer :\n",
      "\n",
      "*** Max gagne ! ***\n"
     ]
    }
   ],
   "source": [
    "# Exercice 2\n",
    "\n",
    "# Définition de classe ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "class Marienbad:\n",
    "    \"\"\"classe du jeu des allumettes.\"\"\"\n",
    "\n",
    "    def __init__(self, joueurs):\n",
    "        \"\"\"Constructeur avec valeurs par défaut.\"\"\"\n",
    "        self.tas = (1, 2, 3, 4)\n",
    "        self.valeurs = [1, 3, 5, 7]\n",
    "        self.joueurs = joueurs\n",
    "        self.tour = 0\n",
    "\n",
    "    def __str__(self):\n",
    "        \"\"\"Affiche l'état du jeu.\"\"\"\n",
    "        return (f\"\\n{'~'*25}\\n\"\n",
    "                f\"rangée     : {self.tas}\\n\"\n",
    "                f\"allumettes : {self.valeurs}\\n\"\n",
    "                f\"{'~'*25}\\n\"\n",
    "                f\"C'est à {self.joueurs[self.tour]} de jouer :\")\n",
    "\n",
    "\n",
    "    def verifie(self, tas, nb):\n",
    "        \"\"\"Retourne True si le coup proposé est valide, sinon False.\"\"\"\n",
    "        return self.valeurs[tas - 1] >= nb\n",
    "\n",
    "    def maj(self, tas, nb):\n",
    "        \"\"\"Met à jour les tas d'allumettes.\"\"\"\n",
    "        if self.verifie(tas, nb):\n",
    "            self.valeurs[tas - 1] -= nb\n",
    "            self.tour = 1 if self.tour % 2 == 0 else 0\n",
    "        else:\n",
    "            print(f\"\\tLa rangée {tas} ne comporte que {self.valeurs[tas - 1]} allumette(s) !\")\n",
    "\n",
    "    def termine(self):\n",
    "        \"\"\"Retourne True si le jeu est terminé, sinon False.\"\"\"\n",
    "        return sum(self.valeurs) == 0\n",
    "\n",
    "\n",
    "# Programme principal =========================================================\n",
    "joueurs = input(\"Noms des deux joueurs : \")\n",
    "\n",
    "marienbad = Marienbad(joueurs.split())\n",
    "print(marienbad)\n",
    "\n",
    "while not marienbad.termine():\n",
    "    num_tas = int(input(\"\\tNuméro de la rangée           : \"))\n",
    "    nb = int(input(\"\\tNombre d'allumettes à enlever : \"))\n",
    "    marienbad.maj(num_tas, nb)\n",
    "    print(marienbad)\n",
    "\n",
    "print(f\"\\n*** {marienbad.joueurs[marienbad.tour]} gagne ! ***\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
