def test_shoot_sunked_31(self): board = Board() result = [] board.set_boat(1, 2, 3, "vertical") for i in range(1, 4): result.append(board.shoot(i, 2)) self.assertEqual(result[len(result) - 1], "sunked")
def test_shoot_sunked_5(self): board = Board() result = [] board.set_boat(5, 5, 5, "vertical") for i in range(5, 10): result.append(board.shoot(i, 5)) self.assertEqual(result[len(result) - 1], "sunked")
def test_shoot_sunked_32(self): board = Board() result = [] board.set_boat(5, 7, 3, "horizontal") for i in range(7, 10): result.append(board.shoot(5, i)) self.assertEqual(result[len(result) - 1], "sunked")
class PlayerCPU(object): def __init__(self): self.board_own = Board() self.board_opponent = Board() def get_boards(self): return [self.board_own, self.board_opponent] # Llena el board con todos los barcos de forma random def fill_own_board(self): if self.board_own.state == 'empty': boats = [1, 2, 3, 3, 4, 5] i = 0 while not self.board_own.is_ready_to_war(): row = randint(0, 9) column = randint(0, 9) orientation_own = choice(orientation) is_possible = self.board_own.set_boat(row, column, boats[i], orientation_own) if is_possible: i += 1 return True else: return False
def test_insert_five_vertifcal_ship(self): board = Board() board.set_boat(3, 4, 5, "vertical") board_expected = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 5, 0, 0, 0, 0, 0], [0, 0, 0, 0, 5, 0, 0, 0, 0, 0], [0, 0, 0, 0, 5, 0, 0, 0, 0, 0], [0, 0, 0, 0, 5, 0, 0, 0, 0, 0], [0, 0, 0, 0, 5, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] result = board.get_board() self.assertEqual(result, board_expected)
def test_insert_three_horizontal_ship(self): board = Board() board.set_boat(0, 7, 3, "horizontal") board_expected = [ [0, 0, 0, 0, 0, 0, 0, 31, 31, 31], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] result = board.get_board() self.assertEqual(result, board_expected)
def test_insert(self): board = Board() board.set_boat(2, 3, 1, "horizontal") board_expected = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] result = board.get_board() self.assertEqual(result, board_expected)
class PlayerHuman(object): def __init__(self): self.board_own = Board() self.board_opponent = Board() def get_boards(self): return [self.board_own, self.board_opponent] def put_boat_own_board(self, row, column, boat, orientation_own): is_possible = self.board_own.set_boat(row, column, boat, orientation_own) if is_possible: return True else: return False
def test_shoot_sunked_4(self): board = Board() result = [] result2 = [] result3 = [] board.set_boat(9, 5, 4, "horizontal") for i in range(5, 9): result.append(board.shoot(9, i)) board.set_boat(0, 0, 3, "horizontal") for i in range(0, 1): result2.append(board.shoot(0, i)) board.set_boat(4, 5, 3, "horizontal") for i in range(5, 8): result3.append(board.shoot(4, i)) self.assertEqual(result[len(result) - 1], "sunked") self.assertEqual(result2[len(result2) - 1], "hit") self.assertEqual(result3[len(result3) - 1], "sunked")
class PlayerCPU(object): def __init__(self): self.board_own = Board() self.board_opponent = Board() self.possible_coordenates = [] self.messages = [] for i in range(9): for j in range(9): self.possible_coordenates.append([i, j]) def get_boards(self): return [self.board_own, self.board_opponent] # Llena el board con todos los barcos de forma random def fill_own_board(self): if self.board_own.state == 'empty': boats = [1, 2, 3, 3, 4, 5] i = 0 while not self.board_own.is_ready_to_war(): row = randint(0, 9) column = randint(0, 9) orientation_own = choice(orientation) is_possible = self.board_own.set_boat(row, column, boats[i], orientation_own) if is_possible: i += 1 return True else: return False def pick_coordenate(self): return self.possible_coordenates.pop( randint(0, len(self.possible_coordenates) - 1))
def test_error_position_four_horizontal_ship(self): board = Board() result = board.set_boat(8, 7, 4, "horizontal") self.assertFalse(result)
def test_error_position(self): board = Board() result = board.set_boat(10, 10, 1, "horizontal") self.assertEqual(result, False)
def test_turn_water(self): board = Board() board.set_boat(3, 3, 4, "vertical") result = board.shoot(1, 2) continue_turn = board.turn_decision_hit(result) self.assertFalse(continue_turn)
def test_shoot_sunked_1(self): board = Board() board.set_boat(1, 1, 1, "horizontal") result = board.shoot(1, 1) self.assertEqual(result, "sunked")
def test_there_are_boats(self): board = Board() board.set_boat(1, 1, 1, "vertical") self.assertTrue(board.there_are_boats()) board.shoot(1, 1) self.assertFalse(board.there_are_boats())
def test_error_position_five_vertical_ship(self): board = Board() result = board.set_boat(7, 7, 5, "vertical") self.assertFalse(result)
def test_error_position_already_has_ship_horizontal(self): board = Board() board.set_boat(2, 2, 4, "horizontal") result = board.set_boat(2, 3, 2, "vertical") self.assertFalse(result)
def test_turn_hit(self): board = Board() board.set_boat(3, 3, 4, "vertical") result = board.shoot(4, 3) continue_turn = board.turn_decision_hit(result) self.assertTrue(continue_turn)