diff --git a/board2.py b/board2.py index f263e12..28581f4 100644 --- a/board2.py +++ b/board2.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +import copy from pos import TwoDPos class BoardTools: @@ -19,12 +20,20 @@ class BoardTools: if b[2][0] == b[1][1] == b[0][2]: if b[2][0] != Board.unplayed: 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 class Board: unplayed = 0 cross = 1 nought = 2 + tie = 3 def __init__(self): self.board = [ @@ -40,6 +49,14 @@ class Board: self.moves = [] 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): self.board[a][b][c][d] = val @@ -79,6 +96,10 @@ class Board: return buf 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: move = self.moves[-1] except IndexError: @@ -92,7 +113,7 @@ class Board: return buf 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() buf = []