diff --git a/.idea/.gitignore b/.idea/.gitignore
index 908ea44..753d56e 100644
--- a/.idea/.gitignore
+++ b/.idea/.gitignore
@@ -1,5 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
-.idea
+.idea/
HW_Java1.iml
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/HW_Java1.iml b/HW_Java1.iml
deleted file mode 100644
index 78b2cc5..0000000
--- a/HW_Java1.iml
+++ /dev/null
@@ -1,2 +0,0 @@
-
-
\ No newline at end of file
diff --git a/src/main/java/Lesson1/HomeWorkApp.java b/src/main/java/Lesson1/HomeWorkApp.java
index c9d45b3..5351624 100644
--- a/src/main/java/Lesson1/HomeWorkApp.java
+++ b/src/main/java/Lesson1/HomeWorkApp.java
@@ -1,13 +1,20 @@
package Lesson1;
-import java.util.Scanner;
-
public class HomeWorkApp {
+
public static void main(String[] args) {
printThreeWords();
- checkSumSign();
- printColor();
- compareNumbers();
+ checkSumSign(3, 6);
+ checkSumSign(-1, 1);
+ checkSumSign(-2, 1);
+ printColor(-7);
+ printColor(0);
+ printColor(7);
+ printColor(100);
+ printColor(101);
+ compareNumbers(9, 9);
+ compareNumbers(9, 5);
+ compareNumbers(3, 9);
}
public static void printThreeWords() {
@@ -16,42 +23,27 @@ public static void printThreeWords() {
System.out.println("Apple");
}
- public static void checkSumSign() {
- Scanner in = new Scanner(System.in);
- System.out.println("Введите первое число");
- int a = in.nextInt();
- System.out.println("Введите второе число");
- int b = in.nextInt();
+ public static void checkSumSign(int a, int b) {
int sum = a + b;
- if (sum >= 0) { // если сумма меньше или равна нулю
+ if (sum >= 0) { // если сумма больше или равна нулю
System.out.println("Сумма положительная");
} else {
System.out.println("Сумма отрицательная");
}
}
- public static void printColor() {
- Scanner in = new Scanner(System.in);
- System.out.println("Введите число");
- int value = in.nextInt();
+ public static void printColor(int value) {
if (value <= 0) {
System.out.println("Красный");
- }
- if (value <= 100){
+ } else if (value <= 100) {
System.out.println("Желтый");
- }
- if (value > 100){
+ } else if (value > 100) {
System.out.println("Зеленый");
}
}
- public static void compareNumbers() {
- Scanner in = new Scanner(System.in);
- System.out.println("Введите первое число");
- int a = in.nextInt();
- System.out.println("Введите второе число");
- int b = in.nextInt();
+ public static void compareNumbers(int a, int b) {
if (a >= b) {
System.out.println(a + " >= " + b);
} else {
diff --git a/src/main/java/Lesson2/HomeWorkApp2.java b/src/main/java/Lesson2/HomeWorkApp2.java
new file mode 100644
index 0000000..1ddf5d2
--- /dev/null
+++ b/src/main/java/Lesson2/HomeWorkApp2.java
@@ -0,0 +1,61 @@
+package Lesson2;
+
+public class HomeWorkApp2 {
+
+ public static void main(String[] args) {
+ checkSum(7, 5);
+ checkSum(3, -2);
+ checkSum(10, 11);
+ printChekInt(10);
+ printChekInt(-1);
+ printChekInt(0);
+ checkInt(-1);
+ checkInt(0);
+ checkInt(2);
+ printStringNTimes("Число равно ", 6);
+ checkYear(200);
+ checkYear(400);
+ checkYear(600);
+ checkYear(2020);
+ checkYear(2022);
+ }
+
+ public static boolean checkSum(int a, int b) {
+ int sum = a + b;
+ if (sum > 9 & sum < 21) {
+ return true;
+ }
+ return false;
+ }
+
+ public static void printChekInt(int n) {
+ if (n >= 0) {
+ System.out.println("Число " + n + " положительное");
+ } else {
+ System.out.println("Число " + n + " отрицательное");
+ }
+ }
+
+ public static boolean checkInt(int i) {
+ if (i == 0) {
+ System.out.println("Число равно нулю");
+ } else if (i < 0) {
+ return true;
+ }
+ return false;
+ }
+
+ public static void printStringNTimes(String text, int n) {
+ for (int i = 0; i < n; i++) {
+ System.out.println(text + (i + 1));
+ }
+ }
+
+ public static boolean checkYear(int year) {
+ if (year % 4 == 0 & year % 100 != 0 || year % 400 == 0) {
+ return true;
+ }
+ return false;
+ }
+
+}
diff --git a/src/main/java/Lesson6/Animal.java b/src/main/java/Lesson6/Animal.java
new file mode 100644
index 0000000..ab61a78
--- /dev/null
+++ b/src/main/java/Lesson6/Animal.java
@@ -0,0 +1,28 @@
+package Lesson6;
+
+public abstract class Animal {
+ private static int counterAnimal = 0;
+ private final int MAX_RUN_LENGTH = 0;
+ private final int MAX_SWIM_LENGTH = 0;
+ private final double MAX_JUMP_HEIGTH = 0;
+ private String name;
+
+ public Animal(String name) {
+ this.name = name;
+ counterAnimal++;
+ }
+
+ public static int getCountAnimal() {
+ return counterAnimal;
+ }
+
+ abstract void run(int length);
+
+ abstract void swim(int length);
+
+ abstract void jump(double height);
+
+ public String getName() {
+ return name;
+ }
+}
diff --git a/src/main/java/Lesson6/Cat.java b/src/main/java/Lesson6/Cat.java
new file mode 100644
index 0000000..3f40d1b
--- /dev/null
+++ b/src/main/java/Lesson6/Cat.java
@@ -0,0 +1,35 @@
+package Lesson6;
+
+public class Cat extends Animal {
+ private final int MAX_RUN_LENGTH = 200;
+ private final double MAX_JUMP_HEIGTH = 2;
+ private static int countCat = 0;
+
+ public Cat(String name) {
+ super(name);
+ countCat++;
+ }
+
+ public static int getCountCat() {
+ return countCat;
+ }
+
+ @Override
+ void run(int length) {
+ if ((length >= 0) && (length <= MAX_RUN_LENGTH))
+ System.out.println("Кошка " + getName() + " пробежала " + length + " м");
+ else System.out.println("Кошка " + getName() + " может пробежать максимум " + MAX_RUN_LENGTH + " м");
+ }
+
+ @Override
+ void swim(int length) {
+ System.out.println("Кошка " + getName() + " не умеет плавать");
+ }
+
+ @Override
+ void jump(double height) {
+ if ((height >= 0) && (height <= MAX_JUMP_HEIGTH))
+ System.out.println("Кошка " + getName() + " прыгнула на " + height + " м");
+ else System.out.println("Кошка " + getName() + " может прыгнуть максимум " + MAX_JUMP_HEIGTH + " м");
+ }
+}
diff --git a/src/main/java/Lesson6/Dog.java b/src/main/java/Lesson6/Dog.java
new file mode 100644
index 0000000..57b80bc
--- /dev/null
+++ b/src/main/java/Lesson6/Dog.java
@@ -0,0 +1,38 @@
+package Lesson6;
+
+public class Dog extends Animal {
+ private final int MAX_RUN_LENGTH = 500;
+ private final int MAX_SWIM_LENGTH = 10;
+ private final double MAX_JUMP_HEIGTH = 0.5;
+ private static int countDog = 0;
+
+ public Dog(String name) {
+ super(name);
+ countDog++;
+ }
+
+ public static int getCountDog() {
+ return countDog;
+ }
+
+ @Override
+ void run(int length) {
+ if ((length >= 0) && (length <= MAX_RUN_LENGTH))
+ System.out.println("Собака " + getName() + " пробежала " + length + " м");
+ else System.out.println("Собака " + getName() + " может пробежать максимум " + MAX_RUN_LENGTH + " м");
+ }
+
+ @Override
+ void swim(int length) {
+ if ((length >= 0) && (length <= MAX_SWIM_LENGTH))
+ System.out.println("Собака " + getName() + " проплыла " + length + " м");
+ else System.out.println("Собака " + getName() + " может проплыть максимум " + MAX_SWIM_LENGTH + " м");
+ }
+
+ @Override
+ void jump(double height) {
+ if ((height >= 0) && (height <= MAX_JUMP_HEIGTH))
+ System.out.println("Собака " + getName() + " прыгнула на " + height + " м");
+ else System.out.println("Собака " + getName() + " может прыгнуть максимум " + MAX_JUMP_HEIGTH + " м");
+ }
+}
diff --git a/src/main/java/Lesson6/homeWorkApp6.java b/src/main/java/Lesson6/homeWorkApp6.java
new file mode 100644
index 0000000..3fb7a40
--- /dev/null
+++ b/src/main/java/Lesson6/homeWorkApp6.java
@@ -0,0 +1,25 @@
+package Lesson6;
+
+public class homeWorkApp6 {
+ public static void main(String[] args) {
+ Cat cat = new Cat("Котюля");
+ cat.run(201);
+ cat.swim(1);
+ cat.jump(1.9);
+
+
+ Dog dog = new Dog("Собачюля");
+ dog.run(500);
+ dog.swim(10);
+ dog.jump(0.4);
+
+ Dog dog1 = new Dog("Собакевич");
+ dog1.run(501);
+ dog1.swim(50);
+ dog1.jump(2);
+
+ System.out.println("Животных в итоге " + Animal.getCountAnimal() + " шт.");
+ System.out.println("Кошачьих в итоге " + Cat.getCountCat() + " шт.");
+ System.out.println("Собачек в итоге " + Dog.getCountDog() + " шт.");
+ }
+}
diff --git a/src/main/java/lesson3/HomeWorkApp3.java b/src/main/java/lesson3/HomeWorkApp3.java
new file mode 100644
index 0000000..df566c8
--- /dev/null
+++ b/src/main/java/lesson3/HomeWorkApp3.java
@@ -0,0 +1,126 @@
+package lesson3;
+
+import java.util.Arrays;
+
+public class HomeWorkApp3 {
+ public static void main(String[] args) {
+ arrayZeroOne();
+ createArray();
+ arrayChangeTwo();
+ createArray2(7);
+ createArrayLenValue(5, 9);
+ findMiniMaxArray(16, -3, 7);
+ System.out.println(checkBalance(new int[]{1, 2, 1, 1, 1})); // true
+ System.out.println(checkBalance(new int[]{2, 3, 1, 2, 11})); // false
+ System.out.println(checkBalance(new int[]{5, 5, 10})); //true
+ shiftArray(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, -3);
+ shiftArray(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0);
+ shiftArray(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 4);
+
+ }
+
+ public static void arrayZeroOne() {
+ int[] array = new int[]{1, 1, 0, 0, 1, 0, 1, 1, 0, 0};
+ System.out.println("Начальный массив: " + (Arrays.toString(array)));
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == 0) array[i] = 1;
+ else array[i] = 0;
+ }
+ System.out.println("Массив после замены: " + (Arrays.toString(array)));
+ }
+
+ public static void createArray() {
+ int[] array = new int[100];
+ System.out.println("\nБыл создан массив: " + (Arrays.toString(array)));
+ for (int i = 0; i < array.length; i++) {
+ array[i] = i + 1;
+ }
+ }
+
+ public static void arrayChangeTwo() {
+ int[] array = new int[]{1, 5, 3, 2, 11, 4, 5, 2, 4, 8, 9, 1};
+ System.out.println("\nНачальный массив: " + (Arrays.toString(array)));
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] < 6) array[i] = array[i] * 2;
+ }
+ System.out.println("Массив после замены: " + (Arrays.toString(array)));
+ }
+
+ static void createArray2(int length) { // length of array
+ int[][] array = new int[length][length];
+ for (int i = 0; i < length; i++) {
+ for (int j = 0; j < length; j++) {
+ if ((i + j) % 2 == 0) {
+ array[i][j] = 1;
+ } else array[i][j] = 0;
+ System.out.print(array[i][j] + " ");
+ }
+ System.out.println();
+ }
+ }
+
+ static void createArrayLenValue(int len, int initialValue) {
+ int[] array = new int[len];
+ for (int i = 0; i < array.length; i++) {
+ array[i] = initialValue;
+ }
+ System.out.println("\nБыл создан массив: " + (Arrays.toString(array)));
+ }
+
+ static void findMiniMaxArray(int length, int min, int max) {
+ int[] array = new int[length];
+ for (int i = 0; i < array.length; i++) {
+ array[i] = (int) (min + Math.random() * max);
+ }
+ System.out.println("\nБыл создан массив: " + (Arrays.toString(array)));
+ min = 0;
+ max = 0;
+ for (int i = 0; i < array.length; i++) {
+ min = (min < array[i]) ? min : array[i];
+ max = (max > array[i]) ? max : array[i];
+ }
+ System.out.println("\nМинимальное значение в массиве: " + min + "\nМаксимальное значение в массиве: " + max);
+ }
+
+ static boolean checkBalance(int[] array) {
+ int leftSum, rightSum;
+ for (int i = 0; i < array.length + 1; i++) {
+ leftSum = 0;
+ rightSum = 0;
+ for (int j = 0; j < i; j++) {
+ leftSum += array[j];
+ }
+ for (int j = i; j < array.length; j++) {
+ rightSum += array[j];
+ }
+ if (leftSum == rightSum) return true;
+ }
+ return false;
+ }
+
+ static void shiftArray(int[] array, int n) {
+ System.out.println("\nНачальный массив: " + (Arrays.toString(array)));
+ if (n == 0) {
+ System.out.print("Сдвиг не может быть равен нулю (n = " + n + ")" + "\nПоэтому массив не будет изменен: ");
+ } else if (n > 0) {
+ System.out.print("Сдвиг будет вправо, т.к. n = " + n + "\nМассив после изменения: ");
+ for (int i = 0; i < n; i++) {
+ int tmp = array[array.length - 1];
+ for (int j = array.length - 1; j > 0; j--) {
+ array[j] = array[j - 1];
+ }
+ array[0] = tmp;
+ }
+ } else {
+ System.out.println("Сдвиг будет влево, так как n = " + n + "\nМассив после изменения: ");
+ for (int i = 0; i < n * (-1); i++) {
+ int tmp = array[0];
+ for (int j = 0; j < array.length - 1; j++) {
+ array[j] = array[j + 1];
+ }
+ array[array.length - 1] = tmp;
+ }
+ }
+ System.out.println(Arrays.toString(array));
+ }
+}
diff --git a/src/main/java/lesson4/HomeWorkApp4.java b/src/main/java/lesson4/HomeWorkApp4.java
new file mode 100644
index 0000000..8f782c2
--- /dev/null
+++ b/src/main/java/lesson4/HomeWorkApp4.java
@@ -0,0 +1,139 @@
+package lesson4;
+
+import java.util.Scanner;
+import java.util.concurrent.TimeUnit;
+
+public class HomeWorkApp4 {
+ private static final char DEFAULT = '_';
+ private static final char X = 'X';
+ private static final char O = 'O';
+ private static final int SIZE = 3;
+ private static final char[][] MAP = new char[SIZE][SIZE];
+
+ private static void initMap() {
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ MAP[i][j] = DEFAULT;
+ }
+ }
+ }
+
+ private static void printMap() {
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ System.out.print(MAP[i][j] + " ");
+ }
+ System.out.println();
+ }
+ }
+
+ private static void game(Scanner in) {
+ initMap();
+ System.out.println("Игра Крестики Нолики");
+ System.out.println("Для хода необходимо ввести номер строки и номер столбца");
+ int stepCounter = 0;
+ while (true) {
+ System.out.println("Ваш ход: ");
+ String line = in.nextLine();
+ String[] args = line.split(" "); // 12 1212 -> [12, 1212]
+ if (args.length != 2) {
+ System.out.println("Введите два числа");
+ } else {
+ try {
+ int x = Integer.parseInt(args[0]);
+ int y = Integer.parseInt(args[1]);
+ x--;
+ y--;
+ if (isValid(x, y)) {
+ makeStep(x, y, X);
+ printMap();
+ stepCounter++;
+ if (checkVictory(X)) {
+ System.out.println("Вы победили");
+ return;
+ }
+ if (stepCounter == 9) {
+ System.out.println("Ничья");
+ return;
+ }
+ System.out.println("Ход компьютера: ");
+ // joke();
+ movePC();
+ printMap();
+ stepCounter++;
+ if (checkVictory(O)) {
+ System.out.println("Вы проиграли");
+ return;
+ }
+ } else {
+ System.out.println("Некорректный ход.\n" +
+ "Введите два числа от 1 до 3");
+ }
+ } catch (Exception e) {
+ System.out.println("Введите два числа");
+ }
+ }
+ }
+ }
+
+ private static void joke() throws InterruptedException {
+ TimeUnit.MILLISECONDS.sleep(700);
+ System.out.println("Думаю о тебе");
+ TimeUnit.MILLISECONDS.sleep(700);
+ System.out.println("Майню биткоин");
+ TimeUnit.MILLISECONDS.sleep(700);
+ System.out.println("Читаю твою личку в фейсбуке");
+ TimeUnit.MILLISECONDS.sleep(700);
+ System.out.println("Ворую рубль со счета");
+ TimeUnit.MILLISECONDS.sleep(700);
+ System.out.println("Отправляю письмо бывшей");
+ }
+
+ private static void movePC() {
+ int x, y;
+ do {
+ x = (int) (Math.random() * 3);
+ y = (int) (Math.random() * 3);
+ } while (!isValid(x, y));
+ makeStep(x, y, O);
+ }
+
+ private static boolean checkVictory(char x) {
+ // написать логику
+ // 2. Переделать проверку победы, чтобы она не была реализована просто набором условий,
+ // например, с использованием циклов.
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
+ if (MAP[i][j] == x && MAP[i][j + 1] == x && MAP[i][j + 2] == x) {
+ return true;
+ } else if (MAP[i][j] == x && MAP[i + 1][j] == x && MAP[i + 2][j] == x) {
+ return true;
+ } else if (MAP[i][j] == x && MAP[i + 1][j + 1] == x && MAP[i + 2][j + 2] == x) {
+ return true;
+ } else if (MAP[i + 2][j] == x && MAP[i + 1][j + 1] == x && MAP[i][j + 2] == x) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+ return false;
+ }
+
+ private static void makeStep(int x, int y, char sym) {
+ MAP[x][y] = sym;
+ }
+
+ private static boolean isValid(int x, int y) {
+ return x >= 0
+ && x < SIZE
+ && y >= 0
+ && y < SIZE
+ && MAP[x][y] == DEFAULT;
+ }
+
+ public static void main(String[] args) {
+ Scanner in = new Scanner(System.in);
+ game(in);
+ }
+}
diff --git a/src/main/java/lesson4/HomeworkLesson4.java b/src/main/java/lesson4/HomeworkLesson4.java
new file mode 100644
index 0000000..c845372
--- /dev/null
+++ b/src/main/java/lesson4/HomeworkLesson4.java
@@ -0,0 +1,312 @@
+package lesson4;
+
+import java.util.Random;
+import java.util.Scanner;
+
+public class HomeworkLesson4 {
+ private static final char DEFAULT = '_';
+ private static final char X = 'X';
+ private static final char O = 'O';
+ private static final char cpu2 = 'W', cpu3 = 'Z';
+ private static final int SIZE = 3;
+ private static final char[][] MAP = new char[SIZE][SIZE];
+ Scanner sc = new Scanner(System.in);
+ Random r = new Random();
+
+ public static void main(String[] args) {
+ HomeworkLesson4 g = new HomeworkLesson4();
+ g.initMap();
+ g.printMap();
+
+ while (true) {
+ //player turn
+ g.playerTurn();
+ g.printMap();
+ if (g.checkWin(g.X)) {
+ System.out.println("Поздравляем! Вы победитель");
+ break;
+ }
+ if (g.isMapFull()) {
+ System.out.println("Игра окончена. НИЧЬЯ");
+ break;
+ }
+
+ //AI-1 turn
+ g.aiTurn(g.O);
+ g.printMap();
+ if (g.checkWin(g.O)) {
+ System.out.println("Игра окончена. Выйграл компьютер");
+ break;
+ }
+ if (g.isMapFull()) {
+ System.out.println("Игра окончена. НИЧЬЯ");
+ break;
+ }
+
+//Too much players
+// //AI-2 turn
+// g.aiTurn(g.cpu2);
+// g.printMap();
+// if (g.checkWin(g.cpu2)) { System.out.println("Игра окончена. Выйграл компьютер_1"); break; }
+// if (g.isMapFull()) { System.out.println("Игра окончена. НИЧЬЯ"); break; }
+//
+//
+// //AI-3 turn
+// g.aiTurn(g.cpu3);
+// g.printMap();
+// if (g.checkWin(g.cpu3)) { System.out.println("Игра окончена. Выйграл компьютер_2"); break; }
+// if (g.isMapFull()) { System.out.println("Игра окончена. НИЧЬЯ"); break; }
+ }
+ }
+
+ static void initMap() {
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ MAP[i][j] = DEFAULT;
+ }
+ }
+ }
+
+ static void printMap() {
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ System.out.print(MAP[i][j] + " ");
+ }
+ System.out.println();
+ }
+ }
+
+ void playerTurn() {
+ int x, y;
+ do {
+ System.out.println("Ваш ход. Введите координаты ячейки");
+ x = sc.nextInt() - 1;
+ y = sc.nextInt() - 1;
+ //System.out.println("Your enter coordinates: x = " + (x + 1) + ", y = " + (y + 1));
+ } while (!isCellValid(x, y));
+ MAP[y][x] = X;
+ }
+
+ boolean isCellValid(int x, int y) {
+ if (x < 0 || y < 0 || x >= 3 || y >= 3) return false;
+ if (MAP[y][x] == DEFAULT) return true;
+ return false;
+ }
+
+ boolean checkWin(char c) {
+ int countV;
+ int countH;
+ int countDiagonalA = 0;
+ int countDiagonalB = 0;
+ for (int i = 0; i <= SIZE - 1; i++) {
+ countH = 0;
+ countV = 0;
+ for (int j = 0; j <= SIZE - 1; j++) {
+ //tested horizontal check
+ if (MAP[i][j] == c) {
+ countH++;
+ if (countH == SIZE) return true;
+ }
+
+ //tested vertical check
+ if (MAP[j][i] == c) {
+ countV++;
+ if (countV == SIZE) return true;
+ }
+ }
+ // tested diagonal A "\" check
+ if (MAP[i][i] == c) {
+ countDiagonalA++;
+ if (countDiagonalA == SIZE) return true;
+ } else countDiagonalA = 0;
+ // tested diagonalB "/" check
+ if (MAP[i][SIZE - 1 - i] == c) {
+ countDiagonalB++;
+ if (countDiagonalB == SIZE) return true;
+ } else countDiagonalB = 0;
+ }
+ return false;
+ }
+
+ boolean isMapFull() {
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ if (MAP[i][j] == DEFAULT) return false;
+ }
+ }
+ return true;
+ }
+
+ void aiTurn(char c) {
+ int x = 0, y = 0, countH = 0, countHNull = 0, countV = 0, countVNull = 0, countDiagonalA = 0, countDiagonalB = 0, countDANull = 0, countDBNull = 0;
+
+ System.out.println("Компьютер сделал ход [" + c + "]:");
+
+ // 1. Atack player
+ for (int i = 0; i < SIZE; i++) {
+ countH = 0;
+ countHNull = 0;
+ countV = 0;
+ countVNull = 0;
+ for (int j = 0; j < SIZE; j++) {
+ //good vertical move
+ if (MAP[j][i] == c) countV++;
+ else if (MAP[j][i] == DEFAULT) countVNull++;
+ if ((countV == SIZE - 1) && (countVNull == 1)) {
+ //System.out.println("Компьютер всегда побеждает! vert line = " + (i + 1)); // i - horiz line
+ for (int k = 0; k < SIZE; k++) {
+ if (MAP[k][i] == DEFAULT) {
+ MAP[k][i] = c;
+ return;
+ }
+ }
+ }
+ //good Horizontal move
+ if (MAP[i][j] == c) countH++;
+ else if (MAP[i][j] == DEFAULT) countHNull++;
+ if ((countH == SIZE - 1) && (countHNull == 1)) {
+ //System.out.println("Компьютер всегда побеждает! horiz line = " + (i + 1)); // i - horiz line
+ for (int k = 0; k < SIZE; k++) {
+ if (MAP[i][k] == DEFAULT) {
+ MAP[i][k] = c;
+ return;
+ }
+ }
+ }
+
+ }
+
+ // good diagonal A "\" move
+ if (MAP[i][i] == c) countDiagonalA++;
+ else if (MAP[i][i] == DEFAULT) countDANull++;
+ if ((countDiagonalA == SIZE - 1) && (countDANull == 1)) {
+ //System.out.println("Компьютер всегда побеждает! diagA line \\");
+ for (int j = 0; j < SIZE; j++) {
+ if (MAP[j][j] == DEFAULT) {
+ MAP[j][j] = c;
+ return;
+ }
+ }
+ }
+
+ // good diagonal B "/" move
+ if (MAP[i][SIZE - 1 - i] == c) countDiagonalB++;
+ else if (MAP[i][SIZE - 1 - i] == DEFAULT) countDBNull++;
+ if ((countDiagonalB == SIZE - 1) && (countDBNull == 1)) {
+ //System.out.println("Компьютер всегда побеждает! diagB line /");
+ for (int j = 0; j < SIZE; j++) {
+ if (MAP[j][SIZE - 1 - j] == DEFAULT) {
+ MAP[j][SIZE - 1 - j] = c;
+ return;
+ }
+ }
+ }
+ }
+
+ countH = 0;
+ countHNull = 0;
+ countV = 0;
+ countVNull = 0;
+ countDiagonalA = 0;
+ countDiagonalB = 0;
+ countDANull = 0;
+ countDBNull = 0;
+
+ // 2. Blocking player
+ for (int i = 0; i < SIZE; i++) {
+ countH = 0;
+ countHNull = 0;
+ countV = 0;
+ countVNull = 0;
+ for (int j = 0; j < SIZE; j++) {
+ //good vertical move
+ if (MAP[j][i] == x) countV++;
+ else if (MAP[j][i] == DEFAULT) countVNull++;
+ if ((countV == SIZE - 1) && (countVNull == 1)) {
+ //System.out.println("Предупреждение для компьютера! vert line = " + (i + 1)); // i - horiz line
+ for (int k = 0; k < SIZE; k++) {
+ if (MAP[k][i] == DEFAULT) {
+ MAP[k][i] = c;
+ return;
+ }
+ }
+ }
+ //good Horizontal move
+ if (MAP[i][j] == x) countH++;
+ else if (MAP[i][j] == DEFAULT) countHNull++;
+ if ((countH == SIZE - 1) && (countHNull == 1)) {
+ //System.out.println("Предупреждение для компьютера! horiz line = " + (i + 1)); // i - horiz line
+ for (int k = 0; k < SIZE; k++) {
+ if (MAP[i][k] == DEFAULT) {
+ MAP[i][k] = c;
+ return;
+ }
+ }
+ }
+
+ }
+
+ // good diagonal A "\" move
+ if (MAP[i][i] == x) countDiagonalA++;
+ else if (MAP[i][i] == DEFAULT) countDANull++;
+ if ((countDiagonalA == SIZE - 1) && (countDANull == 1)) {
+ //System.out.println("Предупреждение для компьютера! diagA line \\");
+ for (int j = 0; j < SIZE; j++) {
+ if (MAP[j][j] == DEFAULT) {
+ MAP[j][j] = c;
+ return;
+ }
+ }
+ }
+
+ // good diagonal B "/" move
+ if (MAP[i][SIZE - 1 - i] == x) countDiagonalB++;
+ else if (MAP[i][SIZE - 1 - i] == DEFAULT) countDBNull++;
+ if ((countDiagonalB == SIZE - 1) && (countDBNull == 1)) {
+ //System.out.println("Предупреждение для компьютера! diagB line /");
+ for (int j = 0; j < SIZE; j++) {
+ if (MAP[j][SIZE - 1 - j] == DEFAULT) {
+ MAP[j][SIZE - 1 - j] = c;
+ return;
+ }
+ }
+ }
+ }
+
+ // 3. Taking center of map
+ if (!(SIZE % 2 == 0)) {
+ int center = (((SIZE + 1) / 2) - 1);
+ if (MAP[center][center] == DEFAULT) {
+ MAP[center][center] = c;
+ return;
+ }
+ }
+
+ // 4. Taking diagonal points of map
+ if (MAP[0][0] == DEFAULT) {
+ MAP[0][0] = c;
+ return;
+ }
+ if (MAP[0][MAP.length - 1] == DEFAULT) {
+ MAP[0][MAP.length - 1] = c;
+ return;
+ }
+ if (MAP[MAP.length - 1][0] == DEFAULT) {
+ MAP[MAP.length - 1][0] = c;
+ return;
+ }
+ if (MAP[MAP.length - 1][MAP.length - 1] == DEFAULT) {
+ MAP[MAP.length - 1][MAP.length - 1] = c;
+ return;
+ }
+
+ // 5. random move
+ //System.out.println("AI random");
+ do {
+ x = r.nextInt(SIZE);
+ y = r.nextInt(SIZE);
+ } while (!isCellValid(x, y));
+ MAP[y][x] = c;
+ System.out.println("AI X: " + (x + 1) + " Y: " + (y + 1));
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/lesson5/Employee.java b/src/main/java/lesson5/Employee.java
new file mode 100644
index 0000000..d0c781c
--- /dev/null
+++ b/src/main/java/lesson5/Employee.java
@@ -0,0 +1,33 @@
+package lesson5;
+
+public class Employee {
+ String firstname, name, middlename, position, email;
+ int salary, age;
+
+ Employee(String firstname,
+ String name,
+ String middlename,
+ String position,
+ String email,
+ int salary,
+ int age) {
+ this.firstname = firstname;
+ this.name = name;
+ this.middlename = middlename;
+ this.position = position;
+ this.email = email;
+ this.salary = salary;
+ this.age = age;
+ }
+
+ void getFullInfo() {
+ System.out.println("Ф.И.О: " + firstname + " " + name + " " + middlename
+ + "\n возраст: " + age
+ + "\n должность: " + position + " | заработная плата: " + salary
+ + "\n e-mail: " + email);
+ }
+
+ int getAge() {
+ return age;
+ }
+}
diff --git a/src/main/java/lesson5/homeWorkApp5.java b/src/main/java/lesson5/homeWorkApp5.java
new file mode 100644
index 0000000..cb700d8
--- /dev/null
+++ b/src/main/java/lesson5/homeWorkApp5.java
@@ -0,0 +1,19 @@
+package lesson5;
+
+public class homeWorkApp5 {
+ public static void main(String[] args) {
+ Employee[] employees = new Employee[5];
+ employees[0] = new Employee("Иванов", "Иван", "Иванович",
+ "Top manager", "ivanovii@mail.me", 150000, 45);
+ employees[1] = new Employee("Петров", "Петр", "Петрович", "manager middle",
+ "petrovpp@mail.me", 50000, 30);
+ employees[2] = new Employee("Сидоров", "Вячеслав", "Станиславович",
+ "manager junior", "sidorovvs@mail.me", 35000, 25);
+ employees[3] = new Employee("Васин", "Василий", "Васильевич",
+ "manager", "vasinVV@mail.me", 650000, 41);
+ employees[4] = new Employee("Семенов", "Семен", "Семенович",
+ "manager", "semenovss@mail.me", 300000, 23);
+
+ for (Employee e : employees) if (e.getAge() > 40) e.getFullInfo();
+ }
+}
diff --git a/src/main/java/lesson7/Cat.java b/src/main/java/lesson7/Cat.java
new file mode 100644
index 0000000..fd1b00b
--- /dev/null
+++ b/src/main/java/lesson7/Cat.java
@@ -0,0 +1,26 @@
+package lesson7;
+
+public class Cat {
+ private String name;
+ private int appetite;
+ private String isFull;
+
+ Cat(String name, int appetite) {
+ this.name = name;
+ this.appetite = appetite;
+ this.isFull = "остался голодным";
+ }
+
+ @Override
+ public String toString() {
+ return name + " ест с аппетитом: " + appetite + " и поэтому он " + isFull;
+ }
+
+ void eat(Plate plate) {
+ if (plate.getAmountOfFood() > appetite) {
+ isFull = "наелся";
+ plate.decreaseFood(appetite);
+ } else plate.decreaseFood(plate.getAmountOfFood());
+
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/lesson7/Main.java b/src/main/java/lesson7/Main.java
new file mode 100644
index 0000000..9106972
--- /dev/null
+++ b/src/main/java/lesson7/Main.java
@@ -0,0 +1,26 @@
+package lesson7;
+
+public class Main {
+ public static void main(String[] args) {
+ System.out.println("Список котов:");
+ Cat[] x = {new Cat("Матроскин", 207), new Cat("Полосатый", 175), new Cat("Рыжий", 150)};
+ Plate plate = new Plate(100);
+ for (Cat c : x) {
+ System.out.println(c);
+ }
+ System.out.println(plate);
+ System.out.println("***********************************************");
+
+
+ plate.increaseFood(250);
+ System.out.println("Добавим ещё 250 гр.");
+ System.out.println(plate);
+ System.out.println("***********************************************");
+ System.out.println("Кошары начинают есть:");
+ for (Cat c : x) {
+ c.eat(plate);
+ System.out.println(c);
+ System.out.println(plate);
+ }
+ }
+}
diff --git a/src/main/java/lesson7/Plate.java b/src/main/java/lesson7/Plate.java
new file mode 100644
index 0000000..ea425ba
--- /dev/null
+++ b/src/main/java/lesson7/Plate.java
@@ -0,0 +1,28 @@
+package lesson7;
+
+public class Plate {
+ private int amountOfFood;
+
+ Plate(int amountOfFood) {
+ this.amountOfFood = amountOfFood;
+ }
+
+ @Override
+ public String toString() {
+ return "В тарелке: " + amountOfFood + " гр.";
+ }
+
+ void decreaseFood(int appetite) {
+ if (amountOfFood >= appetite) {
+ amountOfFood -= appetite;
+ }
+ }
+
+ int getAmountOfFood() {
+ return amountOfFood;
+ }
+
+ void increaseFood(int amount) {
+ amountOfFood += amount;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/lesson8/TicTacToe.java b/src/main/java/lesson8/TicTacToe.java
new file mode 100644
index 0000000..5c2623b
--- /dev/null
+++ b/src/main/java/lesson8/TicTacToe.java
@@ -0,0 +1,333 @@
+package lesson8;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.util.Random;
+
+public class TicTacToe extends JFrame {
+ final int SIZE = 3;
+ JPanel panel = new JPanel(new GridLayout(SIZE, SIZE));
+ JButton[][] buttons = new JButton[SIZE][SIZE];
+
+ //FORM CONSTRUCTOR
+ public TicTacToe() {
+ super("Крестики-нолики");
+ setContentPane(panel);
+ setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
+ setSize(300, 300);
+ setLocationRelativeTo(null);
+ setResizable(false);
+ panel.setOpaque(true);
+ panel.setBackground(Color.DARK_GRAY);
+
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ buttons[i][j] = new JButton();
+ buttons[i][j].setBackground(Color.GREEN);
+ buttons[i][j].setFont(new Font("Шериф", Font.BOLD, (200 / SIZE)));
+ buttons[i][j].setText("");
+ panel.add(buttons[i][j]);
+ }
+ }
+
+ buttons[0][0].addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ super.mouseClicked(e);
+ if (buttons[0][0].getText().equals("") && !checkWin() && !isFull()) {
+ buttons[0][0].setText("X");
+ buttons[0][0].setBackground(new Color(128, 111, 255));
+ if (!checkWin() && !isFull()) aiTurn();
+ }
+ }
+ });
+ buttons[0][1].addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ super.mouseClicked(e);
+ if (buttons[0][1].getText().equals("") && !checkWin() && !isFull()) {
+ buttons[0][1].setText("X");
+ buttons[0][1].setBackground(new Color(128, 111, 255));
+ if (!checkWin() && !isFull()) aiTurn();
+ }
+ }
+ });
+ buttons[0][2].addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ super.mouseClicked(e);
+ if (buttons[0][2].getText().equals("") && !checkWin() && !isFull()) {
+ buttons[0][2].setText("X");
+ buttons[0][2].setBackground(new Color(128, 111, 255));
+ if (!checkWin() && !isFull()) aiTurn();
+ }
+ }
+ });
+ buttons[1][0].addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ super.mouseClicked(e);
+ if (buttons[1][0].getText().equals("") && !checkWin() && !isFull()) {
+ buttons[1][0].setText("X");
+ buttons[1][0].setBackground(new Color(128, 111, 255));
+ if (!checkWin() && !isFull()) aiTurn();
+ }
+ }
+ });
+ buttons[1][1].addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ super.mouseClicked(e);
+ if (buttons[1][1].getText().equals("") && !checkWin() && !isFull()) {
+ buttons[1][1].setText("X");
+ buttons[1][1].setBackground(new Color(128, 111, 255));
+ if (!checkWin() && !isFull()) aiTurn();
+ }
+ }
+ });
+ buttons[1][2].addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ super.mouseClicked(e);
+ if (buttons[1][2].getText().equals("") && !checkWin() && !isFull()) {
+ buttons[1][2].setText("X");
+ buttons[1][2].setBackground(new Color(128, 111, 255));
+ if (!checkWin() && !isFull()) aiTurn();
+ }
+ }
+ });
+ buttons[2][0].addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ super.mouseClicked(e);
+ if (buttons[2][0].getText().equals("") && !checkWin() && !isFull()) {
+ buttons[2][0].setText("X");
+ buttons[2][0].setBackground(new Color(128, 111, 255));
+ if (!checkWin() && !isFull()) aiTurn();
+ }
+ }
+ });
+ buttons[2][1].addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ super.mouseClicked(e);
+ if (buttons[2][1].getText().equals("") && !checkWin() && !isFull()) {
+ buttons[2][1].setText("X");
+ buttons[2][1].setBackground(new Color(128, 111, 255));
+ if (!checkWin() && !isFull()) aiTurn();
+ }
+ }
+ });
+ buttons[2][2].addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ super.mouseClicked(e);
+ if (buttons[2][2].getText().equals("") && !checkWin() && !isFull()) {
+ buttons[2][2].setText("X");
+ buttons[2][2].setBackground(new Color(128, 111, 255));
+ if (!checkWin() && !isFull()) aiTurn();
+ }
+ }
+ });
+
+ setVisible(true);
+ }
+
+ public static void main(String[] args) {
+ //NIMBUS STYLE
+ try {
+ for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
+ if ("Нимбус".equals(info.getName())) {
+ UIManager.setLookAndFeel(info.getClassName());
+ break;
+ }
+ }
+ } catch (Exception e) {
+ try {
+ UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
+ } catch (Exception ex) {
+
+ }
+ }
+
+ //VIEW FORM
+ TicTacToe game = new TicTacToe();
+ }
+
+ public void startNew() {
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ buttons[i][j].setText("");
+ buttons[i][j].setBackground(Color.ORANGE);
+ }
+ }
+ }
+
+ boolean isFull() {
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ if (buttons[i][j].getText().equals("")) return false;
+ }
+ }
+ //JOptionPane.showMessageDialog(null, "Map is full");
+ int reply = JOptionPane.showConfirmDialog(null, "ПЕРЕЗАПУСТИТЬ игру?", "Поле заполнено", JOptionPane.YES_NO_OPTION);
+ if (reply == JOptionPane.YES_OPTION) {
+ startNew();
+ } else {
+ System.exit(0);
+ }
+
+ return true;
+ }
+
+ void aiTurn() {
+ Random r = new Random();
+ int i = r.nextInt(SIZE);
+ int j = r.nextInt(SIZE);
+ while (!isCellValid(i, j)) {
+ i = r.nextInt(SIZE);
+ j = r.nextInt(SIZE);
+ }
+ buttons[i][j].setText("O");
+ buttons[i][j].setBackground(new Color(255, 111, 120));
+ checkWin();
+ }
+
+ boolean isCellValid(int i, int j) {
+ if (buttons[i][j].getText().equals("")) {
+ return true;
+ }
+ return false;
+ }
+
+ boolean checkWin() {
+ int countXH = 0;
+ int countOH = 0;
+ int countXV = 0;
+ int countOV = 0;
+ int countXD = 0;
+ int countOD = 0;
+ int countXD2 = 0;
+ int countOD2 = 0;
+ for (int i = 0; i < SIZE; i++) {
+ countXH = 0;
+ countOH = 0;
+ countXV = 0;
+ countOV = 0;
+ for (int j = 0; j < SIZE; j++) {
+ if (buttons[i][j].getText().equals("X")) {
+ countXH += 1;
+ if (countXH == SIZE) {
+ //JOptionPane.showMessageDialog(null, "You Win");
+ int reply = JOptionPane.showConfirmDialog(null, "Вы выйграли! ПЕРЕЗАПУСТИТЬ игру?", "Игра окончена", JOptionPane.YES_NO_OPTION);
+ if (reply == JOptionPane.YES_OPTION) {
+ startNew();
+ } else {
+ System.exit(0);
+ }
+ return true;
+ }
+ }
+ if (buttons[i][j].getText().equals("O")) {
+ countOH += 1;
+ if (countOH == SIZE) {
+ //JOptionPane.showMessageDialog(null, "AI Win");
+ int reply = JOptionPane.showConfirmDialog(null, "Вы проиграли! ПЕРЕЗАПУСТИТЬ игру?", "Игра окончена", JOptionPane.YES_NO_OPTION);
+ if (reply == JOptionPane.YES_OPTION) {
+ startNew();
+ } else {
+ System.exit(0);
+ }
+ return true;
+ }
+ }
+
+ if (buttons[j][i].getText().equals("X")) {
+ countXV += 1;
+ if (countXV == SIZE) {
+ //JOptionPane.showMessageDialog(null, "You Win");
+ int reply = JOptionPane.showConfirmDialog(null, "Вы выйграли! ПЕРЕЗАПУСТИТЬ игру?", "Игра окончена", JOptionPane.YES_NO_OPTION);
+ if (reply == JOptionPane.YES_OPTION) {
+ startNew();
+ } else {
+ System.exit(0);
+ }
+ return true;
+ }
+ }
+ if (buttons[j][i].getText().equals("O")) {
+ countOV += 1;
+ if (countOV == SIZE) {
+ //JOptionPane.showMessageDialog(null, "AI Win");
+ int reply = JOptionPane.showConfirmDialog(null, "Вы проиграли! ПЕРЕЗАПУСТИТЬ игру?", "Игра окончена", JOptionPane.YES_NO_OPTION);
+ if (reply == JOptionPane.YES_OPTION) {
+ startNew();
+ } else {
+ System.exit(0);
+ }
+ return true;
+ }
+ }
+ }
+
+ if (buttons[i][i].getText().equals("X")) {
+ countXD += 1;
+ if (countXD == SIZE) {
+ //JOptionPane.showMessageDialog(null, "You Win");
+ int reply = JOptionPane.showConfirmDialog(null, "Вы выйграли! ПЕРЕЗАПУСТИТЬ игру?", "Игра окончена", JOptionPane.YES_NO_OPTION);
+ if (reply == JOptionPane.YES_OPTION) {
+ startNew();
+ } else {
+ System.exit(0);
+ }
+ return true;
+ }
+ }
+ if (buttons[i][i].getText().equals("O")) {
+ countOD += 1;
+ if (countOD == SIZE) {
+ //JOptionPane.showMessageDialog(null, "AI Win");
+ int reply = JOptionPane.showConfirmDialog(null, "Вы проиграли! ПЕРЕЗАПУСТИТЬ игру?", "Игра окончена", JOptionPane.YES_NO_OPTION);
+ if (reply == JOptionPane.YES_OPTION) {
+ startNew();
+ } else {
+ System.exit(0);
+ }
+ return true;
+ }
+ }
+
+ if (buttons[i][SIZE - i - 1].getText().equals("X")) {
+ countXD2 += 1;
+ if (countXD2 == SIZE) {
+ //JOptionPane.showMessageDialog(null, "You Win");
+ int reply = JOptionPane.showConfirmDialog(null, "Вы выйграли! ПЕРЕЗАПУСТИТЬ игру?", "Игра окончена", JOptionPane.YES_NO_OPTION);
+ if (reply == JOptionPane.YES_OPTION) {
+ startNew();
+ } else {
+ System.exit(0);
+ }
+ return true;
+ }
+ }
+ if (buttons[i][SIZE - i - 1].getText().equals("O")) {
+ countOD2 += 1;
+ if (countOD2 == SIZE) {
+ //JOptionPane.showMessageDialog(null, "AI Win");
+ int reply = JOptionPane.showConfirmDialog(null, "Вы проиграли! ПЕРЕЗАПУСТИТЬ игру?", "Игра окончена", JOptionPane.YES_NO_OPTION);
+ if (reply == JOptionPane.YES_OPTION) {
+ startNew();
+ } else {
+ System.exit(0);
+ }
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+}
\ No newline at end of file
diff --git a/target/classes/Lesson1/HomeWorkApp.class b/target/classes/Lesson1/HomeWorkApp.class
new file mode 100644
index 0000000..d877e81
Binary files /dev/null and b/target/classes/Lesson1/HomeWorkApp.class differ
diff --git a/target/classes/Lesson2/HomeWorkApp2.class b/target/classes/Lesson2/HomeWorkApp2.class
new file mode 100644
index 0000000..afb777b
Binary files /dev/null and b/target/classes/Lesson2/HomeWorkApp2.class differ
diff --git a/target/classes/lesson3/HomeWorkApp3.class b/target/classes/lesson3/HomeWorkApp3.class
new file mode 100644
index 0000000..f0d9c2c
Binary files /dev/null and b/target/classes/lesson3/HomeWorkApp3.class differ