Motivation
The games you will be implementing during these exercises mostly belong to the group of tile-based games, and their design usually follows a similar pattern. The exercises in the following weeks will therefore describe the sequence of steps leading to the creation of a tile-based game, and the demonstration will be described on the game Minesweeper using Java technology. You will work on the assignment continuously, so among the first steps you will clone the GitLab repository, into which you will regularly upload the implementation from each week.
Your task in this exercise is to create a design of your game in the form of a class diagram and a state diagram. In the last step, you will focus on designing the basic game logic - generation of the game field, controls, etc.
Objectives
- Create and clone the project repository from GitLab Classroom and open it in IntelliJ IDEA.
- Identify the basic entities, relations and operations of a tile-based game and create a conceptual model of your game.
- Identify the fundamental states of the game and their transitions.
- Design the core game logic.
Instructions
Step 1
Task 1.1
Create your project by using the GitLab Classroom application following the steps below.
- By clicking the link above, you will be prompted in GitLab to sign in and authorize the GitLab Classroom application prepared for you by the Manager.
- After authorizing the application, you will be redirected to the page for creating projects, where you select the group according to the timetable slot you attend in the course.
- Enter the name of the game you are solving as part of the assignment (e.g. Minesweeper).
- By clicking the button for creating the project, a repository with a prepared Maven structure of the GameStudio project will be created for you in GitLab. You will use this project throughout the semester.
Task 1.2
Clone the created repository to your computer and open it in IntelliJ IDEA. Get familiar with the structure of the prepared Maven project.
Comment
If your repository is empty, ask the instructor for help.
The basic directory structure of the project is as follows:
text gamestudio // main project directory
├── src
│ ├── main // source files of the project
│ │ └── java
│ │ └── sk
│ │ └── tuke
│ │ └── gamestudio // main package of the GameStudio project
│ │ └── game // package for game implementations
│ │ ├── minesweeper
│ │ └── yourgame - package with the name of your game
│ └── test // source files for tests
│ └── java // your tests go into this directory
├── .gitignore
└── pom.xml // Maven configuration file
The project is managed as a [Maven] project - the pom.xml configuration file contains the basic dependencies (libraries) needed for further work.
Comment
After opening the project in IntelliJ IDEA, the IDE may show a Maven notification to load dependencies. Confirm it so the required libraries are downloaded.
Step 2
Use the game Minesweeper as inspiration when designing your own game. Minesweeper is a computer game for a single player. The goal of the game is to clear the game field from mines without detonating them.
More information about this game can be found for example on Wikipedia and you can play it online at http://minesweeperonline.com/.
When creating a conceptual model, the method of object and action analysis is used, proceeding in two directions: from objects (entities) to actions and from actions to objects.
- First, the basic objects of the designed system (in our case, the game) are identified, and then all actions that can be performed with those objects are identified.
- When no new action appears, all already identified actions are gradually reviewed to check whether a new object is needed for their execution.
This procedure can be repeated. The most important properties of objects and relations between individual objects are also identified.
Task 2.1
Analyse your game. Based on the objects and their actions, properties and relations that you identify in the game, create a conceptual model of your game.
One representation of a conceptual model may be a class diagram.
From objects we derive classes, from their actions we derive methods, from their properties we derive class variables, and from relations between objects we derive relations between classes in the class diagram (inheritance, association, aggregation, composition).
Comment
You may use one of the tools for drawing UML diagrams (Diagrams.net, smartdraw, UMLet, Diagram Editor, Gliffy) or another tool of your choice.
The following image shows an example of a simple class diagram for 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 using packages to structure the code. Place the source code of your game into the package sk.tuke.gamestudio.game in src/main/java. Inside this package, create a subpackage named after your game, for example sk.tuke.gamestudio.game.minesweeper, and inside it further subpackages for the game core (core) and console interface (consoleui).
The basic package structure for the game Minesweeper may look like this:
sk.tuke.gamestudio.game.minesweeper- the main game package, containing the main application classes.Minesweeper- the main application class with themainmethod.
sk.tuke.gamestudio.game.minesweeper.core- package containing classes defining the game field logic, independent of the user interface.Field- class representing the game field and its functionality. The field contains tiles.Tile- class representing one tile of the game field. Tiles are of two types: a mineMineand a clueClue.Mine- class representing a tile of type mine.Clue- class representing a tile of type clue.TileState- enumeration type representing a tile state.GameState- enumeration type representing a game state.
sk.tuke.gamestudio.game.minesweeper.consoleui- package containing classes defining user interaction.ConsoleUI- class defining interaction between the game and the user.
Comment
The structure of packages and classes in tile-based games may be similar, so feel free to take inspiration, but adapt it appropriately to your game.
Step 3
Task 3.1
Identify all tile states and game states. Draw a state diagram to represent and describe them. In the state diagram, also mark the actions (representing method names) that cause transitions between states.
For example, a tile in the game Minesweeper can be in state OPEN (revealed), CLOSED (hidden), or MARKED.
The basic game states in simple tile-based games are usually similar: the game can be in progress (state PLAYING), successfully finished (state SOLVED), or unsuccessfully finished (state FAILED).
However, your game may have more states. For example, in the game Bejeweled, there may be a state NO_POSSIBLE_MOVES, which the player can reach after any move. In this state, one possible action is random shuffling of tiles.
Comment
When identifying transitions between tile and game states, you may discover a new operation (method). Add all such new operations to the class diagram from the previous step. You can work on the class diagram and the state diagram in parallel.
Step 4
Task 4.1
Describe the basic game logic. You may use a flowchart, 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 checking game states - whether the game is won, lost, or still in progress,
- the way of transitions between tile states, i.e. player moves.
In the game Minesweeper, during game field generation, it is necessary to:
- Randomly generate the specified number of mines
mineCountin the game field.- In a loop, a mine - an instance of class
Mine- is placed into the tile field (field) at a randomly chosen row and column. - To ensure that no mine is placed where a mine already exists, check whether the randomly generated position is empty.
- The loop ends when the required number of mines (
mineCount) has been placed.
- In a loop, a mine - an instance of class
- The remaining empty places in the field are filled with clue tiles of type
Clue.- The value of a clue tile is determined by counting the mines in adjacent tiles, which can be implemented in a separate method, e.g.
countAdjacentMines().
- The value of a clue tile is determined by counting the mines in adjacent tiles, which can be implemented in a separate method, e.g.
After each move in the game Minesweeper, it is checked whether the game is won or lost.
- If the player opens a tile of type
Mine, the game is marked as lost and ends. - The game is considered won if: the number of all tiles minus the number of revealed tiles equals the number of mines.
- If the game is neither won nor lost, it continues with the next player move.
A player move consists of choosing an action type:
- opening a tile, only if the tile is in state
CLOSED, implemented for example by the methodopenTile(int row, int column), - marking or unmarking a tile - if the tile is in state
CLOSED, it transitions toMARKEDand vice versa, implemented for example by the methodmarkTile(int row, int column).
Step 5
Task 5.1
Upload (commit and push) your design files (diagrams, text documents) to your GitLab repository. You can update the files as you continue working on them. Before the next exercise, make sure your latest files are in the repository. Also prepare questions you would like to discuss in the next exercise.
Resources
- More detailed information about the game Minesweeper on Wikipedia
- The game Minesweeper online
- Learn more about the Maven project and its structure, for example on the official website or in the documentation.
Tools for creating diagrams: