コード例 #1
0
def everything_within_given_distance_on(the_map, distance, position):
    """Iterates through everything on the_map.bldngs_n_rsrcs that is
    within the given distance of position.

    This portion of the_map is in the shape of a diamond.

    position must be of type Position
    distance must be a non-negative int"""
    if not type(distance) is int:
        print('distance must be an int')
        return
    if distance < 0:
        print('distance must not be negative')
        return

    # The following is the center of the diamond-shaped portion of the map.
    x0, y0 = position.value

    # The following iterates through the diamond shaped portion of the_map
    # from bottom to top, left to right.
    for y_delta in range(-1 * distance, distance + 1):
        sign_to_mult_by = 1 if y_delta <= 0 else -1
        horizontal_radius = distance + y_delta * sign_to_mult_by
        # height is how far up or down the map, we need to go
        for x_delta in range(-1 * horizontal_radius, horizontal_radius + 1):
            x = x0 + x_delta
            y = y0 + y_delta
            this_position = Position(x, y)
            if this_position.is_on_the_map(the_map):
                yield the_map.bldngs_n_rsrcs[y][x]
コード例 #2
0
    def str_char_at(self, x, y):
        """return the string character to be printed

        This is for what is at Position(x,y) on the map."""
        position = Position(x, y)
        if self.has_unit_at(position):
            player_num = self.get_player_num_for_unit_at(position)
            color = get_color_from_player_num(player_num)
            num_villagers = self.num_villagers_at(position)
            if num_villagers >= 1:
                to_print = color + 'v' + Color.ENDC
                if num_villagers >= 2:
                    to_print = Color.BOLD + to_print
                if num_villagers >= 4:
                    to_print = Color.UNDERLINE + to_print
            else:
                unit = self(position, units=True)
                if isinstance(unit, set):
                    # This should never happen
                    print("Error! The supposed unit was a set instead.")
                    to_print = 'u' + Color.ENDC
                else:
                    to_print = color + unit.letter_abbreviation + Color.ENDC
            if self.has_resource_at(position):
                to_print = Color.BACK_GRAY + to_print
        else:
            if self.has_resource_at(position):
                to_print = Color.BACK_GRAY + str(
                    self.bldngs_n_rsrcs[y][x]) + Color.ENDC
            else:
                to_print = str(self.bldngs_n_rsrcs[y][x])

        return to_print
コード例 #3
0
def get_position_from_inpt_as_ls(inpt_as_ls):
    """Return None or an instance of Position.

    This assumes the last two entries of inpt_as_ls are the coordinates
    of a position on the map."""
    if len(inpt_as_ls) < 2:
        return

    x = str_to_int(inpt_as_ls[-2])
    y = str_to_int(inpt_as_ls[-1])
    if x is None or y is None:
        return

    return Position(x, y)
コード例 #4
0
        initial_position = initial_position_of_player(i, game_map)
        players.append(
            Player(number=i, position=initial_position, is_human=False))

    for player in players[1:]:
        print("Is player number {} human? {}".format(player.number,
                                                     player.is_human))


if __name__ == '__main__':
    # Testing the neighbors method of the Position class
    from map_etc.position import Position

    def print_neighbors_of(position):
        for neighbor in position.neighbors(game_map):
            print(neighbor)

    position = Position(80, 79)
    print("Before any buildings are built, here are neighbors of")
    print("position ", position)
    print_neighbors_of(position)

    initialize_players()
    print("After initializing the players, here are the neighbors:")
    print_neighbors_of(position)

    P = Position
    for position in (P(76, 79), P(77, 79), P(76, 84), P(81, 82), P(78, 84)):
        print("The neighbors of ", position, " are ")
        print_neighbors_of(position)
        print()
コード例 #5
0
#Here are some tests for the Building class (and subclasses)

from player import Player
from map_etc.position import Position
from map_etc.make_map import game_map
from buildings.other_bldngs import TownCenter, House

position = Position(80, 80)
player = Player(1, position, True)
towncenter = TownCenter(1, position, player)
towncenter.build_on_map(Position(80, 80), game_map)

for tpl in ((4, 4), (2, 4), (99, 0), (100, 0), (90, 96), (90, 97), (10, 0),
            (10, -1)):
    position = Position(*tpl)
    yes_or_no = towncenter.can_build_on_map(position, game_map)
    print(position, yes_or_no)
    print()

