Mine Sweeper game implementation in Python
We all must have played the Mine Sweeper game for once. Let’s see the Mine Sweeper game implementation in Python. First of all, we need to understand how this game works and rules of the game and at last start coding. This game is totally a random selection game i.e., luck-based game. You need to guess the correct place where there can be no mine present. You can read more about the Mine Sweeper game from here. For its implementation in Python then follow the current post.
- Board Size: You can select the board size of your choice which is stored as mainList in our program. For example, if you choose a board size of 3 then it will take a board of 3X3 size with 9 boxes in it. The higher the board size, the higher will be the difficulty level for you so choose accordingly.
- Mines: 25% of the total boxes are mined and they all will be blown on one wrong choice by you. These mine locations are stored in mineList in our program.
- Random Location Selection: To select any location you need to enter the row and column number of that box. For example, if you want to choose 2nd element from 3rd row then you need to choose like this.
row = 3 column = 2
- run: This is used to control the main loop for the game. If its value is True then the game will continue to run else the game will stop running.
- mine: This is used to have a track on mines. If its value is False then the user is good to go else the game will stop running with a message You Lost!
- numCount: This keeps track of how many choices the user has done. If it reaches max with no mine selected, then the user will win the game and receives an end message You Won!
- mineList: To create the mineList we need the location of mines as row and column. For this, I have used the random.sample() method to select random non-repeating locations of mines.
- mainList: To create the mainList or board of our game, I have used the nested loops concept.
- currentBoardStatus(): This is a function used to print the game board every time we request.
Rest is explained in the program itself using comments. I have used loop control statements like break, continue. You can read about these from here.
Mine Sweeper game implementation using Python program
Do read comments as they explain a lot and also every block of code.
import random # game variables.. run = True mine = False numCount = 0 mainList =  # main board for the game mineList =  # mine list print('''It's a square board so enter your board size in a single number like, 3 for 3*3 = 9 box board 4 for 4*4 = 16 box board''') boardSize = int(input("Enter board size: ")) # n - number of mines.. n = int((boardSize**2)*(0.25)) # (0.25)25% of total size # creating list of mines.. a1 = random.sample(range(boardSize), n) # for creating non-repeating random nums a2 = random.sample(range(boardSize), n) for i in range(n): mineList.append((a1[i], a2[i])) # creating mainList for the game.. mainList = [['-' for i in range(boardSize)] for j in range(boardSize)] #print(mineList) # To see the mine locations #function to print current status of the board.. def currentBoardStatus(boardSize, mainList): # prints the current status of the board.. print('\nCurrent status of your board is:') for i in range(-1, boardSize): if (i == -1): print(" ", end = " ") else: print(i, end = " ") print("\n") for i in range(boardSize): print(i, end = " ") for j in range(boardSize): print(mainList[i][j], end=" ") print("\n") # main game loop.. print('''Here starts your Minesweeper Game.''') while(run): currentBoardStatus(boardSize, mainList) # user inputs.. row = int(input('Enter row number: ')) column = int(input('Enter column number: ')) # if user choice is out of index then show error.. if (row > boardSize-1 or row < 0 or column > boardSize-1 or column < 0): print('\nWrong choice, choose again.') continue # checks if user select mine or not.. if ((row,column) in mineList): print('\nYou Lost!') run = False for i in mineList: a = i b = i mainList[a][b] = '*' else: if (mainList[row][column] == '-'): mainList[row][column] = random.randint(0,9) numCount += 1 else: pass #checks if user won or not.. if (numCount == boardSize**2-n): print('\nYou Won!') run = False else: pass # prints the final status of the board.. currentBoardStatus(boardSize, mainList)
It's a square board so enter your board size in a single number like, 3 for 3*3 = 9 box board 4 for 4*4 = 16 box board Enter board size: 2 [(0, 0)] Here starts your Minesweeper Game. Current status of your board is: 0 1 0 - - 1 - - Enter row number: 0 Enter column number: 1 Current status of your board is: 0 1 0 - 4 1 - - Enter row number: 1 Enter column number: 0 Current status of your board is: 0 1 0 - 4 1 3 - Enter row number: 1 Enter column number: 1 You Won! Current status of your board is: 0 1 0 - 4 1 3 5