patternjavaMinor
Getting input from user about 2×2×2 Rubik's Cube state
Viewed 0 times
usergettinginputrubikaboutcubestatefrom
Problem
I was wondering if there was a way I could shorten this code that gets input from the user about the state of their 2×2×2 Rubik's Cube to solve it. I want it to still be informative so any user knows what to do, the thing is the method is pretty long.
```
static public int[][] scanCube(){
int counter0 = 0;
Scanner in = new Scanner(System.in);
int[][] input = new int [6][4];
System.out.println("Welcome to the 2x2x2 cube solver :)\n \n"
+ "Please enter your cube");
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the TOP side\n");
while(counter0 < 4){
input[0][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the LEFT side");
//reset the counter
counter0 = 0;
while(counter0 < 4){
input[1][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the FRONT side");
//reset the counter
counter0 = 0;
while(counter0 < 4){
input[2][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the RIGHT side");
//reset the counter
counter0 = 0;
while(counter0 < 4){
input[3][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the BACK SIDE");
//reset the counter
counter0 = 0;
while(counter0 < 4){
input[4][counte
```
static public int[][] scanCube(){
int counter0 = 0;
Scanner in = new Scanner(System.in);
int[][] input = new int [6][4];
System.out.println("Welcome to the 2x2x2 cube solver :)\n \n"
+ "Please enter your cube");
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the TOP side\n");
while(counter0 < 4){
input[0][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the LEFT side");
//reset the counter
counter0 = 0;
while(counter0 < 4){
input[1][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the FRONT side");
//reset the counter
counter0 = 0;
while(counter0 < 4){
input[2][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the RIGHT side");
//reset the counter
counter0 = 0;
while(counter0 < 4){
input[3][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
System.out.println("Enter the colors for the BACK SIDE");
//reset the counter
counter0 = 0;
while(counter0 < 4){
input[4][counte
Solution
You can store the sides on an array and loop over that, something roughly like this:
I would personally add a
If you want to have an alternative approach, with this the user can enter something like
Keep in mind that this is just the general idea, not complete / nice code.
You should also check that the user didn't input something presumably wrong like a cube with 10 white squares, because the amount of squares should be the same for every color.
import java.util.Scanner;
public class scanCube {
public static void main(String[] args) {
String[] sides = { "TOP", "LEFT", "FRONT", "RIGHT", "BACK", "BOTTOM" };
int side_index = 0;
Scanner in = new Scanner(System.in);
int[][] input = new int [6][4];
System.out.println("Welcome to the 2x2x2 cube solver :)\n \n"
+ "Please enter your cube");
for (String side: sides) {
System.out.println("Enter the colors for the " + side + " side");
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
int counter = 0;
while(counter < 4){
input[side_index][counter] = Integer.parseInt(in.nextLine());
counter++;
}
side_index++;
}
}
}I would personally add a
String.split so that the user can input the numbers for a single side on a single line, without having to hit enter every time.If you want to have an alternative approach, with this the user can enter something like
0312 and it will be split and assigned to the array.Keep in mind that this is just the general idea, not complete / nice code.
import java.util.Scanner;
public class scanCube {
public static void main(String[] args) {
String[] sides = { "TOP", "LEFT", "FRONT", "RIGHT", "BACK", "BOTTOM" };
int side_index = 0;
int max_colors_per_side = 4;
Scanner in = new Scanner(System.in);
int[][] input = new int [sides.length][max_colors_per_side];
System.out.println("Welcome to the 2x2x2 cube solver :)\n \n"
+ "Please enter your cube");
while (side_index < sides.length) {
System.out.println("Enter the colors for the " + sides[side_index] + " side");
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
String[] side_colors = in.nextLine().toString().split("");
if (side_colors.length != max_colors_per_side+1) { // Account for CR/LF
System.out.println("Invalid input!");
} else {
for (int i = 1; i <= max_colors_per_side; i++) {
// You should check here also that the input is valid (0..3)
input[side_index][i-1] = Integer.parseInt(side_colors[i]);
}
side_index++;
}
}
}
}You should also check that the user didn't input something presumably wrong like a cube with 10 white squares, because the amount of squares should be the same for every color.
Code Snippets
import java.util.Scanner;
public class scanCube {
public static void main(String[] args) {
String[] sides = { "TOP", "LEFT", "FRONT", "RIGHT", "BACK", "BOTTOM" };
int side_index = 0;
Scanner in = new Scanner(System.in);
int[][] input = new int [6][4];
System.out.println("Welcome to the 2x2x2 cube solver :)\n \n"
+ "Please enter your cube");
for (String side: sides) {
System.out.println("Enter the colors for the " + side + " side");
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
int counter = 0;
while(counter < 4){
input[side_index][counter] = Integer.parseInt(in.nextLine());
counter++;
}
side_index++;
}
}
}import java.util.Scanner;
public class scanCube {
public static void main(String[] args) {
String[] sides = { "TOP", "LEFT", "FRONT", "RIGHT", "BACK", "BOTTOM" };
int side_index = 0;
int max_colors_per_side = 4;
Scanner in = new Scanner(System.in);
int[][] input = new int [sides.length][max_colors_per_side];
System.out.println("Welcome to the 2x2x2 cube solver :)\n \n"
+ "Please enter your cube");
while (side_index < sides.length) {
System.out.println("Enter the colors for the " + sides[side_index] + " side");
System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
String[] side_colors = in.nextLine().toString().split("");
if (side_colors.length != max_colors_per_side+1) { // Account for CR/LF
System.out.println("Invalid input!");
} else {
for (int i = 1; i <= max_colors_per_side; i++) {
// You should check here also that the input is valid (0..3)
input[side_index][i-1] = Integer.parseInt(side_colors[i]);
}
side_index++;
}
}
}
}Context
StackExchange Code Review Q#139530, answer score: 2
Revisions (0)
No revisions yet.