def test_start_move(): self.assertEqual( test_pawn.possible_moves(), [Coordinate(0, 2), Coordinate(0, 3)]) test_move(test_pawn, Coordinate(0, 3)) self.assertEqual(Coordinate(test_pawn.x, test_pawn.y), Coordinate(0, 3))
def test_possible_moves(): self.desk.add_piece(Coordinate(0, 1), 'R', Colour.BLACK) self.desk.add_piece(Coordinate(3, 0), 'N', Colour.BLACK) self.assertEqual(len(test_white_king.possible_moves()), 2) self.desk.add_piece(Coordinate(5, 2), 'R', Colour.BLACK) test_white_king.was_moving = False self.assertEqual(len(test_white_king.possible_moves()), 1)
def test_black_start_move_and_last_move(): self.assertEqual( test_enemy_pawn_to_take_on_the_aisle.possible_moves(), [Coordinate(0, 5), Coordinate(0, 4)]) self.desk.set_last_move( Move(test_enemy_pawn_to_take_on_the_aisle, Coordinate(0, 4))) test_move(test_enemy_pawn_to_take_on_the_aisle, Coordinate(0, 4))
def do(self, message): try: if('playerId' not in message): raise Exception("No player ID in message") self.canonicalize(message) if(message['name'] == 'move'): unit = message['unit'] move_to = Coordinate(message['to']['x'], message['to']['y']) self.move(unit, move_to) elif(message['name'] == 'attack'): attacker = message['attacker'] defender = message['defender'] self.attack(attacker, defender) elif(message['name'] == 'build'): army = self._find_army(message['army']) location = Coordinate(message['at']['x'], message['at']['y']) self.build(army, message['unit_name'], location) elif(message['name'] == 'end_turn'): army = self._find_army(message['army']) if(army.is_turn()): army.end_turn() self.scenario.next_army() except Exception as e: # need to do a better job of error reporting here print(e) return self.flat() return self.flat()
def test_bishop(self): self.desk.clear_desk() start_coordinate = Coordinate(3, 4) test_bishop = self.desk.add_piece(start_coordinate, 'B', Colour.WHITE) for coordinate in test_bishop.possible_moves(): self.assertEqual(abs(coordinate.x - start_coordinate.x), abs(coordinate.y - start_coordinate.y)) self.assertEqual(len(test_bishop.possible_moves()), 13) self.desk.add_piece(Coordinate(4, 5), '', Colour.BLACK) self.desk.add_piece(Coordinate(2, 3), '', Colour.WHITE) self.assertEqual(len(test_bishop.possible_moves()), 8)
def test_check_on_mate(self): self.desk.add_piece(Coordinate(1, 0), 'K', Colour.WHITE) self.desk.add_piece(Coordinate(2, 2), 'Q', Colour.BLACK) self.desk.add_piece(Coordinate(0, 2), 'K', Colour.BLACK) self.assertEqual(self.desk.check_on_mate(Colour.WHITE), StatesOfGame.STALEMATE) self.desk.add_piece(Coordinate(1, 1), 'Q', Colour.BLACK) self.assertEqual(self.desk.check_on_mate(Colour.WHITE), StatesOfGame.CHECKMATE) self.assertEqual(self.desk.check_on_mate(Colour.BLACK), StatesOfGame.RUN)
def get_neighbors(self, coordinate): neighbors = [] if (coordinate.x < len(self.board[0]) - 1): neighbors.append(Coordinate(coordinate.x + 1, coordinate.y)) if (coordinate.x > 0): neighbors.append(Coordinate(coordinate.x - 1, coordinate.y)) if (coordinate.y < len(self.board) - 1): neighbors.append(Coordinate(coordinate.x, coordinate.y + 1)) if (coordinate.y > 0): neighbors.append(Coordinate(coordinate.x, coordinate.y - 1)) return neighbors
def get_path_test(): board = MockBoard() cost_table = { 'plain': 1, } finder = PathFinder(board) path = finder.get_path(cost_table, Coordinate(0, 0), Coordinate(1, 2)) json_path = [tile.as_json() for tile in path] assert json_path == [ '{"x": 1, "y": 0}', '{"x": 2, "y": 0}', '{"x": 3, "y": 0}', '{"x": 4, "y": 0}', '{"x": 5, "y": 0}', '{"x": 5, "y": 1}', '{"x": 5, "y": 2}', '{"x": 5, "y": 3}', '{"x": 5, "y": 4}', '{"x": 5, "y": 5}', '{"x": 4, "y": 5}', '{"x": 3, "y": 5}', '{"x": 3, "y": 4}', '{"x": 2, "y": 4}', '{"x": 1, "y": 4}', '{"x": 0, "y": 4}', '{"x": 0, "y": 3}', '{"x": 0, "y": 2}', '{"x": 1, "y": 2}' ] assert_raises(NoPathFound, finder.get_path, cost_table, Coordinate(0, 0), Coordinate(3, 2)) path = finder.get_path(cost_table, Coordinate(0, 0), Coordinate(2, 0)) json_path = [tile.as_json() for tile in path] assert json_path == ['{"x": 1, "y": 0}', '{"x": 2, "y": 0}'] path = finder.get_path(cost_table, Coordinate(0, 0), Coordinate(0, 0)) assert path == []
def tiles_in_range(self, cost_table, from_position, max_cost): can_move_to = [] # add 1 to max cost so we're working on a closed interval for x in range(-max_cost, max_cost + 1): for y in range(-max_cost, max_cost + 1): if y == 0 and x == 0 or abs(y) + abs(x) > max_cost: continue try: to_position = Coordinate(from_position.x + x, from_position.y + y) except BadCoordinateCreation: continue if self.board.is_on_board(to_position): try: path = self.get_path(cost_table, from_position, to_position) if from_position in path: path.remove(from_position) if self.path_cost(path, cost_table) <= max_cost: can_move_to.append(to_position) except NoPathFound: continue return can_move_to
def test_rook(self): self.desk.clear_desk() start_coordinate = Coordinate(3, 4) test_rook = self.desk.add_piece(start_coordinate, 'R', Colour.WHITE) for coordinate in test_rook.possible_moves(): self.assertTrue(start_coordinate.x == coordinate.x or start_coordinate.y == coordinate.y)
def _add_object(self, army_name, data, object_type): location = Coordinate(data['x'], data['y']) if (self.space_occupied(location)): msg = "Attempt to occupy a location that is already taken" raise BadScenarioData(msg) army = self._find_army(army_name) self._build_type(army, data, location, object_type) self.object_coordinates.append(location)
def _get_target(self, msg): targets = [] for msg_target in msg.targets: coordinate = Coordinate(msg_target.x, msg_target.y, msg_target.z) targets.append(Target(coordinate)) target = self.find_closest.find(targets) rospy.loginfo("Closest target is at [{0}, {1}, {2}]".format( target.x, target.y, target.z)) # debugging return target
def get_tile_test(): tiles = [] for row in range(0, 30): tiles.append([]) for col in range(0, 5): tiles[row].append(MockTile("{0} {1}".format(col, row))) board = Board(tiles, {}) tile = board.get_tile_at_coordinate(Coordinate(1, 4)) assert tile.tile_type == "1 4" tile = board.get_tile_at_coordinate(Coordinate(3, 10)) assert tile.tile_type == "3 10" tile = board.get_tile_at_coordinate(Coordinate(0, 0)) assert tile.tile_type == "0 0" tile = board.get_tile_at_coordinate(Coordinate(4, 29)) assert tile.tile_type == "4 29" assert_raises(IndexError, board.get_tile_at_coordinate, Coordinate(100, 100))
def test_queen(self): self.desk.clear_desk() start_coordinate = Coordinate(3, 4) test_queen = self.desk.add_piece(start_coordinate, 'Q', Colour.WHITE) for coordinate in test_queen.possible_moves(): self.assertTrue(start_coordinate.x == coordinate.x or start_coordinate.y == coordinate.y or abs(coordinate.x - start_coordinate.x) == abs(coordinate.y - start_coordinate.y))
def move_test(): unit = test_unit() assert unit.movement_range() == 5 unit.move(Coordinate(5, 5), 3) assert unit.movement_range() == 2 coordinate = unit.get_coordinate() assert coordinate.x == 5 assert coordinate.y == 5 assert not unit.can_move(5) assert not unit.can_move(2)
def test_king(self): def set_rooks(): self.desk.add_piece(Coordinate(0, 0), 'R', Colour.WHITE) self.desk.add_piece(Coordinate(7, 0), 'R', Colour.WHITE) self.desk.add_piece(Coordinate(0, 7), 'R', Colour.BLACK) self.desk.add_piece(Coordinate(7, 7), 'R', Colour.BLACK) def test_castling(): self.assertEqual(len(test_white_king.possible_moves()), 5) set_rooks() self.assertEqual(len(test_white_king.possible_moves()), 7) test_white_king.was_moving = True self.assertEqual(len(test_white_king.possible_moves()), 5) self.assertEqual(len(test_black_king.possible_moves()), 7) self.desk.desk[0][7].piece.was_moving = True self.assertEqual(len(test_black_king.possible_moves()), 6) self.desk.desk[7][7].piece.was_moving = True self.assertEqual(len(test_black_king.possible_moves()), 5) def test_defence(): self.assertEqual(len(test_white_king.possible_moves(defend=True)), 5) def test_possible_moves(): self.desk.add_piece(Coordinate(0, 1), 'R', Colour.BLACK) self.desk.add_piece(Coordinate(3, 0), 'N', Colour.BLACK) self.assertEqual(len(test_white_king.possible_moves()), 2) self.desk.add_piece(Coordinate(5, 2), 'R', Colour.BLACK) test_white_king.was_moving = False self.assertEqual(len(test_white_king.possible_moves()), 1) self.desk.clear_desk() start_coordinate_white = Coordinate(4, 0) start_coordinate_black = Coordinate(4, 7) test_white_king = self.desk.add_piece(start_coordinate_white, 'K', Colour.WHITE) test_black_king = self.desk.add_piece(start_coordinate_black, 'K', Colour.BLACK) test_castling() test_defence() test_possible_moves()
def zero_health_test(): unit = test_unit() assert not unit.is_dead() unit.do_damage(10) assert not unit.is_dead() unit.do_damage(10) assert not unit.is_dead() unit.do_damage(10) assert unit.is_dead() unit = Unit('tank', 'tred', 'cannon', MockArmor('plate', 0), Coordinate(1, 4), 'dragon') assert unit.is_dead()
def test_knight(self): self.desk.clear_desk() start_coordinate = Coordinate(0, 0) test_knight = self.desk.add_piece(start_coordinate, 'N', Colour.WHITE) self.assertListEqual( test_knight.possible_moves(), [Coordinate(1, 2), Coordinate(2, 1)]) self.desk.add_piece(Coordinate(1, 2), '', Colour.WHITE) self.desk.add_piece(Coordinate(2, 1), '', Colour.BLACK) self.assertListEqual(test_knight.possible_moves(), [Coordinate(2, 1)]) self.assertListEqual( test_knight.possible_moves(defend=True), [Coordinate(1, 2), Coordinate(2, 1)]) self.assertEqual(str(test_knight), 'N_w')
def get_neighbors_test(): board = get_test_board() neighbors = board.get_neighbors(Coordinate(0, 0)) coordinate_sort = lambda c: c['x'] + c['y'] assert sorted([tile.flat() for tile in neighbors], key=coordinate_sort) == ([{ 'y': 0, 'x': 1 }, { 'y': 1, 'x': 0 }]) neighbors = board.get_neighbors(Coordinate(2, 2)) assert sorted([tile.flat() for tile in neighbors], key=coordinate_sort) == ([{ 'y': 2, 'x': 1 }, { 'y': 1, 'x': 2 }, { 'y': 2, 'x': 3 }, { 'y': 3, 'x': 2 }]) neighbors = board.get_neighbors(Coordinate(9, 99)) assert sorted([tile.flat() for tile in neighbors], key=coordinate_sort) == ([{ 'y': 99, 'x': 8 }, { 'y': 98, 'x': 9 }]) neighbors = board.get_neighbors(Coordinate(1000, 1000)) assert neighbors == []
def get_range_test(): board = MockBoard() cost_table = { 'plain': 1, } finder = PathFinder(board) path = finder.tiles_in_range(cost_table, Coordinate(0, 0), 3) json_path = [tile.as_json() for tile in path] assert json_path == [ '{"x": 1, "y": 0}', '{"x": 2, "y": 0}', '{"x": 3, "y": 0}' ] cost_table = { 'plain': 2, } path = finder.tiles_in_range(cost_table, Coordinate(0, 0), 4) json_path = [tile.as_json() for tile in path] assert json_path == ['{"x": 1, "y": 0}', '{"x": 2, "y": 0}'] path = finder.tiles_in_range(cost_table, Coordinate(0, 6), 2) json_path = [tile.as_json() for tile in path] assert json_path == ['{"x": 0, "y": 5}', '{"x": 1, "y": 6}'] path = finder.tiles_in_range(cost_table, Coordinate(0, 6), 4) json_path = [tile.as_json() for tile in path] assert json_path == [ '{"x": 0, "y": 4}', '{"x": 0, "y": 5}', '{"x": 1, "y": 5}', '{"x": 1, "y": 6}', '{"x": 2, "y": 6}' ] path = finder.tiles_in_range(cost_table, Coordinate(0, 0), 0) assert path == []
def canonicalize(self, message): if('type' in message and 'name' not in message): message['name'] = message['type'] del message['type'] for unit_key in ['unit', 'attacker', 'defender']: if unit_key in message: if isinstance(message[unit_key], dict): if('x' in message[unit_key] and 'y' in message[unit_key]): location = Coordinate(message[unit_key]['x'], message[unit_key]['y']) message[unit_key] = self.unit_at(location) elif('id' in message[unit_key]): message[unit_key] = self._find_unit(message[unit_key]['id']) if unit_key in ['attacker', 'unit']: if(message[unit_key].army != self.scenario.armies[self.playerNumber(message['playerId'])].name): raise Exception("Player not in control of army")
def comparison_test(): coord1 = Coordinate(5, 5) coord2 = Coordinate(5, 2) coord3 = Coordinate(2, 5) coord4 = Coordinate(5, 5) assert coord1 == coord4 assert coord1 != coord2 assert coord2 != coord3 assert coord3 != coord1 coords = [coord1, coord4, coord2, coord3] assert Coordinate(5, 5) in coords assert Coordinate(1, 5) not in coords
def get_neighbors(self, coordinate): neighbors = [] x = coordinate.x y = coordinate.y potential_neighbors = [ (x + 1, y), (x, y + 1), ] if(x > 0): potential_neighbors.append((x - 1, y)) if(y > 0): potential_neighbors.append((x, y - 1)) for neighbor in potential_neighbors: try: checkOnBoard = Coordinate(neighbor[0], neighbor[1]) if(self.is_on_board(checkOnBoard)): neighbors.append(checkOnBoard) except BadCoordinateCreation: pass return neighbors
def test_possible(): self.assertEqual( test_pawn.possible_moves(), [Coordinate(0, 4), Coordinate(1, 4)]) test_move(test_pawn, Coordinate(1, 4)) self.assertEqual(self.desk.desk[1][4].piece, test_pawn)
def test_pawn(self): self.desk.clear_desk() def test_move(piece, coordinate): self.desk.move_piece(None, piece, coordinate) self.assertEqual(Coordinate(piece.x, piece.y), coordinate) def test_start_move(): self.assertEqual( test_pawn.possible_moves(), [Coordinate(0, 2), Coordinate(0, 3)]) test_move(test_pawn, Coordinate(0, 3)) self.assertEqual(Coordinate(test_pawn.x, test_pawn.y), Coordinate(0, 3)) def test_possible(): self.assertEqual( test_pawn.possible_moves(), [Coordinate(0, 4), Coordinate(1, 4)]) test_move(test_pawn, Coordinate(1, 4)) self.assertEqual(self.desk.desk[1][4].piece, test_pawn) def test_defend(): self.assertEqual( test_pawn.possible_moves(defend=True), [Coordinate(1, 5), Coordinate(2, 5), Coordinate(0, 5)]) def test_black_start_move_and_last_move(): self.assertEqual( test_enemy_pawn_to_take_on_the_aisle.possible_moves(), [Coordinate(0, 5), Coordinate(0, 4)]) self.desk.set_last_move( Move(test_enemy_pawn_to_take_on_the_aisle, Coordinate(0, 4))) test_move(test_enemy_pawn_to_take_on_the_aisle, Coordinate(0, 4)) def test_take_on_the_aisle(): self.assertEqual( test_pawn.possible_moves(), [Coordinate(1, 5), Coordinate(0, 5)]) test_move(test_pawn, Coordinate(0, 5)) self.assertIsNone(self.desk.desk[0][4].piece) def test_transformation(): self.desk.move_piece(None, test_pawn, Coordinate(0, 7), 'Q') self.assertEqual(self.desk.desk[0][7].piece.reduction, 'Q') self.assertEqual(self.desk.desk[0][7].piece.colour, Colour.WHITE) test_pawn = self.desk.add_piece(Coordinate(0, 1), '', Colour.WHITE) test_enemy_pawn_to_take_on_the_aisle = \ self.desk.add_piece(Coordinate(0, 6), '', Colour.BLACK) self.desk.add_piece(Coordinate(1, 4), '', Colour.BLACK) self.desk.add_piece(Coordinate(2, 4), '', Colour.BLACK) test_start_move() test_possible() test_defend() test_black_start_move_and_last_move() test_take_on_the_aisle() test_move(test_pawn, Coordinate(0, 6)) test_transformation()
def create(self, name, coordinate=Coordinate(0, 0)): data = self.get_data(name) return self.creation_class(name, data['revenue'], data['buildable_units'], coordinate)
def test_take_on_the_aisle(): self.assertEqual( test_pawn.possible_moves(), [Coordinate(1, 5), Coordinate(0, 5)]) test_move(test_pawn, Coordinate(0, 5)) self.assertIsNone(self.desk.desk[0][4].piece)
def serializable_test(): coordinate = Coordinate(5, 5) assert coordinate.as_json() == '{"x": 5, "y": 5}'
def test_move(piece, coordinate): self.desk.move_piece(None, piece, coordinate) self.assertEqual(Coordinate(piece.x, piece.y), coordinate)
def test_defend(): self.assertEqual( test_pawn.possible_moves(defend=True), [Coordinate(1, 5), Coordinate(2, 5), Coordinate(0, 5)])
def test_history_of_moves(): self.game.desk.move_piece(self.game, self.game.desk.desk[0][1].piece, Coordinate(0, 2)) self.assertEqual(len(self.game.history_of_moves), 1)