games.ipynb 97,1 ko
Newer Older
  {
   "cell_type": "markdown",
   "source": [
    "# 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."
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "* Game Representation\n",
    "* Game Examples\n",
    "    * Tic-Tac-Toe\n",
    "    * Figure 5.2 Game\n",
    "* Min-Max\n",
    "* Alpha-Beta\n",
    "* Players\n",
    "* Let's Play Some Games!"
   ]
  },
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
   "source": [
    "from games import *"
   ]
  },
  {
   "cell_type": "markdown",
   "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` 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",
   "source": [
    "\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",
    "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`."
   "outputs": [],
   ]
  },
  {
   "cell_type": "markdown",
   "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",
    "* `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",
    "* `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",
    "* `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",
    "* `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",
    "* `display(self, state)`: This method prints/displays the current state of the game."
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 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."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
   },
   "outputs": [],
   "source": [
   ]
  },
  {
   "cell_type": "markdown",
   "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",
    "Additional methods in TicTacToe:\n",
    "\n",
    "* `__init__(self, h=3, v=3, k=3)` :  When you create a class inherited from the `Game` class (class `TicTacToe` in our case), you'll have to create an object of this inherited class to initialize the game. This initialization might require some additional information which would be passed to `__init__` as variables. For the case of our `TicTacToe` game, this additional information would be the number of rows `h`, number of columns `v` and how many consecutive X's or O's are needed in a row, column or diagonal for a win `k`. Also, the initial game state has to be defined here in `__init__`.\n",
    "\n",
    "\n",
    "* `compute_utility(self, board, move, player)` : A method to calculate the utility of TicTacToe game. If 'X' wins with this move, this method returns 1; if 'O' wins return -1; else return 0.\n",
    "\n",
    "\n",
    "* `k_in_row(self, board, move, player, delta_x_y)` : This method returns `True` if there is a line formed on TicTacToe board with the latest move else `False.`"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### TicTacToe GameState\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."
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "To store game states will will use the `GameState` namedtuple.\n",
    "* `to_move`: A string of a single character, either 'X' or 'O'.\n",
    "* `utility`: 1 for win, -1 for loss, 0 otherwise.\n",
    "* `board`: All the positions of X's and O's on the board.\n",
    "* `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",
   "source": [
    "### 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",
    "For our `TicTacToe` game, we'll just represent a move by a tuple, where the first and the second elements of the tuple will represent the row and column, respectively, where the next move is to be made. Whether to make an 'X' or an 'O' will be decided by the `to_move` in the `GameState` namedtuple."
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "For a more trivial example we will represent the game in **Figure 5.2** of the book.\n",
    "<img src=\"images/fig_5_2.png\" width=\"75%\">\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",
    "We will model the moves, utilities and initial state like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "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",
    "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": 5,
   "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": 6,
   "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": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%psource Fig52Game.actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b1', 'b2', 'b3']\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": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%psource Fig52Game.result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "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'). Note that for 'MIN' the value returned is the negative of the utility."
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%psource Fig52Game.utility"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
    "print(fig52.utility('B1', 'MAX'))\n",
    "print(fig52.utility('B1', 'MIN'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`terminal_test`: Returns `True` if the given state is a terminal state, `False` otherwise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
   "source": [
    "%psource Fig52Game.terminal_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "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": 15,
   "metadata": {
   "source": [
    "%psource Fig52Game.to_move"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "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": 17,
   "metadata": {
   "outputs": [],
   "source": [
    "%psource Fig52Game"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MIN-MAX\n",
    "\n",
    "## Overview\n",
    "\n",
    "This algorithm (often called *Minimax*) computes the next move for a player (MIN or MAX) at their current state. It recursively computes the minimax value of successor states, until it reaches terminals (the leaves of the tree). Using the `utility` value of the terminal states, it computes the values of parent states until it reaches the initial node (the root of the tree).\n",
    "It is worth noting that the algorithm works in a depth-first manner."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Implementation\n",
    "\n",
    "In the implementation we are using two functions, `max_value` and `min_value` to calculate the best move for MAX and MIN respectively. These functions interact in an alternating recursion; one calls the other until a terminal state is reached. When the recursion halts, we are left with scores for each move. We return the max. Despite returning the max, it will work for MIN too since for MIN the values are their negative (hence the order of values is reversed, so the higher the better for MIN too)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%psource minimax_decision"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "We will now play the Fig52 game using this algorithm. Take a look at the Fig52Game from above to follow along.\n",
    "\n",
    "It is the turn of MAX to move, and he is at state A. He can move to B, C or D, using moves a1, a2 and a3 respectively. MAX's goal is to maximize the end value. So, to make a decision, MAX needs to know the values at the aforementioned nodes and pick the greatest one. After MAX, it is MIN's turn to play. So MAX wants to know what will the values of B, C and D be after MIN plays.\n",
    "\n",
    "The problem then becomes what move will MIN make at B, C and D. The successor states of all these nodes are terminal states, so MIN will pick the smallest value for each node. So, for B he will pick 3 (from move b1), for C he will pick 2 (from move c1) and for D he will again pick 2 (from move d3).\n",
    "\n",
    "Let's see this in code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b1\n",
      "c1\n",
      "d3\n"
     ]
    }
   ],
   "source": [
    "print(minimax_decision('B', fig52))\n",
    "print(minimax_decision('C', fig52))\n",
    "print(minimax_decision('D', fig52))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now MAX knows that the values for B, C and D are 3, 2 and 2 (produced by the above moves of MIN). The greatest is 3, which he will get with move a1. This is then the move MAX will make. Let's see the algorithm in full action:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1\n"
     ]
    }
   ],
   "source": [
    "print(minimax_decision('A', fig52))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualization\n",
    "\n",
    "Below we have a simple game visualization using the algorithm. After you run the command, click on the cell to move the game along. You can input your own values via a list of 27 integers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from notebook import Canvas_minimax\n",
    "from random import randint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
       "<div>\n",
       "<canvas id=\"minimax_viz\" width=\"800\" height=\"600\" style=\"background:rgba(158, 167, 184, 0.2);\" onclick='click_callback(this, event, \"minimax_viz\")'></canvas>\n",
       "</div>\n",
       "\n",
       "<script> var minimax_viz_canvas_object = new Canvas(\"minimax_viz\");</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<script>\n",
       "minimax_viz_canvas_object.font(\"12px Arial\");\n",
       "minimax_viz_canvas_object.clear();\n",
       "minimax_viz_canvas_object.stroke(0, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(390, 8, 20, 15);\n",
       "minimax_viz_canvas_object.line(390, 8, 410, 8);\n",
       "minimax_viz_canvas_object.line(390, 8, 390, 23);\n",
       "minimax_viz_canvas_object.line(410, 23, 410, 8);\n",
       "minimax_viz_canvas_object.line(410, 23, 390, 23);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(123, 198, 20, 15);\n",
       "minimax_viz_canvas_object.line(123, 198, 143, 198);\n",
       "minimax_viz_canvas_object.line(123, 198, 123, 213);\n",
       "minimax_viz_canvas_object.line(143, 213, 143, 198);\n",
       "minimax_viz_canvas_object.line(143, 213, 123, 213);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(390, 198, 20, 15);\n",
       "minimax_viz_canvas_object.line(390, 198, 410, 198);\n",
       "minimax_viz_canvas_object.line(390, 198, 390, 213);\n",
       "minimax_viz_canvas_object.line(410, 213, 410, 198);\n",
       "minimax_viz_canvas_object.line(410, 213, 390, 213);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(657, 198, 20, 15);\n",
       "minimax_viz_canvas_object.line(657, 198, 677, 198);\n",
       "minimax_viz_canvas_object.line(657, 198, 657, 213);\n",
       "minimax_viz_canvas_object.line(677, 213, 677, 198);\n",
       "minimax_viz_canvas_object.line(677, 213, 657, 213);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(34, 388, 20, 15);\n",
       "minimax_viz_canvas_object.line(34, 388, 54, 388);\n",
       "minimax_viz_canvas_object.line(34, 388, 34, 403);\n",
       "minimax_viz_canvas_object.line(54, 403, 54, 388);\n",
       "minimax_viz_canvas_object.line(54, 403, 34, 403);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(123, 388, 20, 15);\n",
       "minimax_viz_canvas_object.line(123, 388, 143, 388);\n",
       "minimax_viz_canvas_object.line(123, 388, 123, 403);\n",
       "minimax_viz_canvas_object.line(143, 403, 143, 388);\n",
       "minimax_viz_canvas_object.line(143, 403, 123, 403);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(212, 388, 20, 15);\n",
       "minimax_viz_canvas_object.line(212, 388, 232, 388);\n",
       "minimax_viz_canvas_object.line(212, 388, 212, 403);\n",
       "minimax_viz_canvas_object.line(232, 403, 232, 388);\n",
       "minimax_viz_canvas_object.line(232, 403, 212, 403);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(301, 388, 20, 15);\n",
       "minimax_viz_canvas_object.line(301, 388, 321, 388);\n",
       "minimax_viz_canvas_object.line(301, 388, 301, 403);\n",
       "minimax_viz_canvas_object.line(321, 403, 321, 388);\n",
       "minimax_viz_canvas_object.line(321, 403, 301, 403);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(390, 388, 20, 15);\n",
       "minimax_viz_canvas_object.line(390, 388, 410, 388);\n",
       "minimax_viz_canvas_object.line(390, 388, 390, 403);\n",
       "minimax_viz_canvas_object.line(410, 403, 410, 388);\n",
       "minimax_viz_canvas_object.line(410, 403, 390, 403);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(479, 388, 20, 15);\n",
       "minimax_viz_canvas_object.line(479, 388, 499, 388);\n",
       "minimax_viz_canvas_object.line(479, 388, 479, 403);\n",
       "minimax_viz_canvas_object.line(499, 403, 499, 388);\n",
       "minimax_viz_canvas_object.line(499, 403, 479, 403);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(568, 388, 20, 15);\n",
       "minimax_viz_canvas_object.line(568, 388, 588, 388);\n",
       "minimax_viz_canvas_object.line(568, 388, 568, 403);\n",
       "minimax_viz_canvas_object.line(588, 403, 588, 388);\n",
       "minimax_viz_canvas_object.line(588, 403, 568, 403);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(657, 388, 20, 15);\n",
       "minimax_viz_canvas_object.line(657, 388, 677, 388);\n",
       "minimax_viz_canvas_object.line(657, 388, 657, 403);\n",
       "minimax_viz_canvas_object.line(677, 403, 677, 388);\n",
       "minimax_viz_canvas_object.line(677, 403, 657, 403);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(200, 200, 200);\n",
       "minimax_viz_canvas_object.rect(746, 388, 20, 15);\n",
       "minimax_viz_canvas_object.line(746, 388, 766, 388);\n",
       "minimax_viz_canvas_object.line(746, 388, 746, 403);\n",
       "minimax_viz_canvas_object.line(766, 403, 766, 388);\n",
       "minimax_viz_canvas_object.line(766, 403, 746, 403);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(5, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(5, 578, 25, 578);\n",
       "minimax_viz_canvas_object.line(5, 578, 5, 593);\n",
       "minimax_viz_canvas_object.line(25, 593, 25, 578);\n",
       "minimax_viz_canvas_object.line(25, 593, 5, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"44\", 7, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(34, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(34, 578, 54, 578);\n",
       "minimax_viz_canvas_object.line(34, 578, 34, 593);\n",
       "minimax_viz_canvas_object.line(54, 593, 54, 578);\n",
       "minimax_viz_canvas_object.line(54, 593, 34, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"12\", 36, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(64, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(64, 578, 84, 578);\n",
       "minimax_viz_canvas_object.line(64, 578, 64, 593);\n",
       "minimax_viz_canvas_object.line(84, 593, 84, 578);\n",
       "minimax_viz_canvas_object.line(84, 593, 64, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"10\", 66, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(94, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(94, 578, 114, 578);\n",
       "minimax_viz_canvas_object.line(94, 578, 94, 593);\n",
       "minimax_viz_canvas_object.line(114, 593, 114, 578);\n",
       "minimax_viz_canvas_object.line(114, 593, 94, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"7\", 96, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(123, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(123, 578, 143, 578);\n",
       "minimax_viz_canvas_object.line(123, 578, 123, 593);\n",
       "minimax_viz_canvas_object.line(143, 593, 143, 578);\n",
       "minimax_viz_canvas_object.line(143, 593, 123, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"34\", 125, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(153, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(153, 578, 173, 578);\n",
       "minimax_viz_canvas_object.line(153, 578, 153, 593);\n",
       "minimax_viz_canvas_object.line(173, 593, 173, 578);\n",
       "minimax_viz_canvas_object.line(173, 593, 153, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"25\", 155, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(183, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(183, 578, 203, 578);\n",
       "minimax_viz_canvas_object.line(183, 578, 183, 593);\n",
       "minimax_viz_canvas_object.line(203, 593, 203, 578);\n",
       "minimax_viz_canvas_object.line(203, 593, 183, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"35\", 185, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(212, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(212, 578, 232, 578);\n",
       "minimax_viz_canvas_object.line(212, 578, 212, 593);\n",
       "minimax_viz_canvas_object.line(232, 593, 232, 578);\n",
       "minimax_viz_canvas_object.line(232, 593, 212, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"43\", 214, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(242, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(242, 578, 262, 578);\n",
       "minimax_viz_canvas_object.line(242, 578, 242, 593);\n",
       "minimax_viz_canvas_object.line(262, 593, 262, 578);\n",
       "minimax_viz_canvas_object.line(262, 593, 242, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"20\", 244, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(271, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(271, 578, 291, 578);\n",
       "minimax_viz_canvas_object.line(271, 578, 271, 593);\n",
       "minimax_viz_canvas_object.line(291, 593, 291, 578);\n",
       "minimax_viz_canvas_object.line(291, 593, 271, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"11\", 273, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(301, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(301, 578, 321, 578);\n",
       "minimax_viz_canvas_object.line(301, 578, 301, 593);\n",
       "minimax_viz_canvas_object.line(321, 593, 321, 578);\n",
       "minimax_viz_canvas_object.line(321, 593, 301, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"37\", 303, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(331, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(331, 578, 351, 578);\n",
       "minimax_viz_canvas_object.line(331, 578, 331, 593);\n",
       "minimax_viz_canvas_object.line(351, 593, 351, 578);\n",
       "minimax_viz_canvas_object.line(351, 593, 331, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"49\", 333, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(360, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(360, 578, 380, 578);\n",
       "minimax_viz_canvas_object.line(360, 578, 360, 593);\n",
       "minimax_viz_canvas_object.line(380, 593, 380, 578);\n",
       "minimax_viz_canvas_object.line(380, 593, 360, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"47\", 362, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(390, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(390, 578, 410, 578);\n",
       "minimax_viz_canvas_object.line(390, 578, 390, 593);\n",
       "minimax_viz_canvas_object.line(410, 593, 410, 578);\n",
       "minimax_viz_canvas_object.line(410, 593, 390, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"12\", 392, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(420, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(420, 578, 440, 578);\n",
       "minimax_viz_canvas_object.line(420, 578, 420, 593);\n",
       "minimax_viz_canvas_object.line(440, 593, 440, 578);\n",
       "minimax_viz_canvas_object.line(440, 593, 420, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"18\", 422, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(449, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(449, 578, 469, 578);\n",
       "minimax_viz_canvas_object.line(449, 578, 449, 593);\n",
       "minimax_viz_canvas_object.line(469, 593, 469, 578);\n",
       "minimax_viz_canvas_object.line(469, 593, 449, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"40\", 451, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(479, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(479, 578, 499, 578);\n",
       "minimax_viz_canvas_object.line(479, 578, 479, 593);\n",
       "minimax_viz_canvas_object.line(499, 593, 499, 578);\n",
       "minimax_viz_canvas_object.line(499, 593, 479, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"37\", 481, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(509, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(509, 578, 529, 578);\n",
       "minimax_viz_canvas_object.line(509, 578, 509, 593);\n",
       "minimax_viz_canvas_object.line(529, 593, 529, 578);\n",
       "minimax_viz_canvas_object.line(529, 593, 509, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"30\", 511, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(538, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(538, 578, 558, 578);\n",
       "minimax_viz_canvas_object.line(538, 578, 538, 593);\n",
       "minimax_viz_canvas_object.line(558, 593, 558, 578);\n",
       "minimax_viz_canvas_object.line(558, 593, 538, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"44\", 540, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(568, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(568, 578, 588, 578);\n",
       "minimax_viz_canvas_object.line(568, 578, 568, 593);\n",
       "minimax_viz_canvas_object.line(588, 593, 588, 578);\n",
       "minimax_viz_canvas_object.line(588, 593, 568, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"32\", 570, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(597, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(597, 578, 617, 578);\n",
       "minimax_viz_canvas_object.line(597, 578, 597, 593);\n",
       "minimax_viz_canvas_object.line(617, 593, 617, 578);\n",
       "minimax_viz_canvas_object.line(617, 593, 597, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"31\", 599, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(627, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(627, 578, 647, 578);\n",
       "minimax_viz_canvas_object.line(627, 578, 627, 593);\n",
       "minimax_viz_canvas_object.line(647, 593, 647, 578);\n",
       "minimax_viz_canvas_object.line(647, 593, 627, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"39\", 629, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(657, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(657, 578, 677, 578);\n",
       "minimax_viz_canvas_object.line(657, 578, 657, 593);\n",
       "minimax_viz_canvas_object.line(677, 593, 677, 578);\n",
       "minimax_viz_canvas_object.line(677, 593, 657, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"47\", 659, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(686, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(686, 578, 706, 578);\n",
       "minimax_viz_canvas_object.line(686, 578, 686, 593);\n",
       "minimax_viz_canvas_object.line(706, 593, 706, 578);\n",
       "minimax_viz_canvas_object.line(706, 593, 686, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"16\", 688, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(716, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(716, 578, 736, 578);\n",
       "minimax_viz_canvas_object.line(716, 578, 716, 593);\n",
       "minimax_viz_canvas_object.line(736, 593, 736, 578);\n",
       "minimax_viz_canvas_object.line(736, 593, 716, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"7\", 718, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(746, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(746, 578, 766, 578);\n",
       "minimax_viz_canvas_object.line(746, 578, 746, 593);\n",
       "minimax_viz_canvas_object.line(766, 593, 766, 578);\n",
       "minimax_viz_canvas_object.line(766, 593, 746, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"32\", 748, 591);\n",
       "minimax_viz_canvas_object.fill(255, 255, 255);\n",
       "minimax_viz_canvas_object.rect(775, 578, 20, 15);\n",
       "minimax_viz_canvas_object.line(775, 578, 795, 578);\n",
       "minimax_viz_canvas_object.line(775, 578, 775, 593);\n",
       "minimax_viz_canvas_object.line(795, 593, 795, 578);\n",
       "minimax_viz_canvas_object.line(795, 593, 775, 593);\n",
       "minimax_viz_canvas_object.fill(0, 0, 0);\n",
       "minimax_viz_canvas_object.fill_text(\"5\", 777, 591);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(400, 23, 133, 198);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(400, 23, 400, 198);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(400, 23, 667, 198);\n",
       "minimax_viz_canvas_object.stroke(200, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(133, 213, 44, 388);\n",
       "minimax_viz_canvas_object.stroke(200, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(133, 213, 133, 388);\n",
       "minimax_viz_canvas_object.stroke(200, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(133, 213, 222, 388);\n",
       "minimax_viz_canvas_object.stroke(200, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(400, 213, 311, 388);\n",
       "minimax_viz_canvas_object.stroke(200, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(400, 213, 400, 388);\n",
       "minimax_viz_canvas_object.stroke(200, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(400, 213, 489, 388);\n",
       "minimax_viz_canvas_object.stroke(200, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(667, 213, 578, 388);\n",
       "minimax_viz_canvas_object.stroke(200, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(667, 213, 667, 388);\n",
       "minimax_viz_canvas_object.stroke(200, 0, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(667, 213, 756, 388);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(44, 403, 15, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(44, 403, 44, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(44, 403, 74, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(133, 403, 104, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(133, 403, 133, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(133, 403, 163, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(222, 403, 193, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(222, 403, 222, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(222, 403, 252, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(311, 403, 281, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(311, 403, 311, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(311, 403, 341, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(400, 403, 370, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(400, 403, 400, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(400, 403, 430, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(489, 403, 459, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(489, 403, 489, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(489, 403, 519, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(578, 403, 548, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(578, 403, 578, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(578, 403, 607, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(667, 403, 637, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(667, 403, 667, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(667, 403, 696, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(756, 403, 726, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(756, 403, 756, 578);\n",
       "minimax_viz_canvas_object.stroke(0, 200, 0);\n",
       "minimax_viz_canvas_object.strokeWidth(1);\n",
       "minimax_viz_canvas_object.line(756, 403, 785, 578);\n",
       "</script>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "minimax_viz = Canvas_minimax('minimax_viz', [randint(1, 50) for i in range(27)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ALPHA-BETA\n",
    "\n",
    "## Overview\n",
    "\n",
    "While *Minimax* is great for computing a move, it can get tricky when the number of games states gets bigger. The algorithm needs to search all the leaves of the tree, which increase exponentially to its depth.\n",
    "\n",
    "For Tic-Tac-Toe, where the depth of the tree is 9 (after the 9th move, the game ends), we can have at most 9! terminal states (at most because not all terminal nodes are at the last level of the tree; some are higher up because the game ended before the 9th move). This isn't so bad, but for more complex problems like chess, we have over $10^{40}$ terminal nodes. Unfortunately we have not found a way to cut the exponent away, but we nevertheless have found ways to alleviate the workload.\n",
    "\n",
    "Here we examine *pruning* the game tree, which means removing parts of it that we do not need to examine. The particular type of pruning is called *alpha-beta*, and the search in whole is called *alpha-beta search*.\n",
    "\n",
    "To showcase what parts of the tree we don't need to search, we will take a look at the example `Fig52Game`.\n",
    "\n",
    "In the example game, we need to find the best move for player MAX at state A, which is the maximum value of MIN's possible moves at successor states.\n",
    "\n",
    "`MAX(A) = MAX( MIN(B), MIN(C), MIN(D) )`\n",
    "\n",
    "`MIN(B)` is the minimum of 3, 12, 8 which is 3. So the above formula becomes:\n",
    "\n",
    "`MAX(A) = MAX( 3, MIN(C), MIN(D) )`\n",
    "\n",
    "Next move we will check is c1, which leads to a terminal state with utility of 2. Before we continue searching under state C, let's pop back into our formula with the new value:\n",
    "\n",
    "`MAX(A) = MAX( 3, MIN(2, c2, .... cN), MIN(D) )`\n",
    "\n",
    "We do not know how many moves state C allows, but we know that the first one results in a value of 2. Do we need to keep searching under C? The answer is no. The value MIN will pick on C will at most be 2. Since MAX already has the option to pick something greater than that, 3 from B, he does not need to keep searching under C.\n",
    "\n",
    "In *alpha-beta* we make use of two additional parameters for each state/node, *a* and *b*, that describe bounds on the possible moves. The parameter *a* denotes the best choice (highest value) for MAX along that path, while *b* denotes the best choice (lowest value) for MIN. As we go along we update *a* and *b* and prune a node branch when the value of the node is worse than the value of *a* and *b* for MAX and MIN respectively.\n",
    "\n",
    "In the above example, after the search under state B, MAX had an *a* value of 3. So, when searching node C we found a value less than that, 2, we stopped searching under C."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Implementation\n",
    "\n",
    "Like *minimax*, we again make use of functions `max_value` and `min_value`, but this time we utilise the *a* and *b* values, updating them and stopping the recursive call if we end up on nodes with values worse than *a* and *b* (for MAX and MIN). The algorithm finds the maximum value and returns the move that results in it.\n",
    "\n",
    "The implementation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%psource alphabeta_search"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "We will play the Fig52 Game with the *alpha-beta* search algorithm. It is the turn of MAX to play at state A."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1\n"
     ]
    }
   ],
   "source": [
    "print(alphabeta_search('A', fig52))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The optimal move for MAX is a1, for the reasons given above. MIN will pick move b1 for B resulting in a value of 3, updating the *a* value of MAX to 3. Then, when we find under C a node of value 2, we will stop searching under that sub-tree since it is less than *a*. From D we have a value of 2. So, the best move for MAX is the one resulting in a value of 3, which is a1.\n",
    "\n",
    "Below we see the best moves for MIN starting from B, C and D respectively. Note that the algorithm in these cases works the same way as *minimax*, since all the nodes below the aforementioned states are terminal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b1\n",
      "c1\n",
      "d3\n"
     ]
    }
   ],
   "source": [
    "print(alphabeta_search('B', fig52))\n",
    "print(alphabeta_search('C', fig52))\n",
    "print(alphabeta_search('D', fig52))"
   ]
  },
   "cell_type": "markdown",
   "metadata": {},
    "## Visualization\n",
    "\n",
    "Below you will find the visualization of the alpha-beta algorithm for a simple game. Click on the cell after you run the command to move the game along. You can input your own values via a list of 27 integers."
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
    "from notebook import Canvas_alphabeta\n",
    "from random import randint"
   "execution_count": 3,
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
       "<div>\n",
       "<canvas id=\"alphabeta_viz\" width=\"800\" height=\"600\" style=\"background:rgba(158, 167, 184, 0.2);\" onclick='click_callback(this, event, \"alphabeta_viz\")'></canvas>\n",
       "</div>\n",
       "\n",
       "<script> var alphabeta_viz_canvas_object = new Canvas(\"alphabeta_viz\");</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<script>\n",
       "alphabeta_viz_canvas_object.font(\"12px Arial\");\n",
       "alphabeta_viz_canvas_object.clear();\n",
       "alphabeta_viz_canvas_object.stroke(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(390, 23, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(390, 23, 410, 23);\n",
       "alphabeta_viz_canvas_object.line(390, 23, 390, 38);\n",
       "alphabeta_viz_canvas_object.line(410, 38, 410, 23);\n",
       "alphabeta_viz_canvas_object.line(410, 38, 390, 38);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(123, 208, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(123, 208, 143, 208);\n",
       "alphabeta_viz_canvas_object.line(123, 208, 123, 222);\n",
       "alphabeta_viz_canvas_object.line(143, 222, 143, 208);\n",
       "alphabeta_viz_canvas_object.line(143, 222, 123, 222);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(390, 208, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(390, 208, 410, 208);\n",
       "alphabeta_viz_canvas_object.line(390, 208, 390, 222);\n",
       "alphabeta_viz_canvas_object.line(410, 222, 410, 208);\n",
       "alphabeta_viz_canvas_object.line(410, 222, 390, 222);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(657, 208, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(657, 208, 677, 208);\n",
       "alphabeta_viz_canvas_object.line(657, 208, 657, 222);\n",
       "alphabeta_viz_canvas_object.line(677, 222, 677, 208);\n",
       "alphabeta_viz_canvas_object.line(677, 222, 657, 222);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(34, 392, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(34, 392, 54, 392);\n",
       "alphabeta_viz_canvas_object.line(34, 392, 34, 408);\n",
       "alphabeta_viz_canvas_object.line(54, 408, 54, 392);\n",
       "alphabeta_viz_canvas_object.line(54, 408, 34, 408);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(123, 392, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(123, 392, 143, 392);\n",
       "alphabeta_viz_canvas_object.line(123, 392, 123, 408);\n",
       "alphabeta_viz_canvas_object.line(143, 408, 143, 392);\n",
       "alphabeta_viz_canvas_object.line(143, 408, 123, 408);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(212, 392, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(212, 392, 232, 392);\n",
       "alphabeta_viz_canvas_object.line(212, 392, 212, 408);\n",
       "alphabeta_viz_canvas_object.line(232, 408, 232, 392);\n",
       "alphabeta_viz_canvas_object.line(232, 408, 212, 408);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(301, 392, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(301, 392, 321, 392);\n",
       "alphabeta_viz_canvas_object.line(301, 392, 301, 408);\n",
       "alphabeta_viz_canvas_object.line(321, 408, 321, 392);\n",
       "alphabeta_viz_canvas_object.line(321, 408, 301, 408);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(390, 392, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(390, 392, 410, 392);\n",
       "alphabeta_viz_canvas_object.line(390, 392, 390, 408);\n",
       "alphabeta_viz_canvas_object.line(410, 408, 410, 392);\n",
       "alphabeta_viz_canvas_object.line(410, 408, 390, 408);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(479, 392, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(479, 392, 499, 392);\n",
       "alphabeta_viz_canvas_object.line(479, 392, 479, 408);\n",
       "alphabeta_viz_canvas_object.line(499, 408, 499, 392);\n",
       "alphabeta_viz_canvas_object.line(499, 408, 479, 408);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(568, 392, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(568, 392, 588, 392);\n",
       "alphabeta_viz_canvas_object.line(568, 392, 568, 408);\n",
       "alphabeta_viz_canvas_object.line(588, 408, 588, 392);\n",
       "alphabeta_viz_canvas_object.line(588, 408, 568, 408);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(657, 392, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(657, 392, 677, 392);\n",
       "alphabeta_viz_canvas_object.line(657, 392, 657, 408);\n",
       "alphabeta_viz_canvas_object.line(677, 408, 677, 392);\n",
       "alphabeta_viz_canvas_object.line(677, 408, 657, 408);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(200, 200, 200);\n",
       "alphabeta_viz_canvas_object.rect(746, 392, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(746, 392, 766, 392);\n",
       "alphabeta_viz_canvas_object.line(746, 392, 746, 408);\n",
       "alphabeta_viz_canvas_object.line(766, 408, 766, 392);\n",
       "alphabeta_viz_canvas_object.line(766, 408, 746, 408);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(5, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(5, 578, 25, 578);\n",
       "alphabeta_viz_canvas_object.line(5, 578, 5, 592);\n",
       "alphabeta_viz_canvas_object.line(25, 592, 25, 578);\n",
       "alphabeta_viz_canvas_object.line(25, 592, 5, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"50\", 7, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(34, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(34, 578, 54, 578);\n",
       "alphabeta_viz_canvas_object.line(34, 578, 34, 592);\n",
       "alphabeta_viz_canvas_object.line(54, 592, 54, 578);\n",
       "alphabeta_viz_canvas_object.line(54, 592, 34, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"5\", 36, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(64, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(64, 578, 84, 578);\n",
       "alphabeta_viz_canvas_object.line(64, 578, 64, 592);\n",
       "alphabeta_viz_canvas_object.line(84, 592, 84, 578);\n",
       "alphabeta_viz_canvas_object.line(84, 592, 64, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"1\", 66, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(94, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(94, 578, 114, 578);\n",
       "alphabeta_viz_canvas_object.line(94, 578, 94, 592);\n",
       "alphabeta_viz_canvas_object.line(114, 592, 114, 578);\n",
       "alphabeta_viz_canvas_object.line(114, 592, 94, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"30\", 96, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(123, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(123, 578, 143, 578);\n",
       "alphabeta_viz_canvas_object.line(123, 578, 123, 592);\n",
       "alphabeta_viz_canvas_object.line(143, 592, 143, 578);\n",
       "alphabeta_viz_canvas_object.line(143, 592, 123, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"20\", 125, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(153, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(153, 578, 173, 578);\n",
       "alphabeta_viz_canvas_object.line(153, 578, 153, 592);\n",
       "alphabeta_viz_canvas_object.line(173, 592, 173, 578);\n",
       "alphabeta_viz_canvas_object.line(173, 592, 153, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"48\", 155, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(183, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(183, 578, 203, 578);\n",
       "alphabeta_viz_canvas_object.line(183, 578, 183, 592);\n",
       "alphabeta_viz_canvas_object.line(203, 592, 203, 578);\n",
       "alphabeta_viz_canvas_object.line(203, 592, 183, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"11\", 185, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(212, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(212, 578, 232, 578);\n",
       "alphabeta_viz_canvas_object.line(212, 578, 212, 592);\n",
       "alphabeta_viz_canvas_object.line(232, 592, 232, 578);\n",
       "alphabeta_viz_canvas_object.line(232, 592, 212, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"37\", 214, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(242, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(242, 578, 262, 578);\n",
       "alphabeta_viz_canvas_object.line(242, 578, 242, 592);\n",
       "alphabeta_viz_canvas_object.line(262, 592, 262, 578);\n",
       "alphabeta_viz_canvas_object.line(262, 592, 242, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"19\", 244, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(271, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(271, 578, 291, 578);\n",
       "alphabeta_viz_canvas_object.line(271, 578, 271, 592);\n",
       "alphabeta_viz_canvas_object.line(291, 592, 291, 578);\n",
       "alphabeta_viz_canvas_object.line(291, 592, 271, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"47\", 273, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(301, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(301, 578, 321, 578);\n",
       "alphabeta_viz_canvas_object.line(301, 578, 301, 592);\n",
       "alphabeta_viz_canvas_object.line(321, 592, 321, 578);\n",
       "alphabeta_viz_canvas_object.line(321, 592, 301, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"25\", 303, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(331, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(331, 578, 351, 578);\n",
       "alphabeta_viz_canvas_object.line(331, 578, 331, 592);\n",
       "alphabeta_viz_canvas_object.line(351, 592, 351, 578);\n",
       "alphabeta_viz_canvas_object.line(351, 592, 331, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"34\", 333, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(360, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(360, 578, 380, 578);\n",
       "alphabeta_viz_canvas_object.line(360, 578, 360, 592);\n",
       "alphabeta_viz_canvas_object.line(380, 592, 380, 578);\n",
       "alphabeta_viz_canvas_object.line(380, 592, 360, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"35\", 362, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(390, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(390, 578, 410, 578);\n",
       "alphabeta_viz_canvas_object.line(390, 578, 390, 592);\n",
       "alphabeta_viz_canvas_object.line(410, 592, 410, 578);\n",
       "alphabeta_viz_canvas_object.line(410, 592, 390, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"3\", 392, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(420, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(420, 578, 440, 578);\n",
       "alphabeta_viz_canvas_object.line(420, 578, 420, 592);\n",
       "alphabeta_viz_canvas_object.line(440, 592, 440, 578);\n",
       "alphabeta_viz_canvas_object.line(440, 592, 420, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"28\", 422, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(449, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(449, 578, 469, 578);\n",
       "alphabeta_viz_canvas_object.line(449, 578, 449, 592);\n",
       "alphabeta_viz_canvas_object.line(469, 592, 469, 578);\n",
       "alphabeta_viz_canvas_object.line(469, 592, 449, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"9\", 451, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(479, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(479, 578, 499, 578);\n",
       "alphabeta_viz_canvas_object.line(479, 578, 479, 592);\n",
       "alphabeta_viz_canvas_object.line(499, 592, 499, 578);\n",
       "alphabeta_viz_canvas_object.line(499, 592, 479, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"24\", 481, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(509, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(509, 578, 529, 578);\n",
       "alphabeta_viz_canvas_object.line(509, 578, 509, 592);\n",
       "alphabeta_viz_canvas_object.line(529, 592, 529, 578);\n",
       "alphabeta_viz_canvas_object.line(529, 592, 509, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"1\", 511, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(538, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(538, 578, 558, 578);\n",
       "alphabeta_viz_canvas_object.line(538, 578, 538, 592);\n",
       "alphabeta_viz_canvas_object.line(558, 592, 558, 578);\n",
       "alphabeta_viz_canvas_object.line(558, 592, 538, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"21\", 540, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(568, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(568, 578, 588, 578);\n",
       "alphabeta_viz_canvas_object.line(568, 578, 568, 592);\n",
       "alphabeta_viz_canvas_object.line(588, 592, 588, 578);\n",
       "alphabeta_viz_canvas_object.line(588, 592, 568, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"9\", 570, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(597, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(597, 578, 617, 578);\n",
       "alphabeta_viz_canvas_object.line(597, 578, 597, 592);\n",
       "alphabeta_viz_canvas_object.line(617, 592, 617, 578);\n",
       "alphabeta_viz_canvas_object.line(617, 592, 597, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"45\", 599, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(627, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(627, 578, 647, 578);\n",
       "alphabeta_viz_canvas_object.line(627, 578, 627, 592);\n",
       "alphabeta_viz_canvas_object.line(647, 592, 647, 578);\n",
       "alphabeta_viz_canvas_object.line(647, 592, 627, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"33\", 629, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(657, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(657, 578, 677, 578);\n",
       "alphabeta_viz_canvas_object.line(657, 578, 657, 592);\n",
       "alphabeta_viz_canvas_object.line(677, 592, 677, 578);\n",
       "alphabeta_viz_canvas_object.line(677, 592, 657, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"38\", 659, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(686, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(686, 578, 706, 578);\n",
       "alphabeta_viz_canvas_object.line(686, 578, 686, 592);\n",
       "alphabeta_viz_canvas_object.line(706, 592, 706, 578);\n",
       "alphabeta_viz_canvas_object.line(706, 592, 686, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"27\", 688, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(716, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(716, 578, 736, 578);\n",
       "alphabeta_viz_canvas_object.line(716, 578, 716, 592);\n",
       "alphabeta_viz_canvas_object.line(736, 592, 736, 578);\n",
       "alphabeta_viz_canvas_object.line(736, 592, 716, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"33\", 718, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(746, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(746, 578, 766, 578);\n",
       "alphabeta_viz_canvas_object.line(746, 578, 746, 592);\n",
       "alphabeta_viz_canvas_object.line(766, 592, 766, 578);\n",
       "alphabeta_viz_canvas_object.line(766, 592, 746, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"22\", 748, 591);\n",
       "alphabeta_viz_canvas_object.fill(255, 255, 255);\n",
       "alphabeta_viz_canvas_object.rect(775, 578, 20, 15);\n",
       "alphabeta_viz_canvas_object.line(775, 578, 795, 578);\n",
       "alphabeta_viz_canvas_object.line(775, 578, 775, 592);\n",
       "alphabeta_viz_canvas_object.line(795, 592, 795, 578);\n",
       "alphabeta_viz_canvas_object.line(795, 592, 775, 592);\n",
       "alphabeta_viz_canvas_object.fill(0, 0, 0);\n",
       "alphabeta_viz_canvas_object.fill_text(\"24\", 777, 591);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(400, 38, 133, 208);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(400, 38, 400, 208);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(400, 38, 667, 208);\n",
       "alphabeta_viz_canvas_object.stroke(200, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(133, 222, 44, 392);\n",
       "alphabeta_viz_canvas_object.stroke(200, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(133, 222, 133, 392);\n",
       "alphabeta_viz_canvas_object.stroke(200, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(133, 222, 222, 392);\n",
       "alphabeta_viz_canvas_object.stroke(200, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(400, 222, 311, 392);\n",
       "alphabeta_viz_canvas_object.stroke(200, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(400, 222, 400, 392);\n",
       "alphabeta_viz_canvas_object.stroke(200, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(400, 222, 489, 392);\n",
       "alphabeta_viz_canvas_object.stroke(200, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(667, 222, 578, 392);\n",
       "alphabeta_viz_canvas_object.stroke(200, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(667, 222, 667, 392);\n",
       "alphabeta_viz_canvas_object.stroke(200, 0, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(667, 222, 756, 392);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(44, 408, 15, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(44, 408, 44, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(44, 408, 74, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(133, 408, 104, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(133, 408, 133, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(133, 408, 163, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(222, 408, 193, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(222, 408, 222, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(222, 408, 252, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(311, 408, 281, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(311, 408, 311, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(311, 408, 341, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(400, 408, 370, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(400, 408, 400, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(400, 408, 430, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(489, 408, 459, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(489, 408, 489, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(489, 408, 519, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(578, 408, 548, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(578, 408, 578, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(578, 408, 607, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(667, 408, 637, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(667, 408, 667, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(667, 408, 696, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(756, 408, 726, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(756, 408, 756, 578);\n",
       "alphabeta_viz_canvas_object.stroke(0, 200, 0);\n",
       "alphabeta_viz_canvas_object.strokeWidth(1);\n",
       "alphabeta_viz_canvas_object.line(756, 408, 785, 578);\n",
       "</script>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
    "alphabeta_viz = Canvas_alphabeta('alphabeta_viz', [randint(1, 50) for i in range(27)])"
  {
   "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_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:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
   },
   "outputs": [],
   "source": [
    "game52 = Fig52Game()"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "First we try out our `random_player(game, state)`. Given a game state it will give us a random move every time:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1\n",
   "source": [
    "print(random_player(game52, 'A'))\n",
    "print(random_player(game52, 'A'))"
   ]
  },
  {
   "cell_type": "markdown",
    "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": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1\n",
      "b1\n",
      "c1\n"
     ]
    }
   ],
   "source": [
    "print( alphabeta_player(game52, 'A') )\n",
    "print( alphabeta_player(game52, 'B') )\n",
    "print( alphabeta_player(game52, 'C') )"
   ]
  },
  {
   "cell_type": "markdown",
   "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."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
     "execution_count": 30,
   "source": [
   "execution_count": 31,
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alphabeta_search('A', game52)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "Demonstrating the play_game function on the game52:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "game52.play_game(alphabeta_player, alphabeta_player)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
     ]
    },
    {
     "data": {
      "text/plain": [
     "execution_count": 33,
   "source": [
    "game52.play_game(alphabeta_player, random_player)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "current state:\n",
      "A\n",
      "available moves: ['a1', 'a2', 'a3']\n",
      "Your move? a1\n",
      "B1\n"
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "game52.play_game(query_player, alphabeta_player)"
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "current state:\n",
      "B\n",
      "available moves: ['b1', 'b2', 'b3']\n",
      "Your move? b1\n",
      "B1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "game52.play_game(alphabeta_player, query_player)"
   ]
  },
  {
   "cell_type": "markdown",
   "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."
    "Now let's play `TicTacToe`. First we initialize the game by creating an instance of the subclass TicTacToe inherited from the class Game:"
   "execution_count": 36,
   "source": [
    "ttt = TicTacToe()"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "We can print a state using the display method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ". . . \n",
      ". . . \n",
      ". . . \n"
     ]
    }
   ],
   "source": [
    "ttt.display(ttt.initial)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "Hmm, so that's the initial state of the game; no X's and no O's.\n",
    "\n",
    "Let us create a new game state by ourselves to experiment:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
   },
   "outputs": [],
   "source": [
    "my_state = GameState(\n",
    "    to_move = 'X',\n",
    "    utility = '0',\n",
    "    board = {(1,1): 'X', (1,2): 'O', (1,3): 'X',\n",
    "             (2,1): 'O',             (2,3): 'O',\n",
    "             (3,1): 'X',\n",
    "            },\n",
    "    moves = [(2,2), (3,2), (3,3)]\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "So, how does this game state look like?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X O X \n",
      "O . O \n",
      "X . . \n"
     ]
    }
   ],
   "source": [
    "ttt.display(my_state)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "The `random_player` will behave how he is supposed to i.e. *pseudo-randomly*:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
     "execution_count": 40,
   "source": [
    "random_player(ttt, my_state)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
     "execution_count": 41,
   "source": [
    "random_player(ttt, my_state)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "But the `alphabeta_player` will always give the best move, as expected:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
     "execution_count": 42,
   "source": [
    "alphabeta_player(ttt, my_state)"
   ]
  },
  {
   "cell_type": "markdown",
   "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."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "O O . \n",
      "X O X \n",
      "X X O \n"
    },
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
   "source": [
    "ttt.play_game(random_player, alphabeta_player)"
   ]
  },
  {
   "cell_type": "markdown",
   "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",
    "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": 44,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n",
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n",
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n",
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n",
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n",
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n",
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n",
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n",
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n",
      "X X O \n",
      "O O X \n",
      "X O X \n",
      "0\n"
     ]
    }
   ],
   "source": [
    "for _ in range(10):\n",
    "    print(ttt.play_game(alphabeta_player, alphabeta_player))"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "A `random_player` should never win against an `alphabeta_player`. Let's test that."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "O X X \n",
      "O O X \n",
      "O X . \n",
      "-1\n",
      "O O O \n",
      ". X X \n",
      ". . X \n",
      "X . X \n",
      "-1\n",
      "X O X \n",
      "-1\n",
      "O X X \n",
      "O X X \n",
      "O O . \n",
      "-1\n",
      "O O X \n",
      "X O X \n",
      "X O . \n",
      "-1\n",
      "O O X \n",
      "X O . \n",
      "X O X \n",
      "-1\n",
      "O O X \n",
      "X X O \n",
      "O X X \n",
      "0\n"
   "source": [
    "for _ in range(10):\n",
    "    print(ttt.play_game(random_player, alphabeta_player))"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Canvas_TicTacToe(Canvas)\n",
    "\n",
    "This subclass is used to play TicTacToe game interactively in Jupyter notebooks. TicTacToe class is called while initializing this subclass.\n",
    "\n",
    "Let's have a match between `random_player` and `alphabeta_player`. Click on the board to call players to make a move."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from notebook import Canvas_TicTacToe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
       "<div>\n",
       "<canvas id=\"bot_play\" width=\"300\" height=\"350\" style=\"background:rgba(158, 167, 184, 0.2);\" onclick='click_callback(this, event, \"bot_play\")'></canvas>\n",
       "</div>\n",
       "\n",
       "<script> var bot_play_canvas_object = new Canvas(\"bot_play\");</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<script>\n",
       "bot_play_canvas_object.strokeWidth(5);\n",
       "bot_play_canvas_object.font(\"20px Arial\");\n",
       "bot_play_canvas_object.clear();\n",
       "bot_play_canvas_object.stroke(0, 0, 0);\n",
       "bot_play_canvas_object.line(15, 100, 285, 100);\n",
       "bot_play_canvas_object.line(15, 200, 285, 200);\n",
       "bot_play_canvas_object.line(100, 15, 100, 285);\n",
       "bot_play_canvas_object.line(200, 15, 200, 285);\n",
       "bot_play_canvas_object.fill_text(\"Player X's move(random)\", 15, 318);\n",
       "</script>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bot_play = Canvas_TicTacToe('bot_play', 'random', 'alphabeta')"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "Now, let's play a game ourselves against a `random_player`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
       "<div>\n",
       "<canvas id=\"rand_play\" width=\"300\" height=\"350\" style=\"background:rgba(158, 167, 184, 0.2);\" onclick='click_callback(this, event, \"rand_play\")'></canvas>\n",
       "</div>\n",
       "\n",
       "<script> var rand_play_canvas_object = new Canvas(\"rand_play\");</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<script>\n",
       "rand_play_canvas_object.strokeWidth(5);\n",
       "rand_play_canvas_object.font(\"20px Arial\");\n",
       "rand_play_canvas_object.clear();\n",
       "rand_play_canvas_object.stroke(0, 0, 0);\n",
       "rand_play_canvas_object.line(15, 100, 285, 100);\n",
       "rand_play_canvas_object.line(15, 200, 285, 200);\n",
       "rand_play_canvas_object.line(100, 15, 100, 285);\n",
       "rand_play_canvas_object.line(200, 15, 200, 285);\n",
       "rand_play_canvas_object.fill_text(\"Player X's move(human)\", 15, 318);\n",
       "</script>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "rand_play = Canvas_TicTacToe('rand_play', 'human', 'random')"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "Yay! We (usually) win. But we cannot win against an `alphabeta_player`, however hard we try."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
       "<div>\n",
       "<canvas id=\"ab_play\" width=\"300\" height=\"350\" style=\"background:rgba(158, 167, 184, 0.2);\" onclick='click_callback(this, event, \"ab_play\")'></canvas>\n",
       "</div>\n",
       "\n",
       "<script> var ab_play_canvas_object = new Canvas(\"ab_play\");</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<script>\n",
       "ab_play_canvas_object.strokeWidth(5);\n",
       "ab_play_canvas_object.font(\"20px Arial\");\n",
       "ab_play_canvas_object.clear();\n",
       "ab_play_canvas_object.stroke(0, 0, 0);\n",
       "ab_play_canvas_object.line(15, 100, 285, 100);\n",
       "ab_play_canvas_object.line(15, 200, 285, 200);\n",
       "ab_play_canvas_object.line(100, 15, 100, 285);\n",
       "ab_play_canvas_object.line(200, 15, 200, 285);\n",
       "ab_play_canvas_object.fill_text(\"Player X's move(human)\", 15, 318);\n",
       "</script>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ab_play = Canvas_TicTacToe('ab_play', 'human', 'alphabeta')"
  }
 ],
 "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+"