Add method(s) to the unit testing class that you developed to test the rotation
ID: 3829064 • Letter: A
Question
Add method(s) to the unit testing class that you developed to test the rotation of all of the pieces. Write tests that check that the rotation goes as planned when it is allowed. Write also tests that check that no rotation is done when the piece can't rotate because of a lack of space. You may modify some of the existing classes to add methods that return the values of some of the instance fields to check them in your tests. For instance, you may implement a getSquareArray method in AbstractPiece that returns the array of squares of the piece.
Can you help me write the code i don't know how to get it to work about check that no rotation is done when the piece can't rotate because of a lack of space, rotation of all of the pieces
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.awt.Color;
import org.junit.Test;
public class TetrisUnitTest {
@Test
public void testMoveSquare() {
Grid g = new Grid();
Square s = new Square(g, 4, 4, Color.MAGENTA, true);
// s is blocked on the right
g.set(4, 5, Color.YELLOW);
// s should not be able to move to the right
boolean b = s.canMove(Direction.RIGHT);
// System.out.println("b = " + b);
assertFalse(b);
// s should be able to move left and down
assertTrue(s.canMove(Direction.LEFT));
assertTrue(s.canMove(Direction.DOWN));
// move s down
s.move(Direction.DOWN);
assertTrue(s.getRow() == 5 && s.getCol() == 4);
}
@Test
public void testMoveLShape() {
Grid g = new Grid();
// l1 starts in the far left corner
LShape l1 = new LShape(1, 0, g);
// l1 should not be able to move to the left
assertFalse(l1.canMove(Direction.LEFT));
// count how many moves it takes to go to the right
int m = 0;
while (l1.canMove(Direction.RIGHT) == true) {
l1.move(Direction.RIGHT);
m ++;
}
// it should take be two less than the width of the grid
// as we are taking into account the shape of the piece
assertTrue(m == Grid.WIDTH - 2);
// drop down l1
l1.move(Direction.DROP);
// l1 should drop down to the bottom right corner
// we check that each square of the l1 landed in the correct location
// check square 0
assertTrue(l1.getLocations()[0].getX() == (double)(Grid.HEIGHT - 3));
assertTrue(l1.getLocations()[0].getY() == (double)(Grid.WIDTH - 2));
// check square 1
assertTrue(l1.getLocations()[1].getX() == (double)(Grid.HEIGHT - 2));
assertTrue(l1.getLocations()[1].getY() == (double)(Grid.WIDTH - 2));
// check square 2
assertTrue(l1.getLocations()[2].getX() == (double)(Grid.HEIGHT - 1));
assertTrue(l1.getLocations()[2].getY() == (double)(Grid.WIDTH - 2));
// check square 3
assertTrue(l1.getLocations()[3].getX() == (double)(Grid.HEIGHT - 1));
assertTrue(l1.getLocations()[3].getY() == (double)(Grid.WIDTH - 1));
// l2 starts in the middle of the grid
LShape l2 = new LShape(Grid.HEIGHT / 2 - 1, Grid.WIDTH / 2 - 1, g);
// drop down l2
l2.move(Direction.DROP);
// l2 should land at the bottom of the grid, but have the same Y location
// we check that each square of the l2 landed in the correct place
// check square 0
assertTrue(l2.getLocations()[0].getX() == (double)(Grid.HEIGHT - 3));
assertTrue(l2.getLocations()[0].getY() == (double)(Grid.WIDTH / 2 - 1));
// check square 1
assertTrue(l2.getLocations()[1].getX() == (double)(Grid.HEIGHT - 2));
assertTrue(l2.getLocations()[1].getY() == (double)(Grid.WIDTH / 2 - 1));
// check square 2
assertTrue(l2.getLocations()[2].getX() == (double)(Grid.HEIGHT - 1));
assertTrue(l2.getLocations()[2].getY() == (double)(Grid.WIDTH / 2 - 1));
// check square 3
assertTrue(l2.getLocations()[3].getX() == (double)(Grid.HEIGHT - 1));
assertTrue(l2.getLocations()[3].getY() == (double)(Grid.WIDTH / 2));
}
/**
* An example of a unit test for check rows Add your own grid example
*/
@Test
public void testCheckRows() {
// Create a grid with a few complete rows
// e.g.
/**
* <pre>
* -> row = 0
* XXXXXXXXXX -> row = 1
* XXXX -> row = 2
* XX -> row = 3
* XX -> row = 4
* XXXXXXXXXX -> row = 5
* XX -> row = 6
* XX -> row = 7
* XXXXXXXXXX -> row = 8
* XX -> row = 9
* (empty rows from row = 10 to 19)
* </pre>
*/
// After calling checkRows, the grid should be
/**
* <pre>
* -> row = 0
* -> row = 1
* -> row = 2
* -> row = 3
* XXXX -> row = 4
* XX -> row = 5
* XX -> row = 6
* XX -> row = 7
* XX -> row = 8
* XX -> row = 9
* (empty rows from row = 10 to 19)
* </pre>
*/
Grid g = new Grid();
// rows[r] = number of non empty squares on row r (the non empty squares
// are centered)
int[] rows = { 0, 10, 4, 2, 2, 10, 2, 2, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0 };
for (int r = 0; r < Grid.HEIGHT; r++) {
int cLeft = 0, cRight = Grid.WIDTH - 1;
while (cLeft <= cRight) {
if (cRight - cLeft < rows[r]) {
g.set(r, cLeft, Color.MAGENTA);
g.set(r, cRight, Color.MAGENTA);
}
cLeft++;
cRight--;
}
}
g.checkRows();
rows = new int[] { 0, 0, 0, 0, 4, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0 };
for (int r = 0; r < Grid.HEIGHT; r++) {
int cLeft = 0, cRight = Grid.WIDTH - 1;
while (cLeft <= cRight) {
if (cRight - cLeft < rows[r]) {
assertTrue(g.isSet(r, cLeft));
assertTrue(g.isSet(r, cRight));
} else {
assertFalse(g.isSet(r, cLeft));
assertFalse(g.isSet(r, cRight));
}
cLeft++;
cRight--;
}
}
}
@Test
public void checkRows2() {
Grid g = new Grid();
for (int r = 0; r < Grid.HEIGHT; r++) {
if (r == 10) {
for (int c = 6; c <= 7; c++) {
g.set(r, c, Color.RED);
}
} else {
for (int c = 0; c < Grid.WIDTH; c++) {
g.set(r, c, Color.RED);
}
}
}
g.checkRows();
// check the grid
for (int r = 0; r < Grid.HEIGHT - 1; r ++) {
for (int c = 0; c < Grid.WIDTH; c ++) {
assertFalse(g.isSet(r, c));
}
}
// bottom row
for (int c = 0; c < Grid.WIDTH; c ++) {
if (c != 6 && c != 7) {
assertFalse(g.isSet(Grid.HEIGHT - 1, c));
} else {
assertTrue(g.isSet(Grid.HEIGHT - 1, c));
}
}
}
}
This is a link to the files https://www.drive.google.com/drive/folders/0B1dnw73oqZ9wRmlva0N2S3hOMEk?usp=sharing
Explanation / Answer
void bubbleSort(int *arr, int length); int main(int argc, const char * argv[]) { int arr[] = {-3,-2,0,4,5,8}; for(int i = 0; i < 6; i++) { arr[i] *= arr[i]; } bubbleSort(arr, 6); for(int j = 0; j < 6; j++) coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.