adeen-s

TicTacToe.java

Oct 1st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import java.util.*;
  2. public class TicTacToe {
  3.   int arr[][] = new int[3][3];
  4.   //0 for empty, 1 for X and 2 for O
  5.   public static void main(String args[]) {
  6.     TicTacToe game = new TicTacToe();
  7.     while(true) {
  8.       game.display();
  9.       game.check();
  10.       game.input(1);
  11.       game.display();
  12.       game.check();
  13.       game.input(2);
  14.     }
  15.   }
  16.  
  17.   public TicTacToe() {
  18.     for(int i = 0; i < 3; i++) {
  19.       for(int j = 0; j < 3; j++) {
  20.         arr[i][j] = 0;
  21.       }
  22.     }
  23.   }
  24.  
  25.   public void input(int plyr_id) {
  26.     Scanner sc = new Scanner (System.in);
  27.     int i = -1, j = -1;
  28.     System.out.println("Enter the coordinates player " + plyr_id);
  29.     try {
  30.       i = sc.nextInt();
  31.       j = sc.nextInt();
  32.       if(isValid(i,j))
  33.         arr[i][j] = plyr_id;
  34.       else {
  35.         System.out.println("Invalid Input, Try Again !!");
  36.         input(plyr_id);
  37.       }
  38.     } catch(InputMismatchException e) {
  39.       System.out.println("Invalid input, only integers are allowed. Try Again !!");
  40.       input(plyr_id);
  41.     }
  42.   }
  43.  
  44.   public boolean gameOver() {
  45.     for(int i = 0; i < 3; i++) {
  46.       for(int j = 0; j < 3; j++) {
  47.         if(arr[i][j] == 0)
  48.           return false;
  49.       }
  50.     }
  51.     return true;
  52.   }
  53.  
  54.   public boolean isValid(int i, int j) {
  55.     return (i < 3 && j < 3 && i >= 0 && j >=0 && arr[i][j]==0);
  56.   }
  57.  
  58.   public void display() {
  59.     for(int i = 0; i < 3; i++) {
  60.       for(int j = 0; j < 3; j++) {
  61.         if(arr[i][j] == 0)
  62.           System.out.print("|   |");
  63.         if (arr[i][j] == 1)
  64.           System.out.print("| X |");
  65.         if(arr[i][j] == 2)
  66.           System.out.print("| O |");
  67.       }
  68.       if(i != 2)
  69.         System.out.print("\n---------------\n");
  70.       else
  71.         System.out.println();
  72.     }
  73.   }
  74.  
  75.   public void check() {
  76.     for(int i = 0 ; i < 3; i++) {
  77.       //Checking Horizontal win conditions
  78.       if(arr[i][0] == arr[i][1] && arr[i][0] == arr[i][2]) {
  79.         if(arr[i][0] == 1) {
  80.           System.out.println("X Won the match");
  81.           System.exit(0);
  82.         } else if(arr[i][0] == 2) {
  83.           System.out.println("O won the match");
  84.           System.exit(0);
  85.         }
  86.       }
  87.       //Checking Vertical win conditions
  88.       if(arr[0][i] == arr[1][i] && arr[0][i] == arr[2][i]) {
  89.         if(arr[0][i] == 1) {
  90.           System.out.println("X Won the match");
  91.           System.exit(0);
  92.         } else if(arr[0][i] == 2) {
  93.           System.out.println("O won the match");
  94.           System.exit(0);
  95.         }
  96.       }
  97.       //Checking Diagonally 1
  98.       if(arr[0][0] == arr[1][1] && arr[0][0] == arr[2][2]) {
  99.         if(arr[0][0] == 1) {
  100.           System.out.println("X Won the match");
  101.           System.exit(0);
  102.         } else if(arr[0][0] == 2) {
  103.           System.out.println("O won the match");
  104.           System.exit(0);
  105.         }
  106.       }
  107.       //Checking Diagonally 2
  108.       if(arr[0][2] == arr[1][1] && arr[0][2] == arr[2][0]) {
  109.         if(arr[0][2] == 1) {
  110.           System.out.println("X Won the match");
  111.           System.exit(0);
  112.         } else if(arr[0][2] == 2) {
  113.           System.out.println("O won the match");
  114.           System.exit(0);
  115.         }
  116.       }
  117.     }
  118.     if(gameOver()) {
  119.       System.out.println("Its a tie");
  120.       System.exit(0);
  121.     }
  122.   }
  123. }
Add Comment
Please, Sign In to add comment