文字版——代码及讲解
代码——
import random# 初始化游戏棋盘
def init_board():return [[0] * 4 for _ in range(4)]# 在棋盘上随机生成一个2或4
def add_new_tile(board):empty_cells = [(i, j) for i in range(4) for j in range(4) if board[i][j] == 0]if empty_cells:i, j = random.choice(empty_cells)board[i][j] = 2 if random.random() < 0.9 else 4return board# 打印棋盘
def print_board(board):for row in board:for cell in row:print(str(cell).rjust(4), end="")print()# 向左移动
def move_left(board):new_board = [row[:] for row in board]for row in new_board:while 0 in row:row.remove(0)for i in range(len(row) - 1):if i + 1< len(row) and row[i] == row[i + 1]:row[i] *= 2row.pop(i + 1)while len(row) < 4:row.append(0)return new_board# 向右移动
def move_right(board):new_board = [row[::-1] for row in board]new_board = move_left(new_board)return [row[::-1] for row in new_board]# 向上移动
def move_up(board):new_board = list(map(list, zip(*board)))new_board = move_left(new_board)return list(map(list, zip(*new_board)))# 向下移动
def move_down(board):new_board = list(map(list, zip(*board)))new_board = move_right(new_board)return list(map(list, zip(*new_board)))# 检查是否还有可移动的位置
def is_game_over(board):for move in [move_left, move_right, move_up, move_down]:if move(board)!= board:return Falsereturn Truedef main():board = init_board()add_new_tile(board)add_new_tile(board)while not is_game_over(board):print_board(board)move = input("Enter a move (w: up, s: down, a: left, d: right): ")if move == "w":new_board = move_up(board)elif move == "s":new_board = move_down(board)elif move == "a":new_board = move_left(board)elif move == "d":new_board = move_right(board)else:print("Invalid move")continueif new_board!= board:board = new_boardadd_new_tile(board)print("Game Over!")if __name__ == "__main__":main()
讲解:
1.初始化游戏棋盘
def init_board():
return [[0] * 4 for _ in range(4)]
解释:这个函数用于创建一个 4x4 的二维列表,初始值都为 0,代表空的游戏棋盘
2.在棋盘上随机生成一个2或4
def add_new_tile(board):
empty_cells = [(i, j) for i in range(4) for j in range(4) if board[i][j] == 0]
if empty_cells:
i, j = random.choice(empty_cells)
board[i][j] = 2 if random.random() < 0.9 else 4
return board
解释:
首先找到棋盘上所有空的单元格(值为 0)
从空单元格中随机选择一个
在选中的单元格中,90% 的概率生成 2,10% 的概率生成 4
3.打印棋盘
def print_board(board):
for row in board:
for cell in row:
print(str(cell).rjust(4), end="")
print()
解释:
这个函数用于以美观的方式打印出游戏棋盘
对每一行和每一个单元格进行格式化处理,使其对齐显示
4. 向左移动
def move_left(board):
new_board = [row[:] for row in board]
for row in new_board:
while 0 in row:
row.remove(0)
for i in range(len(row) - 1):
if i + 1< len(row) and row[i] == row[i + 1]:
row[i] *= 2
row.pop(i + 1)
while len(row) < 4:
row.append(0)
return new_board
解释:
首先复制当前棋盘
对于每一行,移除所有的 0 元素,然后合并相同的数字,最后在末尾添加 0 元素使每行长度为 4
5.向右移动
def move_right(board):
new_board = [row[::-1] for row in board]
new_board = move_left(new_board)
return [row[::-1] for row in new_board]
解释:
先将每行反转
调用 move_left 函数进行移动操作
再将每行反转回来得到向右移动后的结果
6.向上移动
def move_up(board):
new_board = list(map(list, zip(*board)))
new_board = move_left(new_board)
return list(map(list, zip(*new_board)))
解释:
先对棋盘进行转置(行列互换)
调用 move_left 函数
再转置回来得到向上移动后的结果
7.向下移动
def move_down(board):
new_board = list(map(list, zip(*board)))
new_board = move_right(new_board)
return list(map(list, zip(*new_board)))
解释:
先对棋盘进行转置
调用 move_right 函数
再转置回来得到向下移动后的结果
8.检查是否还有可移动的位置
def is_game_over(board):
for move in [move_left, move_right, move_up, move_down]:
if move(board)!= board:
return False
return True
解释:
尝试所有的移动操作(左、右、上、下)
如果有任何一种移动操作得到的新棋盘与当前棋盘不同,说明游戏还可以继续,返回 False;否则返回 True,表示游戏结束
9.游戏循环
def main():
board = init_board()
add_new_tile(board)
add_new_tile(board)
while not is_game_over(board):
print_board(board)
move = input("Enter a move (w: up, s: down, a: left, d: right): ")
if move == "w":
new_board = move_up(board)
elif move == "s":
new_board = move_down(board)
elif move == "a":
new_board = move_left(board)
elif move == "d":
new_board = move_right(board)
else:
print("Invalid move")
continue
if new_board!= board:
board = new_board
add_new_tile(board)
print("Game Over!")
解释:
初始化棋盘并添加两个初始数字
进入游戏循环,在循环中:
打印当前棋盘
获取用户输入的移动方向
根据输入执行相应的移动操作
如果移动操作有效,更新棋盘并添加新的数字
当游戏结束时,打印 "Game Over!"