Newer
Older
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"# 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."
SnShine
a validé
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
SnShine
a validé
"from games import (GameState, Game, Fig52Game, TicTacToe, query_player, random_player, \n",
" alphabeta_player, minimax_decision, alphabeta_full_search,\n",
SnShine
a validé
" alphabeta_search, Canvas_TicTacToe)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"## `GameState` namedtuple\n",
SnShine
a validé
" `GameState` is a [namedtuple](https://docs.python.org/3.5/library/collections.html#collections.namedtuple) which represents the current state of a game. Let it be Tic-Tac-Toe or any other game."
SnShine
a validé
"metadata": {
"collapsed": true
},
SnShine
a validé
"## `Game` class\n",
" \n",
"Let's have a look at the class `Game` in our module. We see that it has functions, namely `actions`, `result`, `utility`, `terminal_test`, `to_move` and `display`. \n",
"\n",
"We see that these functions have not actually been implemented. This class is actually just a template class; we are supposed to create the class for our game, `TicTacToe` by inheriting this `Game` class and implement all the methods mentioned in `Game`. Do not close the popup so that you can follow along the description of code below."
{
"cell_type": "code",
SnShine
a validé
"execution_count": 2,
"metadata": {
"collapsed": false
},
SnShine
a validé
"%psource Game"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
" 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",
SnShine
a validé
"* `actions(self, state)` : Given a game state, this method generates all the legal actions possible from this state, as a list or a generator. Returning a generator rather than a list has the advantage that it saves space and you can still operate on it as a list.\n",
"\n",
"\n",
"* `result(self, state, move)` : Given a game state and a move, this method returns the game state that you get by making that move on this game state.\n",
SnShine
a validé
"\n",
"\n",
"* `utility(self, state, player)` : Given a terminal game state and a player, this method returns the utility for that player in the given terminal game state. While implementing this method assume that the game state is a terminal game state. The logic in this module is such that this method will be called only on terminal game states.\n",
SnShine
a validé
"\n",
"\n",
"* `terminal_test(self, state)` : Given a game state, this method should return `True` if this game state is a terminal state, and `False` otherwise.\n",
SnShine
a validé
"\n",
"\n",
"* `to_move(self, state)` : Given a game state, this method returns the player who is to play next. This information is typically stored in the game state, so all this method does is extract this information and return it.\n",
"\n",
"\n",
"* `display(self, state)` : This method prints/displays current state of the game."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"## `TicTacToe` class\n",
SnShine
a validé
" Take a look at the class `TicTacToe`. All the methods mentioned in the class `Game` have been implemented here."
SnShine
a validé
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
SnShine
a validé
"%psource TicTacToe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
" 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",
"metadata": {},
"source": [
SnShine
a validé
"## GameState in TicTacToe game\n",
"\n",
" Now, before we start implementing our `TicTacToe` game, we need to decide how we will be representing our game state. Typically, a game state will give you all the current information about the game at any point in time. When you are given a game state, you should be able to tell whose turn it is next, how the game will look like on a real-life board (if it has one) etc. A game state need not include the history of the game. If you can play the game further given a game state, you game state representation is acceptable. While we might like to include all kinds of information in our game state, we wouldn't want to put too much information into it. Modifying this game state to generate a new one would be a real pain then.\n",
SnShine
a validé
" 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",
SnShine
a validé
" Think about what we've done here. We have reduced extra computation by storing additional information in a game state. Now, this information might not be absolutely essential to tell us about the state of the game, but it does save us additional computation time. We'll do more of this later on. \n",
" \n",
" The `TicTacToe` game defines its game state as:\n",
SnShine
a validé
" `GameState = namedtuple('GameState', 'to_move, utility, board, moves')`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"The game state is called, quite appropriately, `GameState`, and it has 4 variables, namely, `to_move`, `utility`, `board` and `moves`. \n",
" \n",
" I'll describe these variables in some more detail:\n",
" \n",
"* `to_move` : It represents whose turn it is to move next. This will be a string of a single character, either 'X' or 'O'.\n",
"\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",
"\n",
"* `board` : A dict that stores all the positions of X's and O's on the board\n",
"\n",
"\n",
"* `moves` : It stores the list of legal moves possible from the current position. Note here, that storing the moves as a list, as it is done here, increases the space complexity of Minimax Search from `O(m)` to `O(bm)`. Refer to Sec. 5.2.1 of the book."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"## Representing a move in TicTacToe game\n",
SnShine
a validé
" 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",
SnShine
a validé
" 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",
"metadata": {},
"source": [
SnShine
a validé
"## Players to play games\n",
"\n",
" So, we have finished implementation of the `TicTacToe` class. What this class does is that, it just defines the rules of the game. We need more to create an AI that can actually play the game. This is where `random_player` and `alphabeta_player` come in. \n",
"\n",
"### query_player\n",
" The `query_player` function allows you, a human opponent, to play the game. This function requires a `display` method to be implemented in your game class, so that successive game states can be displayed on the terminal, making it easier for you to visualize the game and play accordingly. \n",
"\n",
"### random_player\n",
" The `random_player` is a function that plays random moves in the game. That's it. There isn't much more to this guy. \n",
"\n",
"### alphabeta_player\n",
" The `alphabeta_player`, on the other hand, calls the `alphabeta_full_search` function, which returns the best move in the current game state. Thus, the `alphabeta_player` always plays the best move given a game state, assuming that the game tree is small enough to search entirely.\n",
SnShine
a validé
"### 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": [
SnShine
a validé
"## Let's play some games\n",
"### Game52"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/fig_5_2.png\" width=\"75%\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"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:"
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"game52 = Fig52Game()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"First we try out our `random_player(game, state)`. Given a game state it will give us a random move every time:"
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a1\n",
SnShine
a validé
]
}
],
SnShine
a validé
"print(random_player(game52, 'A'))\n",
"print(random_player(game52, 'A'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `alphabeta_player(game, state)` will always give us the best move possible, for the relevant player (MAX or MIN):"
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a1\n",
"b1\n",
"c1\n"
]
}
],
SnShine
a validé
"print( alphabeta_player(game52, 'A') )\n",
"print( alphabeta_player(game52, 'B') )\n",
"print( alphabeta_player(game52, 'C') )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"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 w.r.t time because it prunes the search tree and hence, explores lesser number of states."
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"data": {
"text/plain": [
"'a1'"
]
},
SnShine
a validé
"metadata": {},
"output_type": "execute_result"
}
],
SnShine
a validé
"minimax_decision('A', game52)"
]
},
{
"cell_type": "code",
SnShine
a validé
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'a1'"
]
},
SnShine
a validé
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"alphabeta_full_search('A', game52)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"Demonstrating the play_game function on the game52:"
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B1\n"
]
},
{
"data": {
"text/plain": [
"3"
]
},
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"game52.play_game(alphabeta_player, alphabeta_player)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B3\n"
]
},
{
"data": {
"text/plain": [
"8"
]
},
SnShine
a validé
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"game52.play_game(alphabeta_player, random_player)"
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"current state:\n",
"A\n",
"available moves: ['a2', 'a1', 'a3']\n",
"\n",
"Your move? a3\n",
"D3\n"
SnShine
a validé
]
},
{
"data": {
"text/plain": [
SnShine
a validé
]
},
SnShine
a validé
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"game52.play_game(query_player, alphabeta_player)"
SnShine
a validé
]
},
{
"cell_type": "code",
SnShine
a validé
"metadata": {
SnShine
a validé
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"current state:\n",
"B\n",
"available moves: ['b1', 'b3', 'b2']\n",
"\n",
"Your move? b3\n",
"B3\n"
]
},
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"game52.play_game(alphabeta_player, query_player)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that if you are the first player then alphabeta_player plays as MIN, and if you are the second player then alphabeta_player plays as MAX. This happens because that's the way the game is defined in the class Fig52Game. Having a look at the code of this class should make it clear."
SnShine
a validé
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### TicTacToe game\n",
"Now let's play `TicTacToe`. First we initialize the game by creating an instance of the subclass TicTacToe inherited from the class Game:"
]
},
{
"cell_type": "code",
},
"outputs": [],
"source": [
"ttt = TicTacToe()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can print a state using the display method:"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
". . . \n",
". . . \n",
". . . \n"
]
}
],
"source": [
"ttt.display(ttt.initial)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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",
"metadata": {
"collapsed": false
},
"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",
"metadata": {},
"source": [
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X O X \n",
"O . O \n",
"X . . \n"
]
}
],
"source": [
"ttt.display(my_state)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"The `random_player` will behave how he is supposed to i.e. *pseudo-randomly*:"
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"data": {
"text/plain": [
SnShine
a validé
]
},
SnShine
a validé
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random_player(ttt, my_state)"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"data": {
"text/plain": [
"(3, 2)"
]
},
SnShine
a validé
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random_player(ttt, my_state)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But the `alphabeta_player` will always give the best move, as expected:"
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"data": {
"text/plain": [
"(2, 2)"
]
},
SnShine
a validé
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"alphabeta_player(ttt, my_state)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's make 2 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",
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"O O O \n",
"X X . \n",
". X . \n"
SnShine
a validé
]
},
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
SnShine
a validé
}
],
"ttt.play_game(random_player, alphabeta_player)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The output is (usually) -1, because `random_player` loses to `alphabeta_player`. Sometimes, however, `random_player` manages to draw with `alphabeta_player`.\n",
" 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:"
"metadata": {
"collapsed": false
},
SnShine
a validé
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
"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"
]
}
],
" print(ttt.play_game(alphabeta_player, alphabeta_player))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"A `random_player` should never win against an `alphabeta_player`. Let's test that."
"metadata": {
"collapsed": false
},
SnShine
a validé
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
SnShine
a validé
"X O X \n",
SnShine
a validé
"-1\n",
SnShine
a validé
"X O . \n",
"-1\n",
SnShine
a validé
"X O X \n",
SnShine
a validé
"X O . \n",
SnShine
a validé
"-1\n",
SnShine
a validé
"O X X \n",
"-1\n",
SnShine
a validé
"X X O \n",
". X X \n",
"-1\n",
"O O O \n",
". . X \n",
". X X \n",
SnShine
a validé
"-1\n",
"O O O \n",
SnShine
a validé
". X . \n",
"-1\n",
"X O X \n",
". O X \n",
". O . \n",
"-1\n",
"O X O \n",
"X O X \n",
"O X . \n",
SnShine
a validé
"-1\n"
]
}
],
" print(ttt.play_game(random_player, alphabeta_player))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"## 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 match between `random_player` and `alphabeta_player`. Click on the board to call players to make a move."
"metadata": {
"collapsed": false
},
SnShine
a validé
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
"outputs": [
{
"data": {
"text/html": [
"\n",
"<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
"<div>\n",
"<canvas id=\"bot_play\" width=\"300\" height=\"300\" 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.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 1's move(random)\", 30, 30);\n",
"</script>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
SnShine
a validé
"bot_play = Canvas_TicTacToe('bot_play', 'random', 'alphabeta')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
SnShine
a validé
"Now, let's play a game ourselves against a `random_player`:"
"metadata": {
"collapsed": false
},
SnShine
a validé
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
"outputs": [
{
"data": {
"text/html": [
"\n",
"<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
"<div>\n",
"<canvas id=\"rand_play\" width=\"300\" height=\"300\" 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.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 1's move(human)\", 30, 30);\n",
"</script>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
SnShine
a validé
"rand_play = Canvas_TicTacToe('rand_play', 'human', 'random')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yay! We (usually) win. But we cannot win against an `alphabeta_player`, however hard we try."
"metadata": {
"collapsed": false
},
SnShine
a validé
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
"outputs": [
{
"data": {
"text/html": [
"\n",
"<script type=\"text/javascript\" src=\"./js/canvas.js\"></script>\n",
"<div>\n",
"<canvas id=\"ab_play\" width=\"300\" height=\"300\" 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.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 1's move(human)\", 30, 30);\n",
"</script>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
SnShine
a validé
"ab_play = Canvas_TicTacToe('ab_play', 'human', 'alphabeta')"