import tkinter as tk
import random
from tkinter import messagebox

# ---------- 游戏配置 ----------
GRID_SIZE = 10
CELL_SIZE = 56
CANVAS_SIZE = GRID_SIZE * CELL_SIZE

# 地形类型
DIRT = 0
STONE = 1
GOLD = 2
DIAMOND = 3

# 颜色
COLOR_DIRT = '#9c7b5b'
COLOR_STONE = '#5f7f7f'
COLOR_GOLD = '#f5d742'
COLOR_DIAMOND = '#7ef0e8'
COLOR_MINER = '#3e2e1f'
COLOR_HAT = '#b8860b'


class MiningGame:
    def __init__(self, root):
        self.root = root
        self.root.title("⛏️ 挖矿模拟器")
        self.root.resizable(False, False)

        # 资源计数
        self.gold = 0
        self.diamond = 0

        # 矿工位置
        self.miner_x = 0
        self.miner_y = 0

        # 网格数据
        self.grid = []

        # --- 界面布局 ---
        # 顶部信息栏
        top_frame = tk.Frame(root, bg='#2a3a3e', pady=8)
        top_frame.pack(fill=tk.X)

        self.gold_label = tk.Label(top_frame, text="🪙 0", font=('Segoe UI', 14),
                                   fg='#f5d742', bg='#2a3a3e')
        self.gold_label.pack(side=tk.LEFT, padx=20)

        self.diamond_label = tk.Label(top_frame, text="💎 0", font=('Segoe UI', 14),
                                      fg='#7ef0e8', bg='#2a3a3e')
        self.diamond_label.pack(side=tk.LEFT, padx=20)

        reset_btn = tk.Button(top_frame, text="🔄 新矿脉", font=('Segoe UI', 11, 'bold'),
                              bg='#46646b', fg='white', relief=tk.FLAT,
                              padx=15, pady=3, command=self.reset_game)
        reset_btn.pack(side=tk.RIGHT, padx=20)

        # 画布
        self.canvas = tk.Canvas(root, width=CANVAS_SIZE, height=CANVAS_SIZE,
                                bg='#1b2629', highlightthickness=0)
        self.canvas.pack(padx=15, pady=10)

        # 底部说明
        hint = tk.Label(root, text="方向键移动 | 石头不可通过 | 踩到金矿/钻石自动挖掘",
                        font=('Segoe UI', 9), fg='#bed9d2', bg='#2a3a3e')
        hint.pack(pady=(0, 10))

        root.configure(bg='#2a3a3e')

        # 键盘绑定
        root.bind('<Up>', lambda e: self.try_move(0, -1))
        root.bind('<Down>', lambda e: self.try_move(0, 1))
        root.bind('<Left>', lambda e: self.try_move(-1, 0))
        root.bind('<Right>', lambda e: self.try_move(1, 0))

        # 画布点击
        self.canvas.bind('<Button-1>', self.on_canvas_click)

        # 初始化
        self.init_grid()
        self.draw()

    # ---------- 初始化矿脉 ----------
    def init_grid(self):
        self.grid = [[DIRT for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]

        # 随机石头
        for y in range(GRID_SIZE):
            for x in range(GRID_SIZE):
                if random.random() < 0.20:
                    self.grid[y][x] = STONE

        # 随机金矿
        for y in range(GRID_SIZE):
            for x in range(GRID_SIZE):
                if self.grid[y][x] == DIRT and random.random() < 0.06:
                    self.grid[y][x] = GOLD

        # 随机钻石
        for y in range(GRID_SIZE):
            for x in range(GRID_SIZE):
                if self.grid[y][x] == DIRT and random.random() < 0.03:
                    self.grid[y][x] = DIAMOND

        # 确保出发点干净
        self.grid[0][0] = DIRT

        self.miner_x = 0
        self.miner_y = 0
        self.gold = 0
        self.diamond = 0
        self.update_ui()

    # ---------- 重置 ----------
    def reset_game(self):
        self.init_grid()
        self.draw()

    # ---------- UI 更新 ----------
    def update_ui(self):
        self.gold_label.config(text=f"🪙 {self.gold}")
        self.diamond_label.config(text=f"💎 {self.diamond}")

    # ---------- 绘制 ----------
    def draw(self):
        self.canvas.delete("all")
        for row in range(GRID_SIZE):
            for col in range(GRID_SIZE):
                x1 = col * CELL_SIZE
                y1 = row * CELL_SIZE
                x2 = x1 + CELL_SIZE
                y2 = y1 + CELL_SIZE

                cell = self.grid[row][col]
                color = {
                    DIRT: COLOR_DIRT,
                    STONE: COLOR_STONE,
                    GOLD: COLOR_GOLD,
                    DIAMOND: COLOR_DIAMOND,
                }.get(cell, '#777')

                self.canvas.create_rectangle(x1, y1, x2, y2, fill=color,
                                             outline='#1a2225', width=1.5)

                # 矿石装饰
                if cell == GOLD:
                    cx, cy = x1 + CELL_SIZE / 2, y1 + CELL_SIZE / 2
                    self.canvas.create_oval(cx - 8, cy - 8, cx + 8, cy + 8,
                                            fill='#f8e58c', outline='')
                elif cell == DIAMOND:
                    cx, cy = x1 + CELL_SIZE / 2, y1 + CELL_SIZE / 2
                    r = 10
                    pts = [cx, cy - r, cx + r, cy, cx, cy + r, cx - r, cy]
                    self.canvas.create_polygon(pts, fill='#c2fcf5', outline='')
                elif cell == STONE:
                    self.canvas.create_oval(x1 + 10, y1 + 10, x1 + 22, y1 + 22,
                                            fill='#7a9a9a', outline='')
                    self.canvas.create_oval(x1 + 32, y1 + 32, x1 + 44, y1 + 44,
                                            fill='#4e6b6b', outline='')

        # 绘制矿工
        self.draw_miner()

    def draw_miner(self):
        x = self.miner_x * CELL_SIZE
        y = self.miner_y * CELL_SIZE
        # 身体
        self.canvas.create_oval(x + 14, y + 28, x + 42, y + 46,
                                fill=COLOR_MINER, outline='')
        # 安全帽
        self.canvas.create_oval(x + 16, y + 8, x + 40, y + 26,
                                fill=COLOR_HAT, outline='#8b6b2b', width=1.5)
        # 帽檐
        self.canvas.create_oval(x + 10, y + 18, x + 46, y + 24,
                                fill='#8b6b2b', outline='')
        # 脸
        self.canvas.create_oval(x + 20, y + 18, x + 36, y + 34,
                                fill='#f7d9aa', outline='')
        # 眼睛
        self.canvas.create_oval(x + 24, y + 24, x + 28, y + 28,
                                fill='#1e1e1e', outline='')
        self.canvas.create_oval(x + 32, y + 24, x + 36, y + 28,
                                fill='#1e1e1e', outline='')
        # 镐子
        self.canvas.create_line(x + 38, y + 38, x + 50, y + 26,
                                fill='#b87c4b', width=3)
        self.canvas.create_polygon(x + 50, y + 26, x + 56, y + 20, x + 44, y + 16,
                                   fill='#a0a0a0', outline='')

    # ---------- 移动 ----------
    def try_move(self, dx, dy):
        nx = self.miner_x + dx
        ny = self.miner_y + dy

        if not (0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE):
            return

        # 石头阻挡
        if self.grid[ny][nx] == STONE:
            return

        # 移动
        self.miner_x = nx
        self.miner_y = ny

        # 自动挖掘
        cell = self.grid[ny][nx]
        if cell == GOLD:
            self.gold += 1
            self.grid[ny][nx] = DIRT
        elif cell == DIAMOND:
            self.diamond += 1
            self.grid[ny][nx] = DIRT

        self.update_ui()
        self.draw()

    # ---------- 点击相邻格移动 ----------
    def on_canvas_click(self, event):
        col = event.x // CELL_SIZE
        row = event.y // CELL_SIZE
        if not (0 <= col < GRID_SIZE and 0 <= row < GRID_SIZE):
            return

        dx = abs(col - self.miner_x)
        dy = abs(row - self.miner_y)
        if (dx == 1 and dy == 0) or (dx == 0 and dy == 1):
            if self.grid[row][col] == STONE:
                return
            self.miner_x = col
            self.miner_y = row
            cell = self.grid[row][col]
            if cell == GOLD:
                self.gold += 1
                self.grid[row][col] = DIRT
            elif cell == DIAMOND:
                self.diamond += 1
                self.grid[row][col] = DIRT
            self.update_ui()
            self.draw()


if __name__ == "__main__":
    root = tk.Tk()
    game = MiningGame(root)
    root.mainloop()