def new_game(Self, state, _): util.path_set("game.board", util.get("game.board", initial_state), state) util.path_set("game.player_turn", 1, state) util.path_set("game.controls_state", 1, state) util.path_set("game.game_started", False, state) util.path_set("game.winner", 0, state) return state
def change_active_column(Self, state, column): # returning none skips the redraw (nothing is changing) if (util.get("game.active_column", state) == column and util.get("game.controls_focused", state) == False): return None # fine because assigning a literal util.path_set("game.active_column", column, state) util.path_set("game.controls_focused", False, state) return state
def drop_piece(Self, state, column): if not util.get("game.game_started", state): return state board = util.get("game.board", state) player = util.get("game.player_turn", state) if not logic.valid_move(column, board): return None util.path_set("game.board", logic.drop_piece(player, column, board), state) util.path_set("game.player_turn", 1 if player == 2 else 2, state) new_board = util.get("game.board", state) winner = logic.check_win(new_board) valid_moves = logic.valid_moves(new_board) if winner or not len(valid_moves): util.path_set("game.winner", winner if winner else 3, state) util.path_set("game.game_started", False, state) if (not util.get("game.2_player_game", state) and util.get("game.player_turn", state) == 2): move = ai.generate_move(new_board) if move == -1: return None Self.drop_piece(state, move) return state
def __set(self, path, value): if path == "": self.__state = value return util.path_set(path, value, self.__state)
def set_game_mode(self, state, two_player): util.path_set("game.2_player_game", two_player, state) util.path_set("game.controls_state", 0, state) util.path_set("game.game_started", True, state) return state
def focus_controls(Self, state, _): # if util.get("game.controls_focused", state): # return None util.path_set("game.controls_focused", True, state) return state