diff --git a/src/main/java/hse/java/lectures/lecture1/Game.java b/src/main/java/hse/java/lectures/lecture1/Game.java index bf85f916..65950bca 100644 --- a/src/main/java/hse/java/lectures/lecture1/Game.java +++ b/src/main/java/hse/java/lectures/lecture1/Game.java @@ -2,27 +2,160 @@ import javax.swing.*; import java.awt.*; +import java.util.Random; +import java.util.concurrent.atomic.AtomicBoolean; -public class Game { - public static void main(String[] args) { + +public class Game +{ + + private static boolean checkWin(char[][] board, char sym) + { + for (int row = 0; row < 3; ++row) + { + if (board[row][0] == sym && board[row][1] == sym && board[row][2] == sym) + { + return true; + } + } + + for (int col = 0; col < 3; ++col) + { + if (board[0][col] == sym && board[1][col] == sym && board[2][col] == sym) + { + return true; + } + } + + if (board[0][0] == sym && board[1][1] == sym && board[2][2] == sym) return true; + if (board[0][2] == sym && board[1][1] == sym && board[2][0] == sym) return true; + + return false; + + } + + private static boolean isBoardFull(char[][] board) + { + for (int row = 0; row < 3; row++) + { + for (int col = 0; col < 3; col++) + { + if (board[row][col] == 0) return false; + } + } + return true; + } + + private static void botMove(JButton[][] buttons, char[][] board, Random random) + { + while (true) + { + int i = random.nextInt(3); + int j = random.nextInt(3); + if (board[i][j] == 0) + { + buttons[i][j].setText("O"); + board[i][j] = 'O'; + break; + } + } + } + + public static void main(String[] args) + { JFrame frame = new JFrame("XO"); + JLabel statusLabel = new JLabel("Your turn:", SwingConstants.CENTER); + frame.add(statusLabel, BorderLayout.NORTH); frame.setSize(300, 300); frame.setLocation(200, 200); + JButton[][] buttons = new JButton[3][3]; - JPanel panel = new JPanel(new GridLayout(3,3)); - for (int i = 0; i < 9; i++) { - JButton button = new JButton(); - button.addActionListener(a -> { - button.setText("X"); - }); - panel.add(button); + char[][] board = new char[3][3]; + + Random random = new Random(); + + + JPanel panel = new JPanel(new GridLayout(3, 3)); + + + AtomicBoolean gameActive = new AtomicBoolean(true); + AtomicBoolean playerTurn = new AtomicBoolean(true); + + + for (int i = 0; i < 3; ++i) + { + for (int j = 0; j < 3; ++j) + { + buttons[i][j] = new JButton(); + + int row = i; + int col = j; + + buttons[i][j].addActionListener(a -> + { + if (!gameActive.get() || !playerTurn.get() || !buttons[row][col].getText() + .isEmpty()) + { + return; + } + + buttons[row][col].setText("X"); + board[row][col] = 'X'; + + if (checkWin(board, 'X')) + { + gameActive.set(false); + statusLabel.setText("You win!"); + return; + + } else if (isBoardFull(board)) + { + gameActive.set(false); + statusLabel.setText("Draw"); + return; + } + + playerTurn.set(false); + + statusLabel.setText("Bot turn:"); + + Timer timer = new Timer(500, e -> + { + botMove(buttons, board, random); + + playerTurn.set(true); + + if (checkWin(board, 'O')) + { + gameActive.set(false); + statusLabel.setText("Bot win!"); + } else if (isBoardFull(board)) + { + gameActive.set(false); + statusLabel.setText("Draw"); + } else + { + statusLabel.setText("Your turn"); + } + }); + timer.setRepeats(false); + timer.start(); + }); + + panel.add(buttons[i][j]); + + + } } + frame.add(panel); - JDialog dialog; + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } } + diff --git a/src/main/java/hse/java/lectures/lecture2/task/Const.java b/src/main/java/hse/java/lectures/lecture2/task/Const.java new file mode 100644 index 00000000..547bb160 --- /dev/null +++ b/src/main/java/hse/java/lectures/lecture2/task/Const.java @@ -0,0 +1,28 @@ +package hse.java.lectures.lecture2.task; + + +public class Const implements IntExpression +{ + + private final int value; + + public Const(int value) + { + this.value = value; + } + + + @Override + public int eval() + { + return value; + } + + @Override + public String toString() + { + return Integer.toString(value); + } + + +} diff --git a/src/main/java/hse/java/lectures/lecture2/task/IntExpression.java b/src/main/java/hse/java/lectures/lecture2/task/IntExpression.java new file mode 100644 index 00000000..c826d604 --- /dev/null +++ b/src/main/java/hse/java/lectures/lecture2/task/IntExpression.java @@ -0,0 +1,6 @@ +package hse.java.lectures.lecture2.task; + +public interface IntExpression { + String toString(); + int eval(); +} diff --git a/src/main/java/hse/java/lectures/lecture2/task/Main.java b/src/main/java/hse/java/lectures/lecture2/task/Main.java new file mode 100644 index 00000000..a7a5037f --- /dev/null +++ b/src/main/java/hse/java/lectures/lecture2/task/Main.java @@ -0,0 +1,19 @@ +package hse.java.lectures.lecture2.task; + + + + +public class Main { + + + + public static void main(String[] args) + { + IntExpression expr1 = new Sum(new Sum(new Const(2), new Const(3)), new Const(100)); + + + System.out.println(expr1.toString() + "=" + expr1.eval()); + + + } +} \ No newline at end of file diff --git a/src/main/java/hse/java/lectures/lecture2/task/Operation.java b/src/main/java/hse/java/lectures/lecture2/task/Operation.java new file mode 100644 index 00000000..749a1ece --- /dev/null +++ b/src/main/java/hse/java/lectures/lecture2/task/Operation.java @@ -0,0 +1,24 @@ +package hse.java.lectures.lecture2.task; + + +public abstract class Operation implements IntExpression +{ + + protected final IntExpression left; + protected final IntExpression right; + private final String operationSymbol; + + protected Operation(IntExpression left, IntExpression right, String operationSymbol) + { + this.left = left; + this.right = right; + this.operationSymbol = operationSymbol; + } + + + @Override + public String toString() + { + return "(" + left.toString() + operationSymbol + right.toString() + ")"; + } +} diff --git a/src/main/java/hse/java/lectures/lecture2/task/Sub.java b/src/main/java/hse/java/lectures/lecture2/task/Sub.java new file mode 100644 index 00000000..e3e3eba1 --- /dev/null +++ b/src/main/java/hse/java/lectures/lecture2/task/Sub.java @@ -0,0 +1,15 @@ +package hse.java.lectures.lecture2.task; + +public class Sub extends Operation +{ + protected Sub(IntExpression left, IntExpression right) + { + super(left, right, "-"); + } + + @Override + public int eval() + { + return left.eval() - right.eval(); + } +} diff --git a/src/main/java/hse/java/lectures/lecture2/task/Sum.java b/src/main/java/hse/java/lectures/lecture2/task/Sum.java new file mode 100644 index 00000000..2cfd1060 --- /dev/null +++ b/src/main/java/hse/java/lectures/lecture2/task/Sum.java @@ -0,0 +1,18 @@ +package hse.java.lectures.lecture2.task; + + + +public class Sum extends Operation +{ + + protected Sum(IntExpression left, IntExpression right) + { + super(left, right, "+"); + } + + @Override + public int eval() + { + return left.eval() + right.eval(); + } +} diff --git a/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java b/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java index 08f551e4..bb634ee9 100644 --- a/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java +++ b/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java @@ -1,9 +1,31 @@ package hse.java.lectures.lecture3.tasks.atm; -import java.util.*; +import java.util.Arrays; +import java.util.EnumMap; +import java.util.Map; -public class Atm { - public enum Denomination { +public class Atm +{ + private final Map banknotes = new EnumMap<>(Denomination.class); + + public Atm() + { + } + + public void deposit(Map banknotes) {} + + public Map withdraw(int amount) + { + return Map.of(); + } + + public int getBalance() + { + return 0; + } + + public enum Denomination + { D50(50), D100(100), D500(500), @@ -12,34 +34,22 @@ public enum Denomination { private final int value; - Denomination(int value) { + Denomination(int value) + { this.value = value; } - int value() { - return value; - } - - public static Denomination fromInt(int value) { + public static Denomination fromInt(int value) + { return Arrays.stream(values()).filter(v -> v.value == value) .findFirst() .orElse(null); } - } - - private final Map banknotes = new EnumMap<>(Denomination.class); - - public Atm() { - } - - public void deposit(Map banknotes){} - public Map withdraw(int amount) { - return Map.of(); - } - - public int getBalance() { - return 0; + int value() + { + return value; + } } } diff --git a/src/main/java/hse/java/practice/task1/RubiksCube.java b/src/main/java/hse/java/practice/task1/RubiksCube.java index d986f9f0..b89cd17b 100644 --- a/src/main/java/hse/java/practice/task1/RubiksCube.java +++ b/src/main/java/hse/java/practice/task1/RubiksCube.java @@ -1,12 +1,15 @@ package hse.java.practice.task1; import java.util.Arrays; +import java.util.function.Consumer; + /** * Необходимо реализовать интерфейс Cube * При повороте передней грани, меняются верх низ право и лево */ -public class RubiksCube implements Cube { +public class RubiksCube implements Cube +{ private static final int EDGES_COUNT = 6; @@ -19,49 +22,242 @@ public class RubiksCube implements Cube { * грань 1 -> цвет 1 * ... */ - public RubiksCube() { + public RubiksCube() + { CubeColor[] colors = CubeColor.values(); - for (int i = 0; i < 6; i++) { + for (int i = 0; i < 6; i++) + { edges[i] = new Edge(colors[i]); } } - @Override - public void up(RotateDirection direction) { + private void rotateSides(SideLine s0, SideLine s1, SideLine s2, SideLine s3) + { + CubeColor[] line0 = getLine(s0); + CubeColor[] line1 = getLine(s1); + CubeColor[] line2 = getLine(s2); + CubeColor[] line3 = getLine(s3); + + setLine(s1, line0); + setLine(s2, line1); + setLine(s3, line2); + setLine(s0, line3); + } + + private CubeColor[] getLine(SideLine s) + { + CubeColor[][] matrix = getMatrix(s.pos); + CubeColor[] res = new CubeColor[3]; + for (int i = 0; i < 3; ++i) + { + res[i] = s.isRow ? matrix[s.idx][i] : matrix[i][s.idx]; + } + if (s.rev) + { + CubeColor tmp = res[0]; + res[0] = res[2]; + res[2] = tmp; + } + return res; + } + + private void setLine(SideLine s, CubeColor[] vals) + { + CubeColor[][] matrix = getMatrix(s.pos); + CubeColor[] data = s.rev ? new CubeColor[]{vals[2], vals[1], vals[0]} : vals; + for (int i = 0; i < 3; ++i) + { + if (s.isRow) matrix[s.idx][i] = data[i]; + else matrix[i][s.idx] = data[i]; + } } @Override - public void down(RotateDirection direction) { + public void up(RotateDirection direction) + { + if (direction == RotateDirection.COUNTERCLOCKWISE) + { + rotate3(this::up); + return; + } + rotateFaceMatrix(EdgePosition.UP); + rotateSides( + new SideLine(EdgePosition.FRONT, 0, true, false), + new SideLine(EdgePosition.LEFT, 0, true, false), + new SideLine(EdgePosition.BACK, 0, true, false), + new SideLine(EdgePosition.RIGHT, 0, true, false) + ); } @Override - public void left(RotateDirection direction) { + public void down(RotateDirection direction) + { + if (direction == RotateDirection.COUNTERCLOCKWISE) + { + rotate3(this::down); + return; + } + + rotateFaceMatrix(EdgePosition.DOWN); + + rotateSides( + new SideLine(EdgePosition.FRONT, 2, true, false), + new SideLine(EdgePosition.RIGHT, 2, true, false), + new SideLine(EdgePosition.BACK, 2, true, false), + new SideLine(EdgePosition.LEFT, 2, true, false) + ); } @Override - public void right(RotateDirection direction) { + public void left(RotateDirection direction) + { + if (direction == RotateDirection.COUNTERCLOCKWISE) + { + rotate3(this::left); + return; + } + + rotateFaceMatrix(EdgePosition.LEFT); + + rotateSides( + new SideLine(EdgePosition.UP, 0, false, false), + new SideLine(EdgePosition.FRONT, 0, false, false), + new SideLine(EdgePosition.DOWN, 0, false, false), + new SideLine(EdgePosition.BACK, 2, false, true) + ); } @Override - public void front(RotateDirection direction) { + public void right(RotateDirection direction) + { + if (direction == RotateDirection.COUNTERCLOCKWISE) + { + rotate3(this::right); + return; + } + + rotateFaceMatrix(EdgePosition.RIGHT); + + rotateSides( + new SideLine(EdgePosition.UP, 2, false, false), + new SideLine(EdgePosition.BACK, 0, false, true), + new SideLine(EdgePosition.DOWN, 2, false, false), + new SideLine(EdgePosition.FRONT, 2, false, false) + ); + } + + public void front(RotateDirection direction) + { + if (direction == RotateDirection.COUNTERCLOCKWISE) + { + rotate3(this::front); + return; + } + + rotateFaceMatrix(EdgePosition.FRONT); + + rotateSides( + new SideLine(EdgePosition.UP, 2, true, false), + new SideLine(EdgePosition.RIGHT, 0, false, false), + new SideLine(EdgePosition.DOWN, 0, true, true), + new SideLine(EdgePosition.LEFT, 2, false, true) + ); } @Override - public void back(RotateDirection direction) { + public void back(RotateDirection direction) + { + if (direction == RotateDirection.COUNTERCLOCKWISE) + { + rotate3(this::back); + return; + } + + rotateFaceMatrix(EdgePosition.BACK); + rotateSides( + new SideLine(EdgePosition.UP, 0, true, true), + new SideLine(EdgePosition.LEFT, 0, false, false), + new SideLine(EdgePosition.DOWN, 2, true, false), + new SideLine(EdgePosition.RIGHT, 2, false, true) + ); } - public Edge[] getEdges() { + public Edge[] getEdges() + { return edges; } @Override - public String toString() { + public String toString() + { return Arrays.toString(edges); } + + private CubeColor[][] getMatrix(EdgePosition pos) + { + return edges[pos.ordinal()].getParts(); + } + + private void rotateFaceMatrix(EdgePosition pos) + { + CubeColor[][] matrix = getMatrix(pos); + + transpose(matrix); + reverseRows(matrix); + + } + + private void transpose(CubeColor[][] matrix) + { + for (int i = 0; i < 3; ++i) + { + for (int j = i + 1; j < 3; j++) + { + CubeColor tmp = matrix[i][j]; + matrix[i][j] = matrix[j][i]; + matrix[j][i] = tmp; + } + } + } + + private void reverseRows(CubeColor[][] matrix) + { + for (int i = 0; i < 3; i++) + { + CubeColor tmp = matrix[i][0]; + matrix[i][0] = matrix[i][2]; + matrix[i][2] = tmp; + } + } + + private void rotate3(Consumer f) + { + f.accept(RotateDirection.CLOCKWISE); + f.accept(RotateDirection.CLOCKWISE); + f.accept(RotateDirection.CLOCKWISE); + } + + private static class SideLine + { + EdgePosition pos; + int idx; + boolean isRow; + boolean rev; + + SideLine(EdgePosition p, int i, boolean row, boolean r) + { + pos = p; + idx = i; + isRow = row; + rev = r; + } + } + + } diff --git a/src/test/java/hse/java/BaseTest.java b/src/test/java/hse/java/BaseTest.java index 45385341..b20e7b6f 100644 --- a/src/test/java/hse/java/BaseTest.java +++ b/src/test/java/hse/java/BaseTest.java @@ -6,6 +6,6 @@ public class BaseTest { @Test void test() { - + } }