From e345926ff77e5569d4089973f876ede6d9a92866 Mon Sep 17 00:00:00 2001 From: "Wade Fagen-Ulmschneider (waf)" <waf@illinois.edu> Date: Thu, 6 Apr 2017 09:20:30 -0500 Subject: [PATCH] tictactoe day2 --- demo_tictactoe/py/tictactoe-day2.ipynb | 183 +++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 demo_tictactoe/py/tictactoe-day2.ipynb diff --git a/demo_tictactoe/py/tictactoe-day2.ipynb b/demo_tictactoe/py/tictactoe-day2.ipynb new file mode 100644 index 0000000..db297db --- /dev/null +++ b/demo_tictactoe/py/tictactoe-day2.ipynb @@ -0,0 +1,183 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tic-tac-toe\n", + "\n", + "Using graphs, we will explore the state space of a game of tic-tac-toe!" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import networkx as nx" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def isWinningState(state):\n", + " winningIndexes = [\n", + " # Horz\n", + " [0, 1, 2],\n", + " [3, 4, 5],\n", + " [6, 7, 8],\n", + " \n", + " # Vert\n", + " [0, 3, 6],\n", + " [1, 4, 7],\n", + " [2, 5, 8],\n", + " \n", + " # Diag\n", + " [0, 4, 8],\n", + " [2, 4, 6]\n", + " ]\n", + " \n", + " for i in range(len(winningIndexes)):\n", + " win_index1 = winningIndex[i][0]\n", + " win_index2 = winningIndex[i][1]\n", + " win_index3 = winningIndex[i][2]\n", + " \n", + " if state[win_index1] == state[win_index2] and \\\n", + " state[win_index2] == state[win_index3]:\n", + " if state[win_index1] == \"X\":\n", + " # X is the winner\n", + " return \"X\"\n", + " elif state[win_index1] == \"O\":\n", + " # O is the winner\n", + " return \"O\"\n", + " \n", + " return \"-\"\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def getNextPlayer(state):\n", + " x_ct = 0\n", + " o_ct = 0\n", + " for i in range(len(state)):\n", + " if state[i] == \"X\":\n", + " x_ct += 1\n", + " if state[i] == \"O\":\n", + " o_ct += 1\n", + " \n", + " if x_ct == o_ct:\n", + " return \"X\"\n", + " else:\n", + " return \"O\"" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def getNextMoves(state):\n", + " nextStates = []\n", + " nextPlayer = getNextPlayer(state)\n", + " for i in range(len(state)):\n", + " if state[i] == \"-\":\n", + " nextState = list(state) # makes a copy of the list\n", + " nextState[i] = nextPlayer\n", + " nextStates.append(nextState)\n", + " return nextStates" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[['X', 'X', 'X', 'O', 'O', 'X', '-', 'O', '-'],\n", + " ['X', 'X', 'X', 'O', 'O', '-', 'X', 'O', '-'],\n", + " ['X', 'X', 'X', 'O', 'O', '-', '-', 'O', 'X']]" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "G = nx.DiGraph()\n", + "\n", + "initState = [\"X\", \"X\", \"X\", \"O\", \"O\", \"-\", \"-\", \"O\", \"-\"]\n", + "G.add_node(str(initState), state=initState)\n", + "nextMoves = getNextMoves(initState)\n", + "nextMoves" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import json\n", + "with open(\"../res/tree.json\", \"w\") as f:\n", + " from networkx.readwrite import json_graph\n", + " data = json_graph.tree_data(G, root=1)\n", + " json.dump(data, f, indent=2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "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.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} -- GitLab