FreeRTOS Tetris
Data Structures | Macros | Typedefs | Functions
Logic Module

Module handling the logic components of the game. More...

Collaboration diagram for Logic Module:

Data Structures

struct  tetromino
 Structure representing a Tetromino. More...
 
struct  score
 Structure for the score. More...
 

Macros

#define EMPTY_SPACE   0
 If an tetromino does not contain a block in specific position, the value is set to 0.
 

Typedefs

typedef struct tetromino tetromino_t
 Structure representing a Tetromino.
 
typedef struct score score_t
 Structure for the score.
 

Functions

void vLogicInitTetromino (tetromino_t *tetromino, tetromino_type_t types[], int *index, player_mode_t playerMode)
 Initialize a Tetromino. More...
 
bool bLogicCheckMove (const color_t newShape[FIGURE_SIZE][FIGURE_SIZE], coord_t newPosition, const color_t landed[ROWS][COLS])
 Check if a move to a newPosition is possible. More...
 
bool bLogicCheckGameOver (const tetromino_t *tetromino, const color_t landed[ROWS][COLS])
 Check if the game will be over if the new tetromino is created. More...
 
void vLogicUpdateXCoord (tetromino_t *tetromino, const color_t landed[ROWS][COLS], int pressedButton)
 Update the x coordinate of tetromino. More...
 
bool bLogicUpdateYCoord (tetromino_t *tetromino, const color_t landed[ROWS][COLS])
 Update the y coordinate of tetromino. More...
 
void vLogicRotate (tetromino_t *tetromino, const color_t landed[ROWS][COLS], rotation_t rotationMode)
 Rotate tetromino, if possible. More...
 
void vLogicAddToLanded (const tetromino_t *tetromino, color_t landed[ROWS][COLS])
 Add tetromino to the landed array. More...
 
bool vLogicRowFull (color_t landed[ROWS][COLS], score_t *score)
 Check how many rows are full and remove them. More...
 
static void setTetrominoType (tetromino_t *tetromino, tetromino_type_t types[], int *index)
 Set a Tetromino's type. More...
 
static void increaseScore (score_t *score, uint8_t rowsAmount)
 Increase score if a row is full. More...
 
static void setTetrominoShape (color_t color, tetromino_type_t type, color_t shape[FIGURE_SIZE][FIGURE_SIZE], int rotateAmount, bool init)
 Set the shape array of the tetromino, depending on the rotation & type. More...
 

Initial coordinates

#define INIT_COORDS_X   COLS/2 -1
 Initial x coordinate.
 
#define INIT_COORDS_Y   0
 Initial y coordinate.
 

Pressed keys

#define LEFT_PRESSED   1
 Left-arrow key.
 
#define RIGHT_PRESSED   2
 Right-arrow key.
 
#define DOWN_PRESSED   3
 Down-arrow key.
 

Detailed Description

Module handling the logic components of the game.

This includes calculating new coordinates, checking if a Tetromino can move/rotate, checking for complete rows and more.

Authors
Philipp Karg (phili.nosp@m.pp.k.nosp@m.arg@t.nosp@m.um.d.nosp@m.e)
Date
04.02.2021

Function Documentation

◆ bLogicCheckGameOver()

bool bLogicCheckGameOver ( const tetromino_t tetromino,
const color_t  landed[ROWS][COLS] 
)

#include <logic.h>

Check if the game will be over if the new tetromino is created.

Parameters
[in]tetromino(const tetromino_t *): Tetromino to check.
[in]landed(const color_t [][]): Array of landed Tetrominos.
Returns
(bool): whether the game will be over.

◆ bLogicCheckMove()

bool bLogicCheckMove ( const color_t  newShape[FIGURE_SIZE][FIGURE_SIZE],
coord_t  newPosition,
const color_t  landed[ROWS][COLS] 
)

#include <logic.h>

Check if a move to a newPosition is possible.

Check for possible collisions on the side borders, the ground and with the landed tetrominos.

Parameters
[in]newShape(const color_t): New shape to check.
[in]newPosition( coord_t): New position to check.
[in]landed(const landed): Array of landed Tetrominos.
Returns
(bool): whether a move to a newPosition is possible.

◆ bLogicUpdateYCoord()

bool bLogicUpdateYCoord ( tetromino_t tetromino,
const color_t  landed[ROWS][COLS] 
)

#include <logic.h>

