SabirSazzad

Tic Tac Toe(main class with new features)

Mar 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.85 KB | None | 0 0
  1. package tictactoefull;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. public class TicTacToeFULL {
  8.  
  9.  
  10.     public static void main(String[] args) {
  11.         int index=0,win=0,choose=0;
  12.         boolean done = false;
  13.         do{
  14.             try{
  15.                 System.out.println("For Single Player press 1");
  16.                 System.out.println("For Double Player press 2");
  17.                 System.out.print("Choice: ");
  18.                 Scanner input = new Scanner(System.in);
  19.                 choose = input.nextInt();
  20.                 if(choose==1 || choose==2){done = true;}
  21.                 else{
  22.                     printProblem();
  23.                 }
  24.             }catch(InputMismatchException e){
  25.                 printProblem();
  26.             }
  27.         }while(!done);
  28.         done = false;
  29.         Board board = new Board();      
  30.         Sign player1 = new Sign();
  31.         if(choose == 2){
  32.             Sign player2 = new Sign();
  33.             do{
  34.                 System.out.print("Input 1st Player Name or Sign: ");
  35.                 player1.setSign();            
  36.                 System.out.print("Input 2nd Player Name or Sign: ");
  37.                 player2.setSign();
  38.                 if(player1.getSign() == player2.getSign() || !player1.isDone() || !player2.isDone()){
  39.                     System.out.println("Sign Clash or Invalid Sign !!!");
  40.                 }
  41.             }while(player1.getSign() == player2.getSign() || !player1.isDone() || !player2.isDone());
  42.  
  43.             board.printBoard();
  44.             for(int i=0; i<9; i++){
  45.                 if(board.cheakWin()){
  46.                     if(cheakPlayer1(i)){
  47.                         win = 1;
  48.                         System.out.printf("%s Win the Game !!\n",player2.getPlayerName());
  49.                     }
  50.                     else{
  51.                         win = 1;
  52.                         System.out.printf("%s Win the Game !!\n",player1.getPlayerName());
  53.                     }
  54.                     break;
  55.                 }
  56.                 do{              
  57.                     if(cheakPlayer1(i)){
  58.                         System.out.printf("%s set %c in the board...\n",
  59.                             player1.getPlayerName(),player1.getSign());
  60.                     }
  61.                     else{
  62.                         System.out.printf("%s set %c in the board...\n",
  63.                             player2.getPlayerName(),player2.getSign());
  64.                     }
  65.                     do{
  66.                         try{
  67.                             System.out.print("Select [1 to 9]: ");
  68.                             Scanner input = new Scanner(System.in);
  69.                             index = input.nextInt();
  70.                             done = true;
  71.                         }catch(InputMismatchException e){
  72.                             printProblem();
  73.                         }
  74.                     }while(!done);
  75.                     done = false;
  76.  
  77.                 }while(!board.testValidity(index));
  78.                 if(cheakPlayer1(i)){
  79.                     board.setIndex(index, player1.getSign());
  80.                 }
  81.                 else{
  82.                     board.setIndex(index, player2.getSign());
  83.                 }
  84.                 board.printBoard();          
  85.             }
  86.             if(win==0 && !board.cheakWin() ){
  87.                 System.out.println();
  88.                 System.out.println("Game drow.... !!!");
  89.             }
  90.             else if (win != 1){
  91.                 System.out.printf("%s Win the Game !!\n",player1.getPlayerName());
  92.             }
  93.         }
  94.         //play with computer
  95.         else{
  96.             char computerSign;
  97.             int gameMode=0,attemp=0;
  98.             System.out.println("Two Modes are available Easy & Hard");
  99.             do{
  100.             try{
  101.                 System.out.println("For Easy Mode press 1");
  102.                 System.out.println("For Hard Mode press 2");
  103.                 System.out.print("Game Mode: ");
  104.                 Scanner input = new Scanner(System.in);
  105.                 gameMode = input.nextInt();
  106.                 if(gameMode==1 || gameMode==2){done = true;}
  107.                 else{
  108.                     printProblem();
  109.                 }
  110.             }catch(InputMismatchException e){
  111.                 printProblem();
  112.             }
  113.         }while(!done);
  114.         done = false;
  115.            
  116.             do{
  117.                 System.out.print("Input Your Name or Sign: ");
  118.                 player1.setSign();            
  119.                 if(!player1.isDone()){
  120.                     System.out.println("Invalid Sign !!!");
  121.                 }
  122.             }while(!player1.isDone());
  123.             //setting computer's sign
  124.             if(player1.getSign() == 'X'){
  125.                 computerSign = 'O';
  126.             }
  127.             else{
  128.                 computerSign = 'X';
  129.             }
  130.             System.out.println("Computer will play with sign: "+ computerSign);
  131.             board.printBoard();
  132.             for(int i=0; i<9; i++){
  133.                 if(board.cheakWin()){                    
  134.                     if(cheakPlayer1(i)){
  135.                         win = 1;
  136.                         System.out.println("Computer win... !!!");
  137.                     }
  138.                     else{
  139.                         win = 1;
  140.                         System.out.println("You win the Game... !!!");
  141.                     }
  142.                     break;
  143.                 }
  144.                 if(cheakPlayer1(i)){
  145.                     System.out.println("Place " + player1.getSign() + " in the Board");
  146.                     do{
  147.                         do{
  148.                             try{
  149.                                 System.out.print("Select [1 to 9]: ");
  150.                                 Scanner input = new Scanner(System.in);
  151.                                 index = input.nextInt();
  152.                                 done = true;
  153.                             }catch(InputMismatchException e){
  154.                                 printProblem();
  155.                             }
  156.                         }while(!done);
  157.                         done = false;
  158.                         if(!board.testValidity(index)){
  159.                             printProblem();
  160.                         }                    
  161.                     }while(!board.testValidity(index));
  162.                     board.setIndex(index, player1.getSign());                  
  163.                 }
  164.                 else{
  165.                     //computers turn calculation
  166.                     System.out.println("Computer's Turn...");
  167.                     Random generator = new Random();
  168.                     do{
  169.                         if(gameMode == 2 && attemp==0){                            
  170.                             index = board.setTarget(player1.getSign(),computerSign);
  171.                             attemp++;
  172.                         }
  173.                         else{
  174.                             index = generator.nextInt(10);
  175.                         }
  176.                     }while(!board.testValidity(index));
  177.                     attemp =0;
  178.                     System.out.println("Computer choose Index: "+index);
  179.                     board.setIndex(index, computerSign);
  180.                 }
  181.                 board.printBoard();                
  182.             }
  183.             if(win==0 && !board.cheakWin() ){
  184.                 System.out.println();
  185.                 System.out.println("Game drow.... !!!");
  186.             }
  187.             else if (win != 1){
  188.                 System.out.printf("Congratulation You Win the Game... !!");
  189.             }
  190.            
  191.         }
  192.        
  193.     }
  194.  
  195.     public static boolean cheakPlayer1 (int n){
  196.         if(n==0 || n==2 || n==4 || n==6 || n==8){
  197.             return true;
  198.         }
  199.         return false;
  200.     }
  201.     public static void printProblem(){
  202.         System.out.println("Wrong Input...!!!");
  203.         System.out.println("Input Again...!!!");
  204.     }
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment