2048

def main():
    def init():
        #重置游戏棋盘
        return 'game'
    
    def not_game(state):
        #画出game over或者win的界面
        #读取用户输入得到action,判断是重启游戏还是结束游戏
        responses = defaultdict(lambda:state)#默认当前状态,没有行为会一直循环
        responses
        
    def game():
        #画出棋盘当前状态
        #读取用户输入得到action
        if action == 'restart':
            return 'init'
        if action == 'exit':
            return 'exit'
        #如果成功移动了一步
            if is_win():
                return 'win'
            if is_gameover():
                return 'gameover'
        return 'game'
    
    state_actions = {
        'init':init,
        'win':lambda:not_game('win'),
        'gameover':lambda:not_game('gameover'),
        'game':game
    }
    
    while state != 'exit':
        state = state_actions[state]()

2048主逻辑

 

posted @ 2017-12-03 22:01  张艳晖  阅读(134)  评论(0)    收藏  举报