Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Brickwork Assigment
import java.util.ArrayList;
public class Brick {
public int[] coordinate1 = new int[2];
public int[] coordinate2 = new int[2];
public int number;
public Brick(int[] coordinate1, int[] coordinate2) {
this.coordinate1 = coordinate1;
this.coordinate2 = coordinate2;
this.number = number;
}
public int[] getC1() {
return coordinate1;
}
public int[] getC2() {
return coordinate2;
}
public int getNumber() {
return number;
}
public void setNumber(int newNumber) {
this.number = newNumber;
}
public static Brick[] getBricks(int[][] layout, int length, int width) {
// converts two-dimensional array with brick to Brick array
int brickCount = (length*width)/2;
Brick[] bricks = new Brick[brickCount];
ArrayList<Brick> tempArrList = new ArrayList<>();
for (int i=0; i<length; i++) {
for (int j=0; j<width; j++) {
int current = layout[i][j];
if (j != width-1) {
int down = layout[i][j+1];
if (current == down) {
int[] c1 = {i,j};
int[] c2 = {i,j+1};
Brick downB = new Brick(c1,c2);
downB.setNumber(current);
tempArrList.add(downB);
}
}
if (i != length-1) {
int right = layout[i+1][j];
if (current == right) {
int[] c1 = {i,j};
int[] c2 = {i+1,j};
Brick rightB = new Brick(c1,c2);
rightB.setNumber(current);
tempArrList.add(rightB);
}
}
}
}
bricks = tempArrList.toArray(bricks);
return bricks;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public static void main(String[] args) throws Exception {
System.out.println("Enter size (ex. '2 4')");
Integer[] size = getSize();
System.out.println("Enter layer rows one by one (ex. '1 1 2 2' then '3 3 4 4')");
int[][] layout = getLayout(size[0],size[1]);
Brick[] layer1 = Brick.getBricks(layout, size[1], size[0]);
// prints layer 1
LayerGenerator.getPrintLayout(layer1, size[1], size[0]);
Brick[] layer2 = LayerGenerator.generateLayer(layer1, size[1], size[0]);
// prints layer 2
LayerGenerator.getPrintLayout(layer2, size[1], size[0]);
}
public class Input {
public static String getInput() {
// gets user input
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
return input;
}
public static Integer[] getSize() throws Exception {
// gets size of area input by user
String input = getInput();
String[] size = input.split(" ");
Integer[] nums = new Integer[2];
// width
nums[0]= Integer.valueOf(size[0]);
// length
nums[1]= Integer.valueOf(size[1]);
if (nums[0]>99 || nums[1]>99) {
throw new Exception("Area has to be less than 100 lines and less than 100 columns");
} else if (nums[0]%2!=0 || nums[1]%2!=0) {
throw new Exception("Numbers must be even");
}
System.out.println("Size is: "+nums[0]+" by "+nums[1]);
return nums;
}
public static void validateNumbersCount(String str) throws Exception {
// validates brick locations
String[] array = str.split(" ");
List<String> list = new ArrayList<>();
for (int i=0; i<array.length; i++) {
list.add(array[i]);
}
for (int i=0; i<list.size(); i++) {
int count = Collections.frequency(list, list.get(i));
if (count>2) {
throw new Exception("Bricks cannot be spanning 3 rows/columns");
}
}
}
public static int[][] getLayout(int width, int length) throws Exception {
// converts string input of layout to two dimensional integer array
int[][] intLayout = new int[length][width];
String[] layout = new String[width];
String layoutInOneLine = "";
for (int i=0; i<width; i++) {
String layoutLine = getInput();
layout[i] = layoutLine;
String[] layoutNums = layout[i].split(" ");
for (int j=0; j<length; j++) {
intLayout[j][i] = Integer.parseInt(layoutNums[j]);
}
}
for (int i=0; i<layout.length; i++) {
if (i==0) {
layoutInOneLine = layoutInOneLine + layout[i];
} else {
layoutInOneLine = layoutInOneLine + " " + layout[i];
}
}
validateNumbersCount(layoutInOneLine);
System.out.println("Layer 1:");
return intLayout;
}
}
import java.util.ArrayList;
public class Layer {
public static Brick[] generateLayer (Brick[] layer1, int length, int width) {
// generates layer 2
// initialize arraylist to store layer 2 bricks
ArrayList<Brick> tempArrList = new ArrayList<Brick>();
for (int i=0; i<length; i++) {
for (int j=0; j<width; j++) {
int[] currentBrick = {i,j};
int[] rightBrick = {i+1,j};
int[] lowerBrick = {i,j+1};
boolean cTaken = false;
int brickCount = 0;
// at last position breaks the loop
if (currentBrick[0]==length-1 && currentBrick[1]==width-1) {
break;
}
// checks if current coordinate is taken by a generated brick
if (tempArrList.size()!=0) {
for (int k=0; k<tempArrList.size(); k++) {
int[] brickC1 = tempArrList.get(k).getC1();
int[] brickC2 = tempArrList.get(k).getC2();
if ((brickC1[0] == currentBrick[0] && brickC1[1] == currentBrick[1]) || (brickC2[0] == currentBrick[0] && brickC2[1] == currentBrick[1])) {
cTaken = true;
}
}
}
if (!cTaken) {
Brick currentBrickLayer1 = null;
for (int k=0; k<layer1.length; k++) {
// find brick from layer1 with current coordinates
if (layer1[k].getC1()[0]==currentBrick[0] && layer1[k].getC1()[1]==currentBrick[1] || layer1[k].getC2()[0]==currentBrick[0] && layer1[k].getC2()[1]==currentBrick[1]) {
currentBrickLayer1 = layer1[k];
}
}
if (i<length-1 && j<width-1 && currentBrickLayer1.getC1()[0]==currentBrick[0] && currentBrickLayer1.getC1()[1]==currentBrick[1] && currentBrickLayer1.getC2()[0]==rightBrick[0] && currentBrickLayer1.getC2()[1]==rightBrick[1]) {
// if brick is horizontal, generate vertical brick
Brick newBrick = new Brick(currentBrick, lowerBrick);
tempArrList.add(newBrick);
brickCount++;
continue;
} else if (j<width-1 && currentBrickLayer1.getC1()[0]==currentBrick[0] && currentBrickLayer1.getC1()[1]==currentBrick[1] && currentBrickLayer1.getC2()[0]==lowerBrick[0] && currentBrickLayer1.getC2()[1]==lowerBrick[1]) {
// if brick is vertical, generate horizontal brick
Brick newBrick = new Brick(currentBrick, rightBrick);
tempArrList.add(newBrick);
brickCount++;
continue;
} else if (i<length-1 && currentBrickLayer1.getC2()[0]==currentBrick[0] && currentBrickLayer1.getC2()[1]==currentBrick[1]){
// when there is space on the right, generates a horizontal brick
Brick newBrick = new Brick(currentBrick, rightBrick);
tempArrList.add(newBrick);
brickCount++;
continue;
} else if(i==length-1 && currentBrickLayer1.getC2()[0]==currentBrick[0] && currentBrickLayer1.getC2()[1]==currentBrick[1]) {
// when there is no more space on the right, generates a vertical brick
Brick newBrick = new Brick(currentBrick, lowerBrick);
tempArrList.add(newBrick);
brickCount++;
continue;
}
} else {
continue;
}
}
}
// converts tempArrList to Brick array from arraylist
Object[] array = tempArrList.toArray();
Brick[] layer2 = new Brick[tempArrList.size()];
for (int i=0; i<tempArrList.size();i++) {
Brick brick = (Brick) array[i];
layer2[i] = brick;
}
System.out.println("\nLayer 2:");
// checks for non-occupied coordinates
for (int i=0; i<length; i++) {
for (int j=0; j<width; j++) {
boolean found = false;
for (int k = 0; k < layer2.length; k++) {
if (layer2[k].getC1()[0] == i && layer2[k].getC1()[1] == j) {
found = true;
} else if (layer2[k].getC2()[0] == i && layer2[k].getC2()[1] == j) {
found = true;
}
}
if (!found) {
throw new Error("-1");
}
}
}
return layer2;
}
public static void getPrintLayout(Brick[] layer, int length, int width) {
// prints layout in asterisks table
ArrayList<String> rows = new ArrayList<>();
for (int m=0; m<layer.length; m++) {
layer[m].setNumber(m+1);
}
for (int i=0;i<width;i++) {
String row = "";
ArrayList<Brick> tempArrList = new ArrayList<>();
// checks if it is on the current row
for (int k=0;k<layer.length;k++) {
int[] C1YCoordinate = layer[k].getC1();
int[] C2YCoordinate = layer[k].getC2();
// if it is, add to list
if (C1YCoordinate[1]==i || C2YCoordinate[1]==i) {
tempArrList.add(layer[k]);
}
}
// gets formatted row
for (int l=0;l<tempArrList.size();l++) {
int[] C1CurrentBlock = tempArrList.get(l).getC1();
int[] C2CurrentBlock = tempArrList.get(l).getC2();
int brickNumber = tempArrList.get(l).getNumber();
if (C1CurrentBlock[1]==C2CurrentBlock[1]) {
if (brickNumber<10) {
row = row + "* " + tempArrList.get(l).getNumber() + " " + tempArrList.get(l).getNumber() + " ";
} else if (brickNumber>=10 && brickNumber<=99) {
row = row + "* " + tempArrList.get(l).getNumber() + " " + tempArrList.get(l).getNumber() + " ";
}
} else {
if (brickNumber<10) {
row = row + "* " + tempArrList.get(l).getNumber() + " ";
} else if (brickNumber>=10 && brickNumber<=99) {
row = row + "* " + tempArrList.get(l).getNumber() + " ";
}
}
if (l==tempArrList.size()-1) {
row = row + "*";
}
}
rows.add(row);
}
// gets horizontal line
String horizontalLine = "*";
// gets bricks connected to layer 1
ArrayList<Brick> layer1bricks = new ArrayList<>();
for (int i=0; i<layer.length; i++) {
if (layer[i].getC1()[1]==0) {
layer1bricks.add(layer[i]);
}
}
for (int i=0; i<layer1bricks.size(); i++) {
// if brick is horizontal on first layer add 10 asterisks, if vertical then add 6 asterisks
if (layer1bricks.get(i).getC1()[1]==0 && layer1bricks.get(i).getC2()[1]==0) {
horizontalLine = horizontalLine + "**********";
continue;
} else {
horizontalLine = horizontalLine + "******";
continue;
}
}
System.out.println(horizontalLine);
// gets asterisks in between rows
for (int i=0; i<rows.size(); i++) {
System.out.println(rows.get(i));
String middle = "";
for (int j=0; j<rows.get(i).length(); j++) {
if (i==rows.size()-1) {
break;
} else if (rows.get(i).charAt(j)=='*') {
middle = middle + "*";
} else if (rows.get(i).charAt(j)==' ') {
continue;
} else if (Character.isDigit(rows.get(i).charAt(j)) && !Character.isDigit(rows.get(i).charAt(j+1)) && rows.get(i).charAt(j)==rows.get(i+1).charAt(j)) {
middle = middle + " ";
} else if (Character.isDigit(rows.get(i).charAt(j)) && Character.isDigit(rows.get(i).charAt(j+1)) && rows.get(i).charAt(j)==rows.get(i+1).charAt(j) && rows.get(i).charAt(j+1)==rows.get(i+1).charAt(j+1)) {
middle = middle + " ";
} else {
middle = middle + "*********";
j=j+7;
}
}
if (middle!="") {
System.out.println(middle);
}
}
System.out.println(horizontalLine);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment