跳到正文

剪贴板

剪贴板 ndgasfaz

原文
剪贴板信息

剪贴板信息

收录
更新
Python
import pygame
import sys
import copy

# ===== 配置区 (修改这里的参数来测试不同的测试点) =====
N = 3  # 栈的个数 n
DECK_INITIAL = [1, 3, 2, 4, 2, 3, 4, 1]
# DECK_INITIAL = [1, 2, 3, 4, 5, 2, 3, 4, 5, 1]
# DECK_INITIAL = [1, 3, 2, 4, 5, 2, 1, 5, 4, 3]
# DECK_INITIAL = [1, 3, 2, 4, 5, 2, 2, 1, 5, 2, 4, 3]
# DECK_INITIAL = [1, 3, 2, 4, 5, 4, 4, 1, 2, 5, 4, 3]
# ====================================================

pygame.init()
WIDTH, HEIGHT = 1000, 750
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("喵了个喵 (NOIP 2022) - 策略推演工具 (带撤销版)")

try:
    font = pygame.font.SysFont(['microsoftyahei', 'simhei', 'pingfang', 'stsong'], 20)
    large_font = pygame.font.SysFont(['microsoftyahei', 'simhei', 'pingfang', 'stsong'], 32)
except:
    font = pygame.font.Font(None, 24)
    large_font = pygame.font.Font(None, 36)

BG_COLOR = (240, 248, 255)
STACK_BG = (220, 220, 220)
BLACK = (0, 0, 0)
SELECT_COLOR = (255, 100, 100)
BTN_COLOR = (180, 200, 220)
BTN_HOVER = (150, 180, 210)

# 定义按钮区域
UNDO_RECT = pygame.Rect(WIDTH - 220, 20, 90, 40)
RESET_RECT = pygame.Rect(WIDTH - 110, 20, 90, 40)

def get_color(val):
    colors = [
        (200, 200, 200), (255, 182, 193), (135, 206, 235),
        (144, 238, 144), (255, 215, 0),   (255, 160, 122),
        (147, 112, 219), (0, 206, 209),   (240, 128, 128),
        (152, 251, 152), (221, 160, 221)
    ]
    if val < len(colors): return colors[val]
    return ((val * 73) % 255, (val * 111) % 255, (val * 137) % 255)

def reset_game():
    global deck, stacks, selected_stack, history
    deck = DECK_INITIAL.copy()
    stacks = [[] for _ in range(N)]
    selected_stack = None
    history = []  # 记录历史状态用于撤销

# 保存当前状态到历史记录
def save_state():
    history.append((copy.deepcopy(deck), copy.deepcopy(stacks)))

reset_game()

def draw_card(surface, x, y, w, h, val):
    rect = pygame.Rect(x, y, w, h)
    pygame.draw.rect(surface, get_color(val), rect, border_radius=6)
    pygame.draw.rect(surface, BLACK, rect, 2, border_radius=6)

    current_font = large_font if w >= 50 else font
    text = current_font.render(str(val), True, BLACK)
    text_rect = text.get_rect(center=rect.center)
    surface.blit(text, text_rect)

def draw_button(surface, rect, text, mouse_pos):
    color = BTN_HOVER if rect.collidepoint(mouse_pos) else BTN_COLOR
    pygame.draw.rect(surface, color, rect, border_radius=8)
    pygame.draw.rect(surface, BLACK, rect, 2, border_radius=8)

    text_surf = font.render(text, True, BLACK)
    text_rect = text_surf.get_rect(center=rect.center)
    surface.blit(text_surf, text_rect)

