From b90ee84924e52f70ce4fdb77fe29eea9aa49c2a7 Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Thu, 1 Apr 2021 18:32:43 +0300 Subject: [PATCH 1/8] Version3 Version3 Version2 Revert "Version2" This reverts commit 4f6464c9 Version2 Version2 Version1 Version1 Version1 --- .idea/.gitignore | 2 +- .idea/vcs.xml | 6 ++ src/main/java/Lesson1/HomeWorkApp.java | 44 ++++---- src/main/java/Lesson2/HomeWorkApp2.java | 61 +++++++++++ src/main/java/lesson3/HomeWorkApp3.java | 126 ++++++++++++++++++++++ target/classes/Lesson1/HomeWorkApp.class | Bin 0 -> 1679 bytes target/classes/Lesson2/HomeWorkApp2.class | Bin 0 -> 1927 bytes target/classes/lesson3/HomeWorkApp3.class | Bin 0 -> 4107 bytes 8 files changed, 212 insertions(+), 27 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/Lesson2/HomeWorkApp2.java create mode 100644 src/main/java/lesson3/HomeWorkApp3.java create mode 100644 target/classes/Lesson1/HomeWorkApp.class create mode 100644 target/classes/Lesson2/HomeWorkApp2.class create mode 100644 target/classes/lesson3/HomeWorkApp3.class diff --git a/.idea/.gitignore b/.idea/.gitignore index 908ea44..9824f5d 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/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/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/target/classes/Lesson1/HomeWorkApp.class b/target/classes/Lesson1/HomeWorkApp.class new file mode 100644 index 0000000000000000000000000000000000000000..d877e817664fa0a6f5e8f450f68135944cb92f38 GIT binary patch literal 1679 zcmaJ?-*4Mg6#krFc2YNSlP2quY=n)LY;4PZF&O=^u7R;awhmH?5HGli)p||h$VrBf zcz`1D0%&hT8rs7&Bwh;=S)i=E^W^+FNND1_wnf*jBGx_k-gD0V&UenacK&+$`woDM zSczc*=VLg83qo8J;#DCoMe!P5S8zFoH!!VWM!~FtIR#e~%qzI6U_nMX22GsS@TP*d z#C%PNw-sEMu_)sm36UAIWp?Kz1dAtDB!tRV-H?!|m@VUGueoa2%lc}Anq5UcL zHpSjohPvx!M?$(%F*+Tqb*^;XY8oF}_QutAdzxTP-E2w76hE%q*6--0hTd8$RlBy? zTAOx*>Gm3rrw%ZaLYscw<#pQ-h;{m@wRNMmQSCLW<{Dwc#l=N|BIz899IH&C(`!l? zt#R<(L|&A4WYthHwnIwNvW zaTr+@F9|V;DHW%1T15#{GTv2D!3|b)$!4C0jGHQ!aEposy~T%&Sr&mh={y{M<@~{C ztKj_U>^R#TeP=M-bbfWVhu=C+oUP&AeSSN`&EXgH`I@$UesSkMO22fA_hQT8+Yjw;$3o9+)D2t3dw8EED$LCm#JZ|t86U8$g_#2TbU>F`qFt<| z)!Rm`yGK?1tkX5vlAzV2A?@7*(<J#%Ej2LmLR)rYp~Vn;=vavqPN>d zt1cnWratH5Cq?|z2c7>Pf2C`A*HprI@p<5ByvR3jf-fi#fh4X3WsbWNXNhz8KhAT)u!*iZK z$6cPh@;=X5@)WOd>>?X6OvzMa?EifiI2^^(mo(^vGv( zpUmceKwLnL5aK6{h^6YI1zofZ!t~7%Gf%`LNU?-DmQR$m;NdWxB6mc@39fjT8Sx}n z0lY%3jKDugNunBjaeWe>(p_X}&XV7GEbwndFv@tL{;OEoubAK#LSQmaQfIji G;M~7ZbZrv= literal 0 HcmV?d00001 diff --git a/target/classes/Lesson2/HomeWorkApp2.class b/target/classes/Lesson2/HomeWorkApp2.class new file mode 100644 index 0000000000000000000000000000000000000000..afb777beadcb717039d99a94c762649bf7df4048 GIT binary patch literal 1927 zcmaJ>OK%fb6#nk`F@8)O+j)UwXcC}tk_IW`=p^tEXECDUTmXKY@ZmkFN2NIL!nvZsxe3xS6`6f2Jpw_1r=-T`HQn zg<(6JUR>b$rhT>re3?Zfvy@)V3V0HelRcjZ2%Qf>i#g(5kvk3PlAc+*q8A)QT7kh! z~~tP7&y3Z|sED$w3x{c3H>*jAZY?px2S zjq0~Df%QzoHGHVzBMoUx+pe3{`$SNwer-M3=_+uzEqYe|M*L2odB2Ge2ya8a zI(Ns&u$pyC>ANc>gH3hiSGmzrE3=tTUXwj#j|@G_q4Mg5f{~jS=-;PLJ$riN6om)l z9EShd_LcIrIUxGPUPlIKveEAZB%sbRI(a#pd4~ZNbV4g-8{d-Dg4)yj z9O7ph3Jy?u?L`+3($;DYh@qWte#CKzEAsGf^p=Njg;ie#%B(m1N~&-m9IWXITnZmo zO}l4lxMw|5L_J||1>OjLuRxuRi0^Hsd~7TAubTQ>s%=nX2s4?N3{-^RBL_br1@H!f zI6{~SG;l&g7(^pZ({~gRTtpO8!II-PHets%sO z(Wsr)_=?!pJ21#&$|bKn%6PISlA!kRM?vG9E1;eXSyhG2}-%*~xs60%vGWc*XieWs3oH}S~{k%ex8 VH!vzL+oMn$grK~h?{G`O(7(Y>lXd_A literal 0 HcmV?d00001 diff --git a/target/classes/lesson3/HomeWorkApp3.class b/target/classes/lesson3/HomeWorkApp3.class new file mode 100644 index 0000000000000000000000000000000000000000..72c4483f1de813473c8b141101cfdd24964a8463 GIT binary patch literal 4107 zcmbVPZA@F&8Gf#>?R$MO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OCPKU`kn zv+4n#hVbbcnZjdXHPS4#E&NU``|J}wc`yDdPtCjiJ#jiOQllWp7A-;{Xu<=#=iS3`c@(%4E;_LVi zUY7ZNo(eq8+T`T-B;TsfFqbOgti?_}`z_zV&g^Zz Date: Tue, 13 Apr 2021 17:45:05 +0300 Subject: [PATCH 2/8] Version3 --- src/main/java/lesson4/HomeworkLesson4.java | 312 +++++++++++++++++++++ 1 file changed, 312 insertions(+) create mode 100644 src/main/java/lesson4/HomeworkLesson4.java 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 From 01bda8a376184439c0004faa1e4b5d964897847d Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Tue, 13 Apr 2021 19:27:52 +0300 Subject: [PATCH 3/8] Version3 --- src/main/java/lesson5/Employee.java | 33 +++++++++++++++++++++++++ src/main/java/lesson5/homeWorkApp5.java | 19 ++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/lesson5/Employee.java create mode 100644 src/main/java/lesson5/homeWorkApp5.java 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(); + } +} From 92f70e33bd735f6e57346093711eba6b4cbe8a74 Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Tue, 13 Apr 2021 19:29:26 +0300 Subject: [PATCH 4/8] Version5 --- .idea/.gitignore | 2 +- HW_Java1.iml | 2 - src/main/java/lesson4/HomeWorkApp4.java | 139 ++++++++++++++++++++++ target/classes/lesson3/HomeWorkApp3.class | Bin 4107 -> 4349 bytes 4 files changed, 140 insertions(+), 3 deletions(-) delete mode 100644 HW_Java1.iml create mode 100644 src/main/java/lesson4/HomeWorkApp4.java diff --git a/.idea/.gitignore b/.idea/.gitignore index 9824f5d..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/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/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/target/classes/lesson3/HomeWorkApp3.class b/target/classes/lesson3/HomeWorkApp3.class index 72c4483f1de813473c8b141101cfdd24964a8463..f0d9c2c5f4262e1d87eb4dbc081e48f039794485 100644 GIT binary patch delta 2286 zcma)7OK=ob6g_Xcd%CC7872)OWF~=0CL);tk$@ozB*E|r-{B9F4~G9A`F|7`#Rynh z7FJtYx^Ur!g$oO*3Zo*WRajQJk)0(AbtP^r%N10L0nhE3%%sA`ndaLn|W2ti0yk{`g!E++!if9w@0$y~`jt&Peq07W62d8ny!4fPran`}Bc+J6b ztnl^)IxElNyon1AGI(9Y8)ClbeIGboeGYFbsJMecT$I(RCf;&z372oI)gCDCXCrRt z(|Q^=c}tA9y?=~>OoCL&&xQhFQ9jFAGe^bINZy3HP9=b8l)?NW8mT)4J&7jHLYR&j zT;XAL5Qf;4n?)cCEekzjW?@*7P!_>RB$AV<`6Vptmc1dZ>8Z4Cq|ye*ARkjtWf2;J zo!^kwXw!oQJ9RTsR@xA}Wv4ZZ8~7LzyI}Bs4>lW-Toz^2T9nAaiQ0q) zi(ZBJw-H7eCeW{1+7@uO72_C#2tG#zeuJwjXf1lKT+Ad*;yD8;o_iO}j;Qqwob*kU zyWt^>OJ)(_FocR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ject: [PATCH 5/8] Version6 --- src/main/java/Lesson6/Animal.java | 13 +++++++++++++ src/main/java/Lesson6/Cat.java | 23 +++++++++++++++++++++++ src/main/java/Lesson6/Dog.java | 24 ++++++++++++++++++++++++ src/main/java/Lesson6/homeWorkApp6.java | 15 +++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 src/main/java/Lesson6/Animal.java create mode 100644 src/main/java/Lesson6/Cat.java create mode 100644 src/main/java/Lesson6/Dog.java create mode 100644 src/main/java/Lesson6/homeWorkApp6.java diff --git a/src/main/java/Lesson6/Animal.java b/src/main/java/Lesson6/Animal.java new file mode 100644 index 0000000..49dff25 --- /dev/null +++ b/src/main/java/Lesson6/Animal.java @@ -0,0 +1,13 @@ +package Lesson6; + +public abstract class Animal { + private final int MAX_RUN_LENGTH = 0; + private final int MAX_SWIM_LENGTH = 0; + private final double MAX_JUMP_HEIGTH = 0; + + abstract void run(int length); + + abstract void swim(int length); + + abstract void jump(double height); +} diff --git a/src/main/java/Lesson6/Cat.java b/src/main/java/Lesson6/Cat.java new file mode 100644 index 0000000..2c881a1 --- /dev/null +++ b/src/main/java/Lesson6/Cat.java @@ -0,0 +1,23 @@ +package Lesson6; + +public class Cat extends Animal { + private final int MAX_RUN_LENGTH = 200; + private final double MAX_JUMP_HEIGTH = 2; + + @Override + void run(int length) { + if ((length >= 0) && (length <= MAX_RUN_LENGTH)) System.out.println("run: true"); + else System.out.println("run: false"); + } + + @Override + void swim(int length) { + System.out.println("swim: false"); + } + + @Override + void jump(double height) { + if ((height >= 0) && (height <= MAX_JUMP_HEIGTH)) System.out.println("jump: true"); + else System.out.println("jump: false"); + } +} diff --git a/src/main/java/Lesson6/Dog.java b/src/main/java/Lesson6/Dog.java new file mode 100644 index 0000000..b5c0a59 --- /dev/null +++ b/src/main/java/Lesson6/Dog.java @@ -0,0 +1,24 @@ +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; + + @Override + void run(int length) { + if ((length >= 0) && (length <= MAX_RUN_LENGTH)) System.out.println("run: true"); + } + + @Override + void swim(int length) { + if ((length >= 0) && (length <= MAX_SWIM_LENGTH)) System.out.println("swim: true"); + else System.out.println("swim: false"); + } + + @Override + void jump(double height) { + if ((height >= 0) && (height <= MAX_JUMP_HEIGTH)) System.out.println("jump: true"); + else System.out.println("jump: false"); + } +} diff --git a/src/main/java/Lesson6/homeWorkApp6.java b/src/main/java/Lesson6/homeWorkApp6.java new file mode 100644 index 0000000..91b0084 --- /dev/null +++ b/src/main/java/Lesson6/homeWorkApp6.java @@ -0,0 +1,15 @@ +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); + } +} From e4336b8afba613ee0e8a63d77a9243681b462cc8 Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Mon, 19 Apr 2021 21:45:56 +0300 Subject: [PATCH 6/8] Version6 --- src/main/java/Lesson6/Animal.java | 15 +++++++++++++++ src/main/java/Lesson6/Cat.java | 22 +++++++++++++++++----- src/main/java/Lesson6/Dog.java | 24 +++++++++++++++++++----- src/main/java/Lesson6/homeWorkApp6.java | 14 ++++++++++++-- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/main/java/Lesson6/Animal.java b/src/main/java/Lesson6/Animal.java index 49dff25..ab61a78 100644 --- a/src/main/java/Lesson6/Animal.java +++ b/src/main/java/Lesson6/Animal.java @@ -1,13 +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 index 2c881a1..3f40d1b 100644 --- a/src/main/java/Lesson6/Cat.java +++ b/src/main/java/Lesson6/Cat.java @@ -3,21 +3,33 @@ 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("run: true"); - else System.out.println("run: false"); + 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("swim: false"); + System.out.println("Кошка " + getName() + " не умеет плавать"); } @Override void jump(double height) { - if ((height >= 0) && (height <= MAX_JUMP_HEIGTH)) System.out.println("jump: true"); - else System.out.println("jump: false"); + 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 index b5c0a59..57b80bc 100644 --- a/src/main/java/Lesson6/Dog.java +++ b/src/main/java/Lesson6/Dog.java @@ -4,21 +4,35 @@ 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("run: true"); + 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("swim: true"); - else System.out.println("swim: false"); + 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("jump: true"); - else System.out.println("jump: false"); + 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 index 91b0084..3fb7a40 100644 --- a/src/main/java/Lesson6/homeWorkApp6.java +++ b/src/main/java/Lesson6/homeWorkApp6.java @@ -2,14 +2,24 @@ public class homeWorkApp6 { public static void main(String[] args) { - Cat cat = new Cat(); + Cat cat = new Cat("Котюля"); cat.run(201); cat.swim(1); cat.jump(1.9); - Dog dog = new Dog(); + + 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() + " шт."); } } From 3c064fe929d74739de5581fd3353698a7ded4d5d Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Mon, 19 Apr 2021 22:57:53 +0300 Subject: [PATCH 7/8] Version7 --- src/main/java/lesson7/Cat.java | 26 ++++++++++++++++++++++++++ src/main/java/lesson7/Main.java | 26 ++++++++++++++++++++++++++ src/main/java/lesson7/Plate.java | 28 ++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/main/java/lesson7/Cat.java create mode 100644 src/main/java/lesson7/Main.java create mode 100644 src/main/java/lesson7/Plate.java 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 From 86fb0cfb1ba6eb6e7df36beecea07ede07b65c90 Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Thu, 22 Apr 2021 21:33:12 +0300 Subject: [PATCH 8/8] =?UTF-8?q?Version8=20-=20=D0=98=D0=B3=D1=80=D0=B0=20?= =?UTF-8?q?=D0=BA=D1=80=D0=B5=D1=81=D1=82=D0=B8=D0=BA=D0=B8-=D0=BD=D0=BE?= =?UTF-8?q?=D0=BB=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lesson8/TicTacToe.java | 333 +++++++++++++++++++++++++++ 1 file changed, 333 insertions(+) create mode 100644 src/main/java/lesson8/TicTacToe.java 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