Tetrominoes C++ Program Description In this assignment you will write a program
ID: 3759364 • Letter: T
Question
Tetrominoes C++ Program
Description
In this assignment you will write a program that manipulates “tetrominoes”. Tetrominoes are geometric shapes that are formed by assembling four squares. See https://en.wikipedia.org/wiki/Tetromino Tetrominoes are the shapes used in the game of Tetris.
We will consider the set of one-sided tetrominoes as defined in the above Wikipedia page. There are 7 different one-sided tetrominoes, named I, O, T, J, L, S, Z (the names are reminiscent of the shapes). We can represent tetrominoes by drawing the four squares forming the tetromino as shown in Figure 1:
The cells (or squares) forming a tetromino are indexed by a number in [0,3] as shown in Fig 1. A tetromino can be placed on a board by specifying the position of its “zeroth cell” in the x-y plane. In Figure 2 below, tetrominoes have been placed on a board as follows:
- A tetromino of type “O” placed at (0,7)
- A tetromino of type “J” placed at (5,7)
- A tetromino of type “I” placed at (9,6)
- A tetromino of type “L” placed at (1,3)
- A tetromino of type “T” placed at (5,4)
- A tetromino of type “Z” placed at (4,0)
- A tetromino of type “S” placed at (7,1).
Furthermore, the orientation of a tetromino can be specified by an integer value ranging from 0 to 3. The orientation value indicates the number of times the shape is rotated anti-clockwise by 90 degrees about the position of the zeroth cell, starting from the original orientation given in Figure 1. For example, in Figure 3 below, tetrominoes have been placed on the board as follows:
- A tetromino of type “O” placed at (0,7) with orientation 0.
- A tetromino of type “J” placed at (5,7) with orientation 1.
- A tetromino of type “I” placed at (9,6) with orientation 0.
- A tetromino of type “L” placed at (1,3) with orientation 2.
- A tetromino of type “T” placed at (5,4) with orientation 3.
- A tetromino of type “Z” placed at (4,0) with orientation 0.
- A tetromino of type “S” placed at (7,1) with orientation 0.
The goal of HW2 is to implement a class representing tetrominoes. A given program will use this class to create tetrominoes at arbitrary locations in the x-y plane with arbitrary orientations. The program uses a function to detect the creation of overlapping tetrominoes, since they are not allowed to share a location on the board. The board is assumed to be the entire x-y plane.
The class Tetromino is an abstract base class from which seven classes are derived, named: I, O, T, J, L, S, Z. A detailed specification of the class Tetromino is given below. You are given the header file Tetromino.h. Your task is to write the implementation file Tetromino.cpp containing the implementation of all classes. You are not allowed to modify the file Tetromino.h. Your class implementation file Tetromino.cpp should compile without warning using the command
$ g++ -Wall –c Tetromino.cpp
You are also given a program testTetromino.cpp and a Makefile (same web site as above). These two files should not be modified. The testTetromino.cpp program tests the functionality of the Tetromino class. You should be able to build the executable testTetromino using the command
$ make
You will use the testTetromino executable and the input and output test files provided on the web site to check the functionality of your classes and verify that your program reproduces the test output exactly. Use the diff command to compare your output file with the output file provided. Note that these test files do not check all the functionality and you are responsible for verifying your implementation. Other test files will also be used when grading your implementation. The testTetromino program reads input from stdin and writes to stdout. It can be used e.g. as
$ ./testTetromino < test1.in
It can be assumed that input will consist of two lines. Each line consists of one character followed by three integers.
Specification of the Tetromino class
We describe here the detailed specification of the Tetromino class. The positions of the cells occupied by a tetromino are stored in two integer arrays x[4] and y[4], i.e. a tetromino occupies the cells located at (x[0],y[0]), (x[1],y[1]), (x[2],y[2]) and (x[3],y[3]).
Study carefully the file Tetromino.h. Some members are public and some protected. Some members are virtual. Make sure you understand why. Study the file testTetromino.cpp in order to understand how the classes will be used.
Public members
virtual char name(void) = 0;
This pure virtual function returns the name of the class instance as a character, i.e. I, O, T, J, L, S, Z.
void print(void)
This function prints information about the location of the tetromino by showing the position of the cells that the tetromino occupies:
name at (x[0],y[0]) (x[1],y[1]) (x[2],y[2]) (x[3],y[3])
For example, from Figure 3:
T at (5,4) (6,5) (6,4) (6,3)
bool overlap(const Tetromino &t)
This function should return true if the tetromino calling the function overlaps with the tetromino t. This can be checked by comparing the arrays x and y of both tetrominoes. If there is no overlap, i.e. no cells are shared by the two tetrominoes, the function should return false.
static Tetromino *makeTetromino(char ch, int posx, int posy, int orientation)
This function is a "tetromino factory". It is used to generate instances of tetrominoes. It creates an instance of the appropriate kind of tetromino (specified by the character ch, with values 'I', 'O', 'T', 'J','L', 'S', 'Z') at the given position (posx,posy) and with the given orientation using the operator new, and returns a pointer to the instance. If the character ch is not one of the allowed letters, or if the orientation parameter is not in [0,3], the function should throw an invalid_argument exception.
protected members
void rotate(int nrot)
This function performs a 90-degree anti-clockwise rotation of the tetromino nrot times about the position of the zeroth cell. This function modifies the values of the arrays x and y. Note that this function is the same for all tetrominoes and need not be implemented as a virtual function. Hint 1: use the rotate function in the constructor of the derived classes to take into account the orientation. Hint 2: implement first a 90 degree rotation and include it into a for loop to repeat the 90 degree rotation nrot times. int x[4],y[4] The coordinates of the cells occupied by the tetromino.
3 2 3 1 23 2 0 0 1 0 I C 3 2 0 1 2 0 1 0 1 Figure 1Explanation / Answer
Given below is the completed Tetromino.cpp file and it output. Use it with the .h file given in question. Please don't forget to rate the answer if it helped. Thank you.
Tetromino.cpp
#include "Tetromino.h"
#include <iostream>
using namespace std;
void Tetromino::print(void) const
{
cout << name() << " at ";
for(int i = 0; i < 4; i++)
cout << "(" << x[i] << "," << y[i] << ") " ;
cout << endl;
}
bool Tetromino::overlap(const Tetromino &t) const
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
if(x[i] == t.x[j] && y[i] == t.y[j])
return true;
}
return false;
}
Tetromino * Tetromino::makeTetromino(char ch,int posx,int posy,int orientation)
{
Tetromino *t = NULL;
string errmsg;
if(orientation < 0 || orientation > 3)
throw invalid_argument("orientation out of range 0-3");
switch(ch)
{
case 'O':
t = new O(posx, posy, orientation);
break;
case 'J':
t = new J(posx, posy, orientation);
break;
case 'L':
t = new L(posx, posy, orientation);
break;
case 'I':
t = new I(posx, posy, orientation);
break;
case 'T':
t = new T(posx, posy, orientation);
break;
case 'S':
t = new S(posx, posy, orientation);
case 'Z':
t = new Z(posx, posy, orientation);
break;
default:
errmsg = "Invalid character " + to_string(ch);
throw invalid_argument(errmsg);
}
return t;
}
void Tetromino::rotate(int nrot)
{
int temp;
for(int r = 1; r <= nrot; r++)
{
for(int i = 1; i < 4; i++)
{
//make the points relative to origin instead of starting point
//starting point is x[0], y[0]
x[i] -= x[0];
y[i] -= y[0];
//perform rotation.
//Rotation of a point (x, y) about origin (90 deg) is given as
//(x', y') = (-y, x)
temp = x[i];
x[i] = -y[i];
y[i] = temp;
//now move the points back so that they are relative to original starting point
x[i] += x[0];
y[i] += y[0];
}
}
}
//; I
I::I(int posx, int posy, int orientation)
{
x[0] = posx;
y[0] = posy;
x[1] = posx ;
y[1] = posy + 1;
x[2] = posx;
y[2] = posy + 2;
x[3] = posx ;
y[3] = posy + 3;
rotate(orientation);
}
char I::name(void) const
{
return 'I';
}
//Tetromino O
O::O(int posx, int posy, int orientation)
{
x[0] = posx;
y[0] = posy;
x[1] = posx + 1;
y[1] = posy;
x[2] = posx;
y[2] = posy + 1;
x[3] = posx + 1;
y[3] = posy + 1;
rotate(orientation);
}
char O::name(void) const
{
return 'O';
}
//Tetromino T
T::T(int posx, int posy, int orientation)
{
x[0] = posx;
y[0] = posy;
x[1] = posx - 1;
y[1] = posy + 1;
x[2] = posx;
y[2] = posy + 1;
x[3] = posx + 1;
y[3] = posy + 1;
rotate(orientation);
}
char T::name(void) const
{
return 'T';
}
//Tetromino J
J::J(int posx, int posy, int orientation)
{
x[0] = posx;
y[0] = posy;
x[1] = posx + 1;
y[1] = posy ;
x[2] = posx + 1;
y[2] = posy + 1;
x[3] = posx + 1;
y[3] = posy + 2;
rotate(orientation);
}
char J::name(void) const
{
return 'J';
}
//Tetromino L
L::L(int posx, int posy, int orientation)
{
x[0] = posx;
y[0] = posy;
x[1] = posx + 1;
y[1] = posy ;
x[2] = posx ;
y[2] = posy + 1;
x[3] = posx;
y[3] = posy + 2;
rotate(orientation);
}
char L::name(void) const
{
return 'L';
}
//Tetromino S
S::S(int posx, int posy, int orientation)
{
x[0] = posx;
y[0] = posy;
x[1] = posx + 1;
y[1] = posy ;
x[2] = posx + 1;
y[2] = posy + 1;
x[3] = posx + 2;
y[3] = posy + 1;
rotate(orientation);
}
char S::name(void) const
{
return 'S';
}
//Tetromino Z
Z::Z(int posx, int posy, int orientation)
{
x[0] = posx;
y[0] = posy;
x[1] = posx + 1;
y[1] = posy ;
x[2] = posx - 1;
y[2] = posy + 1;
x[3] = posx ;
y[3] = posy + 1;
rotate(orientation);
}
char Z::name(void) const
{
return 'Z';
}
output of testTetromino.cpp
$ ./a.out
L 1 3 2
L at (1,3) (0,3) (1,2) (1,1)
T 5 4 3
T at (5,4) (6,5) (6,4) (6,3)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.