This commit is contained in:
randomuser 2022-07-11 19:28:20 -05:00
parent 842291d6d6
commit b5f2efb316

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
import copy
from pos import TwoDPos from pos import TwoDPos
class BoardTools: class BoardTools:
@ -19,12 +20,20 @@ class BoardTools:
if b[2][0] == b[1][1] == b[0][2]: if b[2][0] == b[1][1] == b[0][2]:
if b[2][0] != Board.unplayed: if b[2][0] != Board.unplayed:
return b[2][0] return b[2][0]
spaces = 0
for i in range(3):
for j in range(3):
if b[i][j] != Board.unplayed:
spaces += 1
if spaces == 9:
return Board.tie
return Board.unplayed return Board.unplayed
class Board: class Board:
unplayed = 0 unplayed = 0
cross = 1 cross = 1
nought = 2 nought = 2
tie = 3
def __init__(self): def __init__(self):
self.board = [ self.board = [
@ -40,6 +49,14 @@ class Board:
self.moves = [] self.moves = []
self.turn = Board.cross self.turn = Board.cross
def copy(self):
new = Board()
new.board = copy.deepcopy(self.board)
new.moves = copy.deepcopy(self.moves)
new.turn = copy.deepcopy(self.turn)
return new
def set(self, a, b, c, d, val): def set(self, a, b, c, d, val):
self.board[a][b][c][d] = val self.board[a][b][c][d] = val
@ -79,6 +96,10 @@ class Board:
return buf return buf
def possible(self): def possible(self):
# check if board is in final state
isFinal = BoardTools.winning(self, self.metaboard())
if isFinal in [Board.tie, Board.cross, Board.nought]:
return []
try: try:
move = self.moves[-1] move = self.moves[-1]
except IndexError: except IndexError:
@ -92,7 +113,7 @@ class Board:
return buf return buf
local = move.local() local = move.local()
if self.subWin(local[2], local[3]): if self.subWin(local[2], local[3]) in [Board.tie, Board.nought, Board.cross]:
return self.reachableEmpty() return self.reachableEmpty()
buf = [] buf = []