def setUp(self): self.piece1 = Piece(2, 2, [2, 3, 7]) self.piece2 = Piece(1, 3, [1, 2]) self.piece3 = Piece(1, 3, [0, 7]) self.piece4 = Piece(1, 3, [1]) self.player1 = Player(1) self.player2 = Player(2) self.board1 = Board(8) self.board1.place_simple_initial(self.piece1, self.player1) self.board2 = Board(8) self.board2.place_simple_initial(self.piece1, self.player2) self.board2.place(self.piece3, 5, 4, Orientation.East, None)
def test_trivial_board(self): board = Board(2) game = Game(board, self.players, self.objectives, self.pieces) game.make_turn(self.player1, self.pieces[0], 0, 0, Orientation.South) expected_scores = {p: 4 for p in self.players} self.assertDictEqual(expected_scores, game.scores())
def test_large_board(self): """Test score of the following board: 0 1 2 3 4 5 6 7 8 0 +--+oo+--+oo+oo+ + + + o p7 | | | 1 +--+--+--+ + + . . + |p2|p3| 2 + . . + + + . . + | | | 3 + . . +oo+oo+--+--+--+ o o | 4 + . . + p1 + p4 + o o o 5 + . . +oo+oo+--+oo+--+ | | | | 6 + . . . + + + + + |p6| |p5| 7 + . . . + + + + + | | | | 8 + + + + +oo+ +oo+ + """ p1, p2, p3, p4, p5, p6, p7 = self.pieces board = Board(8) game = Game(board, self.players, self.objectives, self.pieces) game.make_turn(self.player1, p1, 3, 3, Orientation.South) game.make_turn(self.player2, p2, 3, 0, Orientation.South) game.make_turn(self.player1, p3, 4, 0, Orientation.South) game.make_turn(self.player2, p4, 5, 3, Orientation.South) game.make_turn(self.player1, p5, 6, 5, Orientation.South) game.make_turn(self.player2, p6, 4, 5, Orientation.South) game.make_turn(self.player1, p7, 0, 0, Orientation.South) expected_scores = {self.player1: 1, self.player2: 6} self.assertDictEqual(expected_scores, game.scores())
class InitialPlacementsTest(unittest.TestCase): def setUp(self): self.piece1 = Piece(1, 3, [1, 7]) self.piece2 = Piece(4, 4, [0]) self.board = Board(8) def test_valid_initial_placement(self): placed_piece = self.board.place_initial(self.piece1, 3, 1, Orientation.South, None) self.assertIsNotNone(placed_piece) def test_valid_initial_placement_all_orientations(self): for o in Orientation: with self.subTest(orientation=o): board = Board(8) placed_piece = board.place_initial(self.piece1, 4, 4, o, None) self.assertIsNotNone(placed_piece) def test_initial_piece_large_area(self): placed_piece = self.board.place_initial(self.piece2, 2, 2, Orientation.South, None) self.assertIsNotNone(placed_piece) def test_invalid_initial_placement_far_away(self): with self.assertRaises(InvalidPlacementError): self.board.place_initial(self.piece1, 0, 0, Orientation.South, None) def test_invalid_initial_placement_close_by(self): with self.assertRaises(InvalidPlacementError): self.board.place_initial(self.piece1, 3, 6, Orientation.East, None) def test_invalid_initial_placement_out_of_board_but_in_initial_area(self): with self.assertRaises(InvalidPlacementError): board = Board(4) board.place_initial(self.piece2, 1, 1, Orientation.South, None) def test_initial_piece_placed_twice(self): self.board.place_initial(self.piece1, 3, 3, Orientation.South, None) with self.assertRaises(InvalidPlacementError): self.board.place_initial(self.piece1, 4, 3, Orientation.South, None)
def build_game(self, players): """Build game according to the gamespec passed in constructor With this method we implement the GameBuilder interface, see also OfficialGameBuilder """ board = Board(self.gamespec.size) chosen_players = [players[id] for id in self.gamespec.players] objectives = {player: self.gamespec.objectives[player.id] for player in chosen_players} return Game(board, chosen_players, objectives, self.gamespec.pieces)
class OverlappingPiecesBoardTest(unittest.TestCase): def setUp(self): self.piece1 = Piece(2, 2, [0, 1, 2, 3, 4, 5, 6, 7]) self.piece2 = Piece(1, 3, [0, 1, 2, 3, 4, 5, 6, 7]) self.board = Board(8) self.player = Player(1) self.board.place_simple_initial(self.piece1, self.player) def test_overlap_left_placement(self): with self.assertRaises(InvalidPlacementError): self.board.place(self.piece2, 2, 5, Orientation.East, None) def test_overlap_right_placement(self): with self.assertRaises(InvalidPlacementError): self.board.place(self.piece2, 6, 3, Orientation.West, None) def test_overlap_top_placement(self): with self.assertRaises(InvalidPlacementError): self.board.place(self.piece2, 3, 1, Orientation.South, None) def test_overlap_bottom_placement(self): with self.assertRaises(InvalidPlacementError): self.board.place(self.piece2, 4, 7, Orientation.North, None)
def from_file(clss, path): with open(path) as f: game = json.load(f) size = game['dimension'] players = list(game['objectives']) all_objectives = {} pieces = [] positions = [] for name, objectives in game['objectives'].items(): all_objectives[name] = [Board.Side(side) for side in objectives] for p in game['stones']: pieces.append(Piece(p['width'], p['height'], p['connectors'])) pos = p['position'] x, y = pos['x'], pos['y'] orientation = Orientation(pos['orientation']) positions.append((x, y, orientation)) return clss(players, all_objectives, pieces, positions, size)
def test_game_spec(self): player1 = Player('Player A') player1.name = player1.id player2 = Player('Player B') player2.name = player2.id objectives = { player1: [Board.Side.North, Board.Side.South], player2: [Board.Side.East, Board.Side.West] } pieces, positions = self.game_spec_pieces() board = Board(10) game = Game(board, [player1, player2], objectives, pieces) for piece, pos in zip(pieces, positions): game.make_turn(game.current_player, piece, *pos) serialized = GameSerializer.serialize(game) spec_file = '../protocol/examples/populated-field.json' self.assertSerializationEqual(spec_file, serialized)
def test_list_valid_placements(self): board = Board(8) board.place_simple_initial(self.piece1, self.player) placements = list(board.valid_placements(self.piece2)) for placed_piece in placements: self.assertIsNone(board.validate_placement(placed_piece)) expected_placements = [ (5, 4, Orientation.East), (6, 4, Orientation.North), (8, 3, Orientation.West), (3, 5, Orientation.South), (3, 6, Orientation.East), (4, 8, Orientation.North) ] for expected_placement in expected_placements: ok = any(expected_placement == (p.x, p.y, p.orientation) for p in placements) self.assertTrue(ok)
class OutOfBoardTest(unittest.TestCase): def setUp(self): self.piece1 = Piece(1, 3, [0, 1, 2, 3, 4, 5, 6, 7]) self.piece2 = Piece(2, 2, [0, 1, 2, 3, 4, 5, 6, 7]) self.board = Board(4) self.player = Player(1) self.board.place_simple_initial(self.piece1, self.player) def test_completely_left_out_of_board_placement(self): with self.assertRaises(InvalidPlacementError): # out of board to the left self.board.place(self.piece1, -1, 0, Orientation.South, None) def test_completely_right_out_of_board_placement(self): with self.assertRaises(InvalidPlacementError): # out of board to the right self.board.place(self.piece1, 4, 3, Orientation.North, None) def test_completely_top_out_of_board_placement(self): with self.assertRaises(InvalidPlacementError): # out of board to the top self.board.place(self.piece1, 0, 0, Orientation.East, None) def test_completely_bottom_out_of_board_placement(self): with self.assertRaises(InvalidPlacementError): # out of board to the bottom self.board.place(self.piece1, 4, 4, Orientation.West, None) def test_slightly_left_out_of_board_placement(self): with self.assertRaises(InvalidPlacementError): # out of board to the left self.board.place(self.piece2, 1, 1, Orientation.West, None) def test_slightly_right_out_of_board_placement(self): with self.assertRaises(InvalidPlacementError): # out of board to the right self.board.place(self.piece2, 3, 3, Orientation.East, None) def test_slightly_top_out_of_board_placement(self): with self.assertRaises(InvalidPlacementError): # out of board to the top self.board.place(self.piece1, 0, -2, Orientation.South, None) def test_slightly_bottom_out_of_board_placement(self): with self.assertRaises(InvalidPlacementError): # out of board to the bottom self.board.place(self.piece1, 1, 5, Orientation.North, None)
def test_valid_initial_placement_all_orientations(self): for o in Orientation: with self.subTest(orientation=o): board = Board(8) placed_piece = board.place_initial(self.piece1, 4, 4, o, None) self.assertIsNotNone(placed_piece)
def setUp(self): self.piece1 = Piece(2, 2, [0, 1, 2, 3, 4, 5, 6, 7]) self.piece2 = Piece(1, 3, [0, 1, 2, 3, 4, 5, 6, 7]) self.board = Board(8) self.player = Player(1) self.board.place_simple_initial(self.piece1, self.player)
def test_score_no_connectors_on_edges(self): board = Board(10) game = Game(board, self.players, self.objectives, self.pieces) expected_scores = {p: 0 for p in self.players} self.assertDictEqual(expected_scores, game.scores())
def test_invalid_initial_placement_out_of_board_but_in_initial_area(self): with self.assertRaises(InvalidPlacementError): board = Board(4) board.place_initial(self.piece2, 1, 1, Orientation.South, None)
def test_valid_placement_p1c5_p2c4(self): board = Board(8) board.place_simple_initial(self.piece1, self.player) placed_piece = board.place(self.piece2, 4, 8, Orientation.North, None) self.assertIsNotNone(placed_piece)
def test_valid_placement_p1c2_p2c0(self): board = Board(8) board.place_simple_initial(self.piece1, self.player) placed_piece = board.place(self.piece2, 5, 4, Orientation.East, None) self.assertIsNotNone(placed_piece)
def setUp(self): self.piece1 = Piece(1, 3, [1, 7]) self.piece2 = Piece(4, 4, [0]) self.board = Board(8)
class NonConnectionPiecesBoardTest(unittest.TestCase): def setUp(self): self.piece1 = Piece(2, 2, [2, 3, 7]) self.piece2 = Piece(1, 3, [1, 2]) self.piece3 = Piece(1, 3, [0, 7]) self.piece4 = Piece(1, 3, [1]) self.player1 = Player(1) self.player2 = Player(2) self.board1 = Board(8) self.board1.place_simple_initial(self.piece1, self.player1) self.board2 = Board(8) self.board2.place_simple_initial(self.piece1, self.player2) self.board2.place(self.piece3, 5, 4, Orientation.East, None) def test_pieces_too_far_apart_1(self): with self.assertRaises(InvalidPlacementError): self.board1.place(self.piece2, 1, 1, Orientation.South, None) def test_pieces_too_far_apart_2(self): with self.assertRaises(InvalidPlacementError): self.board1.place(self.piece2, 4, 7, Orientation.East, None) def test_pieces_too_far_apart_3(self): with self.assertRaises(InvalidPlacementError): self.board1.place(self.piece2, 7, 3, Orientation.North, None) def test_no_connectors_1(self): with self.assertRaises(InvalidPlacementError): self.board1.place(self.piece2, 3, 0, Orientation.South, None) def test_no_connectors_2(self): with self.assertRaises(InvalidPlacementError): self.board1.place(self.piece2, 2, 3, Orientation.East, None) def test_connectors_mismatch_1(self): with self.assertRaises(InvalidPlacementError): self.board1.place(self.piece2, 3, 2, Orientation.South, None) def test_connectors_mismatch_2(self): with self.assertRaises(InvalidPlacementError): self.board1.place(self.piece2, 3, 6, Orientation.North, None) def test_connectors_mismatch_3(self): with self.assertRaises(InvalidPlacementError): self.board1.place(self.piece2, 5, 2, Orientation.South, None) def test_multi_piece_connector_mismatch(self): with self.assertRaises(InvalidPlacementError): # piece4 connects to piece1 but not to piece3 self.board2.place(self.piece4, 5, 5, Orientation.East, None)