1
File size: 2,620 Bytes
d248334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b80930
 
 
 
d248334
1b80930
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import numpy as np

# 初始化游戏板
def initialize_board():
    board = np.zeros((4, 4), dtype=int)
    add_new_tile(board)
    add_new_tile(board)
    return board

# 添加新的数字块
def add_new_tile(board):
    zeros = list(zip(*np.where(board == 0)))
    if zeros:
        x, y = zeros[np.random.choice(len(zeros))]
        board[x, y] = np.random.choice([2, 4], p=[0.9, 0.1])

# 移动数字块
def move(board, direction):
    if direction == 'left':
        board = np.rot90(board, 1)
        board = move_left(board)
        board = np.rot90(board, -1)
    elif direction == 'right':
        board = np.rot90(board, -1)
        board = move_left(board)
        board = np.rot90(board, 1)
    elif direction == 'up':
        board = move_left(board)
    elif direction == 'down':
        board = np.rot90(board, 2)
        board = move_left(board)
        board = np.rot90(board, 2)
    return board

# 向左移动数字块
def move_left(board):
    new_board = np.zeros((4, 4), dtype=int)
    for i in range(4):
        row = board[i, :]
        row = row[row != 0]
        row = merge(row)
        new_board[i, :len(row)] = row
    return new_board

# 合并相同的数字块
def merge(row):
    for i in range(len(row) - 1):
        if row[i] == row[i + 1]:
            row[i] *= 2
            row[i + 1] = 0
    row = row[row != 0]
    return row

# 游戏主循环
def game_loop(board, direction):
    if direction:
        board = move(board, direction)
        add_new_tile(board)
    return board

# 初始化游戏板
board = initialize_board()

# 创建Gradio界面
def update_board(direction):
    global board
    board = game_loop(board, direction)
    return board

with gr.Blocks() as demo:
    gr.Markdown("# 2048 Game")
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("## Controls")
            up_button = gr.Button("Up")
            down_button = gr.Button("Down")
            left_button = gr.Button("Left")
            right_button = gr.Button("Right")
        with gr.Column(scale=2):
            gr.Markdown("## Game Board")
            board_display = gr.Dataframe(board, headers=["", "", "", ""], datatype=["number", "number", "number", "number"])

    up_button.click(update_board, inputs=None, outputs=board_display, _js="() => 'up'")
    down_button.click(update_board, inputs=None, outputs=board_display, _js="() => 'down'")
    left_button.click(update_board, inputs=None, outputs=board_display, _js="() => 'left'")
    right_button.click(update_board, inputs=None, outputs=board_display, _js="() => 'right'")

demo.launch()