Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program help, writing a program named Board.cpp with the following specifica

ID: 3765632 • Letter: C

Question

C++ Program help, writing a program named Board.cpp with the following specifications

Specification of the Board class

The Board class is responsible for managing the tetrominoes added to the board.

It has a member function addTetromino which adds a tetromino to the board after checking that the new tetromino fits inside the board area and does not overlap with any of the previously added tetrominoes.

public members of the Board class

Board(int size_x, int size_y) Constructor taking as arguments the horizontal and vertical size of the board, expressed as the number of cells in each direction.

void addTetromino(char type, int x, int y, int orientation) Creates a tetromino and adds it to the board. The function should first test if the new tetromino fits within the board. If it doesn’t, the function should throw an exception and the program should terminate after printing does not fit on the board Then the function should test if the tetromino overlaps with any tetromino already present on the board.

If there is overlap, the function should throw an exception and the program should terminate after printing overlap

bool fits_on_board(const Tetromino &t) const

A function that returns true if the tetromino t fits inside the board, (i.e. has all its cells within the boundaries of the board) and false otherwise.

void draw(void) const This function draws the current state of the board by printing a complete svg document on stdout. It should first print the following svg header:

<?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="570" height="570" viewBox="0 0 550 550" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" > <g transform="matrix(1,0,0,-1,50,550)">

Note that this header is fixed, i.e. it is independent of the board’s size.

It should then draw a frame with white background defining the board area.

For example, for a board of 8x5 cells, the frame is drawn by printing:

<rect fill="white" stroke="black" x="0" y="0" width="400" height="250"/>

Note that the width and height attributes depend on the size of the board. It should then draw each tetromino on the board by calling the tetrominoes’ draw functions.

Finally it should print the following svg trailer: The resulting svg file can be visualized by opening it with a browser such as Firefox, Safari, Chrome or Internet Explorer.

private members of the Board class

Board(void); The constructor is made private to prevent the creation of a Board with undefined size.

int size_x, size_y ;

The dimensions of the board measured in number of cells in each direction.

vector<Tetromino *> tList ;

A vector of base pointers to store pointers to the tetrominoes added to the board.

Explanation / Answer

Answer:

addTetromino() method:

void Game::addTetromino (int pX, int pY, int pPiece, int pRotation)
{
color mColor;

  
int mPixelsX = mBoard->GetXPosInPixels (pX);
int mPixelsY = mBoard->GetYPosInPixels (pY);


for (int i = 0; i < PIECE_BLOCKS; i++)
{
for (int j = 0; j < PIECE_BLOCKS; j++)
{
  
switch (mPieces->GetBlockType (pPiece, pRotation, j, i))
{
case 1: mColor = GREEN; break;
case 2: mColor = BLUE; break;   
}

if (mPieces->GetBlockType (pPiece, pRotation, j, i) != 0)
mIO->DrawRectangle (mPixelsX + i * BLOCK_SIZE,
mPixelsY + j * BLOCK_SIZE,
(mPixelsX + i * BLOCK_SIZE) + BLOCK_SIZE - 1,
(mPixelsY + j * BLOCK_SIZE) + BLOCK_SIZE - 1,
mColor);
}
}
}

draw() method:

void Game::draw ()
{


int mX1 = BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2)) - 1;
int mX2 = BOARD_POSITION + (BLOCK_SIZE * (BOARD_WIDTH / 2));
int mY = mScreenHeight - (BLOCK_SIZE * BOARD_HEIGHT);


//assert (mY > MIN_VERTICAL_MARGIN);

  
mIO->DrawRectangle (mX1 - BOARD_LINE_WIDTH, mY, mX1, mScreenHeight - 1, BLUE);

mIO->DrawRectangle (mX2, mY, mX2 + BOARD_LINE_WIDTH, mScreenHeight - 1, BLUE);


  
mX1 += 1;
for (int i = 0; i < BOARD_WIDTH; i++)
{
for (int j = 0; j < BOARD_HEIGHT; j++)
{   
  
if (!mBoard->IsFreeBlock(i, j))
mIO->DrawRectangle ( mX1 + i * BLOCK_SIZE,
mY + j * BLOCK_SIZE,
(mX1 + i * BLOCK_SIZE) + BLOCK_SIZE - 1,
(mY + j * BLOCK_SIZE) + BLOCK_SIZE - 1,
RED);
}
}   
}

fits_on_board() method:

bool Board::fits_on_board(int pX, int pY)
{
if (mBoard [pX][pY] == POS_FREE) return true; else return false;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote