Title: Create Minesweeper using Python From the Basic to Advanced - AskPython
Open Graph Title: Create Minesweeper using Python From the Basic to Advanced - AskPython
X Title: Create Minesweeper using Python From the Basic to Advanced - AskPython
Description: In this article, we will build a terminal-based Minesweeper game in Python from scratch. We start with the grid layout, walk through the mine-placement and
Open Graph Description: In this article, we will build a terminal-based Minesweeper game in Python from scratch. We start with the grid layout, walk through the mine-placement and
X Description: In this article, we will build a terminal-based Minesweeper game in Python from scratch. We start with the grid layout, walk through the mine-placement and
Opengraph URL: https://www.askpython.com/python/examples/create-minesweeper-using-python
X: @AskPython
Generator: WordPress 7.0
Domain: www.askpython.com
{"@context":"https://schema.org","@graph":[{"@type":["Person","Organization"],"@id":"https://www.askpython.com/#person","name":"Rnzo1zWtYm","sameAs":["https://twitter.com/AskPython"],"logo":{"@type":"ImageObject","@id":"https://www.askpython.com/#logo","url":"https://www.askpython.com/wp-content/uploads/2019/12/logo.svg","contentUrl":"https://www.askpython.com/wp-content/uploads/2019/12/logo.svg","caption":"Rnzo1zWtYm","inLanguage":"en-US"},"image":{"@type":"ImageObject","@id":"https://www.askpython.com/#logo","url":"https://www.askpython.com/wp-content/uploads/2019/12/logo.svg","contentUrl":"https://www.askpython.com/wp-content/uploads/2019/12/logo.svg","caption":"Rnzo1zWtYm","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.askpython.com/#website","url":"https://www.askpython.com","name":"Rnzo1zWtYm","publisher":{"@id":"https://www.askpython.com/#person"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.askpython.com/wp-content/uploads/2020/06/minesweeper_featured_image.png","url":"https://www.askpython.com/wp-content/uploads/2020/06/minesweeper_featured_image.png","width":"1200","height":"600","caption":"Minesweeper Featured Image","inLanguage":"en-US"},{"@type":["WebPage","FAQPage"],"@id":"https://www.askpython.com/python/examples/create-minesweeper-using-python#webpage","url":"https://www.askpython.com/python/examples/create-minesweeper-using-python","name":"Create Minesweeper using Python From the Basic to Advanced - AskPython","datePublished":"2026-07-05T08:52:29+00:00","dateModified":"2026-07-05T09:16:52+00:00","isPartOf":{"@id":"https://www.askpython.com/#website"},"primaryImageOfPage":{"@id":"https://www.askpython.com/wp-content/uploads/2020/06/minesweeper_featured_image.png"},"inLanguage":"en-US","mainEntity":[{"@type":"Question","url":"https://www.askpython.com/python/examples/create-minesweeper-using-python#faq-question-1783199200020","name":"Why does my game crash when I open a cell with no neighbouring mines?","acceptedAnswer":{"@type":"Answer","text":"The recursion in neighbours() must mark every visited cell, otherwise it loops forever and Python eventually raises RecursionError. The vis list is the guard. Reset vis = [] at the start of each zero-cell expansion so a new move can revisit cells cleared in an earlier turn."}},{"@type":"Question","url":"https://www.askpython.com/python/examples/create-minesweeper-using-python#faq-question-1783199200021","name":"How can I change the grid size or number of mines?","acceptedAnswer":{"@type":"Answer","text":"Edit the n and mines_no values in the if __name__ == '__main__' block. The rest of the code uses those names everywhere, so any change propagates automatically. Keep mines_no below n*n or the placement loop will loop forever."}},{"@type":"Question","url":"https://www.askpython.com/python/examples/create-minesweeper-using-python#faq-question-1783199200022","name":"How do I run this on Windows instead of macOS or Linux?","acceptedAnswer":{"@type":"Answer","text":"The clear() function calls os.system('clear'), which only works on Unix. Replace it with os.system('cls') on Windows, or use a cross-platform helper that picks the right command."}},{"@type":"Question","url":"https://www.askpython.com/python/examples/create-minesweeper-using-python#faq-question-1783199200023","name":"How do I add a difficulty selector?","acceptedAnswer":{"@type":"Answer","text":"Read the grid size and mine count from input() before calling set_mines() and set_values(). Three options (8x8 with 8 mines, 16x16 with 40 mines, 24x24 with 99 mines) are enough for a working Minesweeper clone."}}]},{"@type":"Person","@id":"https://www.askpython.com/author/aprataksh","name":"Aprataksh Anand","description":"A Computer Science Engineer, content writing is Aprataksh's first rodeo. A passionate coder along with a love for gaming, he can be found passing his free time with friends in a CS:GO lobby.","url":"https://www.askpython.com/author/aprataksh","image":{"@type":"ImageObject","@id":"https://secure.gravatar.com/avatar/42c2d19d905cb4279b8efddc7c972f1d5ecd3cc4998f815b592aab1cfe35b563?s=96&d=mm&r=g","url":"https://secure.gravatar.com/avatar/42c2d19d905cb4279b8efddc7c972f1d5ecd3cc4998f815b592aab1cfe35b563?s=96&d=mm&r=g","caption":"Aprataksh Anand","inLanguage":"en-US"}},{"@type":"BlogPosting","headline":"Create Minesweeper using Python From the Basic to Advanced - AskPython","keywords":"minesweeper using python","datePublished":"2026-07-05T08:52:29+00:00","dateModified":"2026-07-05T09:16:52+00:00","author":{"@id":"https://www.askpython.com/author/aprataksh","name":"Aprataksh Anand"},"publisher":{"@id":"https://www.askpython.com/#person"},"description":"In this article, we will build a terminal-based Minesweeper game in Python from scratch. We start with the grid layout, walk through the mine-placement and","name":"Create Minesweeper using Python From the Basic to Advanced - AskPython","@id":"https://www.askpython.com/python/examples/create-minesweeper-using-python#richSnippet","isPartOf":{"@id":"https://www.askpython.com/python/examples/create-minesweeper-using-python#webpage"},"image":{"@id":"https://www.askpython.com/wp-content/uploads/2020/06/minesweeper_featured_image.png"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.askpython.com/python/examples/create-minesweeper-using-python#webpage"}}]}
| og:locale | en_US |
| og:type | article |
| og:site_name | AskPython |
| article:section | Python Programming Examples |
| og:updated_time | 2026-07-05T09:16:52+00:00 |
| og:image | https://www.askpython.com/wp-content/uploads/2020/06/minesweeper_featured_image.png |
| og:image:secure_url | https://www.askpython.com/wp-content/uploads/2020/06/minesweeper_featured_image.png |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:image:alt | Minesweeper Featured Image |
| og:image:type | image/png |
| article:published_time | 2026-07-05T08:52:29+00:00 |
| article:modified_time | 2026-07-05T09:16:52+00:00 |
| twitter:card | summary_large_image |
| twitter:creator | @AskPython |
| twitter:image | https://www.askpython.com/wp-content/uploads/2020/06/minesweeper_featured_image.png |
| twitter:label1 | Written by |
| twitter:data1 | Aprataksh Anand |
| twitter:label2 | Time to read |
| twitter:data2 | 17 minutes |
| msapplication-TileImage | https://www.askpython.com/wp-content/uploads/2019/12/cropped-favicon-1-270x270.png |
Links:
Viewport: width=device-width, initial-scale=1, maximum-scale=5, viewport-fit=cover
Robots: index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large