I am trying to use Java to solve the following.
Given an arrangement of dominos, determine, whether or not it is a legal arrangement and return True or False accordingly. For example, this is an illegal arrangement [2 3][4 2][2 2][3 5] and this is a legal arrangement of Dominos. [2 3][3 5][5 4][4 5].
I have no clue how to begin this to write this program.
Write a Domino class
public class Domino {
private int leftValue;
private int rightValue;
public Domino(int leftValue, int rightValue) {
this.leftValue = leftValue;
this.rightValue = rightValue;
}
public int getLeftValue() {
return leftValue;
}
public int getRightValue() {
return rightValue;
}
}
Create an array or list containing the domino's
Domino[] dominos = new Domino[] {
new Domino(2, 3),
new Domino(3, 2),
new Domino(2, 5),
new Domino(2, 5)
};
Loop and match left and right values
Domino previous = null;
for (Domino current : dominos) {
if (previous != null) {
if (current.getLeftValue() != previous.getRightValue()) {
throw new Exception("Illegal arrangement detected");
}
}
previous = current;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.