Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/main/java/hse/java/IntExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,100 @@ public interface IntExpression {
String toString();
int eval();
}


class Const implements IntExpression {
private final int value;
public Const(int _value) {
this.value = _value;
}

@Override
public String toString() {
return String.valueOf(value);
}

@Override
public int eval() {
return value;
}
}

abstract class BinOp implements IntExpression {
final IntExpression left, right;
public BinOp(IntExpression _left, IntExpression _right) {
this.left = _left;
this.right = _right;
}

@Override
public String toString() {
return "(" + left.toString() + getSymb() + right.toString() + ")";
}

abstract String getSymb();
}

class Sum extends BinOp {
public Sum(IntExpression _left, IntExpression _right) {
super(_left, _right);
}

@Override
public int eval() {
return left.eval() + right.eval();
}

@Override
String getSymb() {
return "+";
}
}

class Sub extends BinOp {
public Sub(IntExpression _left, IntExpression _right) {
super(_left, _right);
}

@Override
public int eval() {
return left.eval() - right.eval();
}

@Override
String getSymb() {
return "-";
}
}

class Mul extends BinOp {
public Mul(IntExpression _left, IntExpression _right) {
super(_left, _right);
}

@Override
public int eval() {
return left.eval() * right.eval();
}

@Override
String getSymb() {
return "*";
}
}

class Div extends BinOp {
public Div(IntExpression _left, IntExpression _right) {
super(_left, _right);
}

@Override
public int eval() {
return left.eval() / right.eval();
}

@Override
String getSymb() {
return "/";
}
}
11 changes: 7 additions & 4 deletions src/main/java/hse/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package hse.java;

//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!");
IntExpression expr = new Mul(
new Sum(new Const(1), new Const(2)),
new Sub(new Const(5), new Const(3))
);
System.out.println(expr.toString());
System.out.println(expr.eval());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ public int getBalance() {
return 0;
}

}
}
2 changes: 1 addition & 1 deletion src/main/java/hse/java/practice/task1/RotateDirection.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public enum RotateDirection {
// Против часовой стрелки
COUNTERCLOCKWISE,
// По часовой стрелке
CLOCKWISE
CLOCKWISE;
}
125 changes: 118 additions & 7 deletions src/main/java/hse/java/practice/task1/RubiksCube.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
* При повороте передней грани, меняются верх низ право и лево
*/
public class RubiksCube implements Cube {

private static final int EDGES_COUNT = 6;
private static final int CUBE_DIMENSION = 3;
private static final int CHANGEABLE = 48;
private static final int[] FACE_ORDER = {2, 4, 3, 5, 0, 1};

private final Edge[] edges = new Edge[EDGES_COUNT];
private final int[] perm = new int[CHANGEABLE + 1];

/**
* Создать валидный собранный кубик
Expand All @@ -21,45 +24,153 @@ public class RubiksCube implements Cube {
*/
public RubiksCube() {
CubeColor[] colors = CubeColor.values();
for (int i = 0; i < 6; i++) {
for (int i = 0; i < EDGES_COUNT; i++) {
edges[i] = new Edge(colors[i]);
}
for (int i = 1; i <= CHANGEABLE; i++) {
perm[i] = i;
}
}

@Override
public void up(RotateDirection direction) {

turn(direction, U);
}

@Override
public void down(RotateDirection direction) {

turn(direction, D);
}

@Override
public void left(RotateDirection direction) {

turn(direction, L);
}

@Override
public void right(RotateDirection direction) {

turn(direction, R);
}

@Override
public void front(RotateDirection direction) {

turn(direction, F);
}

@Override
public void back(RotateDirection direction) {
turn(direction, B);
}

private void turn(RotateDirection direction, int[][] matrix_of_perm) {
for (int[] row : matrix_of_perm) {
if (direction == RotateDirection.CLOCKWISE) {
direct_rotate(row);
}
else {
reverse_rotate(row);
}
}
}

private void direct_rotate(int[] row) {
int last = perm[row[row.length - 1]];
for (int i = 0; i < row.length; i++) {
int current = perm[row[i]];
perm[row[i]] = last;
last = current;
}
}

private void reverse_rotate(int[] row) {
int first = perm[row[0]];
for (int i = row.length - 1; i >= 0; i--) {
int current = perm[row[i]];
perm[row[i]] = first;
first = current;
}
}

private static final int[][] U = {
{33, 35, 40, 38},
{34, 37, 39, 36},
{25, 17, 9, 1},
{26, 18, 10, 2},
{27, 19, 11, 3}
};

private static final int[][] D = {
{41, 43, 48, 46},
{42, 45, 47, 44},
{6, 14, 22, 30},
{7, 15, 23, 31},
{8, 16, 24, 32}
};

private static final int[][] L = {
{1, 3, 8, 6},
{2, 5, 7, 4},
{33, 9, 41, 32},
{36, 12, 44, 29},
{38, 14, 46, 27}
};

private static final int[][] R = {
{17, 19, 24, 22},
{18, 21, 23, 20},
{48, 16, 40, 25},
{45, 13, 37, 28},
{43, 11, 35, 30}
};

private static final int[][] F = {
{9, 11, 16, 14},
{10, 13, 15, 12},
{38, 17, 43, 8},
{39, 20, 42, 5},
{40, 22, 41, 3}
};

private static final int[][] B = {
{25, 27, 32, 30},
{26, 29, 31, 28},
{19, 33, 6, 48},
{21, 34, 4, 47},
{24, 35, 1, 46}
};


public Edge[] getEdges() {
for (int i = 0; i < EDGES_COUNT; i++) {
edges[FACE_ORDER[i]].setParts(build(i));
}
return edges;
}

private CubeColor[][] build(int i) {
CubeColor[] colors = CubeColor.values();
CubeColor[][] result = new CubeColor[CUBE_DIMENSION][CUBE_DIMENSION];
CubeColor center = colors[FACE_ORDER[i]];
for (int j = 0; j < CUBE_DIMENSION; j++) {
for (int k = 0; k < CUBE_DIMENSION; k++) {
if (j == 1 && k == 1) {
result[j][k] = center;
}
else {
int current_idx = get_idx(k, j);
int destination = (perm[i * 8 + current_idx] - 1) / 8;
result[j][k] = colors[FACE_ORDER[destination]];
}
}
}
return result;
}

private int get_idx(int k, int j) {
int current = k + j * CUBE_DIMENSION + 1;
return current > 5 ? current - 1 : current;
}

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