322 lines
10 KiB
Plaintext
322 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%load_ext blackcellmagic"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"from typing import Final\n",
|
|
"from scipy.ndimage import binary_dilation\n",
|
|
"from tqdm.auto import tqdm"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"ENEMY: Final[int] = -1\n",
|
|
"PLAYER: Final[int] = 1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"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]])"
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"DIRECTIONS: Final[np.ndarray] = np.array(\n",
|
|
" [[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",
|
|
"execution_count": 5,
|
|
"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, 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": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"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\n",
|
|
"get_new_games(1)[0]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"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 np.all(get_new_games(test_number_of_games)[:, 3:4, 3:4] != 0)\n",
|
|
"del test_number_of_games"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"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"
|
|
]
|
|
},
|
|
{
|
|
"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]]])"
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"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",
|
|
" return False\n",
|
|
" next_field = _array[tuple(rec_position.tolist())]\n",
|
|
" if next_field == 0:\n",
|
|
" return False\n",
|
|
" if next_field == -1:\n",
|
|
" return recursive_steps(_array, rec_direction, rec_position, step_one=False)\n",
|
|
" if next_field == 1:\n",
|
|
" return not step_one\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",
|
|
" for game in range(boards.shape[0]):\n",
|
|
" for idx in range(8):\n",
|
|
" for idy in range(8):\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",
|
|
" return _poss_turns\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,
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": "(array([2, 2, 2]), array([2, 2, 2]))"
|
|
},
|
|
"execution_count": 9,
|
|
"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",
|
|
"\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
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"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]])"
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"class InvalidTurn(ValueError):\n",
|
|
" pass\n",
|
|
"\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",
|
|
" rec_position = rec_move + rev_direction\n",
|
|
" if np.any((rec_position >= 8) | (rec_position < 0)):\n",
|
|
" return False\n",
|
|
" next_field = board[tuple(rec_position.tolist())]\n",
|
|
" if next_field == 0:\n",
|
|
" return False\n",
|
|
" if next_field == 1:\n",
|
|
" return not step_one\n",
|
|
" if next_field == -1:\n",
|
|
" if _do_directional_move(board, rec_position, rev_direction, step_one=False):\n",
|
|
" board[tuple(rec_position.tolist())] = 1\n",
|
|
" return True\n",
|
|
" return False\n",
|
|
"\n",
|
|
" def _do_move(_board: np.ndarray, move: np.ndarray) -> None:\n",
|
|
" if _board[tuple(move.tolist())] != 0:\n",
|
|
" raise InvalidTurn\n",
|
|
" action = False\n",
|
|
" for direction in DIRECTIONS:\n",
|
|
" if _do_directional_move(_board, move, direction):\n",
|
|
" action = True\n",
|
|
" if not action:\n",
|
|
" raise InvalidTurn()\n",
|
|
" _board[tuple(move.tolist())] = 1\n",
|
|
"\n",
|
|
" for game in range(boards.shape[0]):\n",
|
|
" _do_move(boards[game], moves[game])\n",
|
|
"boards = get_new_games(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,
|
|
"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]])"
|
|
},
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"np.array([[4,3]] * 10)"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"outputs": [],
|
|
"source": [],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"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.10.8"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|