def draw():
    screen.fill(BG_COLOR)
    mouse_pos = pygame.mouse.get_pos()

    # 1. 绘制操作提示
    instructions = [
        "【操作 1】 左键点击栈:将牌堆顶的牌放入该栈 (栈顶两张相同会自动消除)",
        "【操作 2】 右键点击两个栈:选择两个不同的栈 (栈底两张相同会自动消除)"
    ]
    for i, text in enumerate(instructions):
        surf = font.render(text, True, BLACK)
        screen.blit(surf, (20, 15 + i * 25))

    # 绘制按钮
    draw_button(screen, UNDO_RECT, f"撤销 ({len(history)})", mouse_pos)
    draw_button(screen, RESET_RECT, "重置", mouse_pos)

    # 2. 绘制牌堆预览 (水平铺开)
    if deck:
        preview_count = min(len(deck), 20)
        card_w, card_h = 40, 60
        start_x = max(20, (WIDTH - (preview_count * (card_w + 5))) // 2)

        deck_label = font.render(f"当前牌堆 (自左向右发牌,剩余 {len(deck)} 张):", True, BLACK)
        screen.blit(deck_label, (start_x, 110))

        for i in range(preview_count):
            x = start_x + i * (card_w + 5)
            y = 140
            if i == 0:
                pygame.draw.rect(screen, SELECT_COLOR, (x-3, y-3, card_w+6, card_h+6), 3)
            draw_card(screen, x, y, card_w, card_h, deck[i])

        if len(deck) > preview_count:
            more_text = font.render(f"... 等", True, BLACK)
            screen.blit(more_text, (start_x + preview_count * (card_w + 5) + 5, y + 20))
    else:
        win_text = large_font.render("牌堆已空!继续操作直至所有栈清空。", True, (0, 150, 0))
        screen.blit(win_text, (WIDTH // 2 - 250, 140))

    # 3. 绘制所有的栈
    stack_w = 90
    card_h = 35
    spacing = WIDTH / (N + 1)
    base_y = 680

    for i in range(N):
        cx = spacing * (i + 1)
        x = cx - stack_w // 2

        if selected_stack == i:
            pygame.draw.rect(screen, SELECT_COLOR, (x-6, base_y-400-6, stack_w+12, 410), 3)

        pygame.draw.rect(screen, STACK_BG, (x, base_y - 400, stack_w, 400))
        pygame.draw.line(screen, BLACK, (x, base_y), (x + stack_w, base_y), 4)

        label = font.render(f"栈 {i+1}", True, BLACK)
        screen.blit(label, (cx - label.get_width()//2, base_y + 10))

        for j, val in enumerate(stacks[i]):
            card_y = base_y - (j + 1) * card_h
            draw_card(screen, x, card_y, stack_w, card_h, val)

    pygame.display.flip()

running = True
while running:
    draw()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                reset_game()

        elif event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = event.pos

            # 处理按钮点击
            if event.button == 1:
                if UNDO_RECT.collidepoint(mx, my):
                    if history:
                        deck, stacks = history.pop()
                        selected_stack = None
                    continue
                elif RESET_RECT.collidepoint(mx, my):
                    reset_game()
                    continue

            # 处理点击栈
            clicked_stack = -1
            spacing = WIDTH / (N + 1)
            for i in range(N):
                cx = spacing * (i + 1)
                if cx - 60 <= mx <= cx + 60 and 280 <= my <= 700:
                    clicked_stack = i
                    break

            if clicked_stack != -1:
                if event.button == 1: # 左键:操作1
                    if deck:
                        save_state() # 执行有效操作前,记录状态
                        card = deck.pop(0)
                        stacks[clicked_stack].append(card)
                        if len(stacks[clicked_stack]) >= 2:
                            if stacks[clicked_stack][-1] == stacks[clicked_stack][-2]:
                                stacks[clicked_stack].pop()
                                stacks[clicked_stack].pop()
                    selected_stack = None

                elif event.button == 3: # 右键:操作2
                    if selected_stack is None:
                        selected_stack = clicked_stack
                    else:
                        if selected_stack != clicked_stack:
                            s1, s2 = selected_stack, clicked_stack
                            if len(stacks[s1]) > 0 and len(stacks[s2]) > 0:
                                if stacks[s1][0] == stacks[s2][0]:
                                    save_state() # 执行有效消除前,记录状态
                                    stacks[s1].pop(0)
                                    stacks[s2].pop(0)
                        selected_stack = None

pygame.quit()
sys.exit()