Skip to content
Closed
6 changes: 0 additions & 6 deletions src/main/java/hse/java/IntExpression.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/hse/java/exprtree/Const.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
17 changes: 17 additions & 0 deletions src/main/java/hse/java/exprtree/ExprType.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer, Integer> operation;

ExprType(BiFunction<Integer, Integer, Integer> operation) {
this.operation = operation;
}

public int operation(int x, int y) {
return operation.apply(x, y);
}
}
7 changes: 7 additions & 0 deletions src/main/java/hse/java/exprtree/Expression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package hse.java.exprtree;

public sealed interface Expression permits Operation, Const {
String toString();
// int getPriority();
int eval();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package hse.java;
package hse.java.exprtree;

import java.beans.Expression;
import java.util.Scanner;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> 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());

}
}
24 changes: 24 additions & 0 deletions src/main/java/hse/java/exprtree/Operation.java
Original file line number Diff line number Diff line change
@@ -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());
}


}
92 changes: 92 additions & 0 deletions src/main/java/hse/java/practice/task1/RotationPermutations.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> fromWikiToHw = new HashMap<>() {{
put(0, 2);
put(1, 4);
put(2, 3);
put(3, 5);
put(4, 0);
put(5, 1);
}};
}
86 changes: 76 additions & 10 deletions src/main/java/hse/java/practice/task1/RubiksCube.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,106 @@

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];

/**
* Создать валидный собранный кубик
* грани разместить по ордеру в енуме цветов
* грань 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;
}

@Override
public String toString() {
return Arrays.toString(edges);
}
}
}