Added functionality to plot a set of otello boards.

This commit is contained in:
Philipp Horstenkamp 2023-02-01 00:13:35 +01:00
parent 7cacd0ef64
commit 4928463d80
Signed by: Philipp
GPG Key ID: DD53EAC36AFB61B4

View File

@ -18,7 +18,8 @@
"import numpy as np\n",
"from typing import Final\n",
"from scipy.ndimage import binary_dilation\n",
"from tqdm.auto import tqdm"
"from tqdm.auto import tqdm\n",
"import matplotlib.pyplot as plt"
]
},
{
@ -28,16 +29,27 @@
"outputs": [],
"source": [
"ENEMY: Final[int] = -1\n",
"PLAYER: Final[int] = 1"
"PLAYER: Final[int] = 1\n",
"BOARD_SIZE: Final[int] = 8"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "array([[-1, -1],\n [-1, 0],\n [-1, 1],\n [ 0, -1],\n [ 0, 1],\n [ 1, -1],\n [ 1, 0],\n [ 1, 1]])"
"text/plain": [
"array([[-1, -1],\n",
" [-1, 0],\n",
" [-1, 1],\n",
" [ 0, -1],\n",
" [ 0, 1],\n",
" [ 1, -1],\n",
" [ 1, 0],\n",
" [ 1, 1]])"
]
},
"execution_count": 4,
"metadata": {},
@ -49,10 +61,7 @@
" [[i, j] for i in range(-1, 2) for j in range(-1, 2) if j != 0 or i != 0], dtype=int\n",
")\n",
"DIRECTIONS"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
@ -61,7 +70,16 @@
"outputs": [
{
"data": {
"text/plain": "array([[ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, -1, 1, 0, 0, 0],\n [ 0, 0, 0, 1, -1, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0]])"
"text/plain": [
"array([[ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, -1, 1, 0, 0, 0],\n",
" [ 0, 0, 0, 1, -1, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0]])"
]
},
"execution_count": 5,
"metadata": {},
@ -69,10 +87,12 @@
}
],
"source": [
"def get_new_games(number_of_games:int):\n",
" empty = np.zeros([number_of_games, 8,8], dtype=int)\n",
" empty[:, 3:5, 3:5] = np.array([[-1,1], [1, -1]])\n",
"def get_new_games(number_of_games: int):\n",
" empty = np.zeros([number_of_games, BOARD_SIZE, BOARD_SIZE], dtype=int)\n",
" empty[:, 3:5, 3:5] = np.array([[-1, 1], [1, -1]])\n",
" return empty\n",
"\n",
"\n",
"get_new_games(1)[0]"
]
},
@ -83,9 +103,29 @@
"outputs": [],
"source": [
"test_number_of_games = 3\n",
"assert get_new_games(test_number_of_games).shape == (test_number_of_games, 8, 8 )\n",
"np.testing.assert_equal( get_new_games(test_number_of_games).sum(axis=1), np.zeros([test_number_of_games, 8, ]))\n",
"np.testing.assert_equal( get_new_games(test_number_of_games).sum(axis=2), np.zeros([test_number_of_games, 8, ]))\n",
"assert get_new_games(test_number_of_games).shape == (\n",
" test_number_of_games,\n",
" BOARD_SIZE,\n",
" BOARD_SIZE,\n",
")\n",
"np.testing.assert_equal(\n",
" get_new_games(test_number_of_games).sum(axis=1),\n",
" np.zeros(\n",
" [\n",
" test_number_of_games,\n",
" 8,\n",
" ]\n",
" ),\n",
")\n",
"np.testing.assert_equal(\n",
" get_new_games(test_number_of_games).sum(axis=2),\n",
" np.zeros(\n",
" [\n",
" test_number_of_games,\n",
" 8,\n",
" ]\n",
" ),\n",
")\n",
"assert np.all(get_new_games(test_number_of_games)[:, 3:4, 3:4] != 0)\n",
"del test_number_of_games"
]
@ -96,29 +136,84 @@
"metadata": {},
"outputs": [],
"source": [
"def get_new_games(number_of_games:int):\n",
" empty = np.zeros([number_of_games, 8,8], dtype=int)\n",
" empty[:, 3:5, 3:5] = np.array([[-1,1], [1, -1]])\n",
" return empty"
"def plot_othello_board(board, ax=None):\n",
" size = 3\n",
" plot_all = False\n",
" if ax is None:\n",
" plot_all = True\n",
" fig, ax = plt.subplots(figsize=(size, size))\n",
"\n",
" ax.set_facecolor(\"green\")\n",
" for i in range(BOARD_SIZE):\n",
" for j in range(BOARD_SIZE):\n",
" if board[i, j] == -1:\n",
" color = \"white\"\n",
" elif board[i, j] == 1:\n",
" color = \"black\"\n",
" else:\n",
" continue\n",
" ax.scatter(j, i, s=300 if plot_all else 150, c=color)\n",
" for i in range(-1, 8):\n",
" ax.axhline(i + 0.5, color=\"black\", lw=2)\n",
" ax.axvline(i + 0.5, color=\"black\", lw=2)\n",
" ax.set_xlim(-0.5, 7.5)\n",
" ax.set_ylim(7.5, -0.5)\n",
" ax.set_xticks(np.arange(8))\n",
" ax.set_xticklabels(list(\"ABCDEFGH\"))\n",
" ax.set_yticks(np.arange(8))\n",
" ax.set_yticklabels(list(\"12345678\"))\n",
" if plot_all:\n",
" plt.tight_layout()\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def plot_othello_boards(boards: np.ndarray) -> None:\n",
" assert boards.shape[0] < 70\n",
" plots_per_row = 4\n",
" rows = int(np.ceil(boards.shape[0] / plots_per_row))\n",
" fig, axs = plt.subplots(rows, plots_per_row, figsize=(12, 3 * rows))\n",
" for game_index, ax in enumerate(axs.flatten()):\n",
" if game_index >= boards.shape[0]:\n",
" fig.delaxes(ax)\n",
" else:\n",
" plot_othello_board(boards[game_index], ax)\n",
" plt.tight_layout()\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"17.2 ms ± 3.53 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"169 ms ± 33.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
"8.49 ms ± 143 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"80.9 ms ± 537 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
},
{
"data": {
"text/plain": "array([[[False, False, False, False, False, False, False, False],\n [False, False, False, False, False, False, False, False],\n [False, False, False, True, False, False, False, False],\n [False, False, True, False, False, False, False, False],\n [False, False, False, False, False, True, False, False],\n [False, False, False, False, True, False, False, False],\n [False, False, False, False, False, False, False, False],\n [False, False, False, False, False, False, False, False]]])"
"text/plain": [
"array([[[False, False, False, False, False, False, False, False],\n",
" [False, False, False, False, False, False, False, False],\n",
" [False, False, False, True, False, False, False, False],\n",
" [False, False, True, False, False, False, False, False],\n",
" [False, False, False, False, False, True, False, False],\n",
" [False, False, False, False, True, False, False, False],\n",
" [False, False, False, False, False, False, False, False],\n",
" [False, False, False, False, False, False, False, False]]])"
]
},
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@ -126,7 +221,7 @@
"source": [
"def recursive_steps(_array, rec_direction, rec_position, step_one=True):\n",
" rec_position = rec_position + rec_direction\n",
" if np.any((rec_position >= 8) | ( rec_position < 0)):\n",
" if np.any((rec_position >= BOARD_SIZE) | (rec_position < 0)):\n",
" return False\n",
" next_field = _array[tuple(rec_position.tolist())]\n",
" if next_field == 0:\n",
@ -136,78 +231,100 @@
" if next_field == 1:\n",
" return not step_one\n",
"\n",
"\n",
"def get_possible_turns(boards: np.ndarray) -> np.ndarray:\n",
" _poss_turns = (boards == 0) & binary_dilation(boards == -1, np.array([[[1,1,1],[1,0,1],[1,1,1]]]))\n",
" _poss_turns = (boards == 0) & binary_dilation(\n",
" boards == -1, np.array([[[1, 1, 1], [1, 0, 1], [1, 1, 1]]])\n",
" )\n",
" for game in range(boards.shape[0]):\n",
" for idx in range(8):\n",
" for idy in range(8):\n",
" for idx in range(BOARD_SIZE):\n",
" for idy in range(BOARD_SIZE):\n",
"\n",
" position = idx, idy\n",
" if _poss_turns[game, idx, idy]:\n",
" _poss_turns[game, idx, idy] = any(recursive_steps(boards[game, :, :], direction, position) for direction in DIRECTIONS)\n",
" _poss_turns[game, idx, idy] = any(\n",
" recursive_steps(boards[game, :, :], direction, position)\n",
" for direction in DIRECTIONS\n",
" )\n",
" return _poss_turns\n",
"\n",
"\n",
"%timeit get_possible_turns(get_new_games(10))\n",
"%timeit get_possible_turns(get_new_games(100))\n",
"get_possible_turns(get_new_games(3))[:1]"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "(array([2, 2, 2]), array([2, 2, 2]))"
"text/plain": [
"(array([2, 2, 2]), array([2, 2, 2]))"
]
},
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def evaluate_boards(array: np.ndarray):\n",
" return np.sum(array == 1, axis=(1,2)), np.sum(array == -1, axis=(1,2))\n",
"evaluate_boards(get_new_games(3))"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [],
"source": [
"def move_possible(board:np.ndarray, move: np.ndarray) -> bool:\n",
" if np.all(move == -1):\n",
" return np.all(get_possible_turns(board))\n",
" return any(recursive_steps(board[:, :], direction, move) for direction in DIRECTIONS)\n",
" return np.sum(array == 1, axis=(1, 2)), np.sum(array == -1, axis=(1, 2))\n",
"\n",
"def moves_possible(boards:np.ndarray, moves: np.ndarray) -> np.ndarray:\n",
" arr_moves_possible = np.zeros(boards.shape[0], dtype=bool)\n",
" for game in range(boards.shape[0]):\n",
" if np.all(moves[game] == -1):\n",
" arr_moves_possible[game, :, :] = np.all(get_possible_turns(boards[game, : , :]))\n",
" arr_moves_possible[game, :, :] = any(recursive_steps(boards[game, :, :], direction, moves[game]) for direction in DIRECTIONS)\n",
" return arr_moves_possible"
],
"metadata": {
"collapsed": false
}
"\n",
"evaluate_boards(get_new_games(3))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def move_possible(board: np.ndarray, move: np.ndarray) -> bool:\n",
" if np.all(move == -1):\n",
" return np.all(get_possible_turns(board))\n",
" return any(\n",
" recursive_steps(board[:, :], direction, move) for direction in DIRECTIONS\n",
" )\n",
"\n",
"\n",
"def moves_possible(boards: np.ndarray, moves: np.ndarray) -> np.ndarray:\n",
" arr_moves_possible = np.zeros(boards.shape[0], dtype=bool)\n",
" for game in range(boards.shape[0]):\n",
" if np.all(moves[game] == -1):\n",
" arr_moves_possible[game, :, :] = np.all(\n",
" get_possible_turns(boards[game, :, :])\n",
" )\n",
" arr_moves_possible[game, :, :] = any(\n",
" recursive_steps(boards[game, :, :], direction, moves[game])\n",
" for direction in DIRECTIONS\n",
" )\n",
" return arr_moves_possible"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "array([[ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, -1, 0, 0, 0, 0],\n [ 0, 0, 0, -1, -1, 0, 0, 0],\n [ 0, 0, 0, -1, 1, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0]])"
"text/plain": [
"array([[ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, -1, 0, 0, 0, 0],\n",
" [ 0, 0, 0, -1, -1, 0, 0, 0],\n",
" [ 0, 0, 0, -1, 1, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0]])"
]
},
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@ -218,8 +335,9 @@
"\n",
"\n",
"def to_moves(boards: np.ndarray, moves: np.ndarray) -> np.ndarray:\n",
"\n",
" def _do_directional_move(board: np.ndarray, rec_move: np.ndarray, rev_direction, step_one=True) -> bool:\n",
" def _do_directional_move(\n",
" board: np.ndarray, rec_move: np.ndarray, rev_direction, step_one=True\n",
" ) -> bool:\n",
" rec_position = rec_move + rev_direction\n",
" if np.any((rec_position >= 8) | (rec_position < 0)):\n",
" return False\n",
@ -247,54 +365,385 @@
"\n",
" for game in range(boards.shape[0]):\n",
" _do_move(boards[game], moves[game])\n",
"\n",
"\n",
"boards = get_new_games(10)\n",
"to_moves(boards, np.array([[2,3]] * 10))\n",
"to_moves(boards, np.array([[2, 3]] * 10))\n",
"boards = boards * -1\n",
"boards[0]"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 12,
"outputs": [],
"source": [
"to_moves(get_new_games(10), np.array([[2,3]] * 10))"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"to_moves(get_new_games(10), np.array([[2, 3]] * 10))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "array([[4, 3],\n [4, 3],\n [4, 3],\n [4, 3],\n [4, 3],\n [4, 3],\n [4, 3],\n [4, 3],\n [4, 3],\n [4, 3]])"
"text/plain": [
"array([[4, 3],\n",
" [4, 3],\n",
" [4, 3],\n",
" [4, 3],\n",
" [4, 3],\n",
" [4, 3],\n",
" [4, 3],\n",
" [4, 3],\n",
" [4, 3],\n",
" [4, 3]])"
]
},
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array([[4,3]] * 10)"
],
"metadata": {
"collapsed": false
}
"np.array([[4, 3]] * 10)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def create_test_game():\n",
" test_array = []\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 1, 2, 0, 0, 0],\n",
" [0, 0, 0, 2, 1, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 2, 0, 0, 0],\n",
" [0, 0, 0, 2, 1, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 2, 0, 0, 0],\n",
" [0, 0, 1, 1, 1, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 2, 0, 0, 0],\n",
" [0, 0, 2, 1, 1, 0, 0, 0],\n",
" [0, 2, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 1, 2, 0, 0, 0],\n",
" [0, 0, 2, 1, 1, 0, 0, 0],\n",
" [0, 2, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 0, 0, 0],\n",
" [0, 0, 0, 1, 2, 0, 0, 0],\n",
" [0, 0, 2, 1, 1, 0, 0, 0],\n",
" [0, 2, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 0, 0, 0],\n",
" [0, 0, 0, 1, 2, 0, 0, 0],\n",
" [0, 0, 2, 2, 2, 2, 0, 0],\n",
" [0, 2, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 0, 0, 0],\n",
" [0, 0, 0, 1, 1, 1, 0, 0],\n",
" [0, 0, 2, 2, 2, 2, 0, 0],\n",
" [0, 2, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
" [0, 0, 0, 1, 2, 2, 0, 0],\n",
" [0, 0, 2, 2, 2, 2, 0, 0],\n",
" [0, 2, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
" [0, 0, 0, 1, 2, 2, 0, 0],\n",
" [0, 0, 2, 2, 1, 2, 0, 0],\n",
" [0, 2, 0, 0, 0, 1, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
" [0, 0, 0, 1, 2, 2, 0, 0],\n",
" [0, 0, 2, 2, 1, 2, 0, 0],\n",
" [0, 2, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
" [0, 0, 0, 1, 2, 2, 0, 0],\n",
" [0, 1, 1, 1, 1, 2, 0, 0],\n",
" [0, 2, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
" [0, 0, 0, 1, 2, 2, 0, 0],\n",
" [2, 2, 2, 2, 2, 2, 0, 0],\n",
" [0, 2, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
" [0, 0, 0, 1, 1, 1, 1, 0],\n",
" [2, 2, 2, 2, 2, 2, 0, 0],\n",
" [0, 2, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
" [0, 0, 0, 1, 1, 1, 1, 0],\n",
" [2, 2, 2, 1, 2, 2, 0, 0],\n",
" [0, 2, 0, 1, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 2, 2, 2, 0, 0],\n",
" [0, 0, 0, 2, 2, 1, 1, 0],\n",
" [2, 2, 2, 1, 2, 2, 0, 0],\n",
" [0, 2, 0, 1, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 1, 0, 0],\n",
" [0, 0, 0, 2, 2, 1, 0, 0],\n",
" [0, 0, 0, 2, 2, 1, 1, 0],\n",
" [2, 2, 2, 1, 2, 2, 0, 0],\n",
" [0, 2, 0, 1, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 1, 0, 0],\n",
" [0, 0, 0, 2, 2, 2, 2, 0],\n",
" [0, 0, 0, 2, 2, 2, 1, 0],\n",
" [2, 2, 2, 1, 2, 2, 0, 0],\n",
" [0, 2, 0, 1, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 1, 0, 0],\n",
" [0, 0, 0, 2, 1, 2, 2, 0],\n",
" [0, 0, 0, 2, 2, 1, 1, 0],\n",
" [2, 2, 2, 1, 1, 1, 1, 0],\n",
" [0, 2, 0, 1, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 1, 0, 0],\n",
" [0, 0, 0, 2, 1, 2, 2, 0],\n",
" [0, 0, 0, 2, 2, 1, 2, 0],\n",
" [2, 2, 2, 2, 2, 2, 2, 2],\n",
" [0, 2, 0, 1, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array.append(\n",
" np.array(\n",
" [\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 2, 1, 0, 1, 0, 0],\n",
" [0, 0, 0, 2, 1, 2, 2, 0],\n",
" [0, 0, 0, 2, 1, 1, 2, 0],\n",
" [2, 2, 2, 2, 1, 2, 2, 2],\n",
" [0, 2, 0, 1, 1, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
" ]\n",
" )\n",
" )\n",
" test_array = np.array(test_array)\n",
" test_array[test_array == 2] = -1\n",
" assert np.all(np.diff(np.count_nonzero(create_test_game(), axis=(1, 2))) == 1)\n",
" return test_array\n",
"\n",
"\n",
"plot_othello_boards(create_test_game()[-3:])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.diff(create_test_game(), axis=0).shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {