Skip to content

Instantly share code, notes, and snippets.

@elsonidoq
Created September 14, 2020 17:23
Show Gist options
  • Save elsonidoq/bb462c87cf9c15720f16ae591d52a967 to your computer and use it in GitHub Desktop.
Save elsonidoq/bb462c87cf9c15720f16ae591d52a967 to your computer and use it in GitHub Desktop.
Random ta te ti
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Funciones para manipular el tablero"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"from random import choice\n",
"\n",
"def random_move(board, who):\n",
" i, j = choice(get_available(board))\n",
" board[i][j] = who\n",
" \n",
"def _get_diags(board):\n",
" return [board[0][0], board[1][1], board[2][2]], [board[0][2], board[1][1], board[2][0]]\n",
"\n",
"def player_won(board, who):\n",
" target_row = [who] * 3\n",
" return (\n",
" any(row==target_row for row in board) or # filas \n",
" any(list(row)==target_row for row in zip(*board)) or # columnas\n",
" any(row==target_row for row in _get_diags(board)) # diagonales\n",
" )\n",
"\n",
"empty = '-'\n",
"def empty_board():\n",
" return [[empty] * 3 for _ in range(3)]\n",
"\n",
"def get_available(board):\n",
" return [(i, j) for i in range(3) for j in range(3) if board[i][j] is empty]\n",
"\n",
"def is_finished(board):\n",
" return not any(e is empty for row in board for e in row)\n",
"\n",
"def print_board(board):\n",
" [print(row) for row in board]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Funcion principal para simular una jugada random"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"from itertools import cycle\n",
"\n",
"def play():\n",
" board = empty_board()\n",
" player_iter = cycle('XO')\n",
" winner = None\n",
" while not is_finished(board):\n",
" who = next(player_iter)\n",
" random_move(board, who)\n",
" if player_won(board, who): \n",
" winner = who\n",
" break\n",
" return board, winner\n"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X\n",
"['X', 'X', 'O']\n",
"['X', 'O', 'O']\n",
"['X', 'O', 'X']\n"
]
}
],
"source": [
"board, winner = play()\n",
"\n",
"print(winner)\n",
"print_board(board)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Experimentos"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"def experiment(n=1000):\n",
" ends = {'X': 0, 'O': 0, None: 0}\n",
" for _ in range(n):\n",
" _, winner = play()\n",
" ends[winner] += 1\n",
" return {k: v/n for k, v in ends.items()}"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"experiments = pd.DataFrame([experiment() for _ in range(100)])"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x11bb0b0a0>"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAThklEQVR4nO3df2zc933f8ee7pA2ksputc8POliMJq4rQZuJiZe0WIFrRmg2nKeBlzTCfggpZ2AkeYEUtkKrCCDQLCqJ2sqR1Eaea0FOtDNMZRZe6Qq1aGWJyHZEUkNMlnuWDU8WNbUYdsqSDG2pubDLv/cGzd2JO4lfikaf73PMBHHzf7/dzX74PH/h1H32+vyIzkST1vx/odQGSpO4w0CWpEAa6JBXCQJekQhjoklSI4V794RtuuCG3b9/eqz+/4c6fP8+WLVt6XYaukP3Xv0rvuy996Uvfyswf6bStZ4G+fft2nn766V79+Q03NzfHrl27el2GrpD9179K77uIePFi25xykaRCGOiSVAgDXZIKY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esidMXsv/5Vet9ty8wf6bShZ4Feuoh4OjPHe12Hroz9178Gue+ccpGkQhjoklQIA33jHOl1AVoX+69/DWzfOYcuSYVwhC5JhTDQJakQBnoXRcTNEfHXEfHDreV/2Fre1uvatLaI2BoRfxIRfxURX4uIh1t3GFUPRURGxCfalj8cEf++hyVdtQz0LsrMl4HfAx5srXoQOJKZJV9AVYRYea7ZZ4HHM3Mn8OPAdcBMTwsTwHeBfxERN/S6kKudgd59vw38dOsWwhPAJ9Zor6vDncDfZ+YfAGTmMvCrwAcj4gd7WpmWWDlz5VdXb4iIbRHx+Yh4pvXft7fWPxoRvxsRX4iIFyLifW2f+bWION36zEc372tsPAO9yzLzdeDXWAn2X8nM13pckqq5FfhS+4rW/fxfAn6sJxWp3SPA+yPiravWfwr4TGa+C/jPwO+2bfvHrAyqfoHWv5oj4m5gJ3A78BPAT0bEz25w7ZvGQN8Y7wb+BhjrdSGqLOj8rNyLrdcmav24fgb40KpNPwMcb73/T6wE+Bsez8zvZeZzwEhr3d2t1/8A/hJ4BysBX4RKD7hQdRHxE8BdwE8D8xHxWGb+TY/L0trOAL/YviIifoiV5+l+rScVabXfYSWE/+ASbdp/fL/b9j7a/vtbmfkfu1zbVcERehe1Dqz9HitTLS8BHwf+Q2+rUkWfB34wIvYCRMQQK8c/Hs3M/9vTygRAZv4t8IfAVNvqL7DynGOA9wPza+zmFCvHRa4DiIibIuJt3a61Vwz07vo3wEuZ+V9by58G3hERP9fDmlRBrlwy/V7gX0bEXwFfBf4e+Hc9LUyrfYKV2+O+4UPAv46IZ4BfAg5c6sOZ+TlWpmi+GBH/E/gj4PoNqnXTeem/JBXCEbokFcJAl6RCGOiSVAgDXZIKYaBLUiEMdEkqhIEuSYX4fz/qSyk2eZ/6AAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"experiments.boxplot()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment