Mulla Suhel Faiyyaj

    0

    0

    Ninad Bhosale_2114

    import java.util.Random;
    import java.util.Scanner;
    
    public class tictaktoe {
      private static void printBoard(char[][] board) {
        System.out.println(board[0][0]+"|"+ board[0][1]+"|"+board[0][2]);
        System.out.println("-+-+-");
        System.out.println(board[1][0]+"|"+ board[1][1]+"|"+board[1][2]);
        System.out.println("-+-+-");
        System.out.println(board[2][0]+"|"+ board[2][1]+"|"+board[2][2]);
      }
      private static void playerMove(char[][] board, String userInput, char Move) {
        switch(userInput)
        {
          case "1":
            board[0][0]=Move;
            break;
          case "2":
            board[0][1]=Move;
            break;
          case "3":
            board[0][2]=Move;
            break;
          case "4":
            board[1][0]=Move;
            break;   
          case "5":
            board[1][1]=Move;
            break;
          case "6":
            board[1][2]=Move;
            break;
          case "7":
            board[2][0]=Move;
            break;
          case "8":
            board[2][1]=Move;
            break;
          case "9":
            board[2][2]=Move;
            break;
          default:
          System.out.println("wrong  option");
    
        }
      }
      private static boolean isValidMove(char[][] board, String computerMove) {
        switch(computerMove)
        {
          case "1":
          return (board[0][0]==' ');
    
          case "2":
          return (board[0][1]==' ');
    
          case "3":
          return ( board[0][2]==' ');
    
          case "4":
          return (board[1][0]==' ');
    
          case "5":
          return ( board[1][1]==' ');
    
          case "6":
          return ( board[1][2]==' ');
    
          case "7":
          return ( board[2][0]==' ');
    
          case "8":
          return ( board[2][1]==' ');
    
          case "9":
          return ( board[2][2]==' ');  
          default:
          System.out.println(computerMove +" is  Not valid move");
          return false;
        }
    
      } 
    
      private static boolean moveLeft(char[][] board) {
        if(winCheck(board,'X' )){
          printBoard(board);
          System.out.println("player wins.....");
          return true;
        }
        if(winCheck(board,'O' )){
          printBoard(board);
          System.out.println("computer wins.....");
          return true;
        }
        for (int i=0;i<board.length;i++){
          for (int j=0;j<board[i].length;j++)
          {
            if (board[i][j]==' ')
            {
              return false;
            }
          }
        }
        printBoard(board);
        System.out.println("Match is tie !....");
        return true;
      }
      private static boolean winCheck(char[][] board, char Move) {
        if ((board[0][0]==Move && board[0][1]==Move && board[0][2]==Move) ||
        (board[1][0]==Move && board[1][1]==Move && board[1][2]==Move) ||
        (board[2][0]==Move && board[2][1]==Move && board[2][2]==Move) 
        ||
        (board[0][0]==Move && board[1][0]==Move && board[2][0]==Move)||
        (board[0][1]==Move && board[1][1]==Move && board[2][1]==Move)||
        (board[0][2]==Move && board[1][2]==Move && board[2][2]==Move)
        ||
        (board[0][0]==Move && board[1][1]==Move && board[2][2]==Move)||
        (board[0][2]==Move && board[1][1]==Move && board[2][0]==Move))
        {
          return true;
        }
        return false;
      }
      private static void computerTurn(char[][] board) {
        Random rand =new Random();
        int computerMove;
        while(true)
        {
          computerMove = rand.nextInt(10); 
          if(isValidMove(board, Integer.toString(computerMove)))
            {
              playerMove(board, Integer.toString(computerMove), 'O');
              break;
            }
        }
        System.out.println("Computer chose " + computerMove);
      }
      private static void playerTurn(char[][] board, Scanner scanner) {
        String userInput;
        while(true)
        {   
          System.out.println("where do you want to play ?");
          userInput = scanner.nextLine();
          if(isValidMove(board, userInput))
          {
            break;
          }
          System.out.println("This is Not valid Move");
        }
        playerMove(board, userInput,'X');
      }
      public static void main(String [] args) {
        Scanner scanner=new Scanner(System.in);
        char [][] board={
                {' ',' ',' '},
                {' ',' ',' '},
                {' ',' ',' '},
                };
        printBoard(board);
          //to take input from user
        while(true)
        {
          playerTurn(board,scanner);
          if(moveLeft(board)){
            break;
          }
          printBoard(board);
          computerTurn(board);
          if(moveLeft(board)){
            break;
          }
          printBoard(board);
        }
      }
    
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.