Skip to content
Open
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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions src/ru/geekbrains/java1/lesson4/TicTacToe.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

public class TicTacToe {

private static final int SIZE = 5;
private static final int DOTS_TO_WIN = 4;
public static final int SIZE = 3;
private static final int DOTS_TO_WIN = 3;

private static final char DOT_EMPTY = '•';
private static final char DOT_X = 'X';
private static final char DOT_O = 'O';
public static final char DOT_EMPTY = '•';
public static final char DOT_X = 'X';
public static final char DOT_O = 'O';

private static final char[][] map = new char[SIZE][SIZE];
public static final char[][] map = new char[SIZE][SIZE];


public static void main(String[] args) {
Expand Down Expand Up @@ -64,11 +64,11 @@ private static boolean isGameMoveWinning(int rowIndex, int colIndex, char symbol
return result;
}

private static void setCell(int rowIndex, int colIndex, char symbol) {
public static void setCell(int rowIndex, int colIndex, char symbol) {
map[rowIndex][colIndex] = symbol;
}

private static boolean isMapFull() {
public static boolean isMapFull() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (map[i][j] == DOT_EMPTY) {
Expand All @@ -80,7 +80,7 @@ private static boolean isMapFull() {
}


private static boolean isWin(char symbol) {
public static boolean isWin(char symbol) {
if (checkRowsAndCols(symbol)) {
return true;
} else {
Expand Down Expand Up @@ -122,7 +122,7 @@ private static void printMap() {
System.out.println();
}

private static void computerTurn() {
public static int[] computerTurn() {
int[] cell = getNextCellToWin(DOT_O);
if (cell == null) {
cell = getNextCellToWin(DOT_X);
Expand All @@ -134,6 +134,7 @@ private static void computerTurn() {
int colIndex = cell[1];

setCell(rowIndex, colIndex, DOT_O);
return cell;
}

private static int[] getRandomEmptyCell() {
Expand Down Expand Up @@ -199,7 +200,7 @@ private static void printHeader() {
System.out.println();
}

private static void initMap() {
public static void initMap() {
for (int i = 0; i < SIZE; i++) {
Arrays.fill(map[i], DOT_EMPTY);
}
Expand Down
94 changes: 94 additions & 0 deletions src/ru/geekbrains/java1/lesson8/CounterApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ru.geekbrains.java1.lesson8;

import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;

public class CounterApp extends JFrame {
private int value;
private int step = 1;

public CounterApp(int initialValue) {
setBounds(500, 500, 300, 120);
setTitle("Simple Counter");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);

Font font = new Font("Arial", Font.BOLD, 32);

JLabel counterValueView = new JLabel(String.valueOf(value));
counterValueView.setHorizontalAlignment(SwingConstants.CENTER);
counterValueView.setFont(font);
add(counterValueView, BorderLayout.CENTER);

value = initialValue;
refreshCounterLabel(counterValueView);

JButton decrementButton = new JButton("<");
add(decrementButton, BorderLayout.LINE_START);
decrementButton.setFont(font);

JButton incrementButton = new JButton(">");
add(incrementButton, BorderLayout.LINE_END);
incrementButton.setFont(font);

NumberFormat longFormat = NumberFormat.getIntegerInstance();

NumberFormatter numberFormatter = new NumberFormatter(longFormat);
numberFormatter.setAllowsInvalid(false); //this is the key!!

JFormattedTextField stepTextField = new JFormattedTextField(numberFormatter);
stepTextField.setHorizontalAlignment(SwingConstants.CENTER);
stepTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
step = Integer.parseInt(stepTextField.getText());
}
});
add(stepTextField, BorderLayout.NORTH);

decrementButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
value -= step;
refreshCounterLabel(counterValueView);
}
});

incrementButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
value += step;
refreshCounterLabel(counterValueView);
}
});

JButton clearButton = new JButton("Reset");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
step = 1;
value = 0;
stepTextField.setValue(step);
refreshCounterLabel(counterValueView);
}
});
add(clearButton, BorderLayout.SOUTH);

setVisible(true);
}

private void refreshCounterLabel(JLabel counterValueView) {
counterValueView.setText(String.valueOf(value));
}

public static void main(String[] args) {
new CounterApp(0);
}
}

146 changes: 146 additions & 0 deletions src/ru/geekbrains/java1/lesson8/TicTacToeGui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package ru.geekbrains.java1.lesson8;

import ru.geekbrains.java1.lesson4.TicTacToe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TicTacToeGui {

private static final String FRAME_TITLE = "Крестики-Нолики";
private static final String START_GAME_MSG = "Игра началачь!";

private final JLabel[][] gameField = new JLabel[TicTacToe.SIZE][TicTacToe.SIZE];

private JLabel gameStateLabel;
private JPanel gamePanel;
private JFrame frame;

public TicTacToeGui() {
initFrame();
frame.setVisible(true);
}

private void initFrame() {
frame = new JFrame();
frame.setTitle(FRAME_TITLE);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(400, 400);

gameStateLabel = new JLabel(START_GAME_MSG);
gameStateLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(gameStateLabel, BorderLayout.NORTH);

JButton restartGameButton = new JButton("Начать заново");
restartGameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startGame();
}
});

