-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
96 lines (75 loc) · 2.7 KB
/
board.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# by protago90
from abc import abstractmethod
from typing import List, Optional, Sequence
import math
import random
class BoardAPI():
EMPTY = '⠀'
SIGNS = ('X', 'O', EMPTY)
@abstractmethod
def get_state(self) -> List[List[str]]:
pass
@abstractmethod
def process_move(self, sign: str, pos: int) -> None:
pass
@abstractmethod
def undo_move(self) -> None:
pass
@abstractmethod
def get_open_poss(self) -> List[int]:
pass
@abstractmethod
def get_debuts_pos(self) -> int:
pass
@abstractmethod
def get_winner(self) -> Optional[str]:
pass
@abstractmethod
def is_open(self) -> bool:
pass
class XOBoard(BoardAPI):
def __init__(self, empty: Optional[str]=None) -> None:
self._empty: str = empty if empty else self.__class__.EMPTY
self._board: List[str] = [self._empty for _ in range(9)]
self._winner: Optional[str] = None
self._record: List[int] = []
def get_state(self, desc: bool=True) -> List[List[str]]:
rows = [self._board[i*3 : i*3+3] for i in range(3)]
return rows[::-1] if desc else rows
@staticmethod
def _is_win_line(vec: Sequence[str]) -> bool:
return len(set(vec)) == 1
def _is_win_state(self, pos: int) -> bool:
irow = math.floor(pos/3)
if self._is_win_line(self.get_state(desc=False)[irow]):
return True
icol = pos % 3
if self._is_win_line([self._board[i+icol] for i in (0, 3, 6)]):
return True
for diag in ((0, 4, 8), (2, 4, 6)):
if pos in diag:
if self._is_win_line([self._board[i] for i in diag]):
return True
return False
def process_move(self, sign: str, pos: int) -> None:
self._board[pos] = sign
self._record.append(pos)
if self._is_win_state(pos): self._winner = sign
def undo_move(self) -> None:
self._board[self._record.pop()] = self._empty
self._winner = None
def get_open_poss(self) -> List[int]:
return [pos for pos, v in enumerate(self._board) if v == self._empty]
def get_debuts_pos(self) -> int:
corn = (0, 2, 6, 8)
poss = self.get_open_poss()
if len(poss) == 9:
return random.choice(corn)
if len(poss) == 8:
return 4 if 4 in poss else random.choice([pos for pos in poss if pos in corn])
# TODO: implement TicTacToe's openning book for whole gameplay depth
return random.choice(poss)
def get_winner(self) -> Optional[str]:
return self._winner
def is_open(self) -> bool:
return not self._winner and len(self.get_open_poss()) > 0