[ Python ] Minesweeper Game




I made a MineSweeper app using PyQt5 during break

First, I wrote basic information such as game level, board size, flag information, and icon image path into the list. (a kind of config concept) 

and wrote the first classes for the main menu, level map.  

next, wrote the second class includes flag information that can control events, grid style information, etc. When the program starts, a menu window appears like below. 

I‘ve set up 6 difficulty levels. Then assigned lists using QListWidget()


Key point looks like this,  

self.levelChoiceList = QListWidget()
menuLayout.addWidget(self.levelChoiceList, alignment = Qt.AlignCenter)

When the user selects a level and map size and presses the start button, level window is created based on user's values.




In the level window, there are Home(Menu), Status Icon, Reset, and grids to be spread as much as the board size horizontally and vertically(rows and columns) .
All grid of cells arranged in rows and columns using addWidget() method.


When initializing the level status, all list of grids should completely empted, and number of bombs are randomly placed on the whole grid. It is also important to flag for mine = True where mine is placed! (to check for failure or not)

I used this type of function to initialize the grids and find the adjacent positions, also connect the control events at this status. something like this below


    for row in range(0, int(self.board_size)):

        for column in range(0 ,int(self.board_size)):
            self.centralWidget = Position(x, y, int(self.board_size))
            self.grid.addWidget(self.centralWidget, y, x)

            # connect event
            self.MainWidget.clicked.connect(self.eventStart)
            self.MainWidget.expand.connect(self.expandRevealGrid)
            self.MainWidget.expand1.connect(self.evaluation)
            self.MainWidget.warn.connect(self.gameFail)
            ....


When the grid is opened by click event, we need to check whether the mine value displayed or not. For these actions, the location surrounding the mine must be checked with 3x3 blocks.


Top

Left

0,0 

Top

Center

1,0

Top

Right

2,0

Center

Left

0,1

1,1

Center

Right

1,2

Bottom

Left

0,2

Bottom

Center

1,2

Bottom

Right

2,2



9 grid blocks' position must be greater than or equal to 0 and less than or equal to the board size. Repeat this method till board_size


    for rangeX in range(max(0, int(x) - 1), min(int(x) + 2, int(self.board_size))):
        for rangeY in range(max(0, int(y) - 1), min(int(y) + 2, int(self.board_size))):



That also needs surrounding positions information. This means includes grid's block edge does not exceed the boundary, returning the result of searching all cells as a list. 


The basic formulas are roughly as follows. It took me a while to add the GUI functionality to this and add the option to restart the level or decide whether to succeed or fail.



Basic concept of minsweeper mine set up code for console version is here link 


and executable files are here


😉//