-
Notifications
You must be signed in to change notification settings - Fork 23
Евгений Собко. Кубик Рубика #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EvgenySobko
wants to merge
10
commits into
h31:master
Choose a base branch
from
EvgenySobko:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
67a24d0
В процессе))0)
EvgenySobko 547a6cf
Minor update
EvgenySobko 2967a32
Coming soon
EvgenySobko 8387045
Bang
EvgenySobko 11dc407
Add random cube
EvgenySobko 3eee69c
Minor
EvgenySobko ca299c8
Minor
EvgenySobko 6a25a6c
Add easy tests
EvgenySobko 2ed1efd
¯\_(ツ)_/¯
EvgenySobko fe61fc0
¯\_(ツ)_/¯
EvgenySobko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| #Tue Feb 20 16:38:03 MSK 2018 | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-all.zip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package myRubiksCube; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Random; | ||
|
|
||
| public class Cube { | ||
| public char[][][] cube; | ||
| public int size; | ||
|
|
||
| char[] symbols = {'W', 'R', 'B', 'O', 'Y', 'G'}; | ||
|
|
||
| public Cube(int size) { | ||
| this.size = size; | ||
| this.cube = new char[6][size][size]; | ||
|
|
||
| // Кубик заполняется в собранное состояние | ||
|
|
||
| for (int i = 0; i < 6; ++i) { | ||
| for (int j = 0; j < size; ++j) { | ||
| Arrays.fill(cube[i][j], symbols[i]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public char[][][] Get() { | ||
| return cube; | ||
| } | ||
|
|
||
|
|
||
| public void rotateLeft() { | ||
| char[][] help = cube[1]; | ||
| cube[1] = cube[2]; | ||
| cube[2] = cube[3]; | ||
| cube[3] = cube[5]; | ||
| cube[5] = help; | ||
| rotate(1, 0); | ||
| rotate(0, 4); | ||
| rotate(1, 5); | ||
| rotate(1, 5); | ||
| rotate(1, 3); | ||
| rotate(1, 3); | ||
| } | ||
|
|
||
| public void rotateRight() { | ||
| rotateLeft(); | ||
| rotateLeft(); | ||
| rotateLeft(); | ||
| } | ||
|
|
||
| public void rotateUp() { | ||
| char[][] help; | ||
| char[][] help1; | ||
|
|
||
| help1 = cube[0]; | ||
| help = cube[5]; | ||
| cube[5] = help1; | ||
| help1 = cube[2]; | ||
| cube[0] = help1; | ||
| help1 = cube[4]; | ||
| cube[2] = help1; | ||
| cube[4] = help; | ||
| rotate(3, 1); | ||
| rotate(1, 3); | ||
| } | ||
|
|
||
| public void rotateDown() { | ||
| rotateUp(); | ||
| rotateUp(); | ||
| rotateUp(); | ||
| } | ||
|
|
||
| public void spin(int direction) { | ||
| rotate(direction, 2); | ||
| char c; | ||
| for (int i = 0; i < size; ++i) { | ||
| if (direction == 1) { | ||
| c = cube[1][size - i - 1][size - 1]; | ||
| cube[1][size - i - 1][size - 1] = cube[4][0][size - i - 1]; | ||
| cube[4][0][size - i - 1] = cube[3][i][0]; | ||
| cube[3][i][0] = cube[0][size - 1][i]; | ||
| cube[0][size - 1][i] = c; | ||
| } else { | ||
| c = cube[1][size - i - 1][size - 1]; | ||
| cube[1][size - i - 1][size - 1] = cube[0][size - 1][i]; | ||
| cube[0][size - 1][i] = cube[3][i][0]; | ||
| cube[3][i][0] = cube[4][0][size - i - 1]; | ||
| cube[4][0][size - i - 1] = c; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void rotate(int position, int numCube) { | ||
| char[][] help = new char[size][size]; | ||
|
|
||
| if (position == 1) { | ||
| for (int i = 0; i < size; ++i) { | ||
| for (int j = 0; j < size; ++j) { | ||
| help[i][j] = cube[numCube][size - 1 - j][i]; | ||
| } | ||
| } | ||
| } else { | ||
| for (int i = 0; i < size; ++i) { | ||
| for (int j = 0; j < size; ++j) { | ||
| help[i][j] = cube[numCube][j][size - 1 - i]; | ||
| } | ||
| } | ||
| } | ||
| cube[numCube] = help; | ||
| } | ||
|
|
||
| public void shuffle() { | ||
| for (int i = 0; i < 20; ++i) { | ||
| Random random = new Random(); | ||
| int number = random.nextInt(13); | ||
| switch (number) { | ||
| case 0: spin(1); break; | ||
| case 1: spin(2); break; | ||
| case 2: rotateLeft(); spin(1); rotateRight(); break; | ||
| case 3: rotateLeft(); spin(2); rotateRight(); break; | ||
| case 4: rotateRight(); spin(1); rotateLeft(); break; | ||
| case 5: rotateRight(); spin(2); rotateLeft(); break; | ||
| case 6: rotateDown(); spin(1); rotateUp(); break; | ||
| case 7: rotateDown(); spin(2); rotateUp(); break; | ||
| case 8: rotateLeft(); | ||
| case 9: rotateRight(); | ||
| case 10: rotateUp(); | ||
| case 11: rotateDown(); | ||
| case 12: rotateUp(); spin(1); rotateDown(); break; | ||
| case 13: rotateUp(); spin(2); rotateDown(); break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package myRubiksCube; | ||
|
|
||
| public class CubePrinter { | ||
|
|
||
| private char[][] arr; | ||
| private int n; | ||
|
|
||
| CubePrinter(int n) { | ||
| this.arr = new char[n * 8 + 1][n * 18 + 1]; | ||
| this.n = n; | ||
|
|
||
| for (int i = 0; i < n * 8 + 1; ++i) { | ||
| for (int j = 0; j < n * 18 + 1; ++j) { | ||
| arr[i][j] = ' '; | ||
| } | ||
| } | ||
|
|
||
| for (int i = 0; i < n * 8 + 1; i = i + 2) { | ||
| for (int j = 1; j < n * 18 + 1; j = j + 6) { | ||
| for (int k = j; k < j + 5; k++) { | ||
| arr[i][k] = '-'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (int i = 1; i < n * 8 + 1; i = i + 2) { | ||
| for (int j = 0; j < n * 18 + 1; j = j + 6) { | ||
| arr[i][j] = '|'; | ||
| arr[i - 1][j] = '+'; | ||
| } | ||
| } | ||
| for (int j = 0; j < n * 18 + 1; j = j + 6) { | ||
| arr[n * 8][j] = '+'; | ||
| } | ||
| } | ||
|
|
||
| public void Print(Cube cube) { | ||
| char[][][] ptr = cube.Get(); | ||
| for (int i = 0; i < n; ++i) { | ||
| for (int j = 0; j < n; ++j) { | ||
| arr[i * 2 + 1][n * 6 + 3 + j * 6] = ptr[0][i][j]; | ||
| arr[i * 2 + n * 2 + 1][3 + j * 6] = ptr[1][i][j]; | ||
| arr[i * 2 + n * 2 + 1][n * 6 + 3 + j * 6] = ptr[2][i][j]; | ||
| arr[i * 2 + n * 2 + 1][n * 12 + 3 + j * 6] = ptr[3][i][j]; | ||
| arr[i * 2 + n * 4 + 1][n * 6 + 3 + j * 6] = ptr[4][i][j]; | ||
| arr[i * 2 + n * 6 + 1][n * 6 + 3 + j * 6] = ptr[5][i][j]; | ||
| } | ||
| } | ||
|
|
||
| for (int i = 0; i < n * 8 + 1; ++i) { | ||
| for (int j = 0; j < n * 18 + 1; ++j) { | ||
| System.out.print(arr[i][j]); | ||
| } | ||
| System.out.print("\n"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package myRubiksCube; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Scanner reader = new Scanner(System.in); | ||
| System.out.print("Enter cube size: "); | ||
| int n = reader.nextInt(); | ||
|
|
||
| Cube cube = new Cube(n); | ||
|
|
||
| boolean B = true; | ||
|
|
||
| while (B) { | ||
| System.out.println("What'll we do?"); | ||
| Scanner look = new Scanner(System.in); | ||
| String l = look.nextLine(); | ||
| switch (l) { | ||
| case "Print": { | ||
| CubePrinter printC1 = new CubePrinter(n); | ||
| printC1.Print(cube); | ||
| break; | ||
| } | ||
| case "Stop": { | ||
| System.out.println("Досвидули))0)"); | ||
| B = false; | ||
| break; | ||
| } | ||
| case "Left": { | ||
| cube.rotateLeft(); | ||
| break; | ||
| } | ||
| case "Right": { | ||
| cube.rotateRight(); | ||
| break; | ||
| } | ||
| case "Up": { | ||
| cube.rotateUp(); | ||
| break; | ||
| } | ||
| case "Down": { | ||
| cube.rotateDown(); | ||
| break; | ||
| } | ||
| case "F": { | ||
| cube.spin(1); | ||
| break; | ||
| } | ||
| case "F'": { | ||
| cube.spin(2); | ||
| break; | ||
| } | ||
| case "R": { | ||
| cube.rotateLeft(); | ||
| cube.spin(1); | ||
| cube.rotateRight(); | ||
| break; | ||
| } | ||
| case "R'": { | ||
| cube.rotateLeft(); | ||
| cube.spin(2); | ||
| cube.rotateRight(); | ||
| break; | ||
| } | ||
| case "L": { | ||
| cube.rotateRight(); | ||
| cube.spin(1); | ||
| cube.rotateLeft(); | ||
| break; | ||
| } | ||
| case "L'": { | ||
| cube.rotateRight(); | ||
| cube.spin(2); | ||
| cube.rotateLeft(); | ||
| break; | ||
| } | ||
| case "U": { | ||
| cube.rotateDown(); | ||
| cube.spin(1); | ||
| cube.rotateUp(); | ||
| break; | ||
| } | ||
| case "U'": { | ||
| cube.rotateDown(); | ||
| cube.spin(2); | ||
| cube.rotateUp(); | ||
| break; | ||
| } | ||
| case "D": { | ||
| cube.rotateUp(); | ||
| cube.spin(1); | ||
| cube.rotateDown(); | ||
| break; | ||
| } | ||
| case "D'": { | ||
| cube.rotateUp(); | ||
| cube.spin(2); | ||
| cube.rotateDown(); | ||
| break; | ||
| } | ||
| case "Shuffle": { | ||
| cube.shuffle(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Давайте подумаем, как можно разгрузить этот класс от "магии". Я вижу два варианта.
Для каждого блока мы делаем сетку. Вынесем это тоже в отдельный метод. Можно опять же создать одну ячейку, а потом "размножить" её путем склеивания строк. Заполнение ячейки - тоже отдельный метод, пусть и совсем маленький. Путем такого разбиения можно постепенного избавиться от магических констант.
/. Вместо цветов на гранях - буквы. Цифр, заглавных и строчных букв хватит, чтобы отметить все квадраты разными символами. Потом, используяreplace, подставляем вместо символов нужные цвета.Если бы было побольше времени, можно было бы аналогичным образом даже сформировать картинку.
Есть ещё такой вариант. Вы не убираете "магию" из кода, не переделываете
charна enum, исправляете оставшиеся замечания, но оценка будет 4.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не видел Вашего комментария, я подумаю что можно с этим сделать.