null
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 1/16)
Let's write the entertaining Minesweeper game. Rules of the game: 1. The playing field is divided into adjacent cells (squares), some of which contain "mines". 2. The objective of the game is to clear the field, i.e. reveal all cells that don't contain mines. 3. If a cell with a mine is revealed
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 2/16)
Our playing field is kind of small, don't you think? It should be 9x9 cells: 1) There is a setScreenSize(int, int) method to set the size of the field 2) This method takes width and height arguments (the number of cells wide and high) 3) the dimensions of the field will be used frequently, so it's
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 3/16)
Because our game will use game objects (cells), we'll create a separate GameObject class to describe them. Let's think about what the game object needs. Naturally, we need its x and y coordinates on the playing field. And how can we get by without a constructor? After you do everything, don't forge
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 4/16)
We need to store the state of the playing field's cells somewhere. To do this, create a matrix (two-dimensional array) with the dimensions of the playing field. Populate the matrix with GameObject objects using this formula: gameField[y][x] = new GameObject(x, y); Now let's display all the cells o
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 5/16)
Minesweeper is aptly named. The player has to deal with mines, though maybe it would be best for the player to stay far away from them :) Let's add these mines to the game. To do this, we'll adapt the GameObject class to account for the fact that a cell can be a mine (by adding the isMine flag). Do
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 6/16)
Now let's count the mines in adjacent cells. To do this, we'll add a field to each cell of the matrix, which will be responsible for the number of mined neighbors. First, we find all the neighbors, and then we count how many of them are mined. To get the list of neighbors, you now have the getNeigh
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 7/16)
At this stage, we'll deal with the logic to reveal cells. Let's create a corresponding method and a variable that will store the cell state (revealed or hidden). When revealing a cell, the method will: - draw a mine in the cell if it contains a mine; - display the number of mined neighbors if the c
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 8/16)
The rules of the game provide flags to help the player. They are needed to mark cells that might be mined. Accordingly, the number of flags (countFlags) must be equal to the number of mines (countMinesOnField). You need to show flags somehow on the playing field. To do this, we'll create a constant
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 9/16)
According to the rules, when a cell without adjacent mines is revealed, the game reveals the "unmined" area up to cells with mined neighbors. Let's implement it... I suggest using recursion. It is used when the algorithm for solving the problem is the same as the algorithm for solving part of the p
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 10/16)
Now let's tackle the method responsible for flagging a cell. We'll call it markTile. It must: - mark a cell on the playing field with a flag, or remove a flag from a cell; - track the number of flags; - handle drawing and erasing flags on the playing field; - change the a cell's color if a flag is
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 11/16)
In almost any game, the player either wins or loses. Let's first implement the logic for losing. To do this, we need a gameOver() method that will stop the game and inform the player about the loss. To display a message to the player, the engine has a showMessageDialog() method. To track whether th
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 12/16)
A cell can be revealed only once. If a cell is flagged, it must not be revealed. And, of course, cells shouldn't be revealed after the game has stopped. Account for all of these points in the openTile(int, int) method. Now you need to be sure that everything works correctly — start the game and che
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 13/16)
The game is won when the number of remaining hidden cells is equal to the number of mines. To count the number of hidden cells, we'll introduce another variable whose initial value is equal to the total number of cells on the playing field. The value of this variable will decrease as the cells are
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 14/16)
Admit it — It's not very interesting to play without a score. Let's add this functionality to our game. We will need a variable to track points. When an unmined cell is revealed, the score increases by 5 points. To display the score on the playing field, we need to use the Game class's setScore() m
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 15/16)
As you know, a real minesweeper is mistaken only once. But, fortunately, we can make lots of mistakes in our game. Let's implement a way to restart the game. To do this, we'll create a restart() method, where we will start the game, and reset the number of hidden cells, mines, and points. Don't for
undefined
3
Task
Games, level 0, lesson 0
Locked
Minesweeper (Part 16/16)
Congratulations! The game is done! Run it and see what happens. If you have time and motivation, you can improve the game by adding some features of your own. For example: • limit the number of moves; • make it so the first move never hits a mine; • make the game easier or harder by changing