frame.add(restartGameButton, BorderLayout.SOUTH);

gamePanel = new JPanel();
gamePanel.setLayout(new GridLayout(TicTacToe.SIZE, TicTacToe.SIZE));
initCells(gamePanel);
frame.add(gamePanel, BorderLayout.CENTER);
}

private void initCells(JPanel gamePanel) {
Font font = new Font("Arial", Font.BOLD, 32);
for (int i = 0; i < TicTacToe.SIZE; i++) {
for (int j = 0; j < TicTacToe.SIZE; j++) {
JLabel cell = new JLabel(String.valueOf(TicTacToe.DOT_EMPTY));
cell.setHorizontalAlignment(SwingConstants.CENTER);
cell.setVerticalAlignment(SwingConstants.CENTER);
cell.setFont(font);
cell.setBorder(BorderFactory.createLineBorder(Color.black));

final int rowIndex = i;
final int colIndex = j;
cell.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (!cell.isEnabled()) {
return;
}

TicTacToe.setCell(rowIndex, colIndex, TicTacToe.DOT_X);
makeTurn(TicTacToe.DOT_X, cell);

if (checkEndGameState(TicTacToe.DOT_X, "Игрок")) {
return;
}

int[] computerCellIndex = TicTacToe.computerTurn();
JLabel computerCell = gameField[computerCellIndex[0]][computerCellIndex[1]];
makeTurn(TicTacToe.DOT_O, computerCell);

checkEndGameState(TicTacToe.DOT_O, "Компьютер");
}
});

gamePanel.add(cell);
gameField[i][j] = cell;
}
}
}

private void makeTurn(char symbol, JLabel cell) {
cell.setText(String.valueOf(symbol));
cell.setEnabled(false);
}

private boolean checkEndGameState(char symbol, String playerName) {
if (TicTacToe.isWin(symbol)) {
setEndGameState(String.format("%s победил!", playerName));
return true;
}
else if (TicTacToe.isMapFull()) {
setEndGameState("Ничья!");
return true;
}

return false;
}

private void setEndGameState(String messageState) {
setEnabledGameField(false);
gameStateLabel.setText(messageState);
}

private void startGame() {
setEnabledGameField(true);
gameStateLabel.setText(START_GAME_MSG);
TicTacToe.initMap();
refreshGameField();
}

private void refreshGameField() {
for (int i = 0; i < TicTacToe.SIZE; i++) {
for (int j = 0; j < TicTacToe.SIZE; j++) {
gameField[i][j].setText(String.valueOf(TicTacToe.map[i][j]));
}
}
}

private void setEnabledGameField(boolean enabled) {
gamePanel.setEnabled(true);
for (int i = 0; i < TicTacToe.SIZE; i++) {
for (int j = 0; j < TicTacToe.SIZE; j++) {
gameField[i][j].setEnabled(enabled);
}
}
}

public static void main(String[] args) {
TicTacToeGui ticTacToeGui = new TicTacToeGui();
ticTacToeGui.startGame();
}
}
39 changes: 39 additions & 0 deletions src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.geekbrains.java1.lesson8.borderlayout;

import javax.swing.*;
import java.awt.*;

public class BorderLayout1 {

static class MyWindow extends JFrame {
public MyWindow() {
setTitle("Test Window");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(800, 800);
setLocationRelativeTo(null);

JButton button = new JButton("Button 1 (PAGE_START)");
add(button, BorderLayout.PAGE_START);

button = new JButton("Button 2 (CENTER)");
add(button, BorderLayout.CENTER);

button = new JButton("Button 3 (LINE_START)");
button.setSize(new Dimension(200, 200));
add(button, BorderLayout.LINE_START);

button = new JButton("Long-Named Button 4 (PAGE_END)");
add(button, BorderLayout.PAGE_END);

button = new JButton("5 (LINE_END)");
add(button, BorderLayout.LINE_END);

setVisible(true);
}
}

public static void main(String[] args) {
new MyWindow();
}

}
30 changes: 30 additions & 0 deletions src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.geekbrains.java1.lesson8.borderlayout;

import javax.swing.*;
import java.awt.*;

public class BorderLayout2 {

static public class MyWindow extends JFrame {
public MyWindow() {
setBounds(500,500,500,300);
setTitle("BoxLayoutDemo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton[] jbs = new JButton[10];
// setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); // одну из строк надо закомментировать
setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS)); // одну из строк надо закомментировать
for (int i = 0; i < jbs.length; i++) {
jbs[i] = new JButton("#" + i);
// jbs[i].setAlignmentX(Component.CENTER_ALIGNMENT);
jbs[i].setAlignmentY(Component.CENTER_ALIGNMENT);
add(jbs[i]);
}
setVisible(true);
}
}

public static void main(String[] args) {
new MyWindow();
}

}
Loading