Interfaces (Minesweeper Task 3)

Objectives
  1. Practice creating object-based programs in the Java language.
  2. Understand the importance of interfaces in object-oriented programming.
  3. Learn to create interfaces in the Java language.
  4. Implement generating of the playing field for the Minesweeper game and playing field display.
Introduction
    One of the requirements on the game is the possiblity to choose the game user interface (console or graphical Swing user interface). Exactly from this reason it is necessary to ensure the independence of the application game logic on the implementation of the user interface. The reason of using interfaces is just to ensure implementation independence of individual system parts. To create an interface, which will specifiy the prescript to connect different types of user interfaces follow the steps listed below.
Instructions
  1. Task: In the minesweeper package define the interface UserInterface as a prescription, which has to be abided by all user interfaces of the Minesweeper application.
    Generate this interface from the existing source code of the ConsoleUI class using the following steps.
    • Set the cursor into the ConsoleUI source code directly on this class's name.
    • Click the right mouse button and select „Refactor > Extract Interface…” from the context menu.
    • Write the name of the interface UserInterface into the text field.
    • Select methods newGameStarted and update that are to be included in the created interface. Continue next.
    • Move the created interface UserInterface into the package minesweeper by using Drag&Drop in the tree structure of the project (the „Projects” tab).
    • In the Minesweeper class change the userInterface field type from ConsoleUI to UserInterface.
  2. In this step we will display the playing field into the standard output. To display the playing field, the following requirements are given:
    • The rows are marked by upper case letters ordered alphabetically (A, B, ..., I).
    • The columns are marked by numbers (0, 1, ... , 8).
    • To draw an uncovered tile (OPEN) of type mine (Mine) the character X is used.
    • To draw a tile in state (OPEN) of type help (Clue) the number representing the tile value is used.
    • To draw a marked tile (MARKED) the M character is used.
    • To draw a covered and unmarked tile (CLOSED) the dash - character is used.
    Task: Implement the void update() method in class ConsoleUI, which ensures displaying of the playing field.
    To display the formatted output (e.g. when displaying the marked rows) use the System.out.printf(...) method.
  3. After each opening of a tile by the user, a test of completing the game is performed (successfully or unsuccessfully).
    Task: Implement the boolean isSolved() method defined in the Field class, which determines the successfull revelation of the playing field.
    The game is successfully completed, if holds: number of all tiles - number of open tiles = number of mines. From the above stated formula it holds, that the game will be won, if all tiles except mines are open. To determine the number of uncovered open tiles, define a private method int getNumberOf(Tile.State state), which returns the number of tiles in the given state.
  4. The current model of application classes is on the following figure.
    Fig.: Class diagram of the Minesweeper application (at the end of exercise 5)
comments powered by Disqus