Objects and classes (Minesweeper Task 2)

Objectives
  1. Learn to create and use your own packages in the Java language.
  2. Learn to use refactorization Encapsulate Fields in the NetBeans IDE.
  3. 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.
    Fig.: Class diagram of the Minesweeper application (at the and of excercise 4)
Instructions
  1. 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 method int getValue() into the Clue class.
    Select the field value (set the cursor on the value field declaration in the Clue 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 methods int getRowCount(), int getColumnCount(), int getMineCount() and GameState getState() into the Field class by using the refactorization Encapsulate Fields.
    Task: Add the method Tile getTile(int row, int column) into the Field class. This method returns a tile according to the given row and column. Rows and columns are numbered from 0.
    Task: Implement the void markTile(int row, int column) method in the Field 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 from 0.
    Note: When implementing the void markTile(int row, int column) method, get inspired by the void openTile(int row, int column) method.
  2. The next step when implementing the Minesweeper game is to randomly generate the playing field content.
    Task: Implement the method void generate() in the Field 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 the mineCount 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
    Get inspired by the following code fragment.
    
    private void generate() {
        generateMines();
        fillWithClues();
    }
    
    Note: To randomly generate numbers, create an object of class java.util.Random (use the object's int nextInt(int n) method).
    Note: Insert a tile of type Mine 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 type Clue (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 (holds tiles[row][column] == null) determine the number of neighbooring mines by using the already implemented method int countAdjacentMines(int row, int column).
  3. Comment all so far created methods.
comments powered by Disqus