class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("Wumpus") playfields = load_playfields_from_txt("maps.txt") playfields = [augment_shiver_and_breeze(playfield) for playfield in playfields] playfield = playfields[2] self.game_state = GameState(playfield, 1, 1) self.knowledge_base = KnowledgeBase(PLAYFIELD_SIZE) self.hud_widget = QWidget() hud_layout = QHBoxLayout() score_label = QLabel("score:") hud_layout.addWidget(score_label) self.score_widget = QLabel("0") hud_layout.addWidget(self.score_widget) self.hud_widget.setLayout(hud_layout) self.playfield_widget = PlayfieldWidget(PLAYFIELD_SIZE, "gfx/all/") self.game_state.visualize_in(self.playfield_widget) self.knowledge_base_widget = PlayfieldWidget(PLAYFIELD_SIZE, "gfx/all/") self.knowledge_base.visualize_in(self.knowledge_base_widget) self.playfields_container = QWidget() playfields_layout = QHBoxLayout() playfields_layout.addWidget(self.playfield_widget) playfields_layout.addWidget(self.knowledge_base_widget) self.playfields_container.setLayout(playfields_layout) #self.controller = ManualController(self.game_state, self.knowledge_base, self) self.controller = RuleBasedAi(self.game_state, self.knowledge_base, self) self.timer = QTimer(self) self.timer.timeout.connect(self.controller.make_step) self.timer.start(1000) #controller_thread = ControllerThread(self.controller) #controller_thread.start() #controller_thread.run() container = QWidget() vertical_layout = QVBoxLayout() vertical_layout.addWidget(self.hud_widget) #vertical_layout.addWidget(self.controller.controls_widget) vertical_layout.addWidget(self.playfields_container) container.setLayout(vertical_layout) self.setCentralWidget(container) def update_visualization(self): self.game_state.visualize_in(self.playfield_widget) self.knowledge_base.visualize_in(self.knowledge_base_widget) self.score_widget.setText(str(self.game_state.score))
def parse_state(state_input): try: state_json = json.loads(str(state_input)) except TypeError: state_json = state_input level = None players = [] adversaries = [] exit_locked = None try: if state_json[0][TYPE] != STATE: print('Invalid Args: Type is not state') return None input_json = state_json[0] name = state_json[1] coord = to_coord(state_json[2]) except (KeyError, IndexError): input_json = state_json level_input = input_json[LEVEL] players_input = input_json[PLAYERS] adversaries_input = input_json[ADVERSARIES] exit_locked = input_json[EXIT_LOCKED] level = parse_level(level_input)[LEVEL] players = [parse_actor(actor_input) for actor_input in players_input] adversaries = [parse_actor(actor_input) for actor_input in adversaries_input] state = GameState(level, players, adversaries, exit_locked) return {STATE: state, NAME: name if name else None, COORD: coord if coord else None}
def on_newgame(self): ret = self.mainAppWindow.gamestateController.unsaved_changes() if not ret == QMessageBox.Cancel: dialog = DialogNewGame(gamestate=self.model.gamestate,user_settings=self.model.user_settings) if dialog.exec_() == QDialog.Accepted: self.model.gamestate = GameState() self.mainAppWindow.chessboard_view.gs = self.model.gamestate self.mainAppWindow.moves_edit_view.gs = self.model.gamestate self.mainAppWindow.moves_edit_view.update() # strength is only changed if internal engine is used # otherwise the dialog is meaningless if(self.model.user_settings.active_engine == self.model.user_settings.engines[0]): self.model.gamestate.strength_level = dialog.slider_elo.value() self.model.gamestate.computer_think_time = dialog.think_ms self.mainAppWindow.moves_edit_view.on_statechanged() self.model.gamestate.initialize_headers() self.model.database.index_current_game = None self.mainAppWindow.save.setEnabled(True) if(dialog.rb_plays_white.isChecked()): self.mainAppWindow.play_white.setChecked(True) self.mainAppWindow.setLabels() self.mainAppWindow.modeMenuController.on_play_as_white() else: self.mainAppWindow.play_black.setChecked(True) root = self.model.gamestate.current.root() temp = root.headers["White"] root.headers["White"] = root.headers["Black"] root.headers["Black"] = temp self.mainAppWindow.setLabels() self.mainAppWindow.modeMenuController.on_play_as_black()
def apply_player_item_interaction(self, player, item_pos): state = self.gamestate exit_locked = state.exit_locked for room in state.current_level.rooms: room_items = [item.pos for item in room.items] if item_pos in room_items: item = next(item for item in room.items if item.pos == item_pos) if item.type == KEY: exit_locked = False room.items.remove(item) for p in self.players: if p.name == player.name: p.inventory.append(item) res = self.rc.validate_item_interaction(player, item_pos, state) if res: players = self.get_player_actors() adversaries = self.get_adversary_actors() self.gamestate = GameState(levels=state.levels, players=players, adversaries=adversaries, exit_locked=exit_locked, current_level=state.current_level, out_actors=state.out_actors) return res
def request_adversary_move(self, name, new_pos): '''Requests a move from an adversary. If the move is invalid returns False, otherwise it returns a dictionary with information on the move''' if len(self.player_turns) == 0: if name in self.adv_turns: adv = next(adv for adv in self.adversaries if adv.name == name) is_client = isinstance(adv, Adversary) players = self.get_player_actors() a_coords = self.get_adversary_coords(name) adv_move = self.rc.validate_adversary_movement( self.get_adversary_actor(name), new_pos, self.gamestate.current_level, players, a_coords) if adv_move[VALID_MOVE]: self.adversaries.remove(adv) updated_advs = self.get_adversary_actors() door_pos = self.__handle_door_traversal( new_pos, self.gamestate.current_level) if door_pos: new_pos = door_pos if adv_move[EJECT]: # Remove eliminated player self.gamestate.out_actors.append(adv_move[EJECT]) if is_client: adv.adversary_obj.pos = new_pos # print('NEW POS:', new_pos) updated_advs.append(adv.adversary_obj) else: adv.pos = new_pos updated_advs.append(adv) self.adversaries.append(adv) self.gamestate = GameState( levels=self.gamestate.levels, current_level=self.gamestate.current_level, players=players, adversaries=updated_advs, exit_locked=self.gamestate.exit_locked, out_actors=self.gamestate.out_actors) self.adv_turns.remove(name) if len(self.adv_turns) == 0: self.__reset_turns() return adv_move else: print('name not in adv turns') print(self.adv_turns) print(name) return False else: print('player turns not empty') print(self.player_turns) return False
def request_player_move(self, name, new_pos): '''Request a move from a player. If the move is invalid then returns false, otherwise it returns a dictionary with information of the move''' if len(self.player_turns) == len(self.adv_turns) == 0: self.__reset_turns() player = next(player for player in self.players if player.name == name) is_client = isinstance(player, Player) if is_client: player.pos = player.player_obj.pos if player.name in self.player_turns: p_coords = self.get_player_coords(player.name) adversaries = self.get_adversary_actors() player_move = self.rc.validate_player_movement( self.get_player_actor(player.name), new_pos, self.gamestate.current_level, p_coords, adversaries) if player_move[VALID_MOVE] and player_move[INFO].traversable: if player_move[EJECT]: # Remove eliminated adversary self.gamestate.out_actors.append(player_move[EJECT]) self.players.remove(player) updated_players = self.get_player_actors() # If the new position is a door/waypoint, then move the player to the corresponding location. door_pos = self.__handle_door_traversal( new_pos, self.gamestate.current_level) if door_pos: new_pos = door_pos if is_client: player.player_obj.pos = new_pos updated_players.append(player.player_obj) else: player.pos = new_pos updated_players.append(player) self.players.append(player) self.gamestate = GameState( levels=self.gamestate.levels, current_level=self.gamestate.current_level, players=updated_players, adversaries=adversaries, exit_locked=self.gamestate.exit_locked, out_actors=self.gamestate.out_actors) self.player_turns.remove(player.name) # If all players have gone update adversaries for their moves if len(self.player_turns) == 0: update_adversary_players(self.adversaries, self.get_player_coords()) return player_move return False
def gamestate1(level2): '''Initializes example gamestate''' players = [] adversaries = [] for i in range(3): p = PlayerActor('Bruh ' + str(i)) p.health = 3 players.append(p) a = AdversaryActor('Evil Bruh ' + str(i)) a.health = 3 adversaries.append(a) gs_info = create_initial_game_state(level2, players, adversaries) gamestate = GameState(level2, gs_info[0], gs_info[1]) return gamestate
def get_table(emitter=print): """Get an instance of Table""" heroes = [ hero_factory.hero('benji', 'barbarian'), hero_factory.hero('austin', 'healer'), ] # Draws the hero's initial hand for _ in range(0, 5): for hero in heroes: hero.draw_card() # Deal boss mat and door deck boss = doorcard_factory.create_boss() doordeck = doorcard_factory.deal_deck(boss.num_door_cards, len(heroes), boss) game = GameState(doordeck, heroes, logging=mylog) return Table(game, emitter, mylog)
def request_player_moves(self, new_players_locs): """ Updates player info based on player move. :param new_players_locs: [Coord] :param new_players_healths: [int] :return: """ for i in range(len(new_players_locs)): if self.rc.validate_player_movement(self.players[i].pos, new_players_locs[i], self.gamestate.current_level): self.gamestate = GameState(self.gamestate.current_level, self.players, self.adversaries, self.gamestate.exit_locked) else: self.players = self.gamestate.players
def __init_state(self, levels, start_level=0) -> GameState: '''Create and return a GameState from list of levels.''' try: level = levels[start_level] player_objs = self.get_player_actors() adv_objs = self.get_adversary_actors() except ValueError: print('Invalid Starting level') return self.gamestate except TypeError: # Single level case level = levels player_objs = self.players adv_objs = self.adversaries except AttributeError: adv_objs = [a.adversary_obj for a in self.adversaries] player_objs = [a.player_obj for a in self.players] gs_info = create_initial_game_state(level, player_objs, adv_objs) update_adversary_levels(self.adversaries, level) return GameState(levels, gs_info[0], gs_info[1], current_level=level)
def gamestate1(level1): '''Initializes example gamestate''' gs_info = create_initial_game_state(level1, 3, 3) gamestate = GameState(gs_info[0], gs_info[1]) return gamestate