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
153 changes: 143 additions & 10 deletions src/main/java/hse/java/lectures/lecture1/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

28 changes: 28 additions & 0 deletions src/main/java/hse/java/lectures/lecture2/task/Const.java
Original file line number Diff line number Diff line change
@@ -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);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hse.java.lectures.lecture2.task;

public interface IntExpression {
String toString();
int eval();
}
19 changes: 19 additions & 0 deletions src/main/java/hse/java/lectures/lecture2/task/Main.java
Original file line number Diff line number Diff line change
@@ -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());


}
}
24 changes: 24 additions & 0 deletions src/main/java/hse/java/lectures/lecture2/task/Operation.java
Original file line number Diff line number Diff line change
@@ -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() + ")";
}
}
15 changes: 15 additions & 0 deletions src/main/java/hse/java/lectures/lecture2/task/Sub.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
18 changes: 18 additions & 0 deletions src/main/java/hse/java/lectures/lecture2/task/Sum.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
56 changes: 33 additions & 23 deletions src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java
Original file line number Diff line number Diff line change
@@ -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<Denomination, Integer> banknotes = new EnumMap<>(Denomination.class);

public Atm()
{
}

public void deposit(Map<Denomination, Integer> banknotes) {}

public Map<Denomination, Integer> withdraw(int amount)
{
return Map.of();
}

public int getBalance()
{
return 0;
}

public enum Denomination
{
D50(50),
D100(100),
D500(500),
Expand All @@ -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<Denomination, Integer> banknotes = new EnumMap<>(Denomination.class);

public Atm() {
}

public void deposit(Map<Denomination, Integer> banknotes){}

public Map<Denomination, Integer> withdraw(int amount) {
return Map.of();
}

public int getBalance() {
return 0;
int value()
{
return value;
}
}

}
Loading