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

The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages VictorSzoltysek
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleChristopher Curtin
 
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.pdfarjuncorner565
 
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.pdfxlynettalampleyxc
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
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 .pdfellanorfelicityri239
 
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.pdffeelinggifts
 
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.pdfsales88
 

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

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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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. .pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 
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.pdfaminaENT
 

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

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

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