Example #1
0
bool is_row_equal(const int rowIndex, TrnGrid const * const left, TrnGrid const * const right)
{
  TrnPositionInGrid pos;
  pos.rowIndex = rowIndex;
  int columnIndex;
  for (columnIndex = 0 ; columnIndex < left->numberOfColumns ; columnIndex++) {
    pos.columnIndex = columnIndex;
    TrnTetrominoType LeftType = trn_grid_get_cell(left, pos);
    TrnTetrominoType RightType = trn_grid_get_cell(right, pos);
    if ( LeftType != RightType) {
      return false;
    }
  }
  return true;
}
void test_clear_row(void) {
  int numberOfRows = 20;
  int numberOfColumns = 10;
  TrnGrid* grid = trn_grid_new(numberOfRows, numberOfColumns);
  TrnPositionInGrid pos;

  // Initialization of our full grid.
  int rowIndex;
  int columnIndex;
  trn_grid_fill(grid, TRN_TETROMINO_L);

  // Choose a line and clear.
  int rowIndexToClear = 5;
  trn_grid_clear_row(grid, rowIndexToClear);

  // Check that grid values are correct.
  for (rowIndex = 0 ; rowIndex < numberOfRows; rowIndex++) {
    pos.rowIndex = rowIndex;
    TrnTetrominoType tetromino;
    if (pos.rowIndex == rowIndexToClear) {
      tetromino = TRN_TETROMINO_VOID;
    }
    else {
      tetromino = TRN_TETROMINO_L;
    }
    for (columnIndex = 0 ; columnIndex < numberOfColumns ;columnIndex++ ) {
      pos.columnIndex = columnIndex;
      CU_ASSERT_EQUAL( trn_grid_get_cell(grid, pos), tetromino);
    }
  }
}
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);
    }
}
Example #4
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);
  }
}
Example #5
0
bool trn_grid_is_row_complete(TrnGrid const * const grid, int const rowIndex)
{
    int columnIndex;

    TrnPositionInGrid pos;
    pos.rowIndex = rowIndex;

    TrnTetrominoType type;

    for (columnIndex = 0 ; columnIndex < grid->numberOfColumns ; columnIndex++) {
        pos.columnIndex = columnIndex;
        type = trn_grid_get_cell(grid, pos);
        if (type == TRN_TETROMINO_VOID)
            return false;
    }

    return true;
}
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);

    // The piece position in grid.
    TrnPositionInGrid pos0 = {2,5};
    TrnPositionInGrid pos1 = {3,5};
    TrnPositionInGrid pos2 = {4,5};
    TrnPositionInGrid pos3 = {5,5};
    TrnPositionInGrid pos;

    TrnTetrominoType type;
    TrnTetrominoType expectedType;

    int rowIndex;
    int columnIndex;
    for (rowIndex = 0 ; rowIndex < grid->numberOfRows; rowIndex++) {
        for (columnIndex = 0 ; columnIndex < grid->numberOfColumns ; columnIndex++) {
            pos.rowIndex = rowIndex;
            pos.columnIndex = columnIndex;
            if (trn_position_in_grid_equal(pos, pos0) ||
                trn_position_in_grid_equal(pos, pos1) ||
                trn_position_in_grid_equal(pos, pos2) ||
                trn_position_in_grid_equal(pos, pos3))
            {
                expectedType = TRN_TETROMINO_I;
            } else {
                expectedType = TRN_TETROMINO_VOID;
            }
            type = trn_grid_get_cell(grid,pos);
            CU_ASSERT_EQUAL(type,expectedType);
        }
    }
}
void test_set_grid_to_zero(void) {
  int numberOfRows = 20;
  int numberOfColumns = 10;
  TrnGrid* grid = trn_grid_new(numberOfRows, numberOfColumns);
  TrnPositionInGrid pos;

  // Initialization full grid.
  int rowIndex;
  int columnIndex;
  trn_grid_fill(grid, TRN_TETROMINO_L);
  trn_grid_clear(grid);

  // Check that grid is empty
  for (rowIndex = 0 ; rowIndex < numberOfRows; rowIndex++) {
    pos.rowIndex = rowIndex;
    for (columnIndex = 0 ; columnIndex < numberOfColumns ;columnIndex++ ) {
      pos.columnIndex = columnIndex;
      CU_ASSERT_EQUAL( trn_grid_get_cell(grid, pos), TRN_TETROMINO_VOID);
    }
  }
}
Example #8
0
void trn_grid_print(TrnGrid const * const grid)
{
    TrnPositionInGrid pos;
    int rowIndex;
    int columnIndex;
    TrnTetrominoType type;

    char* symbols[] = {"I","O","T","S","Z","J","L"};

    // Print top matrix border.
    printf("+");
    for (columnIndex = 1 ; columnIndex < grid->numberOfColumns+1 ; columnIndex++) {
        printf("-");
    }
    printf("+\n");

    // Print left matrix border, matrix type values, matrix right border.
    for (rowIndex = 0 ; rowIndex < grid->numberOfRows ; rowIndex++) {
        pos.rowIndex = rowIndex;
        printf("|");
        for (columnIndex = 0 ; columnIndex < grid->numberOfColumns ; columnIndex++) {
            pos.columnIndex = columnIndex;
            type = trn_grid_get_cell(grid, pos);
            if (type == TRN_TETROMINO_VOID) {
                printf(" ");
            } else {
                printf("%s",symbols[type]);
            }
        }
        printf("|\n");
    }

    // Print bottom matrix border.
    printf("+");
    for (columnIndex = 1 ; columnIndex < grid->numberOfColumns+1 ; columnIndex++) {
        printf("-");
    }
    printf("+\n");
}
Example #9
0
bool trn_grid_cell_is_in_grid_and_is_void(TrnGrid const * const grid,
                                          TrnPositionInGrid const pos)
{
    return trn_grid_cell_is_in_grid(grid,pos) &&
           trn_grid_get_cell(grid, pos) == TRN_TETROMINO_VOID;
}