for tpl in ((82, 80), (81, 80), (76, 80), (77, 80), (80, 84), (80, 83)):
    position = Position(*tpl)
    house = House(1, position, player)
    yes_or_no = house.can_build_on_map(position, game_map)
    print(position, yes_or_no)
    print()

print(game_map)
コード例 #6
0
#         yield current
#
# def up_right(center, distance):
#     """start at the bottom and go up to the right
#     but stop before the far right point."""
#     start = center + Vector(0,-distance)
#     for i in range(distance):
#         current = start + Vector(i, i)
#         if not current.is_on_the_map(game_map):
#             continue
#         yield current
#
# def up_left(center, distance):
#     """start at the far right and go up to the left
#     but stop before the top point."""
#     start = center + Vector(distance, 0)
#     for i in range(distance):
#         current = start + Vector(-i, i)
#         if not current.is_on_the_map(game_map):
#             continue
#         yield current

if __name__ == '__main__':
    # Testing the function positions_around
    from time import sleep
    center = Position(70, 70)
    for position in positions_around(center, radius=4):
        x, y = position.value
        game_map.bldngs_n_rsrcs[y][x] = '*'
        game_map.print_centered_at(center, width=20, height=20)
        sleep(.2)
コード例 #7
0
from resources import Wood, Stone, Food, Gold, Bronze, Iron
from map_etc.game_map import GameMap
from map_etc.position import Position

# Eventually, I should create a function or class that makes a random map
game_map = GameMap(width=100, height=100)
for i in range(60, 66):
    for j in range(75, 85):
        game_map.bldngs_n_rsrcs[i][j] = Wood(Position(j, i))

# Eh, I feel like adding more wood:
for i in range(66, 72):
    for j in range(80, 90):
        game_map.bldngs_n_rsrcs[i][j] = Wood(Position(j, i))

# Filling the bottom left corner with wood:
for i in range(30):
    for j in range(30 - i):
        game_map.bldngs_n_rsrcs[i][j] = Wood(Position(j, i))

# Filling the top left corner with wood:
for i in range(80, 100):
    for j in range(i - 80):
        game_map.bldngs_n_rsrcs[i][j] = Wood(Position(j, i))

# Filling the bottom right corner with wood:
for i in range(30):
    for j in range(70 + i, 100):
        game_map.bldngs_n_rsrcs[i][j] = Wood(Position(j, i))

# Filling the top right corner with wood:
コード例 #8
0
    # I may eventually change this function so that each player has their own copy of the map,
    # and this would then print their copy. In this case, the variable player would be used.

    position = get_position_from_inpt_as_ls(inpt_as_ls)
    if not position:
        return

    make_map_module.game_map.print_centered_at(position)
    return


if __name__ == '__main__':
    from player import Player
    from map_etc.position import Position

    p1 = Player(1, Position(80, 80), is_human=True)

    # Testing the function print_part_of_map
    inpt_as_ls = ['print', 'map', '80', '80']
    print_part_of_map(p1, inpt_as_ls)
    print(
        "----------------------------------------------------------------------------------"
    )
    inpt_as_ls = ['print', 'map', 'n10', 'e15']
    print_part_of_map(p1, inpt_as_ls)
    inpt_as_ls = ['print', 'map', 'blah', 'yeah']
    print_part_of_map(p1, inpt_as_ls)
    print(
        "----------------------------------------------------------------------------------"
    )
    inpt_as_ls = ['print', 'map', 'centered', 'on', '40', '60']
コード例 #9
0
# breadth first search
def bfs_for_open_spot(unit, game_map, bound_on_search_distance=12):
    """Return Vector or None

    If returning a Vector, this is the smallest vector that unit
    can move by.

    Also, see the comment at the top of the file."""
    queue = deque()
    positions_found = set()
    start_position = unit.position
    queue.append(start_position)
    positions_found.add(start_position)
    while queue:
        position = queue.popleft()
        delta = position - start_position
        if unit.can_move(delta, game_map):
            return delta
        if distance(start_position, position) > bound_on_search_distance:
            return
        for next in position.neighbors(game_map):
            if next not in positions_found:
                positions_found.add(next)
                queue.append(next)


if __name__ == '__main__':
    from map_etc.position import Position

    position = Position(80, 80)
