|
| 1 | +package lesson11; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +/** |
| 7 | + * AIT-TR, cohort 42.1, Java Basic, hw #11 |
| 8 | + * |
| 9 | + * @author Sergey Iryupin |
| 10 | + * @version 09-Feb-24 |
| 11 | + */ |
| 12 | +public class HomeWork11 { |
| 13 | + static char[][] table = new char[3][3]; |
| 14 | + static Scanner scanner = new Scanner(System.in); |
| 15 | + static Random random = new Random(); |
| 16 | + static final char CHAR_EMPTY = '-'; |
| 17 | + static final char CHAR_X = 'x'; |
| 18 | + static final char CHAR_O = 'o'; |
| 19 | + |
| 20 | + public static void main(String[] args) { |
| 21 | + // init table (.) |
| 22 | + initTable(); |
| 23 | + // main game loop |
| 24 | + while (true) { |
| 25 | + // human turn (x) |
| 26 | + turnHuman(); |
| 27 | + // is human win? yes - game over |
| 28 | + if (isWin(CHAR_X)) { |
| 29 | + System.out.println("YOU WON!"); |
| 30 | + break; |
| 31 | + } |
| 32 | + // is table fill? yes - game over |
| 33 | + if (isTableFill()) { |
| 34 | + System.out.println("Sorry, DRAW!"); |
| 35 | + break; |
| 36 | + } |
| 37 | + // AI turn (o) |
| 38 | + turnAI(); |
| 39 | + // is AI win? yes - game over |
| 40 | + if (isWin(CHAR_O)) { |
| 41 | + System.out.println("AI WON!"); |
| 42 | + break; |
| 43 | + } |
| 44 | + // is table fill? yes - game over |
| 45 | + if (isTableFill()) { |
| 46 | + System.out.println("Sorry, DRAW"); |
| 47 | + break; |
| 48 | + } |
| 49 | + // print table |
| 50 | + printTable(); |
| 51 | + } |
| 52 | + // print table |
| 53 | + printTable(); |
| 54 | + } |
| 55 | + |
| 56 | + static void initTable() { |
| 57 | + for (int y = 0; y < 3; y++) { |
| 58 | + for (int x = 0; x < 3; x++) { |
| 59 | + table[y][x] = CHAR_EMPTY; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + static void printTable() { |
| 65 | + for (int y = 0; y < 3; y++) { |
| 66 | + for (int x = 0; x < 3; x++) { |
| 67 | + System.out.print(table[y][x] + " "); |
| 68 | + } |
| 69 | + System.out.println(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static void turnHuman() { |
| 74 | + int x, y; |
| 75 | + do { |
| 76 | + System.out.println("Enter x & y [0..2]:"); |
| 77 | + x = scanner.nextInt(); |
| 78 | + y = scanner.nextInt(); |
| 79 | + } while(!isCellValid(x, y)); |
| 80 | + table[y][x] = CHAR_X; |
| 81 | + } |
| 82 | + |
| 83 | + static void turnAI() { |
| 84 | + int x, y; |
| 85 | + do { |
| 86 | + x = random.nextInt(3); |
| 87 | + y = random.nextInt(3); |
| 88 | + } while(!isCellValid(x, y)); |
| 89 | + table[y][x] = CHAR_O; |
| 90 | + } |
| 91 | + |
| 92 | + static boolean isCellValid(int x, int y) { |
| 93 | + if (x < 0 || y < 0 || x > 2 || y > 2) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + return table[y][x] == CHAR_EMPTY; |
| 97 | + } |
| 98 | + |
| 99 | + static boolean isWin(char chr) { |
| 100 | + for (int i = 0; i < 3; i++) { |
| 101 | + if ((table[i][0] == chr && table[i][1] == chr && table[i][2] == chr) |
| 102 | + || (table[0][i] == chr && table[1][i] == chr && table[2][i] == chr)) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | +// // by x |
| 107 | +// if (table[0][0] == chr && table[0][1] == chr && table[0][2] == chr) return true; |
| 108 | +// if (table[1][0] == chr && table[1][1] == chr && table[1][2] == chr) return true; |
| 109 | +// if (table[2][0] == chr && table[2][1] == chr && table[2][2] == chr) return true; |
| 110 | +// // by y |
| 111 | +// if (table[0][0] == chr && table[1][0] == chr && table[2][0] == chr) return true; |
| 112 | +// if (table[0][1] == chr && table[1][1] == chr && table[2][1] == chr) return true; |
| 113 | +// if (table[0][2] == chr && table[1][2] == chr && table[2][2] == chr) return true; |
| 114 | + // diagonals |
| 115 | + if ((table[0][0] == chr && table[1][1] == chr && table[2][2] == chr) |
| 116 | + || (table[0][2] == chr && table[1][1] == chr && table[2][0] == chr)) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + static boolean isTableFill() { |
| 123 | + for (int y = 0; y < 3; y++) { |
| 124 | + for (int x = 0; x < 3; x++) { |
| 125 | + if (table[y][x] == CHAR_EMPTY) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return true; |
| 131 | + } |
| 132 | +} |
0 commit comments