예제 #1
0
    def test_normal_game(self):
        "state_codes are correct during a normal game, where one player wins"
        (gid, pid1) = b.new_game(Nicks.testNick)
        me = b.players[pid1]
        self.assertEqual(me.state_code, b.PlayerState.WAIT_FOR_JOIN)
        pid2 = b.join_game(gid, Nicks.testNick2)
        other = b.players[pid2]
        self.assertEqual(me.state_code, b.PlayerState.SUBMIT_GRID)
        self.assertEqual(other.state_code, b.PlayerState.SUBMIT_GRID)

        # they both submit the same grid, for simplicity
        b.submit_grid(pid1, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                             [0, 3, 3, 3], [0, 4, 4, 4]])
        self.assertEqual(me.state_code, b.PlayerState.WAIT_FOR_SUBMIT)
        b.submit_grid(pid2, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                             [0, 3, 3, 3], [0, 4, 4, 4]])
        self.assertEqual(me.state_code, b.PlayerState.BOMB)
        self.assertEqual(other.state_code, b.PlayerState.WAIT_FOR_BOMB)

        # here, we just play out some turns, with me bombing each of
        # their ships while they bomb an empty space
        for (x, y) in other.ship_points:
            b.bomb_position(pid1, x, y)
            b.bomb_position(pid2, 5, 5)

        # game should be over
        self.assertEqual(me.state_code, b.PlayerState.GAME_OVER)
        self.assertEqual(other.state_code, b.PlayerState.GAME_OVER)

        # we should have won
        self.assertEqual(b.get_game_end(pid1), (other.grid, 1, 1))

        # they should have lost
        self.assertEqual(b.get_game_end(pid2), (me.grid, 1, 0))
예제 #2
0
 def test_random_ai_wins(self):
     "Random AI wins if we don't bomb his ships"
     pid = b.join_ai_game(b"", 1)
     self.assertTrue(pid in b.players)
     me = b.players[pid]
     other = me.opponent()
     self.assertNotEqual(other, None)
     self.assertEqual(me.state_code, b.PlayerState.SUBMIT_GRID)
     self.assertEqual(other.state_code, b.PlayerState.WAIT_FOR_SUBMIT)
     b.submit_grid(pid, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                         [0, 3, 3, 3], [0, 4, 4, 4]])
     while not me.dead():
         b.bomb_position(pid, 0, 0)
예제 #3
0
 def test_submit_early(self):
     "submit_grid fails when called too early"
     (gid, pid) = b.new_game(Nicks.testNick)
     self.assertEqual(
         b.submit_grid(pid, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                             [0, 3, 3, 3], [0, 4, 4, 4]]),
         b.Error.NO_OPPONENT)
예제 #4
0
 def test_example_ai_loses(self):
     "Example AI can play a game (and lose)"
     pid = b.join_ai_game(b"", 0)
     self.assertTrue(pid in b.players)
     me = b.players[pid]
     other = me.opponent()
     self.assertNotEqual(other, None)
     self.assertEqual(me.state_code, b.PlayerState.SUBMIT_GRID)
     self.assertEqual(other.state_code, b.PlayerState.WAIT_FOR_SUBMIT)
     b.submit_grid(pid, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                         [0, 3, 3, 3], [0, 4, 4, 4]])
     for (x, y) in other.ship_points:
         b.bomb_position(pid, x, y)
     self.assertTrue(other.dead())
     self.assertEqual(other.state_code, b.PlayerState.GAME_OVER)
     self.assertEqual(me.state_code, b.PlayerState.GAME_OVER)
예제 #5
0
    def test_bomb_history(self):
        "get_bomb_history fails with bad args but is correct otherwise"
        self.assertEqual(b.get_bombed_positions(123),
                         (b.Error.INVALID_PLYR_ID, 0, []))
        (gid, pid1) = b.new_game(Nicks.testNick)
        pid2 = b.join_game(gid, Nicks.testNick2)
        b.submit_grid(pid1, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                             [0, 3, 3, 3], [0, 4, 4, 4]])
        b.submit_grid(pid2, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                             [0, 3, 3, 3], [0, 4, 4, 4]])
        b.bomb_position(pid1, 0, 0)
        b.bomb_position(pid2, 0, 4)
        b.bomb_position(pid1, 0, 1)

        self.assertEqual(b.get_bombed_positions(pid1), (0, 1, [0, 4]))
        self.assertEqual(b.get_bombed_positions(pid2), (0, 2, [0, 0, 0, 1]))
예제 #6
0
    def test_submit_invalid(self):
        "submit_grid fails with bad grids"
        # game starts, other player joins, we place first
        (gid, pid) = b.new_game(Nicks.testNick)

        otherpid = b.join_game(gid, Nicks.testNick2)
        # empty grid
        self.assertEqual(b.submit_grid(pid, []), b.Error.INVALID_GRID)
        # too few ships
        self.assertEqual(b.submit_grid(pid, [[0, 0, 1, 0], [0, 1, 0, 3]]),
                         b.Error.INVALID_GRID)
        # ships overlap but otherwise valid
        self.assertEqual(
            b.submit_grid(pid, [[0, 0, 1, 0], [0, 0, 2, 0], [0, 0, 2, 0],
                                [0, 0, 3, 0], [0, 0, 4, 0]]),
            b.Error.INVALID_GRID)
        # ship is rectangle, not line
        self.assertEqual(
            b.submit_grid(pid, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                                [0, 3, 3, 3], [0, 4, 4, 5]]),
            b.Error.INVALID_GRID)
        # ok grid, bad pid
        self.assertEqual(
            b.submit_grid(pid + 100, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                                      [0, 3, 3, 3], [0, 4, 4, 4]]),
            b.Error.INVALID_PLYR_ID)
        # this should work
        self.assertEqual(
            b.submit_grid(pid, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                                [0, 3, 3, 3], [0, 4, 4, 4]]), 0)
        # but you can't submit twice
        self.assertEqual(
            b.submit_grid(pid, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                                [0, 3, 3, 3], [0, 4, 4, 4]]),
            b.Error.OUT_OF_TURN)
예제 #7
0
 def test_bomb_pos_invalid(self):
     "bomb_position fails with bad arguments or if called too early"
     # bad pid
     self.assertEqual(b.bomb_position(123, 0, 0), b.Error.INVALID_PLYR_ID)
     (gid, pid1) = b.new_game(Nicks.testNick)
     # before opponent joins
     self.assertEqual(b.bomb_position(pid1, 0, 0), b.Error.NO_OPPONENT)
     pid2 = b.join_game(gid, Nicks.testNick2)
     # before the bombing phase starts
     self.assertEqual(b.bomb_position(pid1, 0, 0), b.Error.OUT_OF_TURN)
     # players submit grid
     b.submit_grid(pid1, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                          [0, 3, 3, 3], [0, 4, 4, 4]])
     b.submit_grid(pid2, [[0, 0, 1, 0], [0, 1, 2, 1], [0, 2, 2, 2],
                          [0, 3, 3, 3], [0, 4, 4, 4]])
     # bad position
     self.assertEqual(b.bomb_position(pid1, 100, 100),
                      b.Error.INVALID_BOMB_TARGET)
     # this should work and hit
     self.assertEqual(b.bomb_position(pid1, 0, 0), 1)