Objectives
- Familiarize yourself with the issues of interactive systems implementation.
- Learn to use regular expressions to process user input.
- Learn to use recursive functions.
Introduction
-
The task of today's exercise is to implement the user interaction.
During the user interaction it is necessary to ensure requesting the data from the user, confirm the correctness
of the input data, ensure the feedback for the user in case of incorrectly inputted data and perform the requested
operation.
Instructions
-
Task: Implement theThe method
void processInput()
method in theConsoleUI
class ensuring the user interaction.void processInput()
should:-
Write the request to input along with the pattern of expected user input:
X
– end the game,MA1
– mark tile in the row A and column 1,OB4
– open tile in the row B and column 4. -
Load the input user request (method
String readLine()
). - Confirm the correctness of the input string. To confirm the input correctness use regular expressions.
- In case of such a user request, which is not in the required format, it is necessary to request the user to re-enter.
- Based on the identification of the requested action, perform the requested operation (mark tile, open tile, end the program).
-
Write the request to input along with the pattern of expected user input:
-
The next step is to add reactions to successful/unsuccessful game completion.Task: Add to the method
void newGameStarted(Field field)
in theConsoleUI
class a test of successful (field.getState() == GameState.SOLVED
) or unsuccessfull (field.getState() == GameState.FAILED
) game completion.Note: In case of successful or unsuccessful game completion, write a notice about game completion and end the game usingSystem.exit(0)
. -
One of the Minesweeper game functionality is the automatic uncovering of all neighbouring tiles in such a case, when a
Clue
tile is open with a value equal to0
(none of the nighbouring tiles is a mine).Task: Implement a private methodvoid openAdjacentTiles(int row, int column)
in theField
class, that will ensure opening of all neighbouring tiles. In this method, use the methodvoid openTile(int row, int column)
to open all neighbouring tiles.Note: When implementing this method, don't forget the cases when the tile is on the edge or in the corner of the playing field (ArrayIndexOutOfBoundsException
).Task: Supplement the methodvoid openTile(int row, int column)
by the call of the methodvoid openAdjacentTiles(int row, int column)
in case, when a tile of typeClue
with the value0
was opened. -
Test the functionality and usability of the Minesweeper game.
Additional tasks
Task:
Accept accepting a row with both lower case and upper case letter in the user input for
tile marking –
M/m
,
tile opening – O/o
and exit - X/x
.
Pattern
to define the pattern for the input using a regular expression (static methodPattern.compile("O([A-I])([0-8])")
). For the given input, check the consistency with the pattern using the object of classMatcher
. It is possible to get theMatcher
object using theMatcher matcher(CharSequence input)
method of the just createdPattern
object. To confirm the inputs, use the methodsboolean matches()
andString group(int group)
defined in theMatcher
class.