How do I use Object-oriented Programming in this code? I\'m basically asking use
ID: 3577910 • Letter: H
Question
How do I use Object-oriented Programming in this code? I'm basically asking user to input the rubik cube, then I'll solve for white side.
package RubikCube;
import java.util.Scanner;
public class RubikCubeSolver {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*Green = 1 front/F
Red = 2 Right/R
White = 3 Top/U
Orange = 4 Left/L
Blue = 5 Back/B
Yellow = 6 Bottom/D */
//get the color from the user input
int[][] cubeColors = new int[6][9];//6 sides 9 pieces
cubeColors = getColor();
System.out.println("white cross");
cubeColors = whiteCross(cubeColors);
}
public static int[][] getColor(){
int[][] userInput = new int[6][9];//6 sides and there are 9 pieces of each side
int counter0 = 0;//initalize the value
int counter1 = 0;
//Using scanner to get the input
Scanner in = new Scanner(System.in);
//Collect the data
//Get the data for the green side
System.out.println("Please place the cube in a way that such "
+ " Top = White "
+ "Left = Orange Front = Green Right = Red Back = Bule "
+ " Bot = Yellow");
System.out.println("Enter the colors for the ORANGE side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW "
+ "The white side on the top "
+ "The yellow side on the bottom "
+ "The blue side to the left "
+ "And the green side to the right");
while(counter0 <=8){//go through this loop 9 times
userInput[0][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the GREEN side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW "
+ "The white side on the top "
+ "The yellow side on the bottom "
+ "The red side to the right "
+ "And the orange side to the left");//this is already setup when the rubik cube is solved
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[1][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the RED side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW "
+ "The white side on the top "
+ "The yellow side on the bottom "
+ "The blue side to the right "
+ "And green yellow side to the left");
//reset the counter
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[2][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the BLUE side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW ");
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[3][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the WHITE side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW ");
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[4][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the YELLOW side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW ");
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[5][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
return userInput;
}
public static int[][] LeftCW(int color[][]){
//We will have to change the face parts first
int[] primaryFace = new int[58];
primaryFace[0]= color[0][0];
primaryFace[1] = color[0][1];
primaryFace[2] = color[0][2];
primaryFace[3] = color[0][3];
primaryFace[4] = color[0][4];
primaryFace[5] = color[0][5];
primaryFace[6] = color[0][6];
primaryFace[7] = color[0][7];
primaryFace[8] = color[0][8];
color[0][6] = primaryFace[0];
color[0][3] = primaryFace[2];
color[0][0] = primaryFace[3];
color[0][7] = primaryFace[3];
color[0][4] = primaryFace[4];
color[0][1] = primaryFace[5];
color[0][8] = primaryFace[6];
color[0][5] = primaryFace[7];
color[0][2] = primaryFace[8];
//Then we will have to change the outer parts
int[] primaryOuter = new int[58];
primaryOuter[40] = color[4][0];
primaryOuter[43] = color[4][3];
primaryOuter[46] = color[4][6];
primaryOuter[10] = color[1][0];
primaryOuter[13] = color[1][3];
primaryOuter[16] = color[1][6];
primaryOuter[50] = color[5][0];
primaryOuter[53] = color[5][3];
primaryOuter[56] = color[5][6];
primaryOuter[38] = color[3][8];
primaryOuter[35] = color[3][5];
primaryOuter[32] = color[3][2];
color[3][8] = primaryOuter[40];
color[3][5] = primaryOuter[43];
color[3][2] = primaryOuter[46];
color[4][0] = primaryOuter[10];
color[4][3] = primaryOuter[13];
color[4][6] = primaryOuter[16];
color[1][0] = primaryOuter[50];
color[1][3] = primaryOuter[53];
color[1][6] = primaryOuter[56];
color[5][0] = primaryOuter[38];
color[5][3] = primaryOuter[35];
color[5][6] = primaryOuter[32];
//Now we have the new values after turning the front face clockwise once, and return it
System.out.println("Left face clockwise L" );
return color;
}
public static int[][] whiteSide(int cubeColors[][]){//top
//solve for the cross of the white side
cubeColors = whiteCross(cubeColors);
//solve for the corners of the white side
cubeColors = whiteCorner(cubeColors);
return cubeColors;
}
public static int[][] whiteCorner(int[][] cubeColors) {
//get the green-orange-white piece into place
cubeColors = whiteFristCorner(cubeColors);
//get the blue-orange-white piece into place
cubeColors = whiteSecondCorner(cubeColors);
//get the red-blue-white piece into place
cubeColors = whiteThirdCorner(cubeColors);
//get the green-red-white piece into place
cubeColors = whiteFourthCorner(cubeColors);
return null;
}
public static int[][] whiteCross(int cubeColors[][]){
//get the green-white piece into place
cubeColors = whiteFirstEdge(cubeColors);
//get the orange-white piece into place
cubeColors = whiteSecondEdge(cubeColors);
//get the blue-white piece into place
cubeColors = whiteThirdEdge(cubeColors);
//get the red-white piece into place
cubeColors = whiteFourthEdge(cubeColors);
return cubeColors;
}
Explanation / Answer
package RubikCube;
import java.util.Scanner;
public class RubikCubeSolver {
//get the color from the user input
//6 sides 9 pieces
private int[][] cubeColors = new int[6][9];
public static void main(String[] args) {
// TODO Auto-generated method stub
/*Green = 1 front/F
Red = 2 Right/R
White = 3 Top/U
Orange = 4 Left/L
Blue = 5 Back/B
Yellow = 6 Bottom/D */
RubikCubeSolver rcs=new RubikCubeSolver();
rcs.getColor();
System.out.println("white cross");
rcs.whiteCross();
}
public void getColor(){
int[][] userInput = new int[6][9];//6 sides and there are 9 pieces of each side
int counter0 = 0;//initalize the value
int counter1 = 0;
//Using scanner to get the input
Scanner in = new Scanner(System.in);
//Collect the data
//Get the data for the green side
System.out.println("Please place the cube in a way that such "
+ " Top = White "
+ "Left = Orange Front = Green Right = Red Back = Bule "
+ " Bot = Yellow");
System.out.println("Enter the colors for the ORANGE side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW "
+ "The white side on the top "
+ "The yellow side on the bottom "
+ "The blue side to the left "
+ "And the green side to the right");
while(counter0 <=8){//go through this loop 9 times
userInput[0][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the GREEN side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW "
+ "The white side on the top "
+ "The yellow side on the bottom "
+ "The red side to the right "
+ "And the orange side to the left");//this is already setup when the rubik cube is solved
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[1][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the RED side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW "
+ "The white side on the top "
+ "The yellow side on the bottom "
+ "The blue side to the right "
+ "And green yellow side to the left");
//reset the counter
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[2][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the BLUE side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW ");
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[3][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the WHITE side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW ");
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[4][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println("Enter the colors for the YELLOW side 1 = GREEN 2 = RED 3 = WHITE 4 = ORANGE 5 = BLUE 6 = YELLOW ");
counter0 = 0;
while(counter0 <=8){//go through this loop 9 times
userInput[5][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
cubeColors=userInput;
}
public int[][] LeftCW(int color[][]){
//We will have to change the face parts first
int[] primaryFace = new int[58];
primaryFace[0]= color[0][0];
primaryFace[1] = color[0][1];
primaryFace[2] = color[0][2];
primaryFace[3] = color[0][3];
primaryFace[4] = color[0][4];
primaryFace[5] = color[0][5];
primaryFace[6] = color[0][6];
primaryFace[7] = color[0][7];
primaryFace[8] = color[0][8];
color[0][6] = primaryFace[0];
color[0][3] = primaryFace[2];
color[0][0] = primaryFace[3];
color[0][7] = primaryFace[3];
color[0][4] = primaryFace[4];
color[0][1] = primaryFace[5];
color[0][8] = primaryFace[6];
color[0][5] = primaryFace[7];
color[0][2] = primaryFace[8];
//Then we will have to change the outer parts
int[] primaryOuter = new int[58];
primaryOuter[40] = color[4][0];
primaryOuter[43] = color[4][3];
primaryOuter[46] = color[4][6];
primaryOuter[10] = color[1][0];
primaryOuter[13] = color[1][3];
primaryOuter[16] = color[1][6];
primaryOuter[50] = color[5][0];
primaryOuter[53] = color[5][3];
primaryOuter[56] = color[5][6];
primaryOuter[38] = color[3][8];
primaryOuter[35] = color[3][5];
primaryOuter[32] = color[3][2];
color[3][8] = primaryOuter[40];
color[3][5] = primaryOuter[43];
color[3][2] = primaryOuter[46];
color[4][0] = primaryOuter[10];
color[4][3] = primaryOuter[13];
color[4][6] = primaryOuter[16];
color[1][0] = primaryOuter[50];
color[1][3] = primaryOuter[53];
color[1][6] = primaryOuter[56];
color[5][0] = primaryOuter[38];
color[5][3] = primaryOuter[35];
color[5][6] = primaryOuter[32];
//Now we have the new values after turning the front face clockwise once, and return it
System.out.println("Left face clockwise L" );
return color;
}
public void whiteSide(){//top
//solve for the cross of the white side
whiteCross();
//solve for the corners of the white side
whiteCorner();
}
public void whiteCorner() {
//get the green-orange-white piece into place
whiteFristCorner();
//get the blue-orange-white piece into place
whiteSecondCorner();
//get the red-blue-white piece into place
whiteThirdCorner();
//get the green-red-white piece into place
whiteFourthCorner();
}
public void whiteCross(){
//get the green-white piece into place
whiteFirstEdge();
//get the orange-white piece into place
whiteSecondEdge();
//get the blue-white piece into place
whiteThirdEdge();
//get the red-white piece into place
whiteFourthEdge();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.