2. week

Task 1 - Game design

Motivation

Games which you will be creating during these exercises belong to the group of tile-based games and the method of their design is usually common. Thus, the exercise instructions in the following weeks will describe steps leading to creating a tile-based game and the example will be described on the game Minesweeper by using Java technologies. You will continuously work on the assignment, therefore as the first step you will prepare a repository in the GitLab system, into which you will regularly upload (commit and push) your implementation from each week.

Your task in this exercise is to design the architecture of your game in the form of a class diagram and a state transition diagram. In the last step you will work on the design of the core game logic - generating the game field, game controls, etc.

Objectives

  1. Prepare the repository for your assignment in the system GitLab .
  2. Identify the basic entities, relations and operations of a tile-based game and create a conceptual model of your game.
  3. Identify the fundamental states of the game and their transitions.
  4. Design the core game logic.

Instructions

Step 1

Task 1.1

Create your repository by using Gitlab Classroom application by following the steps below.

  1. By clicking on the application link you will be taken to GitLab to sign in and authorize the GitLab Classroom application created for you by Manager.
  2. By authorizing the application, you will be redirected to the page for creating the project, where you select the study group you attend according to your study timetable. (Foreign students with the study programme in English should select group 07-Streda 09.10 Šťastná.)
  3. Write down tha name of the game that was assigned to you into the text field (e.g. Minesweeper).
  4. By clicking on the button for creating the project, a new empty repository will be created for you in GitLab. You will gradually add you solution of exercises into the repository.

Step 2

When designing the architecture of your game, you will get the inspiration from the game Minesweeper. It is a computer game designed for one player. The goal of the game is to clear the game field without detonating any mines.

The Minesweeper game
Fig. 1: The Minesweeper game

More detailed information about the game can be found e.g. on the Wikipedia and you can play it online on http://minesweeperonline.com/.

When creating a conceptual model, the method of analysing objects and actions is used. The method applies in two directions: from objects (entities) to actions and from actions to objects.

  1. First, the fundamental objects of the designed system (in our case game) are identified. After that, all actions which can be performed with these objects are identified.
  2. When there is no new action left, then all already identifed actions are gradually considered and checked whether some new object may arise as a consequence of executing those actions.

You can repeat this procedure in several iterations. Also, you can identify the most important properties of objects and relations between objects.

Task 2.1

Analyse your game. Based on the objects and their actions, properties and relations identified in the game, create a conceptual model of your game.

One of the representations of a conceptual model is a class diagram.

Classes are derived from objects, methods result from their actions and private fields result from the properties. Relations in the class diagram (inheritance, association, aggregation, composition) arise from the relations between objects.

Comment

You can use some of the online tools to draw UML diagrams (Diagrams.net, smartdraw, UMLet, Diagram Editor, Gliffy) or any other tool according to your preference.

The following figure represents an example of a simple class diagram for the game Minesweeper.

Class diagram of the game Minesweeper
Fig. 2: Class diagram of the game Minesweeper

Task 2.2

Based on the analysis of objects and actions, describe the structure of packages and classes.

When designing tile-based games we recommend to use packages for structuring the code - the main package named after the implementd game, e.g. minesweeper, and inside it a package core for the game core implementation, and a package consoleui for the console user interface of the game.

The basic structure of the project for the game Minesweeper may look like this:

  • minesweeper - the root package, contains the main classes of the application.
    • Minesweeper - the main class with the main method.
  • minesweeper.core - a package contains classes that define the logic of the game field, independently from the user interface.
    • Field - a class representing the game field and its functionality. The game field contains tiles.
    • Tile - a class representing one tile of the game field. Tiles are of two types: a mine Mine and a hint when looking for mines Clue.
    • Mine - a class representing a tile of type mine.
    • Clue - a class representing a tile of type hint when looking for mines.
    • TileState - an enumeration type representing the state of a tile.
    • GameState - an enumeration type representing the state of the game.
  • minesweeper.consoleui - a package that contains classes defining user interaction.
    • ConsoleUI - a class that defines user interaction with the game field.

Comment

The structure of packages and classes of your game may be similar to Minesweeper, but of course, adjust it to the needs of your game.

Step 3

Task 3.1

Identify all tile states and game states. To represent them draw a state transition diagram (or shortly state diagram). In the state diagram, define also the actions (representing method names) which cause transitions between the states.

For example, a tile in the Minesweeper game can be in states: uncovered - OPEN, closed - CLOSED, or marked by a flag - MARKED.

State transition diagram of tile states
Fig. 3: State transition diagram of tile states

The basic game states are usually very similar among various tile-based games. The game can be running (state PLAYING), successfully finished (state SOLVED), or unsuccessfully finished (state FAILED).

Your game might have more or less than three states. For example, in the game Bejeweled, there may be the state NO_POSSIBLE_MOVES, which can occur after any move. In this state a possible action to continue would be to randomly shuffle the tiles.

State transition diagram of game states
Fig. 4: State transition diagram of game states

Comment

When identifying the transitions between states, you might identify a new operation (method). Add all such new operations into the class diagram from the previous step. You can work on the class diagram and state diagrams in parallel.

Step 4

Task 4.1

Describe the basic game logic. You can use a flowchart diagram, sequence diagram or a text description.

Comment

To describe the basic game logic, it is usually necessary to describe:

  • The way of game field generation.
  • The way of verifying the game states - whether the game is won, failed or running.
  • The way of transiting between the tile states, player's moves.

In the Minesweeper game, during the game field generation it is necessary to:

  1. Randomly generate a specified number of mines (mineCount) in the game field.
    1. In a loop, on randomly selected row and column a new mine is inserted - a new instance of the Mine class.
    2. To ensure that no other mine will be inserted on a place where there already is a mine, check if the randomly generated position is empty (contains null).
    3. The loop ends when mineCount mines were inserted into the game field.
  2. The remaining empty places in the field are filled by hint tiles of type Clue.
    • The value of the hint tile is specified by the number of mines that are in neighbouring tiles, which can be implemented in a separate method, named e.g. countAdjacentMines().

After each player's move in the game Minesweeper we check whether the game is failed or solved.

  1. If the player opens a tile of type Mine, the game state transitions to FAILED and the game ends.
  2. The game is considered solved, if the number of all tiles minus number of open tiles equals number of mines.
  3. If the game was not solved nor failed, it continues with the next move of the player.

The player's move consists of selecting the type of an action:

  • opening a tile on selected coordinates if the tile is in the state CLOSED, which can be implemented in a method openTile(int row, int column),
  • marking or un-marking a tile on selected coordinates - if the tile is in the state CLOSED it will go to the state MARKED, and vice-versa. This can be implemented in a method markTile(int row, int column).

Step 5

Task 5.1

Upload (commit and push) files with your game design (diagrams, text documents) into your repository on GitLab. You can update the files as you gradually work on them. Before the next exercise, make sure you uploaded your latest work into your repository. Also, prepare questions that you would like to discuss on the next exercise.

Additional links

  1. More detailed information about the game Minesweeper on Wikipedia
  2. The game Minesweeper online

Tools for creating diagrams:

  1. Diagrams.net
  2. smartdraw
  3. UMLet
  4. Diagram Editor
  5. Gliffy