Learn to Code via Tutorials on Repl.it!

← Back to all posts
MineSweeper
ThomasS1 (69)

How to program MineSweeper in Python


Difficulty: moderate

Please note: this tutorial is currently in creation!

Welcome to my tutorial on how to program the classic game, MineSweeper, in Python! Before you begin, I would highly recommend playing a few games to get the hang of the rules. You can do this either online or in my program (which we are going to build).

We will be building our game in the Python terminal, which, as you probably know, has its limitations. Instead of clicking on the square in the grid, as you do in the original game, the player will type its coordinates. Also, the Python terminal cannot print colour in the terminal without extra packages, and those have limitations too. But what we will create is a fully functional 9x9 MineSweeper game, that will entertain you and your friends for hours on end (sorry, that sounded very cheesy and predictable).

Parts List


[Part 1: Planning]
[Part 2: Setting up our program structure.]
[]
[]

Part 1: Planning


You should now understand the game mechanics of MineSweeper, but before we can get started we need to think about what the computer does. This will help us when writing our code later. First, just to avoid comfusion later, a few definitions/variables I will use:

  • Bomb: I will use this instead of mine, to avoid confusion with the possessive pronoun!
  • Marker: a flag which the player can place to help them remember any locations of bombs they have deduced.
  • Solution grid: only the computer knows the locations of all the bombs until the end of the game.
  • Known grid: this contains the squares that the player knows about.
  • To open: to move data from the solution grid to the known grid, i.e. when the player makes their move.

Now we've got that cleared up, here's a quick outline of what the program will do.

  • Display menu to player.
    • If they ask for instructions, print them.
  • Generate random locations of 10 bombs and place them in the solution grid.
  • Update the numbers around them in the solution grid.
  • Until the player wins/loses, loop:
    • Display the known grid to the player and ask for their move.
      • If they chose to open a square, open it.
        • If that square is a bomb, they lose.
          • Offer to play again.
        • If that square is a 0, open up all of the squares around it automatically, as there cannot be any bombs there, and continue the chain reaction.
        • If all squares in the grid except the 10 bomb squares are open, they win!
      • If they chose to place a marker in a square, place that marker.

Part 2: Setting up our program structure.


Because this is not going to be a very long program, (about 250 lines - don't worry if you do think this is quite long!) we will write almost all of it in one file. The only exception will be the instruction text, which we will put in a seperate .txt (text) file to declutter our code a bit. So, if you're going to write the program as we go along, now is the time to create your repl, call it something, and create the new file.

In the bar on the left, click 'files' and then 'new file'. Call it instructions.txt.
Paste into it the following text:

INSTRUCTIONS
============
The aim of MineSweeper is to determine the locations of 10 bombs, randomly placed in a 9x9 grid.
On each go, you type in the coordinates of a square, e.g. E4.
If there is a bomb in that square, you lose. Otherwise, the number of bombs directly surrounding that square, including diagonally, will appear in that square.
If that number is a 0, the squares around it will be 'opened' automatically, as there cannot be any bombs there, to save you time.
If you think you know the position of a bomb, type 'M' followed by the coordinates of that square, e.g. ME4.
You win by 'opening' all of the squares except those with bombs in.
NB: It is luck on the first move.
Good luck!

Part 3: Beggining our code.


Now move back to main.py, and write the following:

import random, time
from termcolor import colored, cprint

This imports three packages, random, time, and certain parts of termcolor, which we will use later in the program. But it doesn't do anything yet. Let's change that.

First things first: we need to write an introduction for the player. Here's one I made earlier, but feel free to edit it / write your own.

#Introduction
print()
cprint('Welcome to MineSweeper v.3.0!', 'red')
cprint('=============================', 'red')
print()
print('Excited to declare version 3.0 of MineSweeper as almost fully functional!')

'cprint()' is a function we imported just a minute ago. It allows us to print colour in the terminal, with the syntax cprint('text', 'colour'). I've set it up to print the title in red, but you can choose any of the following colours:

grey
red
green
yellow
blue
magenta
cyan
white

Termcolor has loads of other cool features such as text highlights - see the package website.

You can now run your program and it will, for the first time, do something! Oh and, if it hasn't already it will first chuck out some rubbish about importing termcolor. Ignore it - it only does it once.

Part 4: A Python Sandwich


Trust me, by the end of this tutorial you're going to be sick of functions (def example():). Apart from one line to initiate all the functions, that was the last stuff you're going to type that's not in a function.

You know what, let's write that line. It goes right at the end of the program, and it's pretty simple:

reset()

Leave plenty of space in between it and what we wrote earlier - this is where the rest of the program is going to be written.

Voila, a python sandwich!

Part 5: reset()