diff --git a/games.ipynb b/games.ipynb
index e1fe1e644..49884b94d 100644
--- a/games.ipynb
+++ b/games.ipynb
@@ -2,12 +2,9 @@
"cells": [
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
- "# Games or Adversarial search\n",
+ "# GAMES OR ADVERSARIAL SEARCH\n",
"\n",
"This notebook serves as supporting material for topics covered in **Chapter 5 - Adversarial Search** in the book *Artificial Intelligence: A Modern Approach.* This notebook uses implementations from [games.py](https://github.com/aimacode/aima-python/blob/master/games.py) module. Let's import required classes, methods, global variables etc., from games module."
]
@@ -16,51 +13,61 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
+ "collapsed": true
},
"outputs": [],
"source": [
- "from games import (GameState, Game, Fig52Game, TicTacToe, query_player, random_player, \n",
- " alphabeta_player, minimax_decision, alphabeta_full_search,\n",
- " alphabeta_search, Canvas_TicTacToe)"
+ "from games import *"
]
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
+ "source": [
+ "# GAME REPRESENTATION\n",
+ "\n",
+ "To represent games we make use of the `Game` class, which we can subclass and override its functions to represent our own games. A helper tool is the namedtuple `GameState`, which in some cases can come in handy, especially when our game needs us to remember a board (like chess)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"## `GameState` namedtuple\n",
"\n",
- "`GameState` is a [namedtuple](https://docs.python.org/3.5/library/collections.html#collections.namedtuple) which represents the current state of a game. Let it be Tic-Tac-Toe or any other game."
+ "`GameState` is a [namedtuple](https://docs.python.org/3.5/library/collections.html#collections.namedtuple) which represents the current state of a game. It is used to help represent games whose states can't be easily represented normally, or for games that require memory of a board, like Tic-Tac-Toe.\n",
+ "\n",
+ "`Gamestate` is defined as follows:\n",
+ "\n",
+ "`GameState = namedtuple('GameState', 'to_move, utility, board, moves')`\n",
+ "\n",
+ "* `to_move`: It represents whose turn it is to move next.\n",
+ "\n",
+ "* `utility`: It stores the utility of the game state. Storing this utility is a good idea, because, when you do a Minimax Search or an Alphabeta Search, you generate many recursive calls, which travel all the way down to the terminal states. When these recursive calls go back up to the original callee, we have calculated utilities for many game states. We store these utilities in their respective `GameState`s to avoid calculating them all over again.\n",
+ "\n",
+ "* `board`: A dict that stores the board of the game.\n",
+ "\n",
+ "* `moves`: It stores the list of legal moves possible from the current position."
]
},
{
"cell_type": "markdown",
"metadata": {
- "collapsed": true,
- "deletable": true,
- "editable": true
+ "collapsed": true
},
"source": [
"## `Game` class\n",
"\n",
"Let's have a look at the class `Game` in our module. We see that it has functions, namely `actions`, `result`, `utility`, `terminal_test`, `to_move` and `display`.\n",
"\n",
- "We see that these functions have not actually been implemented. This class is actually just a template class; we are supposed to create the class for our game, `TicTacToe` by inheriting this `Game` class and implement all the methods mentioned in `Game`. Do not close the popup so that you can follow along the description of code below."
+ "We see that these functions have not actually been implemented. This class is just a template class; we are supposed to create the class for our game, by inheriting this `Game` class and implementing all the methods mentioned in `Game`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
+ "collapsed": true
},
"outputs": [],
"source": [
@@ -69,39 +76,42 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"Now let's get into details of all the methods in our `Game` class. You have to implement these methods when you create new classes that would represent your game.\n",
"\n",
- "* `actions(self, state)` : Given a game state, this method generates all the legal actions possible from this state, as a list or a generator. Returning a generator rather than a list has the advantage that it saves space and you can still operate on it as a list.\n",
+ "* `actions(self, state)`: Given a game state, this method generates all the legal actions possible from this state, as a list or a generator. Returning a generator rather than a list has the advantage that it saves space and you can still operate on it as a list.\n",
"\n",
"\n",
- "* `result(self, state, move)` : Given a game state and a move, this method returns the game state that you get by making that move on this game state.\n",
+ "* `result(self, state, move)`: Given a game state and a move, this method returns the game state that you get by making that move on this game state.\n",
"\n",
"\n",
- "* `utility(self, state, player)` : Given a terminal game state and a player, this method returns the utility for that player in the given terminal game state. While implementing this method assume that the game state is a terminal game state. The logic in this module is such that this method will be called only on terminal game states.\n",
+ "* `utility(self, state, player)`: Given a terminal game state and a player, this method returns the utility for that player in the given terminal game state. While implementing this method assume that the game state is a terminal game state. The logic in this module is such that this method will be called only on terminal game states.\n",
"\n",
"\n",
- "* `terminal_test(self, state)` : Given a game state, this method should return `True` if this game state is a terminal state, and `False` otherwise.\n",
+ "* `terminal_test(self, state)`: Given a game state, this method should return `True` if this game state is a terminal state, and `False` otherwise.\n",
"\n",
"\n",
- "* `to_move(self, state)` : Given a game state, this method returns the player who is to play next. This information is typically stored in the game state, so all this method does is extract this information and return it.\n",
+ "* `to_move(self, state)`: Given a game state, this method returns the player who is to play next. This information is typically stored in the game state, so all this method does is extract this information and return it.\n",
"\n",
"\n",
- "* `display(self, state)` : This method prints/displays the current state of the game."
+ "* `display(self, state)`: This method prints/displays the current state of the game."
]
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
- "## `TicTacToe` class\n",
+ "# GAME EXAMPLES\n",
+ "\n",
+ "Below we give some examples for games you can create and experiment on."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Tic-Tac-Toe\n",
"\n",
"Take a look at the class `TicTacToe`. All the methods mentioned in the class `Game` have been implemented here."
]
@@ -110,9 +120,7 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {
- "collapsed": true,
- "deletable": true,
- "editable": true
+ "collapsed": true
},
"outputs": [],
"source": [
@@ -121,10 +129,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"The class `TicTacToe` has been inherited from the class `Game`. As mentioned earlier, you really want to do this. Catching bugs and errors becomes a whole lot easier.\n",
"\n",
@@ -141,55 +146,37 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
- "## GameState in TicTacToe game\n",
+ "### TicTacToe GameState\n",
"\n",
"Now, before we start implementing our `TicTacToe` game, we need to decide how we will be representing our game state. Typically, a game state will give you all the current information about the game at any point in time. When you are given a game state, you should be able to tell whose turn it is next, how the game will look like on a real-life board (if it has one) etc. A game state need not include the history of the game. If you can play the game further given a game state, you game state representation is acceptable. While we might like to include all kinds of information in our game state, we wouldn't want to put too much information into it. Modifying this game state to generate a new one would be a real pain then.\n",
"\n",
"Now, as for our `TicTacToe` game state, would storing only the positions of all the X's and O's be sufficient to represent all the game information at that point in time? Well, does it tell us whose turn it is next? Looking at the 'X's and O's on the board and counting them should tell us that. But that would mean extra computing. To avoid this, we will also store whose move it is next in the game state.\n",
"\n",
- "Think about what we've done here. We have reduced extra computation by storing additional information in a game state. Now, this information might not be absolutely essential to tell us about the state of the game, but it does save us additional computation time. We'll do more of this later on.\n",
- "\n",
- "The `TicTacToe` game defines its game state as:\n",
- "\n",
- "`GameState = namedtuple('GameState', 'to_move, utility, board, moves')`"
+ "Think about what we've done here. We have reduced extra computation by storing additional information in a game state. Now, this information might not be absolutely essential to tell us about the state of the game, but it does save us additional computation time. We'll do more of this later on."
]
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
- "The game state is called, quite appropriately, `GameState`, and it has 4 variables, namely, `to_move`, `utility`, `board` and `moves`.\n",
- "\n",
- "I'll describe these variables in some more detail:\n",
+ "To store game states will will use the `GameState` namedtuple.\n",
"\n",
- "* `to_move` : It represents whose turn it is to move next. This will be a string of a single character, either 'X' or 'O'.\n",
+ "* `to_move`: A string of a single character, either 'X' or 'O'.\n",
"\n",
+ "* `utility`: 1 for win, -1 for loss, 0 otherwise.\n",
"\n",
- "* `utility` : It stores the utility of the game state. Storing this utility is a good idea, because, when you do a Minimax Search or an Alphabeta Search, you generate many recursive calls, which travel all the way down to the terminal states. When these recursive calls go back up to the original callee, we have calculated utilities for many game states. We store these utilities in their respective `GameState`s to avoid calculating them all over again.\n",
+ "* `board`: All the positions of X's and O's on the board.\n",
"\n",
- "\n",
- "* `board` : A dict that stores all the positions of X's and O's on the board.\n",
- "\n",
- "\n",
- "* `moves` : It stores the list of legal moves possible from the current position. Note here, that storing the moves as a list, as it is done here, increases the space complexity of Minimax Search from `O(m)` to `O(bm)`. Refer to Sec. 5.2.1 of the book."
+ "* `moves`: All the possible moves from the current state. Note here, that storing the moves as a list, as it is done here, increases the space complexity of Minimax Search from `O(m)` to `O(bm)`. Refer to Sec. 5.2.1 of the book."
]
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
- "## Representing a move in TicTacToe game\n",
+ "### Representing a move in TicTacToe game\n",
"\n",
"Now that we have decided how our game state will be represented, it's time to decide how our move will be represented. Becomes easy to use this move to modify a current game state to generate a new one.\n",
"\n",
@@ -198,56 +185,301 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
- "## Players to play games\n",
+ "## Fig52 Game\n",
"\n",
- "So, we have finished the implementation of the `TicTacToe` class. What this class does is that, it just defines the rules of the game. We need more to create an AI that can actually play the game. This is where `random_player` and `alphabeta_player` come in.\n",
+ "For a more trivial example we will represent the game in **Figure 5.2** of the book.\n",
"\n",
- "### query_player\n",
- "The `query_player` function allows you, a human opponent, to play the game. This function requires a `display` method to be implemented in your game class, so that successive game states can be displayed on the terminal, making it easier for you to visualize the game and play accordingly.\n",
+ "\n",
"\n",
- "### random_player\n",
- "The `random_player` is a function that plays random moves in the game. That's it. There isn't much more to this guy. \n",
+ "The states are represented wih capital letters inside the triangles (eg. \"A\") while moves are the labels on the edges between states (eg. \"a1\"). Terminal nodes carry utility values. Note that the terminal nodes are named in this example 'B1', 'B2' and 'B2' for the nodes below 'B', and so forth.\n",
"\n",
- "### alphabeta_player\n",
- "The `alphabeta_player`, on the other hand, calls the `alphabeta_full_search` function, which returns the best move in the current game state. Thus, the `alphabeta_player` always plays the best move given a game state, assuming that the game tree is small enough to search entirely.\n",
+ "We will model the moves, utilities and initial state like this:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "moves = dict(A=dict(a1='B', a2='C', a3='D'),\n",
+ " B=dict(b1='B1', b2='B2', b3='B3'),\n",
+ " C=dict(c1='C1', c2='C2', c3='C3'),\n",
+ " D=dict(d1='D1', d2='D2', d3='D3'))\n",
+ "utils = dict(B1=3, B2=12, B3=8, C1=2, C2=4, C3=6, D1=14, D2=5, D3=2)\n",
+ "initial = 'A'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "In `moves`, we have a nested dictionary system. The outer's dictionary has keys as the states and values the possible moves from that state (as a dictionary). The inner dictionary of moves has keys the move names and values the next state after the move is complete.\n",
"\n",
- "### play_game\n",
- "The `play_game` function will be the one that will actually be used to play the game. You pass as arguments to it, an instance of the game you want to play and the players you want in this game. Use it to play AI vs AI, AI vs human, or even human vs human matches!"
+ "Below is an example that showcases `moves`. We want the next state after move 'a1' from 'A', which is 'B'. A quick glance at the above image confirms that this is indeed the case."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "B\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(moves['A']['a1'])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We will now take a look at the functions we need to implement. First we need to create an object of the `Fig52Game` class."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "fig52 = Fig52Game()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "`actions`: Returns the list of moves one can make from a given state."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "%psource Fig52Game.actions"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "['b1', 'b3', 'b2']\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(fig52.actions('B'))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "`result`: Returns the next state after we make a specific move."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "%psource Fig52Game.result"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "B\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(fig52.result('A', 'a1'))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "`utility`: Returns the value of the terminal state for a player ('MAX' and 'MIN')."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "%psource Fig52Game.utility"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "3\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(fig52.utility('B1', 'MAX'))"
]
},
{
"cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "`terminal_test`: Returns `True` if the given state is a terminal state, `False` otherwise."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
"metadata": {
- "deletable": true,
- "editable": true
+ "collapsed": true
},
+ "outputs": [],
"source": [
- "## Let's play some games\n",
- "### Game52"
+ "%psource Fig52Game.terminal_test"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "True\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(fig52.terminal_test('C3'))"
]
},
{
"cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "`to_move`: Return the player who will move in this state."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
"metadata": {
- "deletable": true,
- "editable": true
+ "collapsed": true
},
+ "outputs": [],
"source": [
- "
"
+ "%psource Fig52Game.to_move"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "MAX\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(fig52.to_move('A'))"
]
},
{
"cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "As a whole the class `Fig52` that inherits from the class `Game` and overrides its functions:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
"metadata": {
- "deletable": true,
- "editable": true
+ "collapsed": true
},
+ "outputs": [],
+ "source": [
+ "%psource Fig52Game"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# PLAYERS\n",
+ "\n",
+ "So, we have finished the implementation of the `TicTacToe` and `Fig52Game` classes. What these classes do is defining the rules of the games. We need more to create an AI that can actually play games. This is where `random_player` and `alphabeta_player` come in.\n",
+ "\n",
+ "## query_player\n",
+ "The `query_player` function allows you, a human opponent, to play the game. This function requires a `display` method to be implemented in your game class, so that successive game states can be displayed on the terminal, making it easier for you to visualize the game and play accordingly.\n",
+ "\n",
+ "## random_player\n",
+ "The `random_player` is a function that plays random moves in the game. That's it. There isn't much more to this guy. \n",
+ "\n",
+ "## alphabeta_player\n",
+ "The `alphabeta_player`, on the other hand, calls the `alphabeta_full_search` function, which returns the best move in the current game state. Thus, the `alphabeta_player` always plays the best move given a game state, assuming that the game tree is small enough to search entirely.\n",
+ "\n",
+ "## play_game\n",
+ "The `play_game` function will be the one that will actually be used to play the game. You pass as arguments to it an instance of the game you want to play and the players you want in this game. Use it to play AI vs AI, AI vs human, or even human vs human matches!"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
+ "# LET'S PLAY SOME GAMES!\n",
+ "\n",
+ "## Game52\n",
+ "\n",
"Let's start by experimenting with the `Fig52Game` first. For that we'll create an instance of the subclass Fig52Game inherited from the class Game:"
]
},
@@ -255,9 +487,7 @@
"cell_type": "code",
"execution_count": 2,
"metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
+ "collapsed": true
},
"outputs": [],
"source": [
@@ -266,10 +496,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"First we try out our `random_player(game, state)`. Given a game state it will give us a random move every time:"
]
@@ -277,11 +504,7 @@
{
"cell_type": "code",
"execution_count": 3,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -299,10 +522,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"The `alphabeta_player(game, state)` will always give us the best move possible, for the relevant player (MAX or MIN):"
]
@@ -310,11 +530,7 @@
{
"cell_type": "code",
"execution_count": 4,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -334,10 +550,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"What the `alphabeta_player` does is, it simply calls the method `alphabeta_full_search`. They both are essentially the same. In the module, both `alphabeta_full_search` and `minimax_decision` have been implemented. They both do the same job and return the same thing, which is, the best move in the current state. It's just that `alphabeta_full_search` is more efficient with regards to time because it prunes the search tree and hence, explores lesser number of states."
]
@@ -345,11 +558,7 @@
{
"cell_type": "code",
"execution_count": 5,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -369,11 +578,7 @@
{
"cell_type": "code",
"execution_count": 6,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -392,10 +597,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"Demonstrating the play_game function on the game52:"
]
@@ -403,11 +605,7 @@
{
"cell_type": "code",
"execution_count": 8,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -434,11 +632,7 @@
{
"cell_type": "code",
"execution_count": 9,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -465,11 +659,7 @@
{
"cell_type": "code",
"execution_count": 12,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -501,11 +691,7 @@
{
"cell_type": "code",
"execution_count": 14,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -536,22 +722,16 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"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."
]
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
- "### TicTacToe game\n",
+ "## TicTacToe\n",
"\n",
"Now let's play `TicTacToe`. First we initialize the game by creating an instance of the subclass TicTacToe inherited from the class Game:"
]
@@ -560,9 +740,7 @@
"cell_type": "code",
"execution_count": 15,
"metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
+ "collapsed": true
},
"outputs": [],
"source": [
@@ -571,10 +749,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"We can print a state using the display method:"
]
@@ -582,11 +757,7 @@
{
"cell_type": "code",
"execution_count": 16,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -604,10 +775,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"Hmm, so that's the initial state of the game; no X's and no O's.\n",
"\n",
@@ -618,9 +786,7 @@
"cell_type": "code",
"execution_count": 17,
"metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
+ "collapsed": true
},
"outputs": [],
"source": [
@@ -637,10 +803,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"So, how does this game state look like?"
]
@@ -648,11 +811,7 @@
{
"cell_type": "code",
"execution_count": 18,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -670,10 +829,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"The `random_player` will behave how he is supposed to i.e. *pseudo-randomly*:"
]
@@ -681,11 +837,7 @@
{
"cell_type": "code",
"execution_count": 19,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -705,11 +857,7 @@
{
"cell_type": "code",
"execution_count": 20,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -728,10 +876,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"But the `alphabeta_player` will always give the best move, as expected:"
]
@@ -739,11 +884,7 @@
{
"cell_type": "code",
"execution_count": 21,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -762,10 +903,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"Now let's make two players play against each other. We use the `play_game` function for this. The `play_game` function makes players play the match against each other and returns the utility for the first player, of the terminal state reached when the game ends. Hence, for our `TicTacToe` game, if we get the output +1, the first player wins, -1 if the second player wins, and 0 if the match ends in a draw."
]
@@ -773,11 +911,7 @@
{
"cell_type": "code",
"execution_count": 22,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -805,10 +939,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"The output is (usually) -1, because `random_player` loses to `alphabeta_player`. Sometimes, however, `random_player` manages to draw with `alphabeta_player`.\n",
"\n",
@@ -818,11 +949,7 @@
{
"cell_type": "code",
"execution_count": 24,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -878,10 +1005,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"A `random_player` should never win against an `alphabeta_player`. Let's test that."
]
@@ -889,11 +1013,7 @@
{
"cell_type": "code",
"execution_count": 25,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -949,10 +1069,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"## Canvas_TicTacToe(Canvas)\n",
"\n",
@@ -964,11 +1081,7 @@
{
"cell_type": "code",
"execution_count": 27,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -1016,10 +1129,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"Now, let's play a game ourselves against a `random_player`:"
]
@@ -1027,11 +1137,7 @@
{
"cell_type": "code",
"execution_count": 28,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -1079,10 +1185,7 @@
},
{
"cell_type": "markdown",
- "metadata": {
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"source": [
"Yay! We (usually) win. But we cannot win against an `alphabeta_player`, however hard we try."
]
@@ -1090,11 +1193,7 @@
{
"cell_type": "code",
"execution_count": 29,
- "metadata": {
- "collapsed": false,
- "deletable": true,
- "editable": true
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -1157,9 +1256,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.5.2"
+ "version": "3.5.2+"
}
},
"nbformat": 4,
- "nbformat_minor": 0
+ "nbformat_minor": 1
}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: