def start_game(self): for i in range(len(self.player_names)): player = Player(self.player_names[i]) self.game_board.register_player(player) self.players.append(player) # game loop logger.info("Starting new game")
def try_place_figure(self, player): if self.roll_d6() == 6: # place new figure player.place_figure(self.game_board) # move figure #player.move_figure(self.game_board, self.roll) logger.info("Player {} placed new figure".format(player.name)) return True return False
def start_player_turn(self, player): player.turns += 1 logger.info("Player {}, Turn {}:".format(player.name, player.turns)) logger.debug( "Player data before turn: start figures: {}, finished figures: {}" .format(reveal_name(player.start_figures), reveal_name(player.finished_figures))) logger.info(self.game_board.display_board()) # grab players figures from cemetery player.grab_figures_from_cemetery(self.game_board) print("Player {}, Turn {}:".format(player.name, player.turns))
def place_figure(self, board): """ method to place a new figure to the start position of a board """ start_pos = board.players_start_pos[self.no] # in case start position blocked if hasattr(board.fields[start_pos], "name"): # remove foreign player figure board.figure_cemetery.append(board.fields[start_pos]) logger.info( "Target field is blocked by foreign player! Banning figure {}!" .format(board.fields[start_pos].name)) removed_figure = self.start_figures.pop() removed_figure.place(board) board.fields[start_pos] = removed_figure
def register_player(self, player): """ method to register a new player """ # only register unknown players if player.id not in self.players: # register player id player.id = player.name self.players.append(player.id) # set player number player.no = len(self.players) - 1 # register player start pos self.players_start_pos.append(self.next_start_index) # increase start index for next player to register self.next_start_index += int(self.field_amount / self.player_amount) logger.info("registered player {}".format(player.id))
def __game_flow_demo(self): self.start_game() while self.no_winner: for player in self.players: self.start_player_turn(player) if player.has_figures_on_board(self.game_board): roll = self.roll_d6() if roll == 6 and len(player.start_figures) != 0: ### PLAYER CAN DECIDE HERE player_wants_moving = True if player_wants_moving: player.move_figure(self.game_board, roll) else: player.place_figure(self.game_board) else: player.move_figure(self.game_board, roll) # player has no figure on board else: # three chances to roll a 6 for i in range(3): if self.try_place_figure(player): break if (self.is_player_winner(player)): logger.info( "Player {} won the game after {} turns!".format( player.name, player.turns)) break logger.debug( "Player data after turn: start figures: {}, finished figures: {}" .format(reveal_name(player.start_figures), reveal_name(player.finished_figures))) # debug output of board fields logger.debug("Board fields after turn: {}".format( reveal_name(self.game_board.fields)))
def move_figure(self, board, move_amount, selected_figure): """ method to select and move a player figure """ # select figure # figure = self.figures[int(selected_figure)-1] figure = self.find_figure_by_name(selected_figure) for field in board.fields: if hasattr(field, "name"): if figure.name in field.name: # set figure figured = field # get field of figure figured.field = board.fields.index(figured) # check if figure can finish if figured.distance_to_target <= move_amount: logger.debug( "figure distance to target: [{}] is lower or equal move_amount: [{}]" .format(figured.distance_to_target, move_amount)) # determine free slots in finished_figures free_slots = [] for i in range(len(self.finished_figures)): if self.finished_figures[i] == "0": free_slots.append(i) logger.debug("Slot {} is free".format(i)) for slot in free_slots: if abs(figured.distance_to_target - move_amount) == slot: figured.finish_slot = slot logger.debug( "Figure finish slot = [{}]".format( figured.finish_slot)) else: # calc target field of figure figured.target_field = figured.field + move_amount # handle field loop if figured.target_field > board.field_amount - 1: diff = board.field_amount - figured.field figured.target_field = move_amount - diff if figure != "undefined": # check if figure has possible finish slot if figure.finish_slot != -1: self.finish_figure(board, figure) else: # remove figure from old field board.fields[figure.field] = "0" # check if new field is blocked by another figure if hasattr(board.fields[figure.target_field], "name"): # check if blocking figure owned by player if self.name in board.fields[figure.target_field].name: # keep player figure on old field board.fields[figure.field] = figure logger.info( "Target field is blocked by player's own figure {}! Revert move!" .format(board.fields[figure.target_field].name)) else: # remove foreign player figure board.figure_cemetery.append( board.fields[figure.target_field]) logger.info( "Target field is blocked by foreign player! Banning figure {}!" .format(board.fields[figure.target_field].name)) # move player figure to new field figure.move(move_amount) board.fields[figure.target_field] = figure else: # move player figure to new field figure.move(move_amount) board.fields[figure.target_field] = figure
def finish_figure(self, board, figure): board.fields[figure.field] = "0" self.finished_figures[figure.finish_slot] = figure logger.info("Player {} reached the finish with figure {}!".format( self.name, figure.name))