示例#1
0
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
示例#2
0
 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