コード例 #10
0
            player.messages += 'from the farm he is listed as currently farming.'
            return
        if self not in the_farm.current_farmers:
            # This should never happen.
            player.messages += 'Error! {} '.format(self)
            player.messages += 'was commanded to farm from a farm the he was\n'
            player.messages += 'not listed as a farmer.'
            return
        player.resources[Food] += self.food_from_farming_per_turn


if __name__ == '__main__':
    from map_etc.position import Position, Vector
    from player import Player

    p1 = Player(1, Position(80, 80), is_human=True)
    towncenter = p1.buildings['towncenter'][1]

    print("WARNING!!!! These tests will currently fail and so are commented out.")
    print("New units are now created with one argument being the building")
    print("that builds them.")
    v1 = Villager(towncenter, p1, Position(50, 50))
    print(v1.number, v1.position)
    v1.move_by(Vector(5, 8), game_map)
    print(v1.number, v1.position)

    v2 = Villager(towncenter, p1, Position(5, 5))
    for tpl in ((-5, -5), (7, 8), (-4, 11), (3, 6)):
        delta = Vector(*tpl)
        assert v2.can_move(delta, game_map)
    print(v2.position)
コード例 #11
0
    cost = Resources({Wood: 150, Gold: 20})
    size = (3, 3)
    letter_abbreviation = 'M'
    kind = 'market'
    time_to_build = 50


building_kind_to_class =  {'house':House, 'lumbercamp':LumberCamp,
                           'stonequarry':StoneQuarry,
                           'miningcamp':MiningCamp, 'woodwall':WoodWall,
                           'barracks':Barracks,
                           'farm':Farm, 'stonewall':StoneWall,
                           'wallfortification':WallFortification,
                           'tower':Tower, 'archeryrange':ArcheryRange,
                           'siegeworks':SiegeWorks,
                           'blacksmith':Blacksmith, 'market':Market,
                           'towncenter':TownCenter,
                           'castle':Castle, 'stable':Stable, 'library':Library,
                           'wonder':Wonder}


if __name__ == '__main__':
    from map_etc.position import Position
    from player import Player

    p1 = Player(1, Position(80, 80), is_human=True)
    t = TownCenter(1, Position(50, 50), p1)
    print(t.cost)
    print(t.number)
    print(t.position)
コード例 #12
0
from player import Player
from map_etc.position import Position, Vector
from input_handling.get_input import get_next_command
from buildings.resource_bldngs import Farm
from command_handling.strings import NOW, LATER, BUILD_BUILDING, BUILD_UNIT
from command_handling.strings import COLLECT_RESOURCE, MOVE, RESEARCH, FARM

from insert_commands import insert_command

p1 = Player(1, Position(80, 80), is_human=True)


def given_this_input_insert_command(inpt, sel_obj=None, player=p1):
    """Given this input, insert the command specified by the input"""
    command = get_next_command(player, inpt, sel_obj)
    insert_command(p1, command)


# The following tests were mostly done by first running them to see what
# they'd get and then creating a test that just states the results I just
# saw. (My purpose here is to check that when editing this file, the behavior
# of this module doesn't change.)
villagers = p1.units['villagers']
villager1 = villagers[1]
villager2 = villagers[2]
villager3 = villagers[3]

################################################################
# Checking the function insert_move_command
vector1 = Vector(-2, 5)
given_this_input_insert_command('move villager 1 n2 e5')
コード例 #13
0
    """This is a placeholder to be used until a building is completely built."""
    def __init__(self, bldng_class, number, position, player):
        self.player = player
        self.number = number
        self.position = position
        self.progress_to_construction = 0
        self.time_to_build = bldng_class.time_to_build
        self.kind = bldng_class.kind

    def __str__(self):
        kind = self.kind.capitalize()
        message = '{} {} at position {}'.format(kind, self.number,
                                                self.position)
        progress = self.progress_to_construction
        total = self.time_to_build
        message += " under construction with progress {}/{}".format(
            progress, total)
        return message


if __name__ == '__main__':
    from player import Player
    from map_etc.position import Position

    p1 = Player(1, Position(80, 80), is_human=True)

    bldng = Building(1, Position(70, 80), p1)
    print("Ok folks...printing...")
    print(bldng.letter_abbreviation)
    print("Finished printing.")