Exemplo n.º 1
0
void test_grid_is_row_complete()
{
    // Create a grid.
    int numberOfRows = 4;
    int numberOfColumns = 4;
    TrnGrid* grid = trn_grid_new(numberOfRows, numberOfColumns);

    // Last row is void only.
    CU_ASSERT_FALSE( trn_grid_is_row_complete(grid,numberOfRows) );

    // Last row is full.
    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);
    }
    CU_ASSERT_TRUE( trn_grid_is_row_complete(grid,numberOfRows) );

    // Last row is full except last element. 
    pos.rowIndex = numberOfRows-1 ;
    pos.columnIndex = numberOfColumns-1 ;
    trn_grid_set_cell(grid, pos, TRN_TETROMINO_VOID);
    CU_ASSERT_FALSE( trn_grid_is_row_complete(grid,numberOfRows) );
}
Exemplo n.º 2
0
void test_grid_find_last_complete_row_index()
{
  int numberOfRows = 4;
  int numberOfColumns = 4;

  int rowIndex;
  int columnIndex;
  TrnPositionInGrid pos;

  /* Initial grid
   * +----+
   * |    | 0
   * |LLLL| 1
   * |LLL | 2
   * |LL  | 3
   * +----+
   */
  TrnGrid* grid = trn_grid_new(numberOfRows, numberOfColumns);
  for (rowIndex = 1 ; rowIndex < numberOfRows ; rowIndex++) {
      pos.rowIndex = rowIndex;
      for (columnIndex = 0 ; columnIndex <= numberOfColumns-rowIndex ; columnIndex++) {
          pos.columnIndex = columnIndex;
          trn_grid_set_cell(grid, pos, TRN_TETROMINO_L);
      }
  }

  CU_ASSERT_EQUAL(tnr_grid_find_last_complete_row_index(grid), 1)

  pos.rowIndex = 1;
  pos.columnIndex = 0;
  trn_grid_set_cell(grid, pos, TRN_TETROMINO_VOID);

  CU_ASSERT_EQUAL(tnr_grid_find_last_complete_row_index(grid), -1)
}
Exemplo n.º 3
0
void test_grid_set_cells_with_piece()
{
    // Create a grid.
    int numberOfRows = 10;
    int numberOfColumns = 10;
    TrnGrid* grid = trn_grid_new(numberOfRows, numberOfColumns);

    // Create a piece;
    TrnPiece piece = trn_piece_create(TRN_TETROMINO_I,2,3,TRN_ANGLE_90);
    
    trn_grid_set_cells_with_piece(grid, &piece, piece.type);

    // Create expected grid
    TrnGrid* expected_grid = trn_grid_new(numberOfRows, 
                                          numberOfColumns);
    TrnPositionInGrid pos = {2,5};
    trn_grid_set_cell(expected_grid, pos, TRN_TETROMINO_I);
    pos.rowIndex = 3;
    pos.columnIndex = 5;
    trn_grid_set_cell(expected_grid, pos, TRN_TETROMINO_I);
    pos.rowIndex = 4;
    pos.columnIndex = 5;
    trn_grid_set_cell(expected_grid, pos, TRN_TETROMINO_I);
    pos.rowIndex = 5;
    pos.columnIndex = 5;
    trn_grid_set_cell(expected_grid, pos, TRN_TETROMINO_I);
    
	// Compare grids
	CU_ASSERT_TRUE(trn_grid_equal(grid, expected_grid));
}
Exemplo n.º 4
0
void test_complete_rows(void) {
  TrnGame* game;
  int numberOfRows = 5;
  int numberOfColumns = 4;
  int actual_score;
  int expected_score = 1200;
  int expected_level = 1;
  int delay = 500 ;
  int rowIndex, columnIndex;
  TrnPositionInGrid pos;

  game = trn_game_new(numberOfRows, numberOfColumns, delay);
  game->lines_count = 19;
  for (rowIndex = 0 ; rowIndex < 4; rowIndex++) {
    pos.rowIndex = rowIndex;
    for (columnIndex = 0 ; columnIndex < numberOfColumns ;columnIndex++ ) {
      pos.columnIndex = columnIndex;
      trn_grid_set_cell(game->grid, pos,TRN_TETROMINO_I);
    }
  }

  trn_game_check_complete_rows(game);
  printf("score: %d\n", game->score);
  printf("lines_count: %d\n", game->lines_count);


  CU_ASSERT_EQUAL(game->score, expected_score);
  CU_ASSERT_EQUAL(game->level, expected_level);
  trn_game_destroy(game);
}
Exemplo n.º 5
0
void test_grid_pop_row_and_make_above_fall()
{
  int numberOfRows = 4;
  int numberOfColumns = 4;

  int rowIndex;
  int columnIndex;
  TrnPositionInGrid pos;

  /* Initial grid
   * +----+
   * |L   |
   * |LL  |
   * |LLL |
   * |LLLL|
   * +----+
   */
  TrnGrid* grid = trn_grid_new(numberOfRows, numberOfColumns);
  for (rowIndex = 0 ; rowIndex < numberOfRows ; rowIndex++) {
      pos.rowIndex = rowIndex;
      for (columnIndex = 0 ; columnIndex <= rowIndex ; columnIndex++) {
          pos.columnIndex = columnIndex;
          trn_grid_set_cell(grid, pos, TRN_TETROMINO_L);
      }
  }

  trn_grid_pop_row_and_make_above_fall(grid, numberOfRows-1);

  /* Expected grid
   * +----+
   * |    |
   * |L   |
   * |LL  |
   * |LLL |
   * +----+
   */
  TrnGrid* expected_grid = trn_grid_new(numberOfRows, numberOfColumns);
  for (rowIndex = 0 ; rowIndex < numberOfRows ; rowIndex++) {
      pos.rowIndex = rowIndex;
      for (columnIndex = 0 ; columnIndex < rowIndex ; columnIndex++) {
          pos.columnIndex = columnIndex;
          trn_grid_set_cell(expected_grid, pos, TRN_TETROMINO_L);
      }
  }

  CU_ASSERT_TRUE( trn_grid_equal(grid, expected_grid) );
}
Exemplo n.º 6
0
void test_grid_set_get_cell()
{
    // Create a 3 rows grid.
    int numberOfRows = 3;
    int numberOfColumns = 2;
    TrnGrid* grid = trn_grid_new(numberOfRows, numberOfColumns);

    int columnIndex;
    TrnPositionInGrid pos;

    // Set first row.
    pos.rowIndex = 0;
    for (columnIndex = 0 ; columnIndex < numberOfColumns ; columnIndex++) {
        pos.columnIndex = columnIndex;
        trn_grid_set_cell(grid, pos, TRN_TETROMINO_O);
    }

    // Set Third row.
    pos.rowIndex = 2;
    for (columnIndex = 0 ; columnIndex < numberOfColumns ; columnIndex++) {
        pos.columnIndex = columnIndex;
        trn_grid_set_cell(grid, pos, TRN_TETROMINO_I);
    }

    // Check first row.
    pos.rowIndex = 0;
    for (columnIndex = 0 ; columnIndex < numberOfColumns ; columnIndex++) {
        pos.columnIndex = columnIndex;
        CU_ASSERT( trn_grid_get_cell(grid, pos) == TRN_TETROMINO_O);
    }

    // Check second row, it has been initialize to TRN_TETROMINO_VOID by grid_new.
    pos.rowIndex = 1;
    for (columnIndex = 0 ; columnIndex < numberOfColumns ; columnIndex++) {
        pos.columnIndex = columnIndex;
        CU_ASSERT( trn_grid_get_cell(grid, pos) == TRN_TETROMINO_VOID);
    }

    // Check third row.
    pos.rowIndex = 2;
    for (columnIndex = 0 ; columnIndex < numberOfColumns ; columnIndex++) {
        pos.columnIndex = columnIndex;
        CU_ASSERT( trn_grid_get_cell(grid, pos) == TRN_TETROMINO_I);
    }
}
Exemplo n.º 7
0
void trn_grid_clear_row(TrnGrid * const grid, int const rowIndex)
{
  TrnPositionInGrid pos;
  pos.rowIndex = rowIndex;
  int columnIndex;
  for (columnIndex = 0 ; columnIndex < grid->numberOfColumns ; columnIndex++) {
    pos.columnIndex = columnIndex;
    trn_grid_set_cell(grid, pos, TRN_TETROMINO_VOID);
  }
}
Exemplo n.º 8
0
static void trn_grid_fill_row(
    TrnGrid * const grid, 
    TrnTetrominoType const type,
    int const rowIndex)
{
    int columnIndex;
    TrnPositionInGrid pos;
    pos.rowIndex = rowIndex;
    for (columnIndex = 0 ; columnIndex < grid->numberOfColumns ; columnIndex++) {
        pos.columnIndex = columnIndex;
        trn_grid_set_cell(grid, pos, type);
    }
}
Exemplo n.º 9
0
void trn_grid_set_cells_with_piece(TrnGrid * const grid,
                                   TrnPiece const * const piece,
                                   TrnTetrominoType const type)
{
    int squareIndex;
    TrnPositionInGrid pos;

    for (squareIndex = 0 ; 
         squareIndex < TRN_TETROMINO_NUMBER_OF_SQUARES ;
         squareIndex++)
    {
        pos = trn_piece_position_in_grid(piece, squareIndex);
        trn_grid_set_cell(grid, pos, type);
    }
}
Exemplo n.º 10
0
void trn_grid_copy_row_bellow(TrnGrid * const grid, int const rowIndex)
{
  TrnPositionInGrid top_pos;
  TrnPositionInGrid bottom_pos;

  top_pos.rowIndex = rowIndex;
  bottom_pos.rowIndex = rowIndex+1;

  int columnIndex;
  TrnTetrominoType top_type;

  for (columnIndex = 0 ; columnIndex < grid->numberOfColumns ; columnIndex++) {
      top_pos.columnIndex = columnIndex;
      bottom_pos.columnIndex = columnIndex;
      top_type = trn_grid_get_cell(grid, top_pos);
      trn_grid_set_cell(grid, bottom_pos, top_type);
  }
}
Exemplo n.º 11
0
void TestGridCellIsInGridAndIsVoid()
{
    // Create a grid.
    int numberOfRows = 2;
    int numberOfColumns = 3;
    TrnGrid* grid = trn_grid_new(numberOfRows, numberOfColumns);

    // TrnGrid has been initialize to TRN_TETROMINO_VOID, modify one cell type.
    TrnPositionInGrid posNotVoid = {1,2};
    trn_grid_set_cell(grid, posNotVoid, TRN_TETROMINO_O);

    int rowIndex;
    int columnIndex;
    TrnPositionInGrid pos;
    bool isInGridAndIsVoid;

    // Check for cells in grid.
    for (rowIndex = 0 ; rowIndex < numberOfRows; rowIndex++) {
        pos.rowIndex = rowIndex;
        for (columnIndex = 0 ; columnIndex < numberOfColumns ; columnIndex++) {
            pos.columnIndex = columnIndex;
            isInGridAndIsVoid = trn_grid_cell_is_in_grid_and_is_void(grid,pos);
            if (trn_position_in_grid_equal(pos,posNotVoid)) {
                CU_ASSERT_FALSE(isInGridAndIsVoid);
            } else {
                CU_ASSERT_TRUE(isInGridAndIsVoid);
            }
        }
    }

    // Check for cell NOT in grid.
    pos.rowIndex = 5;
    pos.columnIndex = 5;
    isInGridAndIsVoid = trn_grid_cell_is_in_grid_and_is_void(grid,pos);
    CU_ASSERT_FALSE(isInGridAndIsVoid);
}
Exemplo n.º 12
0
void stack_some_pieces()
{
    int numberOfRows = 20;
    int numberOfColumns = 10;
    int delay = 500;
    int imove;
    TrnGame* game = trn_game_new(numberOfRows, numberOfColumns, delay);
    
    // TrnPiece 0. While the piece is falling:
    // Rotate it 3 times.
    for (imove = 0; imove < 3; imove++) {
        CU_ASSERT_TRUE( trn_game_try_to_move_down(game) );
        CU_ASSERT_TRUE( trn_game_try_to_rotate_clockwise(game) );
    }

    // Move piece to left.
    CU_ASSERT_TRUE( trn_game_try_to_move_down(game) );
    CU_ASSERT_TRUE( trn_game_try_to_move_left(game) );
    // Reach bottom.
    while (true) {
        if (! trn_game_try_to_move_down(game))
            break;
    }
    
    // TrnPiece 1. While the piece is falling:
    // Rotate it 1 times.
    CU_ASSERT_TRUE( trn_game_try_to_move_down(game) );
    CU_ASSERT_TRUE( trn_game_try_to_rotate_clockwise(game) );

    // Move piece to left.
    for (imove = 0; imove < 4; imove++) {
        CU_ASSERT_TRUE( trn_game_try_to_move_down(game) );
        CU_ASSERT_TRUE( trn_game_try_to_move_left(game) );
    }
    
    // Reach bottom.
    while (true) {
        if (! trn_game_try_to_move_down(game))
            break;
    }
    
    // TrnPiece 2. While the piece is falling:
    // Rotate it 1 times.
    CU_ASSERT_TRUE( trn_game_try_to_move_down(game) );
    CU_ASSERT_TRUE( trn_game_try_to_rotate_clockwise(game) );
    // Move piece to left.
    for (imove = 0; imove < 2; imove++) {
        CU_ASSERT_TRUE( trn_game_try_to_move_down(game) );
        CU_ASSERT_TRUE( trn_game_try_to_move_left(game) );
    }
    // Reach bottom.
    while (true) {
        if (! trn_game_try_to_move_down(game))
            break;
    }
    
    // TrnPiece 3. While the piece is falling:
    // Rotate it 3 times.
    for (imove = 0; imove < 3; imove++) {
        CU_ASSERT_TRUE( trn_game_try_to_move_down(game) );
        CU_ASSERT_TRUE( trn_game_try_to_rotate_clockwise(game) );
    }
    // Move piece to left.
    for (imove = 0; imove < 3; imove++) {
        CU_ASSERT_TRUE( trn_game_try_to_move_down(game) );
        CU_ASSERT_TRUE( trn_game_try_to_move_left(game) );
    }
    // Reach bottom.
    while (true) {
        if (! trn_game_try_to_move_down(game))
            break;
    }

    int rowIndex;
    int columnIndex;
    TrnPositionInGrid pos;
    TrnGrid* expected_grid = trn_grid_new(numberOfRows, numberOfColumns);

    // Expected expected_grid type for pieces 0 and 2
    for (rowIndex = numberOfRows-4 ; rowIndex < numberOfRows ; rowIndex++) {
        pos.rowIndex = rowIndex;
        for (columnIndex = 2 ; columnIndex < 4 ; columnIndex++) {
            pos.columnIndex = columnIndex;
            trn_grid_set_cell(expected_grid, pos, TRN_TETROMINO_J);
        }
    }

    // Expected expected_grid type for pieces 1 and 3
    for (rowIndex = numberOfRows-4 ; rowIndex < numberOfRows ; rowIndex++) {
        pos.rowIndex = rowIndex;
        for (columnIndex = 0 ; columnIndex < 2 ; columnIndex++) {
            pos.columnIndex = columnIndex;
            trn_grid_set_cell(expected_grid, pos, TRN_TETROMINO_L);
        }
    }
/*
    CU_ASSERT_TRUE( trn_grid_equal(game->grid, expected_grid) );
*/
    trn_game_destroy(game);
}
Exemplo n.º 13
0
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) )
}