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,
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)):
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.
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))):