Created
June 4, 2017 11:45
-
-
Save misza222/1806de36c9b82eb487bcc5a9ab19dbad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from isolation import Board\n", | |
"from sample_players import *\n", | |
"from game_agent import *" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" 0 1 2 3 4\n", | |
"\r", | |
"0 | | | | | | \n", | |
"\r", | |
"1 | | | | | | \n", | |
"\r", | |
"2 | | | | 1 | | \n", | |
"\r", | |
"3 | | | | | | \n", | |
"\r", | |
"4 | | | | | | \n", | |
"\r\n", | |
"[(4, 4), (0, 4), (1, 1), (4, 2), (3, 1), (0, 2)]\n", | |
"\n", | |
"Old state:\n", | |
" 0 1 2 3 4\n", | |
"\r", | |
"0 | | | | | | \n", | |
"\r", | |
"1 | | | | | | \n", | |
"\r", | |
"2 | | | | 1 | | \n", | |
"\r", | |
"3 | | | | | | \n", | |
"\r", | |
"4 | | | | | | \n", | |
"\r\n", | |
"\n", | |
"New state:\n", | |
" 0 1 2 3 4\n", | |
"\r", | |
"0 | | | | | | \n", | |
"\r", | |
"1 | | 1 | | | | \n", | |
"\r", | |
"2 | | | | - | | \n", | |
"\r", | |
"3 | | | | | | \n", | |
"\r", | |
"4 | | | | | | \n", | |
"\r\n", | |
"\n", | |
"Winner: <game_agent.MinimaxPlayer object at 0x1063fc748>\n", | |
"Outcome: illegal move\n", | |
" 0 1 2 3 4\n", | |
"\r", | |
"0 | | | - | 2 | | \n", | |
"\r", | |
"1 | - | | - | - | - | \n", | |
"\r", | |
"2 | - | - | - | - | - | \n", | |
"\r", | |
"3 | | - | - | - | 1 | \n", | |
"\r", | |
"4 | | | - | - | | \n", | |
"\r\n", | |
"Move history:\n", | |
"[[4, 2], [2, 4], [2, 1], [4, 3], [3, 3], [3, 1], [1, 2], [1, 0], [2, 0], [0, 2], [3, 2], [1, 4], [1, 3], [2, 2], [3, 4], [0, 3]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# create an isolation board (by default 7x7)\n", | |
"player1 = GreedyPlayer()\n", | |
"player2 = MinimaxPlayer()\n", | |
"game = Board(player1, player2, 5, 5)\n", | |
"\n", | |
"# place player 1 on the board at row 2, column 3, then place player 2 on\n", | |
"# the board at row 0, column 5; display the resulting board state. Note\n", | |
"# that the .apply_move() method changes the calling object in-place.\n", | |
"game.apply_move((2, 3))\n", | |
"game.apply_move((0, 5))\n", | |
"print(game.to_string())\n", | |
"\n", | |
"# players take turns moving on the board, so player1 should be next to move\n", | |
"assert(player1 == game.active_player)\n", | |
"\n", | |
"# get a list of the legal moves available to the active player\n", | |
"print(game.get_legal_moves())\n", | |
"# player1.minimax_score(game, 1)\n", | |
"\n", | |
"# get a successor of the current state by making a copy of the board and\n", | |
"# applying a move. Notice that this does NOT change the calling object\n", | |
"# (unlike .apply_move()).\n", | |
"new_game = game.forecast_move((1, 1))\n", | |
"assert(new_game.to_string() != game.to_string())\n", | |
"print(\"\\nOld state:\\n{}\".format(game.to_string()))\n", | |
"print(\"\\nNew state:\\n{}\".format(new_game.to_string()))\n", | |
"\n", | |
"# play the remainder of the game automatically -- outcome can be \"illegal\n", | |
"# move\", \"timeout\", or \"forfeit\"\n", | |
"winner, history, outcome = game.play()\n", | |
"print(\"\\nWinner: {}\\nOutcome: {}\".format(winner, outcome))\n", | |
"print(game.to_string())\n", | |
"print(\"Move history:\\n{!s}\".format(history))\n", | |
"\n", | |
"\n" | |
] | |
} | |
], | |
"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.6.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment