Advertisement
mmouhib

Untitled

Jul 13th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. package com.mouhib;
  2.  
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) throws IOException, InterruptedException {
  9.  
  10.         long start = System.currentTimeMillis();
  11.         char[][] board = {
  12.                 {'1', '2', '3'},
  13.                 {'4', '5', '6'},
  14.                 {'7', '8', '9'}
  15.         };
  16.  
  17.         char symbol = getSymbol(), ComputerChoice;
  18.         if (symbol == 'X')
  19.             ComputerChoice = 'O';
  20.         else
  21.             ComputerChoice = 'X';
  22.  
  23.         while (true){
  24.             boardPrinter(board);
  25.             char choice = choice(board);
  26.             filler(board,choice,symbol);
  27.             if (Draw(board) || Winner(board,symbol) || Winner(board,ComputerChoice)) break;
  28.             char ComputerPos = ComputerChoice(board);
  29.             filler(board,ComputerPos,ComputerChoice);
  30.             System.out.println("\n*** Computer Choice: " + ComputerPos);
  31.             if (Draw(board) || Winner(board,symbol) || Winner(board,ComputerChoice)) break;
  32.         }
  33.  
  34.         boardPrinter(board);
  35.  
  36.         int winner = 0;
  37.         if (Draw(board))
  38.             winner = 2;
  39.         else if (Winner(board,symbol))
  40.             winner = 1;
  41.  
  42.         switch (winner) {
  43.             case 0 -> System.out.println("\nDETROIT BECOME HUMAN");
  44.             case 1 -> System.out.println("\nCongrats, you've won");
  45.             default -> System.out.println("\nDraw");
  46.         }
  47.  
  48.         System.out.print("\nRound Time: ");
  49.         System.out.println((System.currentTimeMillis() - start)/1000 + " Seconds");
  50.  
  51.     }
  52.  
  53.  
  54.     public static char getSymbol() {
  55.         System.out.println("Make your choice: (X|O)");
  56.         String symbol;
  57.         do {
  58.             Scanner scan = new Scanner(System.in);
  59.             symbol = scan.nextLine().toUpperCase();
  60.         } while (!symbol.equals("X") && !symbol.equals("O"));
  61.         return symbol.charAt(0);
  62.     }
  63.  
  64.     public static void boardPrinter(char[][] board) {
  65.         System.out.println("\n----+---+----");
  66.         for (char[] chars : board) {
  67.             System.out.print("| ");
  68.             for (char aChar : chars) {
  69.                 System.out.print(aChar + " | ");
  70.             }
  71.             System.out.println("\n----+---+----");
  72.         }
  73.     }
  74.  
  75.     public static boolean checker(char[][] board, char position) {
  76.         for (char[] chars : board) {
  77.             for (char aChar : chars) {
  78.                 if (aChar == position) {
  79.                     return true;
  80.                 }
  81.             }
  82.         }
  83.         return false;
  84.     }
  85.  
  86.     public static char choice(char[][] board) {
  87.         Scanner scan = new Scanner(System.in);
  88.         char choice;
  89.         do {
  90.             System.out.println("choice: ");
  91.             choice = scan.nextLine().charAt(0);
  92.         } while (!checker(board, choice) || (choice > '9' || choice < '1'));
  93.         return choice;
  94.     }
  95.  
  96.     public static void filler(char[][] board, char choice, char symbol) {
  97.         for (int i = 0; i < board.length; i++) {
  98.             for (int j = 0; j < board.length; j++) {
  99.                 if (board[i][j] == choice)
  100.                     board[i][j] = symbol;
  101.             }
  102.         }
  103.     }
  104.  
  105.     public static boolean Winner(char[][] board, char symbol) {
  106.         //straight horizontal
  107.         if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] == symbol) return true;
  108.         if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] == symbol) return true;
  109.         if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][2] == symbol) return true;
  110.         //straight vertical
  111.         if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[2][0] == symbol) return true;
  112.         if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[2][1] == symbol) return true;
  113.         if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[2][2] == symbol) return true;
  114.         //diagonal top left to bottom right
  115.         if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == symbol) return true;
  116.         //diagonal top right to bottom left
  117.         return board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] == symbol;
  118.     }
  119.  
  120.     public static boolean Draw(char[][] board) {
  121.         if (Winner(board, 'X') || Winner(board, 'O'))
  122.             return false;
  123.         for (char[] chars : board) {
  124.             for (char aChar : chars) {
  125.                 if (aChar >= '1' && aChar <= '9') {
  126.                     return false;
  127.                 }
  128.             }
  129.         }
  130.         return true;
  131.     }
  132.  
  133.     public static boolean SearchInArray(char[][] board, char value) {
  134.         for (char[] chars : board) {
  135.             for (char aChar : chars) {
  136.                 if (aChar == value)
  137.                     return true;
  138.             }
  139.         }
  140.         return false;
  141.     }
  142.  
  143.     public static char ComputerChoice(char[][] board) {
  144.         char pos;
  145.         do {
  146.             pos = Integer.toString((int) ((Math.random() * (9 - 1)) + 1)).charAt(0);
  147.             System.out.println(pos);
  148.         } while (!SearchInArray(board,pos));
  149.         return pos;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement