示例#1
0
 def _build_command(self, player, command):
     unit = self._find_unit(command.unit)
     if command.command_type == CommandType.MOVE:
         return MoveCommand(player, unit, command.destination)
     if command.command_type == CommandType.SUPPORT:
         supported_unit = self._find_unit(command.source)
         return SupportCommand(player, unit, supported_unit,
                               command.destination)
     if command.command_type == CommandType.CONVOY_MOVE:
         return ConvoyMoveCommand(player, unit, command.destination)
     if command.command_type == CommandType.CONVOY_TRANSPORT:
         transported_unit = self._find_unit(command.source)
         return ConvoyTransportCommand(player, unit, transported_unit,
                                       command.destination)
     if command.command_type == CommandType.HOLD:
         return HoldCommand(player, unit)
     raise ValueError("Invalid command type: {}".format(
         command.command_type))
示例#2
0
def test_a_6__check_player_cannot_command_other_players_units():
    """
    (all troops belong to England)
    GERMANY: F London Coast -> North Sea
             A Liverpool Hold
             A Wales Support Liverpool Hold
             A Convoy Wales -> Picardy
             F English Channel Transport Wales -> Picardy
    """
    # Extended from original check: various command types added, since all should fail
    game_map = generate_map()
    starting_configuration = [
        {
            'territory_name': 'London Coast',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Liverpool',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'Wales',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'English Channel',
            'unit_type': UnitTypes.FLEET
        },
    ]
    england = Player("England", game_map, starting_configuration)
    germany = Player("Germany", game_map, [])

    with pytest.raises(AssertionError):
        MoveCommand(germany, england.units[0], 'North Sea')
    with pytest.raises(AssertionError):
        HoldCommand(germany, england.units[1])
    with pytest.raises(AssertionError):
        SupportCommand(germany, england.units[2], england.units[1],
                       'Liverpool')
    with pytest.raises(AssertionError):
        ConvoyMoveCommand(germany, england.units[2], 'Picardy')
    with pytest.raises(AssertionError):
        ConvoyTransportCommand(germany, england.units[3], england.units[2],
                               'Picardy')
def test_c_2__three_army_circular_movement_with_support():
    """
    TURKEY: F Ankara Coast -> Constantinople Coast
            A Constantinople -> Smyrna
            A Smyrna -> Ankara
            A Bulgaria Support Ankara Coast -> Constantinople Coast
    """
    game_map = generate_map()
    turkey_starting_configuration = [
        {
            'territory_name': 'Ankara Coast',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Constantinople',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'Smyrna',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'Bulgaria',
            'unit_type': UnitTypes.TROOP
        },
    ]
    turkey = Player("Turkey", game_map, turkey_starting_configuration)

    commands = [
        MoveCommand(turkey, turkey.units[0], 'Constantinople Coast'),
        MoveCommand(turkey, turkey.units[1], 'Smyrna'),
        MoveCommand(turkey, turkey.units[2], 'Ankara'),
        SupportCommand(turkey, turkey.units[3], turkey.units[0],
                       'Constantinople Coast'),
    ]
    result = resolve_turn(game_map, commands)
    assert result == {
        'Turkey': {
            Unit(UnitTypes.FLEET, 'Constantinople Coast'): None,
            Unit(UnitTypes.TROOP, 'Smyrna'): None,
            Unit(UnitTypes.TROOP, 'Ankara'): None,
            Unit(UnitTypes.TROOP, 'Bulgaria'): None,
        },
    }
示例#4
0
def test_b_4__support_to_unreachable_coast_allowed():
    """
    FRANCE: F Gascony Coast -> Spain North Coast
            F Marseilles Coast Support F Gascony Coast -> Spain North Coast
    ITALY:  F Western Mediterranean Sea -> Spain South Coast
    """
    game_map = generate_map()
    france_starting_configuration = [
        {
            'territory_name': 'Gascony Coast',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Marseilles Coast',
            'unit_type': UnitTypes.FLEET
        },
    ]
    france = Player("France", game_map, france_starting_configuration)

    italy_starting_configuration = [
        {
            'territory_name': 'Western Mediterranean Sea',
            'unit_type': UnitTypes.FLEET
        },
    ]
    italy = Player("Italy", game_map, italy_starting_configuration)

    commands = [
        MoveCommand(france, france.units[0], 'Spain North Coast'),
        SupportCommand(france, france.units[1], france.units[0],
                       'Spain North Coast'),
        MoveCommand(italy, italy.units[0], 'Spain South Coast'),
    ]
    result = resolve_turn(game_map, commands)
    assert result == {
        'France': {
            Unit(UnitTypes.FLEET, 'Spain North Coast'): None,
            Unit(UnitTypes.FLEET, 'Marseilles Coast'): None,
        },
        'Italy': {
            Unit(UnitTypes.FLEET, 'Western Mediterranean Sea'): None,
        }
    }
