Simple exercise of creating an architecture of a computer game. Tetris game is an easy use case as it has well defined rules.
In this example the following process is followed:
-
Define rules of the game
Very important step. This is a definition of
WHAT
we want to achieve. What kind of product needs to be build. -
Create requrements
Also very important step. This is a list of architectural decisions that will define scope of work, limitations and restrictions. Any compromise that has to be made must be expressed here as a requrement. It is a contract between a customer and a provider. Each requirement has a tag and must be referenced in a design and later in source code. If the code does not reference any requirement, its existance should be questioned. This is also an input for validation to define test criteria and make sure the contract has been met.
-
Use Cases
First step to understand functional requirements. It helps to analyse the product and define interactions between system components. At this step non functional requirements may be captured and the list of requirements have to be updated.
-
Sequence and Collaboration diagrams
Visualisation of what happens in the system. More non functional requirements may be discovered here.
- Tetris Game
I want to write a game where different figures fall from the top of the screen to the bottom. One at the time. Player can shift the figures left and right and also can rotate the figure in both directions. If the player does nothing, the figure will advance one step down after some time. Once the figure is at the bottom, it cannot move anymore. Figure stays there. A new figure shows up at the top of the screen. By moving figures, player can control where the figures will be placed at the botom of the screen. Figures are made of blocks. Screen is made of lines, which are made of blocks as well. Once figures fill in a full line with blocks, that line is removed from the screen. Any remaining blocks above that line will drop one line down.
Game last as long as player can make lines to be removed so there is a space for new figures.
Blocks cannot overlap. So, the game is over when it is impossible to place a new figure on the top of the screen.
Requirement | Description |
---|---|
REQ_SinglePlayer | There is a single player |
REQ_Cmd | Commands are: Translate Left, Translate Right, Translate Down, Rotate Left, Rotate Right. |
REQ_NoPendingCommands | Commands are not buffered. New command overwrites the previews one if that one has not be executed yet |
REQ_LineOfBlocks | Single line is made of blocks. |
REQ_LineFull | Line that has no empty spaces, that is, when is made of blocks, is removed from the screen. |
REQ_ScreenSize | Screen has fixed dimentions of 10 rows and 8 columns. Screen is made of lines. |
REQ_BlocksDrop | When line is removed from the screen, all the lines being above the removed line, are moved down. The most top line of the screen becomes empty. |
REQ_FiguresType | There is 5 defined figures in the game |
REQ_MoveLimit | Figure must not be placed outside borders of the screen. That is, figure cannot move left when is at the left border. Figure cannot move right when is at the right border. Figure cannot move down when is at the bottom of the screen. |
REQ_FigureLifeTime | Player looses a control of a figure that cannot advance down (is at the bottom of the screen). New figure is generated at the top of the screen. |
REQ_BlocksNotOverlap | Single block takes space. Figures are made of blocks. Two blocks cannot take the same space. This implicates that figures cannot overlap. This also implicates that the figures cannot move (translate or rotate) when the new position would overlap with any block of a line. |
REQ_OnTimerCommand | Translate Down command will be generated after 1 second if player does not input any command in that period. |
-
Square
O
-
BigSquare
OO OO
-
Bar
O O O
-
BarT
OOO O
Player and timer trigger state changes. All state control happens in the main loop.
Requirements: REQ_OnTimerCommand
Timer event moves a figure only down. No rotation or shifting left or right.
Requirements: REQ_SinglePlayer
There is a single input. Input represents a player.
Requirements: REQ_Cmd
Requirements: REQ_FiguresType
Creating a game means to create screen, generate a random figure and start the timer.
Restarting a game is to clear the screen and create a figure.
Requirements: REQ_NoPendingCommands, REQ_BlocksNotOverlap, REQ_MoveLimit
Figure rotation succeeds when the figure does not overlap any blocks after a rotation. Any new command arriving during that process is dropped.
Requirements: REQ_NoPendingCommands, REQ_BlocksNotOverlap, REQ_MoveLimit
Figure rotation fails when the figure overlaps other blocks after a rotation. Any new command arriving during that process is dropped.
Requirements: REQ_NoPendingCommands, REQ_BlocksNotOverlap, REQ_MoveLimit
Figure translation succeeds when the figure does not overlap any blocks after a translation. Any new command arriving during that process is dropped.
Requirements: REQ_NoPendingCommands, REQ_BlocksNotOverlap, REQ_MoveLimit
Figure translation fails when the figure overlaps other blocks after a translation. Any new command arriving during that process is dropped.
Requirements: REQ_BlocksNotOverlap, REQ_OnTimerCommand, REQ_MoveLimit
Time advances the figure down after a timeout. It is a regular translation process.
Requirements: REQ_BlocksNotOverlap, REQ_MoveLimit, REQ_BlocksDrop
A new figure is created and will live until it cannot move down anymore. If it can move, either user can request the figure to move or timer tick can move it down.
Requirements: REQ_BlocksNotOverlap, REQ_MoveLimit
A figure cannot move beyond the screen's dimentions and it cannot overlap with blocks already on the screen.
Requirements: REQ_BlocksNotOverlap, REQ_MoveLimit, REQ_BlocksDrop
This is a use case when a figure cannot move down anymore. Its blocks are taken to fill empty gaps. In a case when a line does not have gaps (line is full), the line will be removed and all lines above are moved down. It is recursive operation from the most bottom line to the most top.
Requirements: REQ_BlocksNotOverlap
Game is over when a new figure cannot be created. This will be a case when checking overlap sequence returns error.