def gameplay_loop(): """ Run the game. Continuously loop the gameplay until the user wins or quits. :postcondition: the game will be over once this function terminates. :return: none """ current_position = [2, 2] # starting position victory_reached = False while not victory_reached: describe_room(current_position) describe_character(PLAYER) move(current_position) if current_position == VICTORY_ROOM: # quit victory_reached = True current_monster = GAME_BOARD[current_position[0]][current_position[1]][ 'monster'] # get monster from matrix monster_encounter(current_monster) print(DIVIDING_LINE) victory(current_position)
def run_ai(self): acting_ai = [] for character in self.field.characters: if character.ai and character.player == self.field.turn and not character.done: acting_ai.append(character) character.ai.build_priority_list() def compare_character_priority(character): if len(character.ai.priority_list) > 0: return character.ai.priority_list[0][0] else: return 0 acting_ai.sort(key=compare_character_priority, reverse=True) #Second pass for character in acting_ai: if self.field.phase == 0: character.ai.build_priority_list() #Update list every time if len(character.ai.priority_list) > 0: dest_x, dest_y = character.ai.priority_list[0][ 3], character.ai.priority_list[0][4] character.move(dest_x, dest_y) character.ai.priority_list[0][1].add_to_queue() character.direction = character.ai.priority_list[0][2] character.done = True #AI does not use combos yet else: character.ai.noncombat_act( ) #Custom function for when nothing is in range if not self.field.turn: self.next_phase()
def action(self): for character in self.characters: self.story += character.move() self.story += character.act() # and make sure setting object knows where characters are now self.setting.update_occupants(self.characters) # if characters are in the same room, trigger dialogue: for characters in self.setting.occupants.values(): if len(characters) > 1: r.shuffle(characters) self.story += characters[0].speak(characters[1], line=1) self.story += characters[1].speak(characters[0], line=2) if len(characters) > 2: self.story += characters[2].speak(characters[2], line=3) #3rd character ignored by other 2 (temporarily?)
def play_game(): """ Process the game play loop. POST-CONDITION make appropriate changes to character and monster according to events """ print_introduction() map.print_map(character.get_coordinates()) while True: player_input = input("YOUR COMMAND, CAPTAIN: ") # Quit if player_input.strip().lower() == 'quit': quit_game() # Restart if player_input.strip().lower() == 'restart': restart_game() break # Move if character.move(player_input.strip().lower()): game_event()
def test_move_invalid_input(self, mock_stdout): character.move("bad input") self.assertTrue("INVALID COMMAND" in mock_stdout.getvalue())
def test_move_out_of_bound_return(self): character.set_coordinates(1, 4) self.assertEqual(False, character.move("west"))
def test_move_out_of_bound(self, mock_stdout): character.set_coordinates(1, 4) character.move("west") self.assertTrue("OUT OF BOUND" in mock_stdout.getvalue())
def test_move_valid_return(self): character.set_coordinates(10, 10) self.assertEqual(True, character.move("north"))
def test_move_east_valid(self): character.set_coordinates(10, 10) character.move("east") self.assertEqual((11, 10), character.coordinates)
def test_move_south_valid(self): character.set_coordinates(10, 10) character.move("south") self.assertEqual((10, 11), character.coordinates)
def test_move_invalid_input_return(self): self.assertEqual(False, character.move("bad input"))
def test_move_north(self, mock_input_loop): self.assertEqual(move([2, 2]), [1, 2])
def test_output_dead_end(self, mock_stdout, mock_input_loop): expected_output = 'You\'ve reached a dead end! Best not to continue past here ' \ 'or you will likely be eaten by a grue...\n' move([0, 0]) self.assertEqual(mock_stdout.getvalue(), expected_output)
def test_move_west(self, mock_input_loop): self.assertEqual(move([2, 2]), [2, 1])
def test_move_south(self, mock_input_loop): self.assertEqual(move([2, 2]), [3, 2])