Skip to content

Commit

Permalink
added scoring logic to tg_tick, and attempted CI fix
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjmux committed Mar 21, 2024
1 parent 6f04a3b commit 10302c7
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 60 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DTARGET_GROUP=test
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \
-DTARGET_GROUP=test -DTETRIS_UNIT_TEST_CI=ON
# run: cmake -B ${{github.workspace}}/build -DTARGET_GROUP=test -DTETRIS_UNIT_TEST_MACRO=ON
# build cmake in build dir with CI unit test flag turned on - disable macro bc causes some tests to not run

Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ The game was branched off to it's own repository because the embedded project so

### TODO:
* [ ] Finish tetris game logic
* [X] Set up CI/CD with GH actions
* [ ] fix gameover state not detected
* [X] fix full lines not being removed
* [ ] fix duplicate colors
* [ ] fix crash on trying to clear line
* [ ] 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!
* [ ] fix J piece rotation
* [ ] this might be fixed with new type conversion functions? idk
* [ ] fix gameover state detected prematurely, causing crash
* [ ] fix score not incrementing on line clear
* [ ] add play/pause functionality
* [ ] fix moving left/right choppy feel
* [ ] 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

#### 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


> [!WARNING]
Expand Down
2 changes: 1 addition & 1 deletion include/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

void nc_init_colors(WINDOW *w);

void save_game_state(TetrisGame *tg);
void save_game_state(TetrisGame *tg, const char* filename);

// void old_save_board_to_file(FILE *file, TetrisBoard tb);
void ini_save_board_to_file(FILE *file, TetrisBoard tb);
Expand Down
4 changes: 2 additions & 2 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ void nc_init_colors(WINDOW *w) {
* This function helps with debugging edge cases.
* Will save the board to a file `gamestate.ini`
*/
void save_game_state(TetrisGame *tg) {
void save_game_state(TetrisGame *tg, const char* filename) {
FILE *savefile;
savefile = fopen("gamestate.ini", "w+");
savefile = fopen(filename, "w+");


// pretty-print board state to file for ease of differentiation
Expand Down
16 changes: 11 additions & 5 deletions src/driver_tetris.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int main(void) {
// save game to disk
case 'p':
move = T_NONE;
save_game_state(tg);
save_game_state(tg, "gamestate.ini");
mvwprintw(s_win, 4,1, "GAME STATE SAVED\n");
fprintf(gamelog, "game state saved to file gamestate.ini\n");
wnoutrefresh(s_win);
Expand All @@ -130,13 +130,19 @@ int main(void) {
if (move != T_NONE) print_keypress(move);
#endif
}
#ifdef DEBUG_T
// for debugging, save state when game exits
save_game_state(tg, "final-gamestate.ini");
#endif

printf("Game over! Level=%d, Score=%d\n", tg->level, tg->score);

// if we're here, game is over; dealloc tg
end_game(tg);

// TODO ask player if they want to play again


// if we're here, game is over; dealloc tg
end_game(tg);

endwin();

}
Expand Down Expand Up @@ -177,7 +183,7 @@ void display_board(WINDOW *w, TetrisBoard *tb) {
wrefresh(w);
#ifdef DEBUG_T
fprintf(gamelog, "display_board()\n");
print_board_state(*tb, gamelog);
// print_board_state(*tb, gamelog);
fflush(gamelog);
#endif

Expand Down
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ IF(TETRIS_UNIT_TEST_MACRO)
target_compile_definitions(test_tetris PUBLIC TETRIS_UNIT_TEST_DEF=1)
ENDIF(TETRIS_UNIT_TEST_MACRO)

OPTION(TETRIS_UNIT_TEST_CI "CI-specific path options" OFF) # disabled by default
IF(TETRIS_UNIT_TEST_CI)
target_compile_definitions(test_tetris PUBLIC TETRIS_UNIT_TEST_CI=1)
ENDIF(TETRIS_UNIT_TEST_CI)

target_link_libraries(test_tetris
tetris
Unity
Expand Down
122 changes: 122 additions & 0 deletions test/files/gamestate-J-lined-up-dbl-clear.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[BOARD_IMAGE]
Highest occupied cell: 24
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
--------------------------------------------------------------------
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | 4 4 |
23 | 4 |
24 | 4 1 |
25 | 1 1 1 2 |
26 | 1 3 1 1 0 2 2 |
27 | 6 3 5 5 3 1 0 0 0 2 |
28 | 6 1 1 3 5 5 3 3 2 0 0 0 1 |
29 | 6 6 2 1 1 3 3 0 0 1 2 2 0 1 1 |
30 | 6 6 2 2 2 0 0 0 1 1 2 5 5 1 2 |
31 | 5 5 6 4 4 2 0 2 2 5 5 2 2 2 2 |


[TETRIS_GAME_STRUCT]
active_board_highest_occupied_cell = 24
board_highest_occupied_cell = 24

game_over = 0
score = 1200
level = 1
gravity_tick_rate_usec = 200000
last_gravity_tick_usec = 1710983821,800170

[ACTIVE_PIECE]
ptype = 4
loc_row = 22
loc_col = 1
orientation = 2
falling = 1

[active_board]
row_0 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_1 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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,1
row_30 = 6,-1,6,2,2,2,0,0,0,1,1,2,5,5,1,2
row_31 = 5,5,6,4,4,2,-1,0,2,2,5,5,2,2,2,2
122 changes: 122 additions & 0 deletions test/files/gamestate-lined-up-I-piece-clear.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[BOARD_IMAGE]
Highest occupied cell: 27
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
--------------------------------------------------------------------
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | 6 |
18 | 6 |
19 | 6 |
20 | 6 |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | 5 5 |
28 | 2 5 5 2 |
29 | 5 5 0 2 2 2 5 5 3 2 2 |
30 | 2 4 3 5 5 0 0 1 1 5 5 3 2 6 |
31 | 1 5 5 3 2 1 1 3 3 3 2 2 2 5 5 |


[TETRIS_GAME_STRUCT]
active_board_highest_occupied_cell = 27
board_highest_occupied_cell = 27

game_over = 0
score = 0
level = 1
gravity_tick_rate_usec = 200000
last_gravity_tick_usec = 1710983028,800179

[ACTIVE_PIECE]
ptype = 6
loc_row = 17
loc_col = 1
orientation = 1
falling = 1

[active_board]
row_0 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_1 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_2 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_3 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_4 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_5 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_6 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_15 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_16 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_17 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_18 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_19 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_20 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_21 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_22 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_23 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_24 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_25 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_26 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
row_27 = -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,5,5,-1,-1,-1
row_28 = -1,-1,-1,-1,-1,-1,-1,-1,2,-1,-1,5,5,-1,2,-1
row_29 = -1,-1,-1,-1,5,5,0,2,2,2,5,5,3,-1,2,2
row_30 = 2,-1,4,3,5,5,0,0,1,1,5,5,3,-1,2,6
row_31 = 1,-1,5,5,3,2,1,1,3,3,3,2,2,2,5,5
Loading

0 comments on commit 10302c7

Please sign in to comment.