Interactive programs (Minesweeper Task 4)

Objectives
  1. Familiarize yourself with the issues of interactive systems implementation.
  2. Learn to use regular expressions to process user input.
  3. 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.
    Fig.: State diagram of user interaction
Instructions
  1. Task: Implement the void processInput() method in the ConsoleUI class ensuring the user interaction.
    The method 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).
    Note: Create an object of class Pattern to define the pattern for the input using a regular expression (static method Pattern.compile("O([A-I])([0-8])")). For the given input, check the consistency with the pattern using the object of class Matcher. It is possible to get the Matcher object using the Matcher matcher(CharSequence input) method of the just created Pattern object. To confirm the inputs, use the methods boolean matches() and String group(int group) defined in the Matcher class.
  2. The next step is to add reactions to successful/unsuccessful game completion.
    Task: Add to the method void newGameStarted(Field field) in the ConsoleUI 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 using System.exit(0).
  3. 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 to 0 (none of the nighbouring tiles is a mine).
    Task: Implement a private method void openAdjacentTiles(int row, int column) in the Field class, that will ensure opening of all neighbouring tiles. In this method, use the method void 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 method void openTile(int row, int column) by the call of the method void openAdjacentTiles(int row, int column) in case, when a tile of type Clue with the value 0 was opened.
  4. The actual class model of the application is in the following figure.
    Fig.: Class diagram of the Minesweeper game (at the end of exercise 6)
  5. 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.
comments powered by Disqus