Local init

This commit is contained in:
Philipp Horstenkamp 2023-01-30 18:53:20 +01:00
commit f2801d76fe
Signed by: Philipp
GPG Key ID: DD53EAC36AFB61B4
3 changed files with 2336 additions and 0 deletions

210
main.ipynb Normal file
View File

@ -0,0 +1,210 @@
{
"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": [],
"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": 6,
"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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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": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_new_games(10)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"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": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8.58 ms ± 214 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
"82.7 ms ± 2.17 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": 16,
"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",
"\n",
"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[rec_position[0], rec_position[1]]\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]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [
{
"data": {
"text/plain": "(array([2, 2, 2]), array([2, 2, 2]))"
},
"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": null,
"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
}

2103
poetry.lock generated Normal file

File diff suppressed because it is too large Load Diff

23
pyproject.toml Normal file
View File

@ -0,0 +1,23 @@
[tool.poetry]
name = "reversi"
version = "0.1.0"
description = ""
authors = ["Philipp Horstenkamp <philipp@horstenkamp.de>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "3.10.*"
jupyter = "^1.0.0"
numpy = "^1.24.1"
pytest = "^7.2.1"
ipytest = "^0.13.0"
scipy = "^1.10.0"
tqdm = "^4.64.1"
[tool.poetry.group.build.dependencies]
blackcellmagic = "^0.0.3"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"