When I run the following test case:
@Test(timeout=1000) public void shape_displayChar2_rot(){
String layout =
"b....\n"+
".....\n"+
"....b\n"+
"";
String expect =
"SHAPE z\n"+
"height: 5; width: 3; rotation: CW90\n"+
"..z\n"+
"...\n"+
"...\n"+
"...\n"+
"z..\n"+
"";
char newDC = 'z';
char dc = getDisplayChar(layout);
Shape shape = FitIt.makeShape(layout,dc);
shape.setDisplayChar(newDC);
shape.rotateCW();
assertEquals(newDC, shape.getDisplayChar());
String actual = shape.toString();
assertEquals(expect,actual);
}
I get the following crash:
Expected:
SHAPE z
height: 5; width: 3; rotation: CW90
..z
...
...
...
z..
My actual code result:
Actual:
SHAPE z
height: 5; width: 3; rotation: CW90
b....
.....
....b
..z
...
...
...
z..
String layoutfrom the above test example rotateCW(), ClockWise rotates 90 degrees, and also changes its nature. My question is: why am I getting:
b.... <<<<----- WHY AM I GETTING THIS ?
..... <<<<----- WHY AM I GETTING THIS ?
....b <<<<----- WHY AM I GETTING THIS ?
..z
...
...
...
z..
instead of just:
..z
...
...
...
z..
the code:
import java.util. *;
public class CreateShape implements Shape {
private String layout;
private int height;
private int width;
private char dc;
private Rotation initialPos;
private char[][] shape;
public CreateShape(int height, int width, char dc, char[][] charLayout, String layout) {
this.height = height;
this.width = width;
this.dc = dc;
this.shape = charLayout;
this.layout = layout;
initialPos = Rotation.CW0;
}
public Rotation getRotation() {
return initialPos;
}
public int getHeight() {
return this.height;
}
public int getWidth() {
return this.width;
}
public char getDisplayChar() {
return dc;
}
public void setDisplayChar(char c) {
this.dc = c;
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[0].length; j++) {
if (shape[i][j] != '.') {
shape[i][j] = c;
}
}
}
}
public void rotateCW() {
initialPos = initialPos.next();
int w = shape.length;
int h = shape[0].length;
char[][] swapped = new char[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
swapped[i][j] = shape[w - j - 1][i];
layout += swapped[i][j];
}
layout += "\n";
}
height = swapped.length;
width = swapped[0].length;
}
public boolean isFilledAt(int row, int col) {
if (row < 0 || row >= height || col < 0 || col >= width) {
throw new FitItException("Oops! Out of bounds!");
} else {
for (int r = 0; r <= height; r++) {
for (int c = 0; c <= width; c++) {
if (shape[row][col] == dc) {
return true;
}
}
}
}
return false;
}
public String toString() {
return "SHAPE " + this.dc + "\n" +
"height: " + this.height + ";" + " width: " + this.width + "; " + getRotation().toString() + "\n" +
this.layout;
}
}
class fitit
public class FitIt {
public static Shape makeShape(String layout, char displayChar) {
Shape result;
int height = 0;
int width = 0;
Scanner data = new Scanner(layout);
while (data.hasNextLine()) {
String line = data.nextLine();
width = line.length();
height++;
}
char[][] charLayout = new char[height][width];
Scanner data2 = new Scanner(layout);
for (int i = 0; i < height; i++) {
String line = data2.nextLine();
for (int j = 0; j < width; j++) {
if (line.charAt(j) != '.') {
charLayout[i][j] = displayChar;
}
if (line.charAt(j) == '.') {
charLayout[i][j] = line.charAt(j);
}
}
}
data2.close();
result = new CreateShape(height, width, displayChar, charLayout, layout);
return result;
}
user4833210
source
share