Update the y coordinate of tetromino.

Parameters
[in,out]tetromino( tetromino_t *): Tetromino object to increase y coordinate of.
[in]landed(const color_t [][]): Array of landed Tetrominos.
Returns
(bool): true if the y coordinate could be increased.

◆ increaseScore()

static void increaseScore ( score_t score,
uint8_t  rowsAmount 
)
static

#include <logic.c>

Increase score if a row is full.

Parameters
[in]score( score_t *): Score object to change.
[in]rowsAmount(uint8_t): NUmber of rows cleared.

◆ setTetrominoShape()

static void setTetrominoShape ( color_t  color,
tetromino_type_t  type,
color_t  shape[FIGURE_SIZE][FIGURE_SIZE],
int  rotateAmount,
bool  init 
)
static

#include <logic.c>

Set the shape array of the tetromino, depending on the rotation & type.

Parameters
[in]color( color_t): Color of the tetromino.
[in]type( tetromino_type_t): Type of the tetromino.
[in]shape( color_t [][]): Shape array to set.
[in]rotateAmount(int): Amount of rotation for the shape.
[in]init(bool): For some Tetrominos, the shape is set differently at the start.

◆ setTetrominoType()

static void setTetrominoType ( tetromino_t tetromino,
tetromino_type_t  types[],
int *  index 
)
static

#include <logic.c>

Set a Tetromino's type.

Randomly take one of the 7 possible tetromino types and reduce the number of possible types, so that every type is selected once for every seven new tetrominos.

Parameters
[out]tetromino( tetromino_t *): Tetromino to set type for.
[in,out]types( tetromino_type_t []): Array of tetromino types.
[in,out]index(int*): Current index of the types array.

◆ vLogicAddToLanded()

void vLogicAddToLanded ( const tetromino_t tetromino,
color_t  landed[ROWS][COLS] 
)

#include <logic.h>

Add tetromino to the landed array.

Parameters
[in]tetromino(const tetromino_t *): Tetromino to add
[out]landed( color_t [][]): Array of landed Tetrominos.

◆ vLogicInitTetromino()

void vLogicInitTetromino ( tetromino_t tetromino,
tetromino_type_t  types[],
int *  index,
player_mode_t  playerMode 
)

#include <logic.h>

Initialize a Tetromino.

  1. Set the Tetromino's type by calling setTetrominoType().
  2. Set rotation
  3. Set color
  4. Init the position
  5. Set the shape array.
Parameters
[out]tetromino( tetromino_t *): Tetromino to initialize.
[in]types( tetromino_type_t [][]): Array of possible types.
[in]index(int*): Index of the types-array.
[in]playerMode( player_mode_t): Current player mode.

◆ vLogicRotate()

void vLogicRotate ( tetromino_t tetromino,
const color_t  landed[ROWS][COLS],
rotation_t  rotationMode 
)

#include <logic.h>

Rotate tetromino, if possible.

  1. Increase/decrease tetromino's rotation counter, depending on rotationMode.
  2. Set tetromino's new shape, by calling setTetrominoShape().
  3. Check if that new shape is possible with bLogicCheckMove().
  4. If it is, copy the new shape to the old shape.
Parameters
[in,out]tetromino( tetromino_t *): Tetromino object to rotate.
[in]landed(const color_t [][]): Array of landed Tetrominos.
[in]rotationMode( rotation_t): Rotation mode, either left or right.

◆ vLogicRowFull()

bool vLogicRowFull ( color_t  landed[ROWS][COLS],
score_t score 
)

#include <logic.h>

Check how many rows are full and remove them.

Parameters
[in,out]landed( color_t [][]): Array of landed Tetrominos.
[in,out]score( score_t *): Score object to increase if rows are full.
Returns
(bool): true if one or more rows are full. False otherwise.

◆ vLogicUpdateXCoord()

void vLogicUpdateXCoord ( tetromino_t tetromino,
const color_t  landed[ROWS][COLS],
int  pressedButton 
)

#include <logic.h>

Update the x coordinate of tetromino.

If the pressedButton is LEFT_PRESSED/RIGHT_PRESSED, try to decrease/increase the position by 1.

Parameters
[in,out]tetromino( tetromino_t *): Tetromino object to increase x coordinate of.
[in]landed(const color_t [][]): Array of landed Tetrominos.
[in]pressedButton(int): The button that was pressed.