From a3f70ddb43f64d8d2936c84a081996d874beb603 Mon Sep 17 00:00:00 2001 From: valkonsky Date: Tue, 25 May 2021 00:52:23 +0300 Subject: [PATCH 1/2] lesson 4 homework --- .../main/java/ru/gb/savilin/lesson4/Main.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 lesson2/src/main/java/ru/gb/savilin/lesson4/Main.java diff --git a/lesson2/src/main/java/ru/gb/savilin/lesson4/Main.java b/lesson2/src/main/java/ru/gb/savilin/lesson4/Main.java new file mode 100644 index 0000000..b79a5da --- /dev/null +++ b/lesson2/src/main/java/ru/gb/savilin/lesson4/Main.java @@ -0,0 +1,106 @@ +package ru.gb.savilin.lesson4; + +import java.util.Random; +import java.util.Scanner; +public class Main { + public static int SIZE = 3; + public static int DOTS_TO_WIN = 3; + public static final char DOT_EMPTY = '•'; + public static final char DOT_X = 'X'; + public static final char DOT_O = 'O'; + public static char[][] map; + public static Scanner sc = new Scanner(System.in); + public static Random rand = new Random(); + public static void main(String[] args) { + initMap(); + printMap(); + while (true) { + humanTurn(); + printMap(); + if (checkWin(DOT_X)) { + System.out.println("Победил человек"); + break; + } + if (isMapFull()) { + System.out.println("Ничья"); + break; + } + aiTurn(); + printMap(); + if (checkWin(DOT_O)) { + System.out.println("Победил Искуственный Интеллект"); + break; + } + if (isMapFull()) { + System.out.println("Ничья"); + break; + } + } + System.out.println("Игра закончена"); + } + public static boolean checkWin(char symb) { + if(map[0][0] == symb && map[0][1] == symb && map[0][2] == symb) return true; + if(map[1][0] == symb && map[1][1] == symb && map[1][2] == symb) return true; + if (map[2][0] == symb && map[2][1] == symb && map[2][2] == symb) return true; + if (map[0][0] == symb && map[1][0] == symb && map[2][0] == symb) return true; + if (map[0][1] == symb && map[1][1] == symb && map[2][1] == symb) return true; + if (map[0][2] == symb && map[1][2] == symb && map[2][2] == symb) return + true; + if (map[0][0] == symb && map[1][1] == symb && map[2][2] == symb) return true; + if (map[2][0] == symb && map[1][1] == symb && map[0][2] == symb) return true; + return false; + } + public static boolean isMapFull() { + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + if (map[i][j] == DOT_EMPTY) return false; + } + } + return true; + } + public static void aiTurn() { + int x, y; + do { + x = rand.nextInt(SIZE); + y = rand.nextInt(SIZE); + } while (!isCellValid(x, y)); + System.out.println("Компьютер походил в точку " + (x + 1) + " " + (y + 1)); + map[y][x] = DOT_O; + } + public static void humanTurn() { + int x, y; + do { + System.out.println("Введите координаты в формате X Y"); + x = sc.nextInt() - 1; + y = sc.nextInt() - 1; + } while (!isCellValid(x, y)); // while(isCellValid(x, y) == false) + map[y][x] = DOT_X; + } + public static boolean isCellValid(int x, int y) { + if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) return false; + if (map[y][x] == DOT_EMPTY) return true; + return false; + } + public static void initMap() { + map = new char[SIZE][SIZE]; + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + map[i][j] = DOT_EMPTY; + } + } + } + public static void printMap() { + for (int i = 0; i <= SIZE; i++) { + System.out.print(i + " "); + } + System.out.println(); + for (int i = 0; i < SIZE; i++) { + System.out.print((i + 1) + " "); + for (int j = 0; j < SIZE; j++) { + System.out.print(map[i][j] + " "); + } + System.out.println(); + } + System.out.println(); + } +} \ No newline at end of file From 0029d3efe8233c35c0bbfa994b480d4d04cb88da Mon Sep 17 00:00:00 2001 From: valkonsky Date: Sun, 6 Jun 2021 21:52:43 +0300 Subject: [PATCH 2/2] lesson 4 homework --- .../main/java/ru/gb/savilin/lesson4/Main.java | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/lesson2/src/main/java/ru/gb/savilin/lesson4/Main.java b/lesson2/src/main/java/ru/gb/savilin/lesson4/Main.java index b79a5da..502171a 100644 --- a/lesson2/src/main/java/ru/gb/savilin/lesson4/Main.java +++ b/lesson2/src/main/java/ru/gb/savilin/lesson4/Main.java @@ -39,17 +39,38 @@ public static void main(String[] args) { System.out.println("Игра закончена"); } public static boolean checkWin(char symb) { - if(map[0][0] == symb && map[0][1] == symb && map[0][2] == symb) return true; - if(map[1][0] == symb && map[1][1] == symb && map[1][2] == symb) return true; - if (map[2][0] == symb && map[2][1] == symb && map[2][2] == symb) return true; - if (map[0][0] == symb && map[1][0] == symb && map[2][0] == symb) return true; - if (map[0][1] == symb && map[1][1] == symb && map[2][1] == symb) return true; - if (map[0][2] == symb && map[1][2] == symb && map[2][2] == symb) return - true; - if (map[0][0] == symb && map[1][1] == symb && map[2][2] == symb) return true; - if (map[2][0] == symb && map[1][1] == symb && map[0][2] == symb) return true; + boolean column = true; + boolean row = true; + if(checkDiagonal(symb)){ + return true; + }else{ + for (int i=0;i