Objectives
- Practice creating object-based programs in the Java language.
- Understand the importance of interfaces in object-oriented programming.
- Learn to create interfaces in the Java language.
- 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
-
Task: In theGenerate this interface from the existing source code of the
minesweeper
package define the interfaceUserInterface
as a prescription, which has to be abided by all user interfaces of the Minesweeper application.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
andupdate
that are to be included in the created interface. Continue next. -
Move the created interface
UserInterface
into the packageminesweeper
by using Drag&Drop in the tree structure of the project (the „Projects” tab). -
In the
Minesweeper
class change theuserInterface
field type fromConsoleUI
toUserInterface
.
-
Set the cursor into the
-
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 characterX
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
) theM
character is used. -
To draw a covered and unmarked tile (
CLOSED
) the dash-
character is used.
Task: Implement theTo display the formatted output (e.g. when displaying the marked rows) use thevoid update()
method in classConsoleUI
, which ensures displaying of the playing field.System.out.printf(...)
method. -
After each opening of a tile by the user, a test of completing the game is performed (successfully or unsuccessfully).Task: Implement theThe 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
boolean isSolved()
method defined in theField
class, which determines the successfull revelation of the playing field.int getNumberOf(Tile.State state)
, which returns the number of tiles in the given state.