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..502171a --- /dev/null +++ b/lesson2/src/main/java/ru/gb/savilin/lesson4/Main.java @@ -0,0 +1,127 @@ +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) { + boolean column = true; + boolean row = true; + if(checkDiagonal(symb)){ + return true; + }else{ + for (int i=0;i= 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