Instructions
Objective
Write a java assignment program to create a domino game.
Requirements and Specifications
Introduction
A domino is a game piece that has 2 numbers on it, one number on top and another underneath. Standard domino pieces have a number from 0 to 6, but these dominos are specially made and can have any nonnegative number in either the top or bottom position. Domino numbers are not required to be unique. A family places a set of dominos on a table, one after the other, in a row (see picture) in such a way that the number on bottom of the domino in front is equal to the number on top of the domino behind. At night, the family goes to sleep and leaves the dominos on the table, and the family cat jumps on the table and scatters the dominos all over the floor. In the morning, the kids wake up to find the dominos scattered all over the floor and look to you to put them back the way they were. Your goal in this assignment will be to arrange all the dominos in any configuration (the kids won’t know the difference if it’s not exactly the same) such that the number on bottom of the domino in front is equal to the number on top of the domino behind.
HINT: Consider a walk along the string of dominoes as being a change in state from one number to another. Think about a \state" as being which number you happen to currently be. Questions to ask yourself: Is it possible to reassemble the dominoes if there is exactly 1 number that occurs an odd number of times? Is it possible to reassemble the dominoes if there are 3 or more numbers that occur an odd number of times?
Source Code
import java.util.*;
import java.util.stream.Collectors;
public class StudentSolver {
private static ArrayList findEulerianPath(int[][] graph) {
int n = graph.length;
List degrees = new ArrayList<>();
for (int i = 0; i < n; i++) {
degrees.add(Arrays.stream(graph[i]).sum());
}
int startVertex = 0;
int odds = 0;
for (int i = n - 1; i >= 0; i--) {
if (degrees.get(i) % 2 == 1) {
odds++;
startVertex = i;
}
}
if (odds > 2) {
return null;
}
Stack stack = new Stack<>();
List path = new LinkedList<>();
int curr = startVertex;
while (!stack.isEmpty() || Arrays.stream(graph[curr]).sum() != 0) {
if (Arrays.stream(graph[curr]).sum() == 0) {
path.add(curr);
curr = stack.pop();
} else {
for (int i = 0; i < n; i++) {
if (graph[curr][i] == 1) {
stack.add(curr);
graph[curr][i] = 0;
curr = i;
break;
}
}
}
}
ArrayList resultPath = new ArrayList<>();
for (int i = 0; i
int from = path.get(i);
int to = path.get(i+1);
resultPath.add(new int[]{from, to});
}
return resultPath;
}
public static ArrayList solve(ArrayList dominos) {
int[][] adj = new int[7][7];
for (int[] edges : dominos) {
adj[edges[0]][edges[1]]++;
}
return findEulerianPath(adj);
}
public static void main(String[] args) {
ArrayList test = new ArrayList<>();
test.add(new int[]{1,1});
test.add(new int[]{3,1});
test.add(new int[]{2,1});
test.add(new int[]{1,3});
test.add(new int[]{2,2});
test.add(new int[]{1,2});
ArrayList result = solve(test);
System.out.println(
result.stream()
.map(is -> "(" + Arrays.stream(is).mapToObj(Integer::toString)
.collect(Collectors.joining(",")) + ")")
.collect(Collectors.joining(",")));
}
}
Similar Samples
At ProgrammingHomeworkHelp.com, explore our curated samples demonstrating expert solutions across a spectrum of programming tasks. Whether it's algorithms, data structures, or application development, our samples showcase our commitment to delivering clear, effective solutions. Discover how our expertise can elevate your understanding and performance in programming.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java