Publicité
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
Publicité
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
Prochain SlideShare
Note that colors from C can be used more than once and that not all co.docxNote that colors from C can be used more than once and that not all co.docx
Chargement dans ... 3
1 sur 8
Publicité

Contenu connexe

Plus de aminaENT(20)

Publicité

Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf

  1. Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language package model; import java.util.Set; public interface Cube { public Set getAllPossibleCubeNets(); } package model; import java.awt.Color; public interface CubeNet { public Color getTop(); public Color getFront(); public Color getRight(); public Color getBack(); public Color getLeft(); public Color getBottom(); } package model; public enum Face { TOP, FRONT, RIGHT, BACK, LEFT, BOTTOM; } package model; public class CubeNetImpl implements CubeNet { private Color top; private Color front; private Color right; private Color left; private Color bottom; private Color back; private Set> allPossibleMaps = null; private Integer hashCode = null; private static long timeSpentHashCode = 0;
  2. private static long timeSpentInSetAddMillis = 0; private static boolean CALCULATE_POSSIBLE_MAPS_ONE_TIME = false; private static boolean CALCULATE_HASHCODE_ONE_TIME = false; private static int HASHCODE_LEVEL = 1; public CubeNetImpl(Map faceToColorMap) { assert faceToColorMap != null : "faceToColorMap is null!"; top = faceToColorMap.get(Face.TOP); front = faceToColorMap.get(Face.FRONT); right = faceToColorMap.get(Face.RIGHT); left = faceToColorMap.get(Face.LEFT); back = faceToColorMap.get(Face.BACK); bottom = faceToColorMap.get(Face.BOTTOM); if (CALCULATE_POSSIBLE_MAPS_ONE_TIME) { long startTime = System.currentTimeMillis(); allPossibleMaps = generateAllPossibleMaps(); long endTime = System.currentTimeMillis(); timeSpentInSetAddMillis += endTime - startTime; CALCULATE_POSSIBLE_MAPS_ONE_TIME = false; } if (CALCULATE_HASHCODE_ONE_TIME) { long startTime = System.currentTimeMillis(); hashCode(HASHCODE_LEVEL); long endTime = System.currentTimeMillis(); timeSpentHashCode += endTime - startTime; CALCULATE_HASHCODE_ONE_TIME = false; } } @Override public Color getTop() { return top; }
  3. @Override public Color getFront() { return front; } @Override public Color getRight() { return right; } @Override public Color getBack() { return back; } @Override public Color getLeft() { return left; } @Override public Color getBottom() { return bottom; } private int getDistinctColorCount() { Set distinctColors = new HashSet(); distinctColors.add(top); distinctColors.add(front); distinctColors.add(right); distinctColors.add(left); distinctColors.add(bottom); return distinctColors.size(); }
  4. private Set> generateAllPossibleMaps() { Set> allMaps = new HashSet<>(); Map initialMap = new HashMap<>(); initialMap.put(Face.FRONT, Color.RED); initialMap.put(Face.BACK, Color.ORANGE); initialMap.put(Face.TOP, Color.WHITE); initialMap.put(Face.BOTTOM, Color.YELLOW); initialMap.put(Face.LEFT, Color.GREEN); initialMap.put(Face.RIGHT, Color.BLUE); generateAllPossibleMapsHelper(allMaps, initialMap); return allMaps; } private void generateAllPossibleMapsHelper(Set> allMaps, Map currentMap) { if (currentMap.size() == 6) { allMaps.add(new HashMap<>(currentMap)); return; } Set usedColors = new HashSet<>(currentMap.values()); for (Color color : Color.class.getEnumConstants()) { if (!usedColors.contains(color)) { currentMap.put(getNextFace(currentMap), color); generateAllPossibleMapsHelper(allMaps, currentMap); currentMap.remove(getNextFace(currentMap)); } } } private Face getNextFace(Map currentMap) { for (Face face : Face.values()) { if (!currentMap.containsKey(face)) { return face; } }
  5. return null; } public int hashCode(int level) { int hashCode = 0; if(this.hashCode != null) { hashCode = this.hashCode(); } else if (level == 0) { hashCode = 0; } else if (level == 1) { hashCode = (top.getRGB() + bottom.getRGB() + left.getRGB() + front.getRGB() + back.getRGB() + right.getRGB()); } else if(level == 2) { hashCode = getDistinctColorCount(); } else { assert level == 0 : "level = " + level + "is uniterpretable"; } return hashCode; } public static Color getColorForFace(Face face, CubeNetImpl_Opinca cubeNet) { switch (face) { case TOP: return cubeNet.getTop(); case FRONT: return cubeNet.getFront(); case RIGHT: return cubeNet.getRight(); case BACK:
  6. return cubeNet.getBack(); case LEFT: return cubeNet.getLeft(); case BOTTOM: return cubeNet.getBottom(); default: return null; } } @Override public int hashCode() { if (CALCULATE_HASHCODE_ONE_TIME) { long startTime = System.currentTimeMillis(); hashCode(HASHCODE_LEVEL); long endTime = System.currentTimeMillis(); timeSpentHashCode += endTime - startTime; CALCULATE_HASHCODE_ONE_TIME = false; } return hashCode; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj == null || obj.getClass() != this.getClass()) { return false; } CubeNetImpl other = (CubeNetImpl) obj; return top.equals(other.top) && front.equals(other.front) && right.equals(other.right) && left.equals(other.left) &&
  7. bottom.equals(other.bottom) && back.equals(other.back); } @Override public String toString() { return "CubeNetImpl{" + " top = " + top + ", front = " + front + ", right = " + right + ", back = " + back + ", left = " + left + ", bottom = " + bottom + '}'; } } package model; import java.util.Set; public class CubeImpl_Skeleton implements Cube { public CubeImpl_Skeleton(CubeNet cubeNetRepresentative) { assert cubeNetRepresentative != null : "cubeNetRepresentative is null!"; throw new RuntimeException("NOT IMPLEMENTED YET!"); } @Override public Set getAllPossibleCubeNets() { throw new RuntimeException("NOT IMPLEMENTED YET!"); } } public class CubeSolverImpl{ private CubeSolverImpl() { assert false : "DO NOT INSTANTIATE!"; } public static Set getDistinctSolidCubes(Set colors) { Set cubes = new HashSet<>();
  8. // Generate all possible nets for a cube // For each net, try to color it with the given set of colors // Check if each cube in the net can be colored with the given set of colors // If all cubes in the net can be colored, create a Cube object and add it to the set } }
Publicité