def play(self) -> int: board = Board() last_x = None last_y = None while board.round <= MAX_ROUND: current_color = board.next_color (x, y) = self.players[current_color].play(last_x, last_y) if x == -1 and y == -1: self.viewer.player_undoes() board.undo() (last_x, last_y) = (-1, -1) else: if 0 > x > 4 or 0 > y > 4: raise RuleViolation("out of bounds move") if board.field(x, y, 4) != EMPTY: raise RuleViolation("already full at {},{}".format(x, y)) is_winning = board.move(x, y) LOG.debug("player {} plays to {},{}".format( current_color, x, y)) self.viewer.player_plays(x, y) if is_winning: LOG.debug("player {} wins!".format(current_color)) self.viewer.finish(board.winning_coords()) return current_color (last_x, last_y) = (x, y) LOG.debug("game ended in a draw") self.viewer.finish([]) return EMPTY
def test_tostr(): board = Board() board.move(0, 0) board.move(0, 0) board.move(0, 0) board.move(1, 0) board.move(3, 3) print("\n") print(board)
def test_alternating_players(): board = Board() assert board.field(0, 0, 0) == EMPTY assert board.field(1, 1, 0) == EMPTY board.move(0, 0) assert board.field(0, 0, 0) == RED assert board.field(1, 1, 0) == EMPTY board.move(1, 1) assert board.field(0, 0, 0) == RED assert board.field(1, 1, 0) == BLUE assert board.round == 2
def __init__(self, viewer): self.viewer = viewer self.board = Board() self.play_both_sides = False
def __init__(self): self.board = Board()
def test_win_forward2(): board = Board() for i in range(3, 0, -1): assert not board.move(i, 3-i) assert not board.move(i, 3-i) assert board.move(0, 3)
def test_win_backward2(): board = Board() for i in range(3): assert not board.move(i, 3-i) assert not board.move(i, 3-i) assert board.move(3, 0)