Skip to content

Commit

Permalink
Reduce max board size to 128x128 for space savings
Browse files Browse the repository at this point in the history
Max board size used to be 256x256, but since `tetris_location` has to be
able to represent negative values for offset calculation, that meant it
needed to use `int16_t`. Since the piece array uses 4*4*7
tetris_locations, this added up to a good amount of space.

By reducing the max board size to 128x128 (still larger than should ever
be needed), `tetris_location` can use a `int8_t`, which means a 50%
reduction in needed space.

Changes:
* Readme reorganized, controls added, more detail
    * `Cmake options listed in README
* Updated `tetris_location` size to uint8_t
* Updated check_and_clear_rows() to use the uint8_t values, which meant
  I could eliminate my array conversion functions since they're no
  longer needed. Simplification is always good.
* Tests updated to reflect the removal of array conversion and the
  modification of the smallest_in_arr() function signature to use
  uint8_t instead of int16
  • Loading branch information
0xjmux committed Mar 31, 2024
1 parent 4286314 commit 061cb04
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 135 deletions.
87 changes: 55 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,29 @@ This game engine is part of a larger project where I'm implementing a version of

The game was branched off to its own repository because the embedded project source tree only needs `tetris.h` and `tetris.c`, but all the other driver and testing files take up a lot of space.

### TODO:
* [ ] fix duplicate colors
* this is an ncurses color issue with TERM, the game logic is setting it correctly
* [ ] maybe rewrite render_active_board to no-copy to increase speed?

#### fixed
* [X] fix J piece rotation
* [X] Set up CI/CD with GH actions
* [X] fix full lines not being removed
* [X] fix crash on trying to clear line
* [X] fix gameover state detected prematurely, causing crash [Issue #1](https://github.com/0xjmux/tetris/issues/1)
* [X] fix score not incrementing on line clear
* [X] maybe fixed: figure out why check_and_clear being called on rows at very top of board
* this preceeds premature gameover, this crash seems to happen on another piece clear
* [X] figure out what's causing a bunch of out-of-bounds numbers to show up in row 1 of board - i think this is a type issue with uint & int types!
* [X] add play/pause functionality
* [X] Finish tetris game logic
* [X] fix gameover state not detected
* [X] add confirmation on quit to driver



### About
* One of the main goals of this project is to gain experience developing and debugging components on a workstation that can then be easily ported to MCU platforms. As such, the linux driver for this game is filled with debugging features to make hunting bugs in the game logic as easy as possible, since that same process will be much more difficult when in the resource-constrained environment of an MCU.
* Some features include
* Some debugging features include
* Saving entire game state to a file at any point by pressing `p`. Game state can then be restored, but these saves are largely used for unit testing purposes since having a start point of a game that you know preceded a crash can be very helpful when trying to force the game into invalid states.
* Some of these files can be found in [test/files/](./test/files/)
* Game state is also saved to a file on gameover, which was done to debug issues that cause premature gameover conditions.
* Debug sub-window in game enableable via `-DDEBUG_WINDOW=ON` during cmake build. This shows current game state information, so weird behavior is easier to spot.
* Everything is written from scratch, with the exception of the graphics library (ncurses), Unit test harness (Unity), and `.ini` file parser, inih.
*

### Project Goals
This project has several goals.
* Game needs to be runnable on low-powered devices using interrupts
* Game needs to be runnable on low-powered devices using interrupts. Memory and compute requirements should be as minimal as possible.
* Driver code needed to make a given display or controller work should be kept as minimal as possible. Currently, the game exposes a 2D array of `int8_t` values representing different piece colors, and all the display implementation needs to do is render that to a grid of the board size.
* Teach myself embedded test-driven development using techniques from James Greening's "Test-Driven Development for Embedded C" ([link](https://pragprog.com/titles/jgade/test-driven-development-for-embedded-c/))
* Teach myself embedded test-driven development and embedded unit testing using techniques from James Greening's "Test-Driven Development for Embedded C" ([link](https://pragprog.com/titles/jgade/test-driven-development-for-embedded-c/))
* Set up CI from scratch for an embedded project
* Learn CMake and set up the build system from scratch



This game is my first attempt at writing a component of medium complexity, and one of my primary goals is bettering my understanding of development best practices to accelerate my work on future projects.


### Some of what I've learned
mostly for myself to not forget
* CI build options in CMake, so that CI can build a different version depending on paths and such

### Compilation
#### Game Driver (runs in terminal on Linux workstations)
Expand Down Expand Up @@ -94,17 +73,33 @@ The code is documented using Doxygen style comments. Custom types are documented
##### Linux Game Controls
These are the controls for my implementation (`driver_tetris.c`) for POSIX terminals via ncurses.
```
Use arrow keys to move
SPACE pauses game
'q' quits
'p' prints current game state to .ini file (for debugging purposes)
←/→ - Move left/right
↑ - Rotate piece
↓ - Move piece down
SPACE - pause game
'q' - quit
'p' prints current game state to `gamestate.ini` (for debugging purposes)
```
#### Flags
* There are many compilation flags to enable/disable features, mostly for debugging. I've also created several compilation flags that enable extra output on CI builds so it's easier to see what went wrong from the build report console. The default options should be fine, but for finer tuning you can see the options available to you across the project's `CMakeLists.txt` files.
Options:
```cmake
OPTION(TETRIS_UNIT_TEST_MACRO "Print gamelog to stdout (for CI)" OFF) # disabled by default
OPTION(TETRIS_UNIT_TEST_CI "CI-specific path options" OFF) # disabled by default
OPTION(TETRIS_DEBUG_T_MACRO "Enable Debug logging from inside tetris" OFF)
OPTION(INI_LIB_INCLUDE_OPTION "Include inih library for saving game state to disk" ON)
```

For example, to enable `DEBUG_T` you would do
```sh
cmake -B build -DTARGET_GROUP=test -DTETRIS_DEBUG_T_MACRO=ON
```


##### Other
I have these aliases in my `~/.zshrc` to make the testing process more fluid.
```sh
Expand All @@ -113,3 +108,31 @@ alias cmakeregen='cmake . -B build && cmake --build build'
alias cmaketest='cmake . -B build -DTARGET_GROUP=test && cmake --build build'
```


---

### TODO:
* [ ] fix duplicate colors
* this is an ncurses color issue with TERM, the game logic is setting it correctly
* [ ] maybe rewrite render_active_board to no-copy to increase speed?

#### fixed
* [X] fix J piece rotation
* [X] Set up CI/CD with GH actions
* [X] fix full lines not being removed
* [X] fix crash on trying to clear line
* [X] fix gameover state detected prematurely, causing crash [Issue #1](https://github.com/0xjmux/tetris/issues/1)
* [X] fix score not incrementing on line clear
* [X] maybe fixed: figure out why check_and_clear being called on rows at very top of board
* this preceeds premature gameover, this crash seems to happen on another piece clear
* [X] figure out what's causing a bunch of out-of-bounds numbers to show up in row 1 of board - i think this is a type issue with uint & int types!
* [X] add play/pause functionality
* [X] Finish tetris game logic
* [X] fix gameover state not detected
* [X] add confirmation on quit to driver


#### Some of what I've learned
mostly for myself to not forget
* CI build options in CMake, so that CI can build a different version depending on paths and such
* static variables inside functions can be useful, but generally make unit testing more difficult.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ target_include_directories(tetris_driver PUBLIC ${PROJECT_SOURCE_DIR}/include)

find_library(NCURSES_FOUND ncurses REQUIRED)

OPTION(DEBUG_WINDOW "Enable window with game state information for debugging" ON)
OPTION(DEBUG_WINDOW "Enable debug window with game state information in ncurses" OFF)
IF(DEBUG_WINDOW)
target_compile_definitions(tetris_driver PUBLIC DEBUG_T_WIN=1)
ENDIF()
Expand Down
2 changes: 0 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ IF(TETRIS_UNIT_TEST_MACRO)
ENDIF(TETRIS_UNIT_TEST_MACRO)
##### END OPTIONAL BUILD FLAGS ######



target_link_libraries(test_tetris
tetris
Unity
Expand Down
42 changes: 17 additions & 25 deletions test/suite_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,42 +415,34 @@ void test_getElapsedUs(void) {
*/
void test_arr_helpers(void) {
// test int16_to_uint8_arr()
int16_t arr1[5] = {5,2,1,4,3};
uint8_t out_arr1[5];
uint8_t exp_arr1[5] = {5,2,1,4,3};
int16_to_uint8_arr(arr1, out_arr1, 5);
TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(exp_arr1, \
out_arr1, 5, "Array conversion to uint8 incorrect");

TEST_ASSERT_EQUAL_UINT8_MESSAGE(1, (uint8_t)smallest_in_arr(arr1, 5), \
uint8_t arr1[5] = {5,2,1,4,3};

TEST_ASSERT_EQUAL_UINT8_MESSAGE(1, smallest_in_arr(arr1, 5), \
"smallest_in_arr failed on all positive values");

int16_t arr2[6] = {INT8_MAX,1,2,255,4,0};
uint8_t out_arr2[6];
uint8_t exp_arr2[6] = {INT8_MAX,1,2,255,4,0};
int16_to_uint8_arr(arr2, out_arr2, 6);
TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(exp_arr2, \
out_arr2, 6, "arr2 conversion to uint8 incorrect");
uint8_t arr2[6] = {UINT8_MAX,1,2,255,4,0};

TEST_ASSERT_EQUAL_UINT8_MESSAGE(0, (uint8_t)smallest_in_arr(arr2, 6), \
TEST_ASSERT_EQUAL_UINT8_MESSAGE(0, smallest_in_arr(arr2, 6), \
"smallest_in_arr failed on all positive values");

// test smallest in arr
int16_t arr4[5] = {5,2,1,4,3};
TEST_ASSERT_EQUAL_INT16(1, smallest_in_arr(arr4, 5));
uint8_t arr4[5] = {5,2,1,4,3};
TEST_ASSERT_EQUAL_UINT8(1, smallest_in_arr(arr4, 5));

int16_t arr6[6] = {INT16_MAX,-1,2,-2,4,5};
TEST_ASSERT_EQUAL_INT16(-2, smallest_in_arr(arr6, 6));
uint8_t arr6[6] = {UINT8_MAX,1,2,3,4,5};
TEST_ASSERT_EQUAL_UINT8(1, smallest_in_arr(arr6, 6));


#define ARR7_SIZE 6
uint8_t arr7_uint[ARR7_SIZE] = {2,UINT8_MAX,2,255,0,5};
int16_t arr7_int16[ARR7_SIZE];
int16_t arr7_exp[ARR7_SIZE] = {2,UINT8_MAX,2,255,0,5};
uint8_to_int16_arr(arr7_uint, arr7_int16, ARR7_SIZE);
TEST_ASSERT_EQUAL_INT16_ARRAY_MESSAGE(arr7_int16, \
arr7_exp, ARR7_SIZE, "arr7 conversion to int16 incorrect");
TEST_ASSERT_EQUAL_INT16(0, smallest_in_arr(arr7_int16, ARR7_SIZE));
/*
// int16_t arr7_int16[ARR7_SIZE];
// int16_t arr7_exp[ARR7_SIZE] = {2,UINT8_MAX,2,255,0,5};
// uint8_to_int16_arr(arr7_uint, arr7_int16, ARR7_SIZE);
// TEST_ASSERT_EQUAL_INT16_ARRAY_MESSAGE(arr7_int16, \
// arr7_exp, ARR7_SIZE, "arr7 conversion to int16 incorrect");
*/
TEST_ASSERT_EQUAL_UINT8(0, smallest_in_arr(arr7_uint, ARR7_SIZE));


// test val_in_arr
Expand Down
Loading

0 comments on commit 061cb04

Please sign in to comment.