Files
tic_tac_toe/a_player.py
T
jens c1f64943c3 - simplified debug flags
- added params class
- cleaned up
2024-06-09 18:43:42 +02:00

64 lines
1.4 KiB
Python

import numpy as np
class APlayer(object):
def __init__(self, mark):
self.mark = mark
self.other_mark = 'O'
if 'O' in mark:
self.other_mark = 'X'
self.state = None
self.state_last = None
def set_debug(self, with_debug):
pass
def move(self, state: np.array):
return state, False
def reward(self, value):
pass
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():
if np.all(state[f_state_slice] == ref):
result = True
break
return result