Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package HW_XO.Lesson_4;
- import java.util.Random;
- import java.util.Scanner;
- public class Main {
- public static char[][] map;
- public static final int SIZE = 5;
- // public static final int DOTS_TO_WIN = 4;
- public static final char DOTS_EMPTY = '.';
- public static final char DOTS_X = 'X';
- public static final char DOTS_O = 'O';
- 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 (chekcWin(DOTS_X)) {
- System.out.println("Человек победил");
- break;
- }
- if (isMapFull()) {
- System.out.println("Ничья");
- break;
- }
- aiTurn();
- printMap();
- if (chekcWin(DOTS_O)) {
- System.out.println("Компьютер победил");
- break;
- }
- if (isMapFull()) {
- System.out.println("Ничья");
- break;
- }
- }
- System.out.println("Игра закончена");
- sc.close();
- }
- 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] = DOTS_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();
- }
- 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));
- map[y][x] = DOTS_X;
- }
- public static boolean isCellValid(int x, int y) {
- if (x < 0 || x >=SIZE || y < 0 || y >= SIZE) return false;
- if (map[y][x] == DOTS_EMPTY) return true;
- return false;
- }
- 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] = DOTS_O;
- }
- public static boolean chekcWin(char symb) {
- int col = 0;
- int row = 0;
- int dg = 0;
- int dp = 0;
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- if (map[i][j] == symb) {
- row++;
- }
- if (map[j][i] == symb) {
- col++;
- }
- if (i == j && map[i][j] == symb || (i == (j - 1)) && map[i][j] == symb || (i == (j + 1)) && map[i][j] == symb) {
- dg++;
- }
- if ((i + j) == (SIZE - 1) && map[i][j] == symb || ((i + j) == SIZE - 2) && map[i][j] == symb || (i + j) == (SIZE) && map[i][j] == symb) {
- dp++;
- }
- }
- if (col == (SIZE - 1) || row == (SIZE - 1) || dg == (SIZE - 1) || dp == (SIZE - 1)) {
- return true;
- } else col = 0; row = 0;
- }
- // 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] == DOTS_EMPTY) return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement