コード例 #1
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));
}
コード例 #2
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) );
}
コード例 #3
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;
    }

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

    // 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);
        }
    }

    // Expected next piece
    pos.rowIndex = 0;
    pos.columnIndex = 3;
    trn_grid_set_cell(expected_grid, pos, TRN_TETROMINO_J);
    pos.rowIndex = 1;
    for (columnIndex = 3 ; columnIndex < 6 ; columnIndex++) {
      pos.columnIndex = columnIndex;
      trn_grid_set_cell(expected_grid, pos, TRN_TETROMINO_J);
    }

    CU_ASSERT_TRUE( trn_grid_equal(game->grid, expected_grid) );

    trn_game_destroy(game);
}