static bool move_piece_to_column_center(TrnPiece * const piece, TrnGame const * const game) { int columnIndex = (game->grid->numberOfColumns - TRN_TETROMINO_GRID_SIZE)/2; piece->topLeftCorner.columnIndex = columnIndex; bool success = trn_grid_can_set_cells_with_piece(game->grid,game->current_piece); trn_grid_fill_piece(game->grid, game->current_piece); return success; }
bool trn_game_try_to_move(TrnGame* game,void (*move)(TrnPiece * const), void (*unmove)(TrnPiece * const)) { if (game->status != TRN_GAME_ON) return false; bool managedToMove = true; trn_grid_remove_piece(game->grid, game->current_piece); move(game->current_piece); if (! trn_grid_can_set_cells_with_piece(game->grid, game->current_piece)) { managedToMove = false; unmove(game->current_piece); } trn_grid_fill_piece(game->grid, game->current_piece); return managedToMove; }
void TestGridCanSetCellsWithPiece() { /* Reminder on tetromino_srs_i at TRN_ANGLE_0 and TRN_ANGLE_90: T---+---+---+---+ | | | 9| | T: topLeftCorner position in grid +---+---+---+---+ 0: tetromino cells a TRN_ANGLE_0 | 0 | 0 | 01| 0 | 9: tetromino cells a TRN_ANGLE_90 +---+---+---+---+ | | | 9| | +---+---+---+---+ | | | 9| | +---+---+---+---+ */ // Create a grid. int numberOfRows = 10; int numberOfColumns = 10; TrnGrid* grid = trn_grid_new(numberOfRows, numberOfColumns); // For now, the grid has only void cells. // Ok, in grid and void. TrnPiece piece0 = trn_piece_create(TRN_TETROMINO_I,0,0,TRN_ANGLE_0); CU_ASSERT_TRUE( trn_grid_can_set_cells_with_piece(grid, &piece0) ) // Ok, still in grid (in the first row). TrnPiece piece1 = trn_piece_create(TRN_TETROMINO_I,-1,0,TRN_ANGLE_0); CU_ASSERT_TRUE( trn_grid_can_set_cells_with_piece(grid, &piece1) ) // No more in grid. TrnPiece piece2 = trn_piece_create(TRN_TETROMINO_I,-2,0,TRN_ANGLE_0); CU_ASSERT_FALSE( trn_grid_can_set_cells_with_piece(grid, &piece2) ) // Ok, in grid and void. TrnPiece piece3 = trn_piece_create(TRN_TETROMINO_I,5,0,TRN_ANGLE_90); CU_ASSERT_TRUE( trn_grid_can_set_cells_with_piece(grid, &piece3) ) // Ok, still in grid (in the first column). TrnPiece piece4 = trn_piece_create(TRN_TETROMINO_I,5,-2,TRN_ANGLE_90); CU_ASSERT_TRUE( trn_grid_can_set_cells_with_piece(grid, &piece4) ) // No more in grid. TrnPiece piece5 = trn_piece_create(TRN_TETROMINO_I,5,-3,TRN_ANGLE_90); CU_ASSERT_FALSE( trn_grid_can_set_cells_with_piece(grid, &piece5) ) // Now, fill the last grid row with non-void tetrominos. TrnPositionInGrid pos; int columnIndex; pos.rowIndex = numberOfRows-1 ; for (columnIndex = 0 ; columnIndex < grid->numberOfColumns ; columnIndex++) { pos.columnIndex = columnIndex; trn_grid_set_cell(grid, pos, TRN_TETROMINO_I); } // Ok, in grid and void. TrnPiece piece6 = trn_piece_create(TRN_TETROMINO_I,0,0,TRN_ANGLE_90); CU_ASSERT_TRUE( trn_grid_can_set_cells_with_piece(grid, &piece6) ) // Still in grid and void, just above the non-void row. TrnPiece piece7 = trn_piece_create(TRN_TETROMINO_I,numberOfRows-5,0,TRN_ANGLE_90); CU_ASSERT_TRUE( trn_grid_can_set_cells_with_piece(grid, &piece7) ) // In grid, but last cell of piece overlap a non-void cell of the grid. TrnPiece piece8 = trn_piece_create(TRN_TETROMINO_I,numberOfRows-4,0,TRN_ANGLE_90); CU_ASSERT_FALSE( trn_grid_can_set_cells_with_piece(grid, &piece8) ) }