建立遊戲檔案 2048.py
首先匯入需要的包:
1 2 3 |
import curses from random import randrange, choice from collections import defaultdict |
主邏輯
使用者行為
所有的有效輸入都可以轉換為”上,下,左,右,遊戲重置,退出”這六種行為,用 actions
表示
1 |
actions = ['Up', 'Left', 'Down', 'Right', 'Restart', 'Exit'] |
有效輸入鍵是最常見的 W(上),A(左),S(下),D(右),R(重置),Q(退出),這裡要考慮到大寫鍵開啟的情況,獲得有效鍵值列表:
1 |
letter_codes = [ord(ch) for ch in 'WASDRQwasdrq'] |
將輸入與行為進行關聯:
1 |
actions_dict = dict(zip(letter_codes, actions * 2)) |
狀態機
處理遊戲主邏輯的時候我們會用到一種十分常用的技術:狀態機,或者更準確的說是有限狀態機(FSM)
你會發現 2048 遊戲很容易就能分解成幾種狀態的轉換。
state
儲存當前狀態, state_actions
這個詞典變數作為狀態轉換的規則,它的 key 是狀態,value 是返回下一個狀態的函式:
- Init: init()
- Game
- Game: game()
- Game
- Win
- GameOver
- Exit
- Win: lambda: not_game(‘Win’)
- Init
- Exit
- Gameover: lambda: not_game(‘Gameover’)
- Init
- Exit
- Exit: 退出迴圈
狀態機會不斷迴圈,直到達到 Exit 終結狀態結束程式。
下面是經過提取的主邏輯的程式碼,會在後面進行補全:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
def main(stdscr): def init(): #重置遊戲棋盤 return 'Game' def not_game(state): #畫出 GameOver 或者 Win 的介面 #讀取使用者輸入得到action,判斷是重啟遊戲還是結束遊戲 responses = defaultdict(lambda: state) #預設是當前狀態,沒有行為就會一直在當前介面迴圈 responses['Restart'], responses['Exit'] = 'Init', 'Exit' #對應不同的行為轉換到不同的狀態 return responses[action] def game(): #畫出當前棋盤狀態 #讀取使用者輸入得到action if action == 'Restart': return 'Init' if action == 'Exit': return 'Exit' #if 成功移動了一步: if 遊戲勝利了: return 'Win' if 遊戲失敗了: return 'Gameover' return 'Game' state_actions = { 'Init': init, 'Win': lambda: not_game('Win'), 'Gameover': lambda: not_game('Gameover'), 'Game': game } state = 'Init' #狀態機開始迴圈 while state != 'Exit': state = state_actions[state]() |
使用者輸入處理
阻塞+迴圈,直到獲得使用者有效輸入才返回對應行為:
1 2 3 4 5 |
def get_user_action(keyboard): char = "N" while char not in actions_dict: char = keyboard.getch() return actions_dict[char] |
矩陣轉置與矩陣逆轉
加入這兩個操作可以大大節省我們的程式碼量,減少重複勞動,看到後面就知道了。
矩陣轉置:
1 2 |
def transpose(field): return [list(row) for row in zip(*field)] |
矩陣逆轉(不是逆矩陣):
1 2 |
def invert(field): return [row[::-1] for row in field] |
建立棋盤
初始化棋盤的引數,可以指定棋盤的高和寬以及遊戲勝利條件,預設是最經典的 4×4~2048。
1 2 3 4 5 6 7 8 |
class GameField(object): def __init__(self, height=4, width=4, win=2048): self.height = height #高 self.width = width #寬 self.win_value = 2048 #過關分數 self.score = 0 #當前分數 self.highscore = 0 #最高分 self.reset() #棋盤重置 |
棋盤操作
隨機生成一個 2 或者 4
1 2 3 4 |
def spawn(self): new_element = 4 if randrange(100) > 89 else 2 (i,j) = choice([(i,j) for i in range(self.width) for j in range(self.height) if self.field[i][j] == 0]) self.field[i][j] = new_element |
重置棋盤
1 2 3 4 5 6 7 |
def reset(self): if self.score > self.highscore: self.highscore = self.score self.score = 0 self.field = [[0 for i in range(self.width)] for j in range(self.height)] self.spawn() self.spawn() |
一行向左合併
(注:這一操作是在 move 內定義的,拆出來是為了方便閱讀)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
def move_row_left(row): def tighten(row): # 把零散的非零單元擠到一塊 new_row = [i for i in row if i != 0] new_row += [0 for i in range(len(row) - len(new_row))] return new_row def merge(row): # 對鄰近元素進行合併 pair = False new_row = [] for i in range(len(row)): if pair: new_row.append(2 * row[i]) self.score += 2 * row[i] pair = False else: if i + 1 < len(row) and row[i] == row[i + 1]: pair = True new_row.append(0) else: new_row.append(row[i]) assert len(new_row) == len(row) return new_row #先擠到一塊再合併再擠到一塊 return tighten(merge(tighten(row))) |
棋盤走一步
通過對矩陣進行轉置與逆轉,可以直接從左移得到其餘三個方向的移動操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def move(self, direction): def move_row_left(row): #一行向左合併 moves = {} moves['Left'] = lambda field: [move_row_left(row) for row in field] moves['Right'] = lambda field: invert(moves['Left'](invert(field))) moves['Up'] = lambda field: transpose(moves['Left'](transpose(field))) moves['Down'] = lambda field: transpose(moves['Right'](transpose(field))) if direction in moves: if self.move_is_possible(direction): self.field = moves[direction](self.field) self.spawn() return True else: return False |
判斷輸贏
1 2 3 4 5 |
def is_win(self): return any(any(i >= self.win_value for i in row) for row in self.field) def is_gameover(self): return not any(self.move_is_possible(move) for move in actions) |
判斷能否移動
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def move_is_possible(self, direction): defrow_is_left_movable(row): def change(i): if row[i] == 0 and row[i + 1] != 0: # 可以移動 return True if row[i] != 0 and row[i + 1] == row[i]: # 可以合併 return True return False return any(change(i) for i in range(len(row) - 1)) check = {} check['Left'] = lambda field: any(row_is_left_movable(row) for row in field) check['Right'] = lambda field: check['Left'](invert(field)) check['Up'] = lambda field: check['Left'](transpose(field)) check['Down'] = lambda field: check['Right'](transpose(field)) if direction in check: return check[direction](self.field) else: return False |
繪製遊戲介面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
def draw(self, screen): help_string1 = '(W)Up (S)Down (A)Left (D)Right' help_string2 = ' (R)Restart (Q)Exit' gameover_string = ' GAME OVER' win_string = ' YOU WIN!' def cast(string): screen.addstr(string + 'n') #繪製水平分割線 def draw_hor_separator(): line = '+' + ('+------' * self.width + '+')[1:] separator = defaultdict(lambda: line) if not hasattr(draw_hor_separator, "counter"): draw_hor_separator.counter = 0 cast(separator[draw_hor_separator.counter]) draw_hor_separator.counter += 1 def draw_row(row): cast(''.join('|{: ^5} '.format(num) if num > 0 else '| ' for num in row) + '|') screen.clear() cast('SCORE: ' + str(self.score)) if 0 != self.highscore: cast('HGHSCORE: ' + str(self.highscore)) for row in self.field: draw_hor_separator() draw_row(row) draw_hor_separator() if self.is_win(): cast(win_string) else: if self.is_gameover(): cast(gameover_string) else: cast(help_string1) cast(help_string2) |
完成主邏輯
完成以上工作後,我們就可以補完主邏輯了!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
def main(stdscr): def init(): #重置遊戲棋盤 game_field.reset() return 'Game' def not_game(state): #畫出 GameOver 或者 Win 的介面 game_field.draw(stdscr) #讀取使用者輸入得到action,判斷是重啟遊戲還是結束遊戲 action = get_user_action(stdscr) responses = defaultdict(lambda: state) #預設是當前狀態,沒有行為就會一直在當前介面迴圈 responses['Restart'], responses['Exit'] = 'Init', 'Exit' #對應不同的行為轉換到不同的狀態 return responses[action] def game(): #畫出當前棋盤狀態 game_field.draw(stdscr) #讀取使用者輸入得到action action = get_user_action(stdscr) if action == 'Restart': return 'Init' if action == 'Exit': return 'Exit' if game_field.move(action): # move successful if game_field.is_win(): return 'Win' if game_field.is_gameover(): return 'Gameover' return 'Game' state_actions = { 'Init': init, 'Win': lambda: not_game('Win'), 'Gameover': lambda: not_game('Gameover'), 'Game': game } curses.use_default_colors() game_field = GameField(win=32) state = 'Init' #狀態機開始迴圈 while state != 'Exit': state = state_actions[state]() |
執行
填上最後一行程式碼:
1 |
curses.wrapper(main) |
完整版程式碼地址
https://github.com/JLUNeverMore/easy_2048-in-200-lines