manish

Tic Tac Toe by manish

Nov 14th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.90 KB | None | 0 0
  1. /*
  2. Tic Tac Toe Game by manish
  3. Created on request for Lucifer
  4. */
  5. import java.util.*;
  6. public class TicTacToe {
  7.   static int size,turn=0,moves=0,win=0;
  8.   static String cur,reason="",players[] = new String[2];
  9.   static String board[][] = new String[size][size];
  10.   static String symbol[] = {"X","O"};
  11.   static boolean curb;
  12.  public static void main(String s[])
  13.  {
  14.      String players[] = new String[2];
  15.      Scanner sc = new Scanner(System.in);
  16.      System.out.println("   * * *            Welcome to Tic Tac Toe!  * * *    ");
  17.      System.out.println("Enter the size of the board. The size cannot be greater than 6 or smaller than 3.");
  18.      size = sc.nextInt();
  19.      if (size <= 2 || size >= 7)
  20.      {
  21.          System.out.println("The size you have entered is out of range.");
  22.                  return;
  23.      }
  24.      else {
  25.      board = new String[size][size];
  26.      fill();
  27.      input();
  28.      display();
  29.      turn();
  30.              }
  31.  }
  32.  static void input()
  33.  {
  34.      System.out.println("  Enter the names of the two players to begin.");
  35.      players[0] = new Scanner(System.in).nextLine();
  36.      players[1]= new Scanner(System.in).nextLine();
  37.  }
  38.  static void fill()
  39.  {
  40.      for(int i=0; i<size; i++)
  41.      {
  42.          for(int j=0; j<size; j++)
  43.          {
  44.              board[i][j] = " ";
  45.            
  46.          }
  47.      }
  48.  }
  49.      static void display() {
  50.         String line = "|-------";
  51.         for (int i = 0; i < size+1; i++) {
  52.             for (int j = 0; j < size; j++) {
  53.                 System.out.print(line);
  54.             }
  55.             System.out.println("|");
  56.             if (i < size) {
  57.                 System.out.print("    ");
  58.                 for (int j = 0; j < size; j++) {
  59.                     System.out.print(board[i][j] + "         ");
  60.                 }
  61.             }
  62.             System.out.println();
  63.         }
  64.         System.out.println("");
  65.     }
  66.  static void turn()
  67.  {
  68.      System.out.println("( "+players[turn]+" ) :- Please make a move.(eg:2x2 or 2*2)");
  69.      String x = new Scanner(System.in).nextLine();
  70.      x = x.replace(" ","");
  71.      if (validate(x) == true)
  72.      {
  73.          int a = Character.getNumericValue(x.charAt(0));
  74.          int b = Character.getNumericValue(x.charAt(2));
  75.       if ( a > size || b > size || a < 1 || b < 1)
  76.       {
  77.           System.out.println("Input is out of bounds or invalid! Try again!");
  78.           turn();
  79.       }
  80.       else if (board[a-1][b-1].compareToIgnoreCase(" ") != 0)
  81.       {
  82.           System.out.println("The position you selected is already taken. Please try again.");
  83.           turn();
  84.       }
  85.       else
  86.       {
  87.           board[a-1][b-1] = symbol[turn];
  88.           verify();
  89.       }
  90.          
  91.      }
  92.      else
  93.      {
  94.          System.out.println("Invalid input! Try again.");
  95.          turn();
  96.      }
  97.  }
  98.  static boolean validate(String p)
  99.  {
  100.      if (p.length() != 3)
  101.      {
  102.          return false;
  103.      }
  104.      else if (p.charAt(1) != '*' && p.charAt(1) != 'x')
  105.      {
  106.          return false;
  107.      }
  108.      else if (!Character.isDigit(p.charAt(0)) || !Character.isDigit(p.charAt(2)))
  109.              {
  110.                  return false;
  111.              }
  112.  return true;
  113.  }
  114.  static void verify()
  115.  {
  116.      int c=0,cmax=0;
  117.      for(int i=0; i<size; i++)
  118.      {
  119.          c=0;
  120.        if (board[i][0].compareToIgnoreCase(" ") != 0) {  cur = board[i][0]; }
  121.        else { cur = "."; }
  122.         curb = true;
  123.          for(int j=0; j<size; j++)
  124.          {
  125.              if (cur.compareToIgnoreCase(" ") == 0) { cur = "."; }
  126.              if(board[i][j].compareToIgnoreCase(cur) != 0)
  127.              {
  128.              curb = false;
  129.              cur = board[i][j];
  130.              if (c >= cmax) { cmax = c; }
  131.              c = 1;
  132.              }
  133.              else
  134.             ++c;
  135.          }
  136.          if (c >= cmax) { cmax = c; }
  137.          if (curb == true || cmax >= 3)
  138.          {
  139.              cmax = 0;
  140.              win = 1;
  141.              reason = reason + " " + "(Three In A Row)";
  142.          }
  143.      }
  144.      c=0;
  145.      cmax = 0;
  146.  for(int i=0; i<size; i++)
  147.  {
  148.      c=0;
  149.      if (board[0][i].compareToIgnoreCase(" ") != 0) { cur = board[0][i]; }
  150.      else { cur = "."; }
  151.         curb = true;
  152.      for(int j=0; j<size; j++)
  153.      {
  154.          if (cur.compareToIgnoreCase(" ") == 0) { cur = "."; }
  155.          if(board[j][i].compareToIgnoreCase(cur) != 0)
  156.          {
  157.              cur = board[j][i];
  158.              curb = false;
  159.              if (c >= cmax ) { cmax = c; }
  160.              c = 1;
  161.          }
  162.          else
  163.              ++c;
  164.      }
  165.      if (c >= cmax) { cmax = c; }
  166.      if (curb == true || cmax >= 3)
  167.          {
  168.              cmax=0;
  169.              win = 1;
  170.              reason = reason + " " + "(Three In A Column)";
  171.          }
  172.  }
  173.  c = 0;
  174.  cmax = 0;
  175.  if (board[0][0].compareToIgnoreCase(" ") != 0) { cur = board[0][0]; }
  176.  else { cur = "."; }
  177.  curb = true;
  178.  for(int i=0; i<size; i++)
  179.  {
  180.      if (cur.compareToIgnoreCase(" ") == 0) { cur = "."; }
  181.      if(board[i][i].compareToIgnoreCase(cur) != 0)
  182.      {
  183.          cur = board[i][i];
  184.          curb = false;
  185.          if (c >= cmax ) { cmax = c; }
  186.          c=1;
  187.      }
  188.      else
  189.          ++c;
  190.  }
  191.  if (c >= cmax) { cmax = c; }
  192.  if (curb == true || cmax >= 3)
  193.          {
  194.              cmax=0;
  195.              win = 1;
  196.              reason = reason + " " + "(Three In A Diagonal)";
  197.          }
  198.  c = 0;
  199.  cmax = 0;
  200.  if (board[0][size-1].compareToIgnoreCase(" ") != 0) { cur = board[0][size-1]; }
  201.  else { cur = "."; }
  202.  curb = true;
  203.  for(int i=0; i<size; i++)
  204.  {
  205.      if (cur.compareToIgnoreCase(" ") == 0) { cur = "."; }
  206.      if (board[i][size-1-i].compareToIgnoreCase(cur) != 0)
  207.      {
  208.          cur = board[i][size-1-i];
  209.          curb = false;
  210.          if (c >= cmax ) { cmax = c; }
  211.          c=1;
  212.      }
  213.      else
  214.          ++c;
  215.  }
  216.  if (c >= cmax) { cmax = c; }
  217.  if (curb == true || cmax >= 3)
  218.          {
  219.              cmax = 0;
  220.              win = 1;
  221.              reason = reason + " " + "(Three In A Diagonal)";
  222.          }
  223.  if (win == 1)
  224.  {
  225.      display();
  226.     System.out.println("So we have a winner now! Congratulations, "+players[turn]+ " has won the game!! "+reason);
  227.     replay();
  228.  }
  229.  else {
  230.      ++moves;
  231.      if (moves == size * size)
  232.      {
  233.          System.out.println(" Nobody won! Nobody lost! Congratulations, it's a draw!");
  234.          replay();
  235.      }
  236.      else
  237.      {
  238.          ++turn;
  239.          if (turn == 2)
  240.          {
  241.              turn = 0;
  242.          }
  243.          display();
  244.          turn();
  245.      }
  246.  }
  247.  }
  248.  static void replay()
  249.  {
  250.      System.out.println("To replay the game with the same board, type R. Otherwise, type anything else to exit.");
  251.      String rep = new Scanner(System.in).nextLine();
  252.      if (rep.compareToIgnoreCase("R") != 0)
  253.          return;
  254.      else
  255.      {
  256.          fill();
  257.          turn = 0;
  258.          moves = 0;
  259.          win = 0;
  260.          reason = "";
  261.          curb = false;
  262.          display();
  263.          turn();
  264.      }
  265.  }
  266.  }
Add Comment
Please, Sign In to add comment