示例#5
0
def test_d_22__impossible_fleet_move_cannot_be_supported():
    """
    Adapted from DATC test, because the DATC test requires illegal moves to be
    specified, which this system prevents
    """
    game_map = generate_map()
    germany_starting_configuration = [
        {
            'territory_name': 'Kiel Coast',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Burgundy',
            'unit_type': UnitTypes.TROOP
        },
    ]
    germany = Player("Germany", game_map, germany_starting_configuration)

    with pytest.raises(AssertionError):
        SupportCommand(germany, germany.units[1], germany.units[0], 'Munich')
示例#6
0
def test_b_5__support_from_unreachable_coast_not_allowed():
    """
    FRANCE: F Marseilles Coast -> Gulf of Lyon
            F Spain North Coast Support F Marseilles Coast -> Gulf of Lyon
    """
    game_map = generate_map()
    france_starting_configuration = [
        {
            'territory_name': 'Marseilles Coast',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Spain North Coast',
            'unit_type': UnitTypes.FLEET
        },
    ]
    france = Player("France", game_map, france_starting_configuration)

    with pytest.raises(AssertionError):
        SupportCommand(france, france.units[1], france.units[0],
                       'Gulf of Lyon')
示例#7
0
def test_d_24__impossible_army_move_cannot_be_supported():
    """
    Adapted from DATC test, because the DATC test requires illegal moves to be
    specified, which this system prevents
    """
    game_map = generate_map()
    france_starting_configuration = [
        {
            'territory_name': 'Marseilles',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'Spain South Coast',
            'unit_type': UnitTypes.FLEET
        },
    ]
    france = Player("France", game_map, france_starting_configuration)

    with pytest.raises(AssertionError):
        SupportCommand(france, france.units[1], france.units[0],
                       'Gulf of Lyon')
示例#8
0
def test_d_34__support_targetting_own_area_not_allowed():
    """
    Adapted from DATC test, because the DATC test requires illegal moves to be
    specified, which this system prevents
    """
    game_map = generate_map()
    italy_starting_configuration = [
        {
            'territory_name': 'Prussia',
            'unit_type': UnitTypes.TROOP
        },
    ]
    italy = Player("Italy", game_map, italy_starting_configuration)

    russia_starting_configuration = [
        {
            'territory_name': 'Warsaw',
            'unit_type': UnitTypes.TROOP
        },
    ]
    russia = Player("Russia", game_map, russia_starting_configuration)

    with pytest.raises(AssertionError):
        SupportCommand(italy, italy.units[0], russia.units[0], 'Prussia')
示例#9
0
def test_b_6__support_can_be_cut_from_other_coast():
    """
    ENGLAND: F North Atlantic Ocean -> Mid-Atlantic Ocean
             F Irish Sea Supports North Atlantic Ocean -> Mid-Atlantic Ocean

    FRANCE:  F Mid-Atlantic Ocean Hold
             F Spain North Coast Support Mid-Atlantic Ocean Hold

    ITALY:   F Gulf of Lyon -> Spain South Coast
    """
    game_map = generate_map()
    england_starting_configuration = [
        {
            'territory_name': 'North Atlantic Ocean',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Irish Sea',
            'unit_type': UnitTypes.FLEET
        },
    ]
    england = Player("England", game_map, england_starting_configuration)

    france_starting_configuration = [
        {
            'territory_name': 'Mid-Atlantic Ocean',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Spain North Coast',
            'unit_type': UnitTypes.FLEET
        },
    ]
    france = Player("France", game_map, france_starting_configuration)

    italy_starting_configuration = [
        {
            'territory_name': 'Gulf of Lyon',
            'unit_type': UnitTypes.FLEET
        },
    ]
    italy = Player("Italy", game_map, italy_starting_configuration)

    commands = [
        MoveCommand(england, england.units[0], 'Mid-Atlantic Ocean'),
        SupportCommand(england, england.units[1], england.units[0],
                       'Mid-Atlantic Ocean'),
        HoldCommand(france, france.units[0]),
        SupportCommand(france, france.units[1], france.units[0],
                       'Mid-Atlantic Ocean'),
        MoveCommand(italy, italy.units[0], 'Spain South Coast'),
    ]
    result = resolve_turn(game_map, commands)
    assert result == {
        'England': {
            Unit(UnitTypes.FLEET, 'Mid-Atlantic Ocean'): None,
            Unit(UnitTypes.FLEET, 'Irish Sea'): None,
        },
        'France': {
            Unit(UnitTypes.FLEET, 'Mid-Atlantic Ocean'): {
                'English Channel',
                'Brest Coast',
                'Gascony Coast',
                'Portugal Coast',
                'Western Mediterranean Sea',
                'North Africa Coast',
            },
            Unit(UnitTypes.FLEET, 'Spain North Coast'): None,
        },
        'Italy': {
            Unit(UnitTypes.FLEET, 'Gulf of Lyon'): None,
        }
    }
