コード例 #1
0
ファイル: game.py プロジェクト: rikardsjostedt/battleship
    def event_handler(self, event):
        print(self.current_player.id)
        if self.current_gamestate == "setup":
            px, py = event.pos
            size = self.current_player.board.cell_size
            x = int(px / size)
            y = int(py / size)
            if (x < self.amount_of_cols) & (y < self.amount_of_cols - 2):
                #FUUULT
                boat_cells = [
                    self.current_player.board.cells[x][y],
                    self.current_player.board.cells[x][y + 1],
                    self.current_player.board.cells[x][y + 2]
                ]
                boat = Boat(boat_cells, self)
                if self.current_player.board.check_cell(event.pos, boat):
                    boat.place()
                    self.current_player.boats.append(boat)

        else:
            hit_not_occupied = self.current_player.board.check_cell(event.pos)
            self.current_player.board.render()
            has_alive_boats = False
            if hit_not_occupied:
                print("hit")
                for boat in self.current_player.boats:
                    if boat.alive:
                        has_alive_boats = True
                        break
                if not has_alive_boats:
                    self.current_player.board.render_winner_text(
                        self.current_player)
                    return
                sleep(1)
                self.change_current_player()
コード例 #2
0
 def test_placingTwoBoats(self):
     boat1 = Boat(5)
     boat2 = Boat(2)
     grid = Grid(10, 10)
     boat1.place(grid, 0, 0, Boat.DIR_RIGHT)
     # Can't place over an existing boat
     self.assertEqual(boat2.place(grid, 0, 0, Boat.DIR_DOWN), False)
     # Can place anywhere else
     self.assertEqual(boat2.place(grid, 0, 1, Boat.DIR_DOWN), True)
コード例 #3
0
 def test_placingBoatRight(self):
     boat = Boat(5)
     grid = Grid(10, 10)
     # Can't
     self.assertEqual(boat.place(grid, 9, 2, boat.DIR_RIGHT), False)
     self.assertEqual(grid.get(9, 2), Grid.EMPTY)
     # Can
     self.assertEqual(boat.place(grid, 2, 2, boat.DIR_RIGHT), True)
     self.assertEqual(grid.get(2, 2), Grid.BOAT)
     self.assertEqual(grid.get(3, 2), Grid.BOAT)
     self.assertEqual(grid.get(4, 2), Grid.BOAT)
     self.assertEqual(grid.get(5, 2), Grid.BOAT)
     self.assertEqual(grid.get(6, 2), Grid.BOAT)
     # Can't anymore
     self.assertEqual(boat.place(grid, 0, 2, boat.DIR_RIGHT), False)
コード例 #4
0
 def test_placingBoatLeft(self):
     boat = Boat(5)
     grid = Grid(10, 10)
     # Can't
     self.assertEqual(boat.place(grid, 0, 7, boat.DIR_LEFT), False)
     self.assertEqual(grid.get(0, 7), Grid.EMPTY)
     # Can
     self.assertEqual(boat.place(grid, 7, 7, boat.DIR_LEFT), True)
     self.assertEqual(grid.get(7, 7), Grid.BOAT)
     self.assertEqual(grid.get(6, 7), Grid.BOAT)
     self.assertEqual(grid.get(5, 7), Grid.BOAT)
     self.assertEqual(grid.get(4, 7), Grid.BOAT)
     self.assertEqual(grid.get(3, 7), Grid.BOAT)
     # Can't anymore
     self.assertEqual(boat.place(grid, 7, 7, boat.DIR_LEFT), False)
コード例 #5
0
 def test_placingBoatUp(self):
     boat = Boat(5)
     grid = Grid(10, 10)
     # Can't
     self.assertEqual(boat.place(grid, 0, 0, boat.DIR_UP), False)
     self.assertEqual(grid.get(0, 0), Grid.EMPTY)
     # Can
     self.assertEqual(boat.place(grid, 6, 6, boat.DIR_UP), True)
     self.assertEqual(grid.get(6, 6), Grid.BOAT)
     self.assertEqual(grid.get(6, 5), Grid.BOAT)
     self.assertEqual(grid.get(6, 4), Grid.BOAT)
     self.assertEqual(grid.get(6, 3), Grid.BOAT)
     self.assertEqual(grid.get(6, 2), Grid.BOAT)
     # Can't anymore
     self.assertEqual(boat.place(grid, 6, 6, boat.DIR_UP), False)
コード例 #6
0
 def test_placingRandomly(self):
     boat = Boat(5)
     grid = Grid(10, 10)
     boat.place(grid)
     self.assertEqual(len(boat.pos_x), 5)
     self.assertEqual(len(boat.pos_y), 5)
     # Trying to hit the boat
     while boat.remaining > 0:
         remaining = boat.remaining
         x = boat.pos_x[0]
         y = boat.pos_y[0]
         self.assertEqual(boat.hit(x, y), True)
         self.assertEqual(boat.remaining, remaining - 1)
         self.assertEqual(boat.hit(x, y), False)
         self.assertEqual(boat.remaining, remaining - 1)
コード例 #7
0
 def test_placingBoatDown(self):
     boat = Boat(5)
     grid = Grid(10, 10)
     # Can't
     self.assertEqual(boat.place(grid, 0, 9, boat.DIR_DOWN), False)
     self.assertEqual(grid.get(0, 9), Grid.EMPTY)
     # Can
     self.assertEqual(boat.place(grid, 0, 0, boat.DIR_DOWN), True)
     self.assertEqual(grid.get(0, 0), Grid.BOAT)
     self.assertEqual(grid.get(0, 1), Grid.BOAT)
     self.assertEqual(grid.get(0, 2), Grid.BOAT)
     self.assertEqual(grid.get(0, 3), Grid.BOAT)
     self.assertEqual(grid.get(0, 4), Grid.BOAT)
     # Can't anymore
     self.assertEqual(boat.place(grid, 0, 0, boat.DIR_DOWN), False)
コード例 #8
0
 def test_hit(self):
     boat = Boat(5)
     grid = Grid(10, 10)
     self.assertEqual(boat.place(grid, 2, 2, boat.DIR_RIGHT), True)
     self.assertEqual(boat.remaining, 5)
     # Miss
     self.assertEqual(boat.hit(1, 2), False)
     self.assertEqual(boat.hit(2, 1), False)
     self.assertEqual(boat.hit(2, 3), False)
     self.assertEqual(boat.hit(7, 2), False)
     # Hit
     self.assertEqual(boat.hit(2, 2), True)
     self.assertEqual(boat.remaining, 4)
     # Can't hit a second time at the same pos
     self.assertEqual(boat.hit(2, 2), False)
     self.assertEqual(boat.remaining, 4)