{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7eb94415-ba03-4766-9f7d-349960f70241",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C'est le tour de Ana\n",
      "------------\n",
      "|   |   |   | \n",
      "------------\n",
      "|   | X |   | \n",
      "------------\n",
      "|   |   |   | \n",
      "------------\n",
      "\n",
      "C'est le tour de Sam\n",
      "------------\n",
      "|   |   |   | \n",
      "------------\n",
      "| O | X |   | \n",
      "------------\n",
      "|   |   |   | \n",
      "------------\n",
      "\n",
      "C'est le tour de Ana\n",
      "------------\n",
      "|   | X |   | \n",
      "------------\n",
      "| O | X |   | \n",
      "------------\n",
      "|   |   |   | \n",
      "------------\n",
      "\n",
      "C'est le tour de Sam\n",
      "------------\n",
      "|   | X |   | \n",
      "------------\n",
      "| O | X |   | \n",
      "------------\n",
      "|   |   | O | \n",
      "------------\n",
      "\n",
      "C'est le tour de Ana\n",
      "------------\n",
      "|   | X |   | \n",
      "------------\n",
      "| O | X |   | \n",
      "------------\n",
      "|   | X | O | \n",
      "------------\n",
      "\n",
      "Ana a gagné!\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "\n",
    "class Board:\n",
    "    def __init__(self, n):\n",
    "        self.size = n\n",
    "        self.grille = [[\"\" for i in range(n)] for j in range(n)]\n",
    "\n",
    "    def __str__(self):\n",
    "        n = self.size\n",
    "        horizontale = \"----\" * n\n",
    "        output = horizontale + \"\\n\"\n",
    "        for i in range(n):\n",
    "            ligne = \"| \"\n",
    "            for j in range(n):\n",
    "                if self.grille[i][j]:\n",
    "                    ligne += self.grille[i][j] + \" | \"\n",
    "                else:\n",
    "                    ligne += \"  | \"\n",
    "            output += ligne + \"\\n\" + horizontale + \"\\n\"\n",
    "\n",
    "        return output\n",
    "\n",
    "    def empty_cells(self):\n",
    "        return [(i, j) for i in range(self.size) for j in range(self.size) if not self.grille[i][j]]\n",
    "\n",
    "\n",
    "# b = Board(3)\n",
    "# b.grille[1][0] = \"X\"\n",
    "# b.grille[0][2] = \"O\"\n",
    "# print(b)\n",
    "# print(b.empty_cells())\n",
    "\n",
    "class Player:\n",
    "    def __init__(self, name, strategy = None):\n",
    "        self.name = name\n",
    "        self.symbol = None\n",
    "        self.strategy = strategy\n",
    "        \n",
    "    def __str__(self):\n",
    "        return self.name \n",
    "    \n",
    "    def detect_winning_move(self, board):\n",
    "        n = board.size\n",
    "        for i in range(n):\n",
    "            ligne = [board.grille[i][j] for j in range(n)]\n",
    "            if ligne.count(self.symb) == n - 1 and ligne.count(\"\") == 1:\n",
    "                return i, ligne.index(\"\")\n",
    "            \n",
    "            col = [board.grille[j][i] for j in range(n)]\n",
    "            if col.count(self.symb) == n-1 and col.count(\"\") == 1:\n",
    "                return col.index(\"\"), i\n",
    "            \n",
    "        diag = [board.grille[i][i] for i in range(n)]\n",
    "        if diag.count(self.symb) == n-1 and diag.count(\"\") == 1:\n",
    "            i = diag.index(\"\")\n",
    "            return i, i\n",
    "        \n",
    "        antidiag = [board.grille[i][n-1-i] for i in range(n)]\n",
    "        if antidiag.count(self.symb) == n-1 and antidiag.count(\"\") == 1:\n",
    "            i = antidiag.index(\"\")\n",
    "            return i, n- 1 - i\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    def play(self, board):\n",
    "\n",
    "        if self.strategy == \"human\":\n",
    "            i = int(input(\"x-coord de ta case\"))\n",
    "            j = int(input(\"y-coord de ta case\"))\n",
    "            return i, j\n",
    "\n",
    "        if self.strategy == \"smart\":\n",
    "            t = self.detect_winning_move(board)\n",
    "            if t:\n",
    "                return t\n",
    "            \n",
    "\n",
    "\n",
    "        \n",
    "        return random.choice(board.empty_cells())\n",
    "    \n",
    "    \n",
    "\n",
    "\n",
    "# sam = Player(\"Sam\")\n",
    "# sam.symbol = \"X\"\n",
    "# i, j = sam.play(b)\n",
    "# print(sam, \"veut jouer\", i, j)\n",
    "# b.grille[i][j] = sam.symbol\n",
    "# print(b)\n",
    "\n",
    "# ana = Player(\"Ana\")\n",
    "# ana.symbol = \"O\"\n",
    "# i, j = ana.play(b)\n",
    "# print(ana, \"veut jouer\", i, j)\n",
    "# b.grille[i][j] = ana.symbol\n",
    "# print(b)\n",
    "\n",
    "def liste_gagnante(L):\n",
    "    n = len(L)\n",
    "    gagnant = L[0]\n",
    "    for i in range(n):\n",
    "        if not L[i] or L[i] != gagnant:\n",
    "            return False\n",
    "    return gagnant\n",
    "\n",
    "class GameMaster:\n",
    "    def __init__(self, n):\n",
    "        self.n = n\n",
    "        self.plateau = Board(n)\n",
    "        self.turn = 1\n",
    "  \n",
    "    def move(self, i, j, symb):\n",
    "        self.plateau.grille[i][j] = symb\n",
    "\n",
    "    def detecter_gagnant(self):\n",
    "        for i in range(self.n):\n",
    "            # on teste si la ligne i a un gagnant\n",
    "            ligne = [self.plateau.grille[i][j] for j in range(self.n)]\n",
    "            g = liste_gagnante(ligne)\n",
    "            if g:\n",
    "                return g\n",
    "            \n",
    "            # on teste si la colonne i a un gagnant\n",
    "            colonne = [self.plateau.grille[j][i] for j in range(self.n)]\n",
    "            g = liste_gagnante(colonne)\n",
    "            if g:\n",
    "                return g\n",
    "            \n",
    "        # on teste si la diagonale a un gagnant:\n",
    "        diag = [self.plateau.grille[i][i] for i in range(self.n)]\n",
    "        g = liste_gagnante(diag)\n",
    "        if g:\n",
    "            return g\n",
    "        \n",
    "         # on teste si l'anti-diagonale a un gagnant:\n",
    "        antidiag = [self.plateau.grille[i][self.n - 1 - i] for i in range(self.n)]\n",
    "        g = liste_gagnante(antidiag)\n",
    "        if g:\n",
    "            return g\n",
    "\n",
    "\n",
    "    def play(self, player1, player2):\n",
    "        player1.symb = \"X\"\n",
    "        player2.symb = \"O\"\n",
    "        gagnant = None\n",
    "        \n",
    "        while not gagnant and self.turn <= self.n ** 2:\n",
    "            if self.turn % 2:\n",
    "                joueur = player1\n",
    "            else:\n",
    "                joueur = player2\n",
    "\n",
    "            print(\"C'est le tour de\", joueur)\n",
    "            i, j = joueur.play(self.plateau)\n",
    "            self.move(i, j, joueur.symb)\n",
    "            print(self.plateau)\n",
    "            self.turn += 1\n",
    "\n",
    "            gagnant = self.detecter_gagnant()\n",
    "\n",
    "        if gagnant:\n",
    "            print(joueur, \"a gagné!\")\n",
    "        else:\n",
    "            print(\"Egalité.\")\n",
    "\n",
    "\n",
    "sam = Player(\"Sam\")\n",
    "ana = Player(\"Ana\", \"smart\")\n",
    "game = GameMaster(3)\n",
    "game.play(ana, sam)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "52779de6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "L = [30, 40, 50, 60]\n",
    "print(random.choice(L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33b9266a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "algo-8wShGBKp",
   "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.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
