Objectives
- Learn to create and use your own packages in the Java language.
- Learn to use refactorization Encapsulate Fields in the NetBeans IDE.
- Try the implementation of object-oriented programs.
Introduction
-
In the model, there are methods, which are not present in the source code.
The today's exercise's task is to modify the source code so it would correspond to the model.
Instructions
-
During the implementation use the refactorization
Encapsulate Fields
in the NetBeans IDE. Using this refactorization it is possible to generate set or get method for the selected field - encapsulation.Task: Add the methodSelect the fieldint getValue()
into theClue
class.value
(set the cursor on thevalue
field declaration in theClue
class), press the right mouse button and select „Refactor > Encapsulate Fields” from the context menu. Ensure that the „Create Getter” option is selected and continue by pressing on the „Next” button.Note: In the IntelliJ Idea environment the "Create Getter" option is not available, you have to select the "Get access" and "Set access" options in the dialog. You can also select all fields that you need the get and set methods to generate.Task: Add the methodsint getRowCount()
,int getColumnCount()
,int getMineCount()
andGameState getState()
into theField
class by using the refactorizationEncapsulate Fields
.Task: Add the methodTile getTile(int row, int column)
into theField
class. This method returns a tile according to the given row and column. Rows and columns are numbered from0
.Task: Implement thevoid markTile(int row, int column)
method in theField
class. The method serves to marking and unmarking tiles specified by the row and column. In case the tile si covered (CLOSED
), its state will be changed to (MARKED
). If the tile is marked (MARKED
), its state will be changed to covered (CLOSED
). Rows and columns are numbered from0
.Note: When implementing thevoid markTile(int row, int column)
method, get inspired by thevoid openTile(int row, int column)
method. -
The next step when implementing the Minesweeper game is to randomly generate the playing field content.Task: Implement the method
void generate()
in theField
class so that it would put mines randomly into the playing field (Mine
) and supplemented the auxiliary fields (Clue
), while the number of mines, which should be put into the playing fiels is given by themineCount
field.Note: Divide the random generation of the playing field into two phases- randomly insert mines into the field - the
void generateMines()
method - adding the auxiliary fields to places, which don't contain any mine - the
void fillWithClues()
method
private void generate() { generateMines(); fillWithClues(); }
Note: To randomly generate numbers, create an object of classjava.util.Random
(use the object'sint nextInt(int n)
method).Note: Insert a tile of typeMine
into the playing field on randomly chosen coordinates, but only if there no mine yet inserted on those coordinates (tiles[row][column] == null
). Repeat the procedure until the required number of mines is inserted. After realizing this task, the playing field will contain only tiles of type mine.Note: Complete the playing field by adding tiles of typeClue
(mine search help). For this type of tile it is necessary to set the value identifying the number of mine tiles in its direct neighborhood.Note: Go through the whole playing field in a loop. For each unfilled position (holdstiles[row][column] == null
) determine the number of neighbooring mines by using the already implemented methodint countAdjacentMines(int row, int column)
. - randomly insert mines into the field - the
-
Comment all so far created methods.