{"id":142,"date":"2026-04-24T14:46:28","date_gmt":"2026-04-24T14:46:28","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/l_rankins\/?p=142"},"modified":"2026-04-24T14:46:28","modified_gmt":"2026-04-24T14:46:28","slug":"book-25-mine-maddness","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/l_rankins\/2026\/04\/24\/book-25-mine-maddness\/","title":{"rendered":"BOOK 25: MINE MADDNESS"},"content":{"rendered":"\n<p>Today I made Mine Sweeper<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from tkinter import Button, Label\nimport random\nimport settings\nimport ctypes\nimport sys\n\nclass Cell:\n    all = &#91;]\n    cell_count = settings.CELL_COUNT\n    cell_count_label_object = None\n    def __init__(self,x, y, is_mine=False):\n        self.is_mine = is_mine\n        self.is_opened = False\n        self.is_mine_candidate = False\n        self.cell_btn_object = None\n        self.x = x\n        self.y = y\n\n        \n        Cell.all.append(self)\n\n    def create_btn_object(self, location):\n        btn = Button(\n            location,\n            width=12,\n            height=4,\n        )\n        btn.bind('&lt;Button-1>', self.left_click_actions ) \n        btn.bind('&lt;Button-3>', self.right_click_actions ) \n        self.cell_btn_object = btn\n\n    @staticmethod\n    def create_cell_count_label(location):\n        lbl = Label(\n            location,\n            bg='black',\n            fg='white',\n            text=f\"Cells Left:{Cell.cell_count}\",\n            font=(\"\", 30)\n        )\n        Cell.cell_count_label_object = lbl\n\n    def left_click_actions(self, event):\n        if self.is_mine:\n            self.show_mine()\n        else:\n            if self.surrounded_cells_mines_length == 0:\n                for cell_obj in self.surrounded_cells:\n                    cell_obj.show_cell()\n            self.show_cell()\n            \n            if Cell.cell_count == settings.MINES_COUNT:\n                ctypes.windll.user32.MessageBoxW(0, 'Congratulations! You won the game!', 'Game Over', 0)\n\n        \n        self.cell_btn_object.unbind('&lt;Button-1>')\n        self.cell_btn_object.unbind('&lt;Button-3>')\n\n    def get_cell_by_axis(self, x,y):\n        \n        for cell in Cell.all:\n            if cell.x == x and cell.y == y:\n                return cell\n\n    @property\n    def surrounded_cells(self):\n        cells = &#91;\n            self.get_cell_by_axis(self.x - 1, self.y -1),\n            self.get_cell_by_axis(self.x - 1, self.y),\n            self.get_cell_by_axis(self.x - 1, self.y + 1),\n            self.get_cell_by_axis(self.x, self.y - 1),\n            self.get_cell_by_axis(self.x + 1, self.y - 1),\n            self.get_cell_by_axis(self.x + 1, self.y),\n            self.get_cell_by_axis(self.x + 1, self.y + 1),\n            self.get_cell_by_axis(self.x, self.y + 1)\n        ]\n\n        cells = &#91;cell for cell in cells if cell is not None]\n        return cells\n\n    @property\n    def surrounded_cells_mines_length(self):\n        counter = 0\n        for cell in self.surrounded_cells:\n            if cell.is_mine:\n                counter += 1\n\n        return counter\n\n    def show_cell(self):\n        if not self.is_opened:\n            Cell.cell_count -= 1\n            self.cell_btn_object.configure(text=self.surrounded_cells_mines_length)\n            \n            if Cell.cell_count_label_object:\n                Cell.cell_count_label_object.configure(\n                    text=f\"Cells Left:{Cell.cell_count}\"\n                )\n            \n            self.cell_btn_object.configure(\n                bg='SystemButtonFace'\n            )\n\n        \n        self.is_opened = True\n\n    def show_mine(self):\n        self.cell_btn_object.configure(bg='red')\n        ctypes.windll.user32.MessageBoxW(0, 'You clicked on a mine', 'Game Over', 0)\n        sys.exit()\n\n\n    def right_click_actions(self, event):\n        if not self.is_mine_candidate:\n            self.cell_btn_object.configure(\n                bg='orange'\n            )\n            self.is_mine_candidate = True\n        else:\n            self.cell_btn_object.configure(\n                bg='SystemButtonFace'\n            )\n            self.is_mine_candidate = False\n\n    @staticmethod\n    def randomize_mines():\n        picked_cells = random.sample(\n            Cell.all, settings.MINES_COUNT\n        )\n        for picked_cell in picked_cells:\n            picked_cell.is_mine = True\n\n    def __repr__(self):\n        return f\"Cell({self.x}, {self.y})\"<\/code><\/pre>\n\n\n\n<p><strong>This is the code for the Cells and how they interact with the mouse actions.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from tkinter import *\nfrom cell import Cell\nimport settings\nimport utils\n\n\nroot = Tk()\n\nroot.configure(bg=\"black\")\nroot.geometry(f'{settings.WIDTH}x{settings.HEIGHT}')\nroot.title(\"Minesweeper Game\")\nroot.resizable(False, False)\n\ntop_frame = Frame(\n    root,\n    bg='black',\n    width=settings.WIDTH,\n    height=utils.height_prct(25)\n)\ntop_frame.place(x=0, y=0)\n\ngame_title = Label(\n    top_frame,\n    bg='black',\n    fg='white',\n    text='Minesweeper Game',\n    font=('', 48)\n)\n\ngame_title.place(\n    x=utils.width_prct(25), y=0\n)\n\nleft_frame = Frame(\n    root,\n    bg='black',\n    width=utils.width_prct(25),\n    height=utils.height_prct(75)\n)\nleft_frame.place(x=0, y=utils.height_prct(25))\n\ncenter_frame = Frame(\n    root,\n    bg='black',\n    width=utils.width_prct(75),\n    height=utils.height_prct(75)\n)\ncenter_frame.place(\n    x=utils.width_prct(25),\n    y=utils.height_prct(25),\n)\n\nfor x in range(settings.GRID_SIZE):\n    for y in range(settings.GRID_SIZE):\n        c = Cell(x, y)\n        c.create_btn_object(center_frame)\n        c.cell_btn_object.grid(\n            column=x, row=y\n        )\n\nCell.create_cell_count_label(left_frame)\nCell.cell_count_label_object.place(\n    x=0, y=0\n)\n\nCell.randomize_mines()\n\n\nroot.mainloop()<\/code><\/pre>\n\n\n\n<p><strong>this is the code that controls the look of the game along with randomizing the bombs.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>WIDTH=1440\nHEIGHT=720\nGRID_SIZE=6\nCELL_COUNT=GRID_SIZE ** 2\nMINES_COUNT=(CELL_COUNT) \/\/ 4<\/code><\/pre>\n\n\n\n<p><strong>these are the settings.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import settings\n\n\ndef height_prct(percentage):\n    return (settings.HEIGHT \/ 100) * percentage\n\ndef width_prct(percentage):\n    return (settings.WIDTH \/ 100) * percentage<\/code><\/pre>\n\n\n\n<p><strong>theses let you change the settings.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I made Mine Sweeper This is the code for the Cells and how they interact with the mouse actions. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":1,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts\/142\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}