From d9f18e8ea2f03cc74e887ec8f0a4a91f8e062661 Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sun, 19 Mar 2017 05:03:43 +0530 Subject: [PATCH 1/2] updated games.ipynb with refactored games.py --- games.ipynb | 226 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 150 insertions(+), 76 deletions(-) diff --git a/games.ipynb b/games.ipynb index 1dc5f5ca9..f2486c186 100644 --- a/games.ipynb +++ b/games.ipynb @@ -18,7 +18,7 @@ "outputs": [], "source": [ "from games import (GameState, Game, Fig52Game, TicTacToe, query_player, random_player, \n", - " alphabeta_player, play_game, minimax_decision, alphabeta_full_search,\n", + " alphabeta_player, minimax_decision, alphabeta_full_search,\n", " alphabeta_search, Canvas_TicTacToe)" ] }, @@ -209,7 +209,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -227,7 +227,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -237,7 +237,7 @@ "output_type": "stream", "text": [ "a1\n", - "a3\n" + "a1\n" ] } ], @@ -250,12 +250,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The `alphabeta_player(game, state)` will always give us the best move possible:" + "The `alphabeta_player(game, state)` will always give us the best move possible, for the relevant player (MAX or MIN):" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -285,7 +285,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -296,7 +296,7 @@ "'a1'" ] }, - "execution_count": 7, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -307,7 +307,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -318,7 +318,7 @@ "'a1'" ] }, - "execution_count": 8, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -336,7 +336,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -354,18 +354,47 @@ "3" ] }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "game52.play_game(alphabeta_player, alphabeta_player)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "B3\n" + ] + }, + { + "data": { + "text/plain": [ + "8" + ] + }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "play_game(game52, alphabeta_player, alphabeta_player)" + "game52.play_game(alphabeta_player, random_player)" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -374,34 +403,61 @@ "name": "stdout", "output_type": "stream", "text": [ - "B2\n" + "current state:\n", + "A\n", + "available moves: ['a2', 'a1', 'a3']\n", + "\n", + "Your move? a3\n", + "D3\n" ] }, { "data": { "text/plain": [ - "12" + "2" ] }, - "execution_count": 10, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "play_game(game52, alphabeta_player, random_player)" + "game52.play_game(query_player, alphabeta_player)" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "metadata": { - "collapsed": true + "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "current state:\n", + "B\n", + "available moves: ['b1', 'b3', 'b2']\n", + "\n", + "Your move? b3\n", + "B3\n" + ] + }, + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#play_game(game52, query_player, alphabeta_player)\n", - "#play_game(game52, alphabeta_player, query_player)" + "game52.play_game(alphabeta_player, query_player)" ] }, { @@ -421,7 +477,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 15, "metadata": { "collapsed": false }, @@ -439,7 +495,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 16, "metadata": { "collapsed": false }, @@ -469,7 +525,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 17, "metadata": { "collapsed": false }, @@ -495,7 +551,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 18, "metadata": { "collapsed": false }, @@ -523,7 +579,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 19, "metadata": { "collapsed": false }, @@ -531,10 +587,10 @@ { "data": { "text/plain": [ - "(3, 3)" + "(3, 2)" ] }, - "execution_count": 16, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -545,7 +601,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 20, "metadata": { "collapsed": false }, @@ -556,7 +612,7 @@ "(3, 2)" ] }, - "execution_count": 17, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -574,7 +630,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 21, "metadata": { "collapsed": false }, @@ -585,7 +641,7 @@ "(2, 2)" ] }, - "execution_count": 18, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -603,7 +659,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 22, "metadata": { "collapsed": false }, @@ -612,29 +668,38 @@ "name": "stdout", "output_type": "stream", "text": [ - "O X O \n", - "O . X \n", - "O X X \n", - "-1\n" + "O O O \n", + "X X . \n", + ". X . \n" ] + }, + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print(play_game(ttt, random_player, alphabeta_player))" + "ttt.play_game(random_player, alphabeta_player)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The output is -1, hence `random_player` loses implies `alphabeta_player` wins. \n", + "The output is (usually) -1, because `random_player` loses to `alphabeta_player`. Sometimes, however, `random_player` manages to draw with `alphabeta_player`.\n", " \n", - " Since, an `alphabeta_player` plays perfectly, a match between two `alphabeta_player`s should always end in a draw. Let's see if this happens:" + " Since an `alphabeta_player` plays perfectly, a match between two `alphabeta_player`s should always end in a draw. Let's see if this happens:" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 24, "metadata": { "collapsed": false }, @@ -688,7 +753,7 @@ ], "source": [ "for _ in range(10):\n", - " print(play_game(ttt, alphabeta_player, alphabeta_player))" + " print(ttt.play_game(alphabeta_player, alphabeta_player))" ] }, { @@ -700,7 +765,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 25, "metadata": { "collapsed": false }, @@ -709,52 +774,52 @@ "name": "stdout", "output_type": "stream", "text": [ - "X . . \n", - "O O O \n", - ". X X \n", - "-1\n", - "O O O \n", - "X X O \n", - "X X . \n", - "-1\n", - "O X . \n", - ". O X \n", - "X . O \n", - "-1\n", - "O . . \n", - ". O X \n", - "X X O \n", - "-1\n", + "O . X \n", "X O X \n", - "X O O \n", - ". O X \n", + ". . O \n", "-1\n", - "O . X \n", + "X O X \n", + "O O X \n", "X O . \n", - ". X O \n", "-1\n", - "O O X \n", + "O X O \n", "X O X \n", + "X O X \n", + "0\n", + "O X O \n", "X O . \n", + "O X X \n", "-1\n", - "O O O \n", + ". . O \n", + ". O X \n", "O X X \n", - "X . X \n", "-1\n", + "O O O \n", "X X O \n", - "O O X \n", - "O X . \n", + ". X X \n", + "-1\n", + "O O O \n", + ". . X \n", + ". X X \n", "-1\n", - "X . X \n", "O O O \n", + ". X X \n", ". X . \n", + "-1\n", + "X O X \n", + ". O X \n", + ". O . \n", + "-1\n", + "O X O \n", + "X O X \n", + "O X . \n", "-1\n" ] } ], "source": [ "for _ in range(10):\n", - " print(play_game(ttt, random_player, alphabeta_player))" + " print(ttt.play_game(random_player, alphabeta_player))" ] }, { @@ -770,7 +835,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 27, "metadata": { "collapsed": false }, @@ -828,7 +893,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 28, "metadata": { "collapsed": false }, @@ -881,12 +946,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Yay! We win. But we cannot win against an `alphabeta_player`, however hard we try." + "Yay! We (usually) win. But we cannot win against an `alphabeta_player`, however hard we try." ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 29, "metadata": { "collapsed": false }, @@ -934,6 +999,15 @@ "source": [ "ab_play = Canvas_TicTacToe('ab_play', 'human', 'alphabeta')" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": { @@ -952,7 +1026,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.4.3" } }, "nbformat": 4, From 829fed4d060fb7d7d875f07679403eee6313fafe Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sun, 19 Mar 2017 05:04:13 +0530 Subject: [PATCH 2/2] fixed typos in games.ipynb --- games.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/games.ipynb b/games.ipynb index f2486c186..da7652cf8 100644 --- a/games.ipynb +++ b/games.ipynb @@ -464,7 +464,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Note that, here, if you are the first player, the alphabeta_player plays as MIN, and if you are the second player, the alphabeta_player plays as MAX. This happens because that's the way the game is defined in the class Fig52Game. Having a look at the code of this class should make it clear." + "Note that if you are the first player then alphabeta_player plays as MIN, and if you are the second player then alphabeta_player plays as MAX. This happens because that's the way the game is defined in the class Fig52Game. Having a look at the code of this class should make it clear." ] }, { @@ -546,7 +546,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "So, how does this game state looks like?" + "So, how does this game state look like?" ] }, {