示例#10
0
def test__several_commands():
    game_map = generate_map()
    starting_configuration = [
        {
            'territory_name': 'Ankara',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'Black Sea',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Budapest',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'Rumania Coast',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Ukraine',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'Moscow',
            'unit_type': UnitTypes.TROOP
        },
    ]
    player = Player("Turkey", game_map, starting_configuration)

    commands = [
        ConvoyMoveCommand(player, player.units[0], 'Sevastopol'),
        ConvoyTransportCommand(player, player.units[1], player.units[0],
                               'Sevastopol'),
        MoveCommand(player, player.units[2], 'Rumania'),
        MoveCommand(player, player.units[3], 'Sevastopol Coast'),
        SupportCommand(player, player.units[4], player.units[3],
                       'Sevastopol Coast'),
        MoveCommand(player, player.units[5], 'Sevastopol'),
    ]
    command_map = CommandMap(game_map, commands)

    assert command_map._home_map == {
        'Ankara': commands[0],
        'Black Sea': commands[1],
        'Budapest': commands[2],
        'Rumania': commands[3],
        'Ukraine': commands[4],
        'Moscow': commands[5],
    }
    assert command_map._attacker_map == {
        'Rumania': [commands[2]],
        'Sevastopol': [commands[3], commands[5]],
    }
    assert command_map._convoy_attacker_map == {
        'Sevastopol': [commands[0]],
    }
    assert command_map._transport_map == {
        ('Ankara', 'Sevastopol'): [commands[1]],
    }
    assert command_map._support_map == {
        ('Rumania', 'Sevastopol'): [commands[4]],
    }
def test_c_5__disrupted_circular_movement_due_to_dislodged_convoy():
    """
    AUSTRIA: A Trieste -> Serbia
             A Serbia -> Bulgaria

    TURKEY:  A Bulgaria -> Trieste (Convoy)
             F Aegean Sea Transports Bulgaria -> Trieste
             F Ionian Sea Transports Bulgaria -> Trieste
             F Adriatic Sea Transports Bulgaria -> Trieste

    ITALY:   F Naples Coast -> Ionian Sea
             F Tunis Coast Supports Naples Coast -> Ionian Sea
    """
    game_map = generate_map()

    austria_starting_configuration = [
        {
            'territory_name': 'Trieste',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'Serbia',
            'unit_type': UnitTypes.TROOP
        },
    ]
    austria = Player("Austria", game_map, austria_starting_configuration)

    turkey_starting_configuration = [
        {
            'territory_name': 'Bulgaria',
            'unit_type': UnitTypes.TROOP
        },
        {
            'territory_name': 'Aegean Sea',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Ionian Sea',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Adriatic Sea',
            'unit_type': UnitTypes.FLEET
        },
    ]
    turkey = Player("Turkey", game_map, turkey_starting_configuration)

    italy_starting_configuration = [
        {
            'territory_name': 'Naples Coast',
            'unit_type': UnitTypes.FLEET
        },
        {
            'territory_name': 'Tunis Coast',
            'unit_type': UnitTypes.FLEET
        },
    ]
    italy = Player("Italy", game_map, italy_starting_configuration)

    commands = [
        MoveCommand(austria, austria.units[0], 'Serbia'),
        MoveCommand(austria, austria.units[1], 'Bulgaria'),
        ConvoyMoveCommand(turkey, turkey.units[0], 'Trieste'),
        ConvoyTransportCommand(turkey, turkey.units[1], turkey.units[0],
                               'Trieste'),
        ConvoyTransportCommand(turkey, turkey.units[2], turkey.units[0],
                               'Trieste'),
        ConvoyTransportCommand(turkey, turkey.units[3], turkey.units[0],
                               'Trieste'),
        MoveCommand(italy, italy.units[0], 'Ionian Sea'),
        SupportCommand(italy, italy.units[1], italy.units[0], 'Ionian Sea'),
    ]
    result = resolve_turn(game_map, commands)
    assert result == {
        'Austria': {
            Unit(UnitTypes.TROOP, 'Trieste'): None,
            Unit(UnitTypes.TROOP, 'Serbia'): None,
        },
        'Turkey': {
            Unit(UnitTypes.TROOP, 'Bulgaria'): None,
            Unit(UnitTypes.FLEET, 'Aegean Sea'): None,
            Unit(UnitTypes.FLEET, 'Ionian Sea'): {
                'Apulia Coast',
                'Albania Coast',
                'Greece Coast',
                'Eastern Mediterranean Sea',
                'Tyrrhenian Sea',
            },
            Unit(UnitTypes.FLEET, 'Adriatic Sea'): None,
        },
        'Italy': {
            Unit(UnitTypes.FLEET, 'Ionian Sea'): None,
            Unit(UnitTypes.FLEET, 'Tunis Coast'): None,
        },
    }