Refactored

This commit is contained in:
2024-06-10 20:48:50 +02:00
parent 285549568d
commit 61d1ab0431
4 changed files with 47 additions and 44 deletions
+4 -38
View File
@@ -1,4 +1,5 @@
import numpy as np
from state import f_state_slices
class APlayer(object):
@@ -14,17 +15,11 @@ class APlayer(object):
def set_debug(self, with_debug):
pass
@staticmethod
def get_potential_moves(state: np.array) -> np.array:
st = state.reshape(state.size)
indices = [idx for idx, s in enumerate(st) if '-' in s]
return np.array(indices)
def move(self, state: np.array):
return state, False
def state_from_move(self, state: np.array, field):
state.reshape(state.size)[field] = self.mark
def to_state(self, state: np.array, move):
state.reshape(state.size)[move] = self.mark
return state
def reward(self, value):
@@ -33,39 +28,10 @@ class APlayer(object):
def new_game(self):
pass
@staticmethod
def f_state_slices():
nr = 3
nc = 3
result = []
# Create row finishing states
d1 = ()
d2_r = ()
for r in range(0, nr):
d1 += (r,)
for c in range(0, nc):
d2 = (c,) * nc
result.append((d1, d2))
# Create column finishing states
for c in range(0, nc):
d2 = (c,) * nc
result.append((d2, d1))
for c in range(0, nc):
d2_r += (nc - c - 1,)
# Create diagonal finishing states #1
result.append((d1, d1))
# Create diagonal finishing states #2
result.append((d1, d2_r))
return result
def has_won(self, state: np.array):
result = False
ref = [self.mark, self.mark, self.mark]
for f_state_slice in APlayer.f_state_slices():
for f_state_slice in f_state_slices():
if np.all(state[f_state_slice] == ref):
result = True
break