StormFalcon32

TicTacToe

Dec 4th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.33 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TicTacToe {
  4.  
  5.     public static void main(String[] args) throws InterruptedException {
  6.         // 0 is nothing, 1 is X, -1 is O
  7.         int[][] board = new int[3][3];
  8.         Scanner in = new Scanner(System.in);
  9.         System.out.println("Try to beat the AI!");
  10.         System.out.println();
  11.         System.out.println("Player is X");
  12.         System.out.println();
  13.         Thread.sleep(2000);
  14.         System.out.println("Type 1 to go first, 2 to go second");
  15.         int playerOrder = in.nextInt();
  16.         while (playerOrder != 2 && playerOrder != 1) {
  17.             System.out.println("Invalid num, type 1 to go first, 2 to go second");
  18.             playerOrder = in.nextInt();
  19.         }
  20.         if (playerOrder == 2) {
  21.             // Use minMax to calculate best AI move
  22.  
  23.             int bestScore = Integer.MAX_VALUE;
  24.             Move aiBestMove = new Move();
  25.  
  26.             for (int r = 0; r < 3; r++) {
  27.                 for (int c = 0; c < 3; c++) {
  28.                     // If empty cell
  29.                     if (board[r][c] == 0) {
  30.                         // Try current cell
  31.                         board[r][c] = -1;
  32.                         int currScore = minMax(board, 0, true, new Move(r, c));
  33.                         // Undo try
  34.                         board[r][c] = 0;
  35.                         if (currScore < bestScore) {
  36.                             aiBestMove.row = r;
  37.                             aiBestMove.col = c;
  38.                             bestScore = currScore;
  39.                         }
  40.                     }
  41.                 }
  42.             }
  43.             board[aiBestMove.row][aiBestMove.col] = -1;
  44.         }
  45.         while (isWinner(board) == 0) {
  46.             printBoard(board);
  47.             System.out.println();
  48.             System.out.println("Choose a numbered square");
  49.             int choice = in.nextInt() - 1;
  50.             int cChoice = choice % 3;
  51.             int rChoice = (choice - cChoice) / 3;
  52.             while (choice < 0 || choice > 8 || board[rChoice][cChoice] != 0) {
  53.                 System.out.println("Invalid num, choose another one");
  54.                 choice = in.nextInt() - 1;
  55.                 cChoice = choice % 3;
  56.                 rChoice = (choice - cChoice) / 3;
  57.             }
  58.             board[rChoice][cChoice] = 1;
  59.             System.out.println();
  60.  
  61.             // Use minMax to calculate best AI move
  62.  
  63.             int bestScore = Integer.MAX_VALUE;
  64.             Move aiBestMove = new Move();
  65.  
  66.             for (int r = 0; r < 3; r++) {
  67.                 for (int c = 0; c < 3; c++) {
  68.                     // If empty cell
  69.                     if (board[r][c] == 0) {
  70.                         // Try current cell
  71.                         board[r][c] = -1;
  72.                         int currScore = minMax(board, 0, true, new Move(r, c));
  73.                         // Undo try
  74.                         board[r][c] = 0;
  75.                         if (currScore < bestScore) {
  76.                             aiBestMove.row = r;
  77.                             aiBestMove.col = c;
  78.                             bestScore = currScore;
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.             if (aiBestMove.row != -1 && aiBestMove.col != -1) {
  84.                 board[aiBestMove.row][aiBestMove.col] = -1;
  85.             }
  86.         }
  87.         printBoard(board);
  88.         System.out.println((isWinner(board) == 1) ? "Player Wins!" : ((isWinner(board) == -1) ? "AI Wins!" : "Tie!"));
  89.         in.close();
  90.     }
  91.  
  92.     static int minMax(int[][] grid, int depth, boolean maximizer, Move root) {
  93.         int result = isWinner(grid);
  94.         if (result == 1) {
  95.             return 50 - depth;
  96.         } else if (result == -1) {
  97.             return -50 + depth;
  98.         } else if (result == -100) {
  99.             return 0;
  100.         }
  101.         if (maximizer) {
  102.             int max = Integer.MIN_VALUE;
  103.             for (int r = 0; r < 3; r++) {
  104.                 for (int c = 0; c < 3; c++) {
  105.                     // If empty cell
  106.                     if (grid[r][c] == 0) {
  107.                         grid[r][c] = 1;
  108.                         max = Math.max(max, minMax(grid, depth + 1, false, new Move(r, c)));
  109.                         // Undo try
  110.                         grid[r][c] = 0;
  111.                     }
  112.                 }
  113.             }
  114.             return max;
  115.         } else {
  116.             int min = Integer.MAX_VALUE;
  117.             for (int r = 0; r < 3; r++) {
  118.                 for (int c = 0; c < 3; c++) {
  119.                     // If empty cell
  120.                     if (grid[r][c] == 0) {
  121.                         grid[r][c] = -1;
  122.                         min = Math.min(min, minMax(grid, depth + 1, true, new Move(r, c)));
  123.                         // Undo try
  124.                         grid[r][c] = 0;
  125.                     }
  126.                 }
  127.             }
  128.             return min;
  129.         }
  130.     }
  131.  
  132.     static int isWinner(int[][] grid) {
  133.         int count = 0;
  134.         int val = 0;
  135.         // Check rows
  136.         for (int r = 0; r < 3; r++) {
  137.             count = 0;
  138.             val = 0;
  139.             for (int c = 0; c < 2; c++) {
  140.                 if (grid[r][c] == grid[r][c + 1] && grid[r][c] != 0) {
  141.                     val = grid[r][c];
  142.                     count++;
  143.                 }
  144.             }
  145.             if (count == 2) {
  146.                 return val;
  147.             }
  148.         }
  149.         // Check cols
  150.         for (int c = 0; c < 3; c++) {
  151.             count = 0;
  152.             val = 0;
  153.             for (int r = 0; r < 2; r++) {
  154.                 if (grid[r][c] == grid[r + 1][c] && grid[r][c] != 0) {
  155.                     val = grid[r][c];
  156.                     count++;
  157.                 }
  158.             }
  159.             if (count == 2) {
  160.                 return val;
  161.             }
  162.         }
  163.         // Check diagonals
  164.         count = 0;
  165.         val = 0;
  166.         for (int i = 0; i < 2; i++) {
  167.             if (grid[i][i] == grid[i + 1][i + 1] && grid[i][i] != 0) {
  168.                 val = grid[i][i];
  169.                 count++;
  170.             }
  171.         }
  172.         if (count == 2) {
  173.             return val;
  174.         }
  175.         count = 0;
  176.         val = 0;
  177.         for (int i = 1; i < 3; i++) {
  178.             if (grid[i][2 - i] == grid[i - 1][3 - i] && grid[i][2 - i] != 0) {
  179.                 val = grid[i][2 - i];
  180.                 count++;
  181.             }
  182.         }
  183.         if (count == 2) {
  184.             return val;
  185.         }
  186.         // Check for tie
  187.         for (int r = 0; r < 3; r++) {
  188.             for (int c = 0; c < 3; c++) {
  189.                 if (grid[r][c] == 0) {
  190.                     return 0;
  191.                 }
  192.             }
  193.         }
  194.         return -100;
  195.     }
  196.  
  197.     static void printBoard(int[][] grid) {
  198.         for (int r = 0; r < 3; r++) {
  199.             for (int c = 0; c < 3; c++) {
  200.                 int x = grid[r][c];
  201.                 System.out.print(((x == 1) ? "X" : (x == -1) ? "O" : ((r * grid.length) + c) + 1));
  202.                 if (c != grid[r].length - 1) {
  203.                     System.out.print("|");
  204.                 }
  205.             }
  206.             System.out.println();
  207.             if (r != grid.length - 1) {
  208.                 System.out.println("-----");
  209.             }
  210.         }
  211.         System.out.println();
  212.     }
  213.  
  214.     static class Move {
  215.         int row = -1;
  216.         int col = -1;
  217.  
  218.         public Move() {
  219.  
  220.         }
  221.  
  222.         public Move(int a, int b) {
  223.             row = a;
  224.             col = b;
  225.         }
  226.     }
  227. }
Add Comment
Please, Sign In to add comment