diff --git a/src/main/java/hse/java/IntExpression.java b/src/main/java/hse/java/IntExpression.java deleted file mode 100644 index e5f4ad89..00000000 --- a/src/main/java/hse/java/IntExpression.java +++ /dev/null @@ -1,6 +0,0 @@ -package hse.java; - -public interface IntExpression { - String toString(); - int eval(); -} diff --git a/src/main/java/hse/java/exprtree/Const.java b/src/main/java/hse/java/exprtree/Const.java new file mode 100644 index 00000000..27a27579 --- /dev/null +++ b/src/main/java/hse/java/exprtree/Const.java @@ -0,0 +1,25 @@ +package hse.java.exprtree; + + +public non-sealed class Const implements Expression { + private final int value; + + public Const(int value) { + this.value = value; + } + +// @Override +// public int getPriority() { +// return 10; +// } + + @Override + public int eval() { + return value; + } + + @Override + public String toString(){ + return Integer.toString(value); + } +} diff --git a/src/main/java/hse/java/exprtree/ExprType.java b/src/main/java/hse/java/exprtree/ExprType.java new file mode 100644 index 00000000..914ed483 --- /dev/null +++ b/src/main/java/hse/java/exprtree/ExprType.java @@ -0,0 +1,17 @@ +package hse.java.exprtree; + +import java.util.function.BiFunction; + +public enum ExprType { + SUM(Integer::sum), + PRODUCT((Integer x, Integer y) -> x * y); + private final BiFunction operation; + + ExprType(BiFunction operation) { + this.operation = operation; + } + + public int operation(int x, int y) { + return operation.apply(x, y); + } +} diff --git a/src/main/java/hse/java/exprtree/Expression.java b/src/main/java/hse/java/exprtree/Expression.java new file mode 100644 index 00000000..0fa3e5a2 --- /dev/null +++ b/src/main/java/hse/java/exprtree/Expression.java @@ -0,0 +1,7 @@ +package hse.java.exprtree; + +public sealed interface Expression permits Operation, Const { + String toString(); +// int getPriority(); + int eval(); +} diff --git a/src/main/java/hse/java/Main.java b/src/main/java/hse/java/exprtree/Main.java similarity index 50% rename from src/main/java/hse/java/Main.java rename to src/main/java/hse/java/exprtree/Main.java index 9b81c1ef..ae042cd0 100644 --- a/src/main/java/hse/java/Main.java +++ b/src/main/java/hse/java/exprtree/Main.java @@ -1,9 +1,14 @@ -package hse.java; +package hse.java.exprtree; + +import java.beans.Expression; +import java.util.Scanner; //TIP To Run code, press or // click the icon in the gutter. public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + Operation z = new Operation(new Const(5), new Const(7),ExprType.PRODUCT); + System.out.println(z.eval()); + } } \ No newline at end of file diff --git a/src/main/java/hse/java/exprtree/Operation.java b/src/main/java/hse/java/exprtree/Operation.java new file mode 100644 index 00000000..2aa4e70c --- /dev/null +++ b/src/main/java/hse/java/exprtree/Operation.java @@ -0,0 +1,24 @@ +package hse.java.exprtree; + +public non-sealed class Operation implements Expression { + private final Expression l, r; + private final ExprType type; + + public Operation(Expression l, Expression r, ExprType type) { + this.l = l; + this.r = r; + this.type = type; + } + +// @Override +// public int getPriority() { +// return 0; +// } + + @Override + public int eval() { + return type.operation(l.eval(),r.eval()); + } + + +} diff --git a/src/main/java/hse/java/practice/task1/RotationPermutations.java b/src/main/java/hse/java/practice/task1/RotationPermutations.java new file mode 100644 index 00000000..01bdb9fb --- /dev/null +++ b/src/main/java/hse/java/practice/task1/RotationPermutations.java @@ -0,0 +1,92 @@ +package hse.java.practice.task1; + +import java.util.HashMap; +import java.util.Map; + +public class RotationPermutations { + static final int[][] U_CW = { + {33, 35, 40, 38}, + {34, 37, 39, 36}, + {25, 17, 9, 1}, + {26, 18, 10, 2}, + {27, 19, 11, 3} + }; + + static final int[][] U_CCW = reverseCycles(U_CW); + + static final int[][] D_CW = { + {41, 43, 48, 46}, + {42, 45, 47, 44}, + {6, 14, 22, 30}, + {7, 15, 23, 31}, + {8, 16, 24, 32} + }; + + static final int[][] D_CCW = reverseCycles(D_CW); + + static final int[][] L_CW = { + {1, 3, 8, 6}, + {2, 5, 7, 4}, + {33, 9, 41, 32}, + {36, 12, 44, 29}, + {38, 14, 46, 27} + }; + + static final int[][] L_CCW = reverseCycles(L_CW); + + static final int[][] R_CW = { + {17, 19, 24, 22}, + {18, 21, 23, 20}, + {48, 16, 40, 25}, + {45, 13, 37, 28}, + {43, 11, 35, 30} + }; + + static final int[][] R_CCW = reverseCycles(R_CW); + + static final int[][] F_CW = { + {9, 11, 16, 14}, + {10, 13, 15, 12}, + {38, 17, 43, 8}, + {39, 20, 42, 5}, + {40, 22, 41, 3} + }; + + static final int[][] F_CCW = reverseCycles(F_CW); + + static final int[][] B_CW = { + {25, 27, 32, 30}, + {26, 29, 31, 28}, + {19, 33, 6, 48}, + {21, 34, 4, 47}, + {24, 35, 1, 46} + }; + + static final int[][] B_CCW = reverseCycles(B_CW); + + private static int[][] reverseCycles(int[][] cycles) { + int[][] reversed = new int[cycles.length][]; + for (int i = 0; i < cycles.length; i++) { + int[] cycle = cycles[i]; + int[] revCycle = new int[cycle.length]; + revCycle[0] = cycle[0]; + for (int j = 1; j < cycle.length; j++) { + revCycle[j] = cycle[cycle.length - j]; + } + reversed[i] = revCycle; + } + return reversed; + } + + /** + * Эта штука нужна, так как в кубике рубика на вики грани расположены по-другому и я не сильно хочу разбираться в том, где какие перестановки + */ + static final Map fromWikiToHw = new HashMap<>() {{ + put(0, 2); + put(1, 4); + put(2, 3); + put(3, 5); + put(4, 0); + put(5, 1); + }}; +} diff --git a/src/main/java/hse/java/practice/task1/RubiksCube.java b/src/main/java/hse/java/practice/task1/RubiksCube.java index 2091b657..e5fa3257 100644 --- a/src/main/java/hse/java/practice/task1/RubiksCube.java +++ b/src/main/java/hse/java/practice/task1/RubiksCube.java @@ -2,14 +2,9 @@ import java.util.Arrays; -/** - * Необходимо реализовать интерфейс Cube - * При повороте передней грани, меняются верх низ право и лево - */ -public class RubiksCube { - +public class RubiksCube implements Cube { + private final int[] state; private static final int EDGES_COUNT = 6; - private final Edge[] edges = new Edge[EDGES_COUNT]; /** @@ -17,20 +12,91 @@ public class RubiksCube { * грани разместить по ордеру в енуме цветов * грань 0 -> цвет 0 * грань 1 -> цвет 1 - * ... + * и тд 3 */ public RubiksCube() { + state = new int[49]; + for (int i = 1; i <= 48; i++) { + state[i] = i; + } CubeColor[] colors = CubeColor.values(); for (int i = 0; i < 6; i++) { edges[i] = new Edge(colors[i]); } } + private void applyCycles(int[][] cycles) { + for (int[] cycle : cycles) { + applyCycle(cycle); + } + } + + private void applyCycle(int[] cycle) { + int first = state[cycle[cycle.length - 1]]; + for (int i = cycle.length - 1; i > 0; i--) { + state[cycle[i]] = state[cycle[i - 1]]; + } + state[cycle[0]] = first; + } + + @Override + public void up(RotateDirection direction) { + applyCycles(direction == RotateDirection.CLOCKWISE ? + RotationPermutations.U_CW : RotationPermutations.U_CCW); + } + + @Override + public void down(RotateDirection direction) { + applyCycles(direction == RotateDirection.CLOCKWISE ? + RotationPermutations.D_CW : RotationPermutations.D_CCW); + } + + @Override + public void left(RotateDirection direction) { + applyCycles(direction == RotateDirection.CLOCKWISE ? + RotationPermutations.L_CW : RotationPermutations.L_CCW); + } + + @Override + public void right(RotateDirection direction) { + applyCycles(direction == RotateDirection.CLOCKWISE ? + RotationPermutations.R_CW : RotationPermutations.R_CCW); + } + + @Override public void front(RotateDirection direction) { + applyCycles(direction == RotateDirection.CLOCKWISE ? + RotationPermutations.F_CW : RotationPermutations.F_CCW); + } + @Override + public void back(RotateDirection direction) { + applyCycles(direction == RotateDirection.CLOCKWISE ? + RotationPermutations.B_CW : RotationPermutations.B_CCW); } - + + public Edge[] getEdges() { + CubeColor[] colors = CubeColor.values(); + + for (int i = 0; i < EDGES_COUNT; i++) { + CubeColor[][] parts = new CubeColor[3][3]; + for (int x = 0; x < 3; x++) { + for (int y = 0; y < 3; y++) { + int pos = x + y * 3 + 1; + if (pos == 5) { + parts[y][x] = colors[RotationPermutations.fromWikiToHw.get(i)]; + continue; + } + if (pos > 5) { + pos--; + } + int colorId = RotationPermutations.fromWikiToHw.get((state[i*8+pos] - 1) / 8); + parts[y][x] = colors[colorId]; + } + } + edges[RotationPermutations.fromWikiToHw.get(i)].setParts(parts); + } return edges; } @@ -38,4 +104,4 @@ public Edge[] getEdges() { public String toString() { return Arrays.toString(edges); } -} +} \ No newline at end of file