Advertisement
LegendSujay2019

tictactoe game

Aug 24th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package sujay;
  2. import java.util.Scanner;
  3. public class sujay
  4. {
  5.    public static final int EMPTY = 0;
  6.    public static final int CROSS = 1;
  7.    public static final int NOUGHT = 2;
  8.    public static final int PLAYING = 0;
  9.    public static final int DRAW = 1;4;
  10.    public static final int CROSS_WON = 2;
  11.    public static final int NOUGHT_WON = 3;
  12.    public static final int ROWS = 3, COLS = 3;
  13.    public static int[][] board = new int[ROWS][COLS];
  14.    public static int currentState;
  15.    public static int currentPlayer;
  16.    public static int currntRow, currentCol;
  17.    public static Scanner in = new Scanner(System.in);public static void main(String[] args) {
  18.       initGame();
  19.             do {
  20.          playerMove(currentPlayer);
  21.          updateGame(currentPlayer, currntRow, currentCol);
  22.          printBoard();
  23.          if (currentState == CROSS_WON) {
  24.             System.out.println("'X' won! Bye!");
  25.          } else if (currentState == NOUGHT_WON) {
  26.             System.out.println("'O' won! Bye!");
  27.          } else if (currentState == DRAW) {
  28.             System.out.println("It's a Draw! Bye!");
  29.          }
  30.          currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
  31.       } while (currentState == PLAYING);
  32.    }
  33.    public static void initGame() {
  34.       for (int row = 0; row < ROWS; ++row) {
  35.          for (int col = 0; col < COLS; ++col) {
  36.             board[row][col] = EMPTY;
  37.          }
  38.       }
  39.       currentState = PLAYING;
  40.       currentPlayer = CROSS;
  41.    }
  42.    public static void playerMove(int theSeed) {
  43.       boolean validInput = false;  // for input validation
  44.       do {
  45.          if (theSeed == CROSS) {
  46.             System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
  47.          } else {
  48.             System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
  49.          }
  50.          int row = in.nextInt() - 1;
  51.          int col = in.nextInt() - 1;
  52.          if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
  53.             currntRow = row;
  54.             currentCol = col;
  55.             board[currntRow][currentCol] = theSeed;  
  56.             validInput = true;
  57.          } else {
  58.             System.out.println("This move at (" + (row + 1) + "," + (col + 1)
  59.                   + ") is not valid. Try again...");
  60.          }
  61.       } while (!validInput);
  62.    }
  63.  public static void updateGame(int theSeed, int currentRow, int currentCol) {
  64.       if (hasWon(theSeed, currentRow, currentCol)) {  // check if winning move
  65.          currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;
  66.       } else if (isDraw()) {  
  67.          currentState = DRAW;
  68.       }
  69.    }
  70.    public static boolean isDraw() {
  71.       for (int row = 0; row < ROWS; ++row) {
  72.          for (int col = 0; col < COLS; ++col) {
  73.             if (board[row][col] == EMPTY) {
  74.                return false;
  75.             }
  76.          }
  77.       }
  78.       return true;
  79.    }
  80.    public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
  81.       return (board[currentRow][0] == theSeed  
  82.                    && board[currentRow][1] == theSeed
  83.                    && board[currentRow][2] == theSeed
  84.               || board[0][currentCol] == theSeed
  85.                    && board[1][currentCol] == theSeed
  86.                    && board[2][currentCol] == theSeed
  87.               || currentRow == currentCol        
  88.                    && board[0][0] == theSeed
  89.                    && board[1][1] == theSeed
  90.                    && board[2][2] == theSeed
  91.               || currentRow + currentCol == 2  
  92.                    && board[0][2] == theSeed
  93.                    && board[1][1] == theSeed
  94.                    && board[2][0] == theSeed);
  95.    }
  96.    public static void printBoard() {
  97.       for (int row = 0; row < ROWS; ++row) {
  98.          for (int col = 0; col < COLS; ++col) {
  99.             printCell(board[row][col]);
  100.             if (col != COLS - 1) {
  101.                System.out.print("|");  
  102.             }
  103.          }
  104.          System.out.println();
  105.          if (row != ROWS - 1) {
  106.             System.out.println("-----------");
  107.          }
  108.       }
  109.       System.out.println();
  110.    }
  111.    public static void printCell(int content) {
  112.       switch (content) {
  113.          case EMPTY:  System.out.print("   "); break;
  114.          case NOUGHT: System.out.print(" O "); break;
  115.          case CROSS:  System.out.print(" X "); break;
  116.       }
  117.    }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement