1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

マインスイーパー作ってみた #100日チャレンジに感化されて

Last updated at Posted at 2025-04-19

マインスイーパーをCahtGPTで作ってみた

#100日チャレンジ

クリックした部分を. にするよう修正しました!教えてくれた方ありがとうございます!

import tkinter as tk
import random

class Cell:
    def __init__(self, master, x, y, game):
        self.master = master
        self.x = x
        self.y = y
        self.game = game
        self.is_bomb = False
        self.is_revealed = False

        self.button = tk.Button(master, width=1, height=1, command=self.reveal)
        self.button.grid(row=x + 1, column=y)  # 行+1で上にリセットボタンを置くスペースを確保

    def reveal(self):
        if self.is_revealed:
            return
        self.is_revealed = True
        self.button.config(relief=tk.SUNKEN)

        if self.is_bomb:
            self.button.config(text='💣', bg='red')
            self.game.game_over()
        else:
            count = self.game.count_bombs_around(self.x, self.y)
            if count > 0:
                self.button.config(text=str(count))
            else:
                self.button.config(text='.')
                for dx in [-1, 0, 1]:
                    for dy in [-1, 0, 1]:
                        nx, ny = self.x + dx, self.y + dy
                        if 0 <= nx < self.game.size and 0 <= ny < self.game.size:
                            neighbor = self.game.cells[nx][ny]
                            if not neighbor.is_revealed:
                                neighbor.reveal()

    def destroy(self):
        self.button.destroy()


class Minesweeper:
    def __init__(self, master, size=8, bombs=10):
        self.master = master
        self.size = size
        self.bombs = bombs
        self.cells = []

        # リセットボタンの設置(上の行に配置)
        self.reset_button = tk.Button(master, text="🔄 リセット", command=self.reset)
        self.reset_button.grid(row=0, column=0, columnspan=size)

        self.create_board()

    def create_board(self):
        # 既存のマスを削除
        for row in self.cells:
            for cell in row:
                cell.destroy()

        self.cells = [[Cell(self.master, x, y, self) for y in range(self.size)] for x in range(self.size)]
        self._place_bombs()

    def reset(self):
        self.create_board()

    def _place_bombs(self):
        placed = 0
        while placed < self.bombs:
            x = random.randint(0, self.size - 1)
            y = random.randint(0, self.size - 1)
            cell = self.cells[x][y]
            if not cell.is_bomb:
                cell.is_bomb = True
                placed += 1

    def count_bombs_around(self, x, y):
        count = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                nx, ny = x + dx, y + dy
                if 0 <= nx < self.size and 0 <= ny < self.size:
                    if self.cells[nx][ny].is_bomb:
                        count += 1
        return count

    def game_over(self):
        for row in self.cells:
            for cell in row:
                if cell.is_bomb:
                    cell.button.config(text='💣', bg='gray')
        print("💥 ゲームオーバー!")


# 実行
if __name__ == "__main__":
    root = tk.Tk()
    root.title("マインスイーパー")
    game = Minesweeper(root)
    root.mainloop()

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?