SlideShare a Scribd company logo
1 of 8
Download to read offline
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;
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;
}
@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();
}
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;
}
}
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:
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) &&
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<>();
// 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
}
}

More Related Content

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

Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
arjuncorner565
 
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdfSet up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
xlynettalampleyxc
 
Martian Cubics and Unit TestingYou must implement all of the data .pdf
Martian Cubics and Unit TestingYou must implement all of the data .pdfMartian Cubics and Unit TestingYou must implement all of the data .pdf
Martian Cubics and Unit TestingYou must implement all of the data .pdf
ellanorfelicityri239
 
Need help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdfNeed help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdf
feelinggifts
 
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfcan you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
sales88
 

Similar to Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf (13)

Kill the DBA
Kill the DBAKill the DBA
Kill the DBA
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading example
 
rubik_solve
rubik_solverubik_solve
rubik_solve
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdfSet up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Martian Cubics and Unit TestingYou must implement all of the data .pdf
Martian Cubics and Unit TestingYou must implement all of the data .pdfMartian Cubics and Unit TestingYou must implement all of the data .pdf
Martian Cubics and Unit TestingYou must implement all of the data .pdf
 
はじめてのGroovy
はじめてのGroovyはじめてのGroovy
はじめてのGroovy
 
Presentatie - Introductie in Groovy
Presentatie - Introductie in GroovyPresentatie - Introductie in Groovy
Presentatie - Introductie in Groovy
 
Need help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdfNeed help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdf
 
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfcan you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
 
Sneaking inside Kotlin features
Sneaking inside Kotlin featuresSneaking inside Kotlin features
Sneaking inside Kotlin features
 

More from aminaENT

Bianca Genova has worked hard to make networking events for LinkedB2.pdf
Bianca Genova has worked hard to make networking events for LinkedB2.pdfBianca Genova has worked hard to make networking events for LinkedB2.pdf
Bianca Genova has worked hard to make networking events for LinkedB2.pdf
aminaENT
 
Berlisle Inc. is a manufacturer of sails for small boats. The manage.pdf
Berlisle Inc. is a manufacturer of sails for small boats. The manage.pdfBerlisle Inc. is a manufacturer of sails for small boats. The manage.pdf
Berlisle Inc. is a manufacturer of sails for small boats. The manage.pdf
aminaENT
 
Below you have the following directory structure. At the top there i.pdf
Below you have the following directory structure. At the top there i.pdfBelow you have the following directory structure. At the top there i.pdf
Below you have the following directory structure. At the top there i.pdf
aminaENT
 
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdfBELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
aminaENT
 
Be familiar with1.The main events which affected fashion ~ refer.pdf
Be familiar with1.The main events which affected fashion ~ refer.pdfBe familiar with1.The main events which affected fashion ~ refer.pdf
Be familiar with1.The main events which affected fashion ~ refer.pdf
aminaENT
 

More from aminaENT (20)

Biology Skin Color Questions1. How does high UV light affect folat.pdf
Biology Skin Color Questions1. How does high UV light affect folat.pdfBiology Skin Color Questions1. How does high UV light affect folat.pdf
Biology Skin Color Questions1. How does high UV light affect folat.pdf
 
Bicoid (bcd) is a maternal-effect gene in Drosophilarequired for nor.pdf
Bicoid (bcd) is a maternal-effect gene in Drosophilarequired for nor.pdfBicoid (bcd) is a maternal-effect gene in Drosophilarequired for nor.pdf
Bicoid (bcd) is a maternal-effect gene in Drosophilarequired for nor.pdf
 
BHP is setting up a lithium mine that costs $120 million today to op.pdf
BHP is setting up a lithium mine that costs $120 million today to op.pdfBHP is setting up a lithium mine that costs $120 million today to op.pdf
BHP is setting up a lithium mine that costs $120 million today to op.pdf
 
Bill Nye goes to the beach to collect data on certain types of nonli.pdf
Bill Nye goes to the beach to collect data on certain types of nonli.pdfBill Nye goes to the beach to collect data on certain types of nonli.pdf
Bill Nye goes to the beach to collect data on certain types of nonli.pdf
 
Bianca Genova has worked hard to make networking events for LinkedB2.pdf
Bianca Genova has worked hard to make networking events for LinkedB2.pdfBianca Genova has worked hard to make networking events for LinkedB2.pdf
Bianca Genova has worked hard to make networking events for LinkedB2.pdf
 
