Beispiel #1
0
    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
Beispiel #2
0
    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
Beispiel #3
0
    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
Beispiel #4
0
 def __set(self, path, value):
     if path == "":
         self.__state = value
         return
     util.path_set(path, value, self.__state)
Beispiel #5
0
 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
Beispiel #6
0
 def focus_controls(Self, state, _):
     # if util.get("game.controls_focused", state):
     #     return None
     util.path_set("game.controls_focused", True, state)
     return state