Berth Vader is a Canadian citizen who has always lived in London, On.pdf
Berth Vader is a Canadian citizen who has always lived in London, On.pdfBerth Vader is a Canadian citizen who has always lived in London, On.pdf
Berth Vader is a Canadian citizen who has always lived in London, On.pdf
 
Berlisle Inc. is a manufacturer of sails for small boats. The manage.pdf
Berlisle Inc. is a manufacturer of sails for small boats. The manage.pdfBerlisle Inc. is a manufacturer of sails for small boats. The manage.pdf
Berlisle Inc. is a manufacturer of sails for small boats. The manage.pdf
 
below you will find the condensed finacial statements for Koko Inc. .pdf
below you will find the condensed finacial statements for Koko Inc. .pdfbelow you will find the condensed finacial statements for Koko Inc. .pdf
below you will find the condensed finacial statements for Koko Inc. .pdf
 
Below is the January operating budget for Casey Corp., a retailer.pdf
Below is the January operating budget for Casey Corp., a retailer.pdfBelow is the January operating budget for Casey Corp., a retailer.pdf
Below is the January operating budget for Casey Corp., a retailer.pdf
 
Below you have the following directory structure. At the top there i.pdf
Below you have the following directory structure. At the top there i.pdfBelow you have the following directory structure. At the top there i.pdf
Below you have the following directory structure. At the top there i.pdf
 
Basic SEIR1. What is the equation that describes R0 for this model.pdf
Basic SEIR1. What is the equation that describes R0 for this model.pdfBasic SEIR1. What is the equation that describes R0 for this model.pdf
Basic SEIR1. What is the equation that describes R0 for this model.pdf
 
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdfBELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
BELOW IS MY CODE FOR THIS ASSIGMENT BUT IT NOT WORKING WELL PLEASE H.pdf
 
Be familiar with1.The main events which affected fashion ~ refer.pdf
Be familiar with1.The main events which affected fashion ~ refer.pdfBe familiar with1.The main events which affected fashion ~ refer.pdf
Be familiar with1.The main events which affected fashion ~ refer.pdf
 
Based on the above Project Charter and the planning meeting, develop.pdf
Based on the above Project Charter and the planning meeting, develop.pdfBased on the above Project Charter and the planning meeting, develop.pdf
Based on the above Project Charter and the planning meeting, develop.pdf
 
Belinda decided to adopt a Pit Bull (Buddy) to save him from being.pdf
Belinda decided to adopt a Pit Bull (Buddy) to save him from being.pdfBelinda decided to adopt a Pit Bull (Buddy) to save him from being.pdf
Belinda decided to adopt a Pit Bull (Buddy) to save him from being.pdf
 
Before you begin this assignment, take a moment to collect the names.pdf
Before you begin this assignment, take a moment to collect the names.pdfBefore you begin this assignment, take a moment to collect the names.pdf
Before you begin this assignment, take a moment to collect the names.pdf
 
Beer menu. Your twelve (12) beeralecider selections will include t.pdf
Beer menu. Your twelve (12) beeralecider selections will include t.pdfBeer menu. Your twelve (12) beeralecider selections will include t.pdf
Beer menu. Your twelve (12) beeralecider selections will include t.pdf
 
Broxton Group, a consumer electronics conglomerate, is reviewing.pdf
Broxton Group, a consumer electronics conglomerate, is reviewing.pdfBroxton Group, a consumer electronics conglomerate, is reviewing.pdf
Broxton Group, a consumer electronics conglomerate, is reviewing.pdf
 
Based on the information provided by the LEFS, I can track Toms pro.pdf
Based on the information provided by the LEFS, I can track Toms pro.pdfBased on the information provided by the LEFS, I can track Toms pro.pdf
Based on the information provided by the LEFS, I can track Toms pro.pdf
 
Bruce Banner was exposed to high levels of radiation (10-5 nm) durin.pdf
Bruce Banner was exposed to high levels of radiation (10-5 nm) durin.pdfBruce Banner was exposed to high levels of radiation (10-5 nm) durin.pdf
Bruce Banner was exposed to high levels of radiation (10-5 nm) durin.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

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 } }