Advertisement
keevosp

Simple Tic-Tac-Toe (Java)

Jun 6th, 2023
744
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | Source Code | 0 0
  1. package tictactoe;
  2. import java.util.InputMismatchException;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     static boolean isGameRunning = true;
  7.    
  8.     public static void main(String[] args) {
  9.         // write your code here
  10.         Grid grid = new Grid();
  11.         grid.fillGrid();
  12.         grid.showGrid();
  13.         while (isGameRunning) {
  14.             grid.makeMove();
  15.         }
  16.     }
  17. }
  18.  
  19. class Grid{
  20.    
  21.     public static final char EMPTY_CELL = ' ';
  22.     public static final char O_CELL = 'O';
  23.     public static final char X_CELL = 'X';
  24.     public static int moveCounter = 0;
  25.     static String input;
  26.    
  27.     private char[][] field = new char[3][3];
  28. //    Scanner s = new Scanner(System.in);
  29.     void fillGrid () {
  30.         for (int i = 0; i < field.length; i++) {
  31.             for (int j = 0; j < field[i].length; j++)
  32.                 field[i][j] = Grid.EMPTY_CELL;
  33.         }
  34.     }
  35.    
  36.     public void makeMove () {
  37.         Scanner s = new Scanner(System.in);
  38.         try {
  39.             int x = s.nextInt();
  40.                 int y = s.nextInt();
  41.                 x--;
  42.                 y--;
  43.                 if (EMPTY_CELL == field[x][y] && moveCounter % 2 == 0) {
  44.                     field[x][y] = Grid.X_CELL;
  45.                     showGrid();
  46.                     moveCounter++;
  47.                     checkGameState();
  48.                 } else if (EMPTY_CELL == field[x][y] && moveCounter % 2 != 0) {
  49.                     field[x][y] = Grid.O_CELL;
  50.                     showGrid();
  51.                     moveCounter++;
  52.                     checkGameState();
  53.                 } else if (EMPTY_CELL != field[x][y]) {
  54.                     System.out.println("This cell is occupied! Choose another one!");
  55.                 }
  56.             } catch (InputMismatchException e) {
  57.                 System.out.println("You should enter numbers!");
  58.             } catch (IndexOutOfBoundsException e) {
  59.                 System.out.println("Coordinates should be from 1 to 3!");
  60.             }
  61.     }
  62.    
  63.     void showGrid () {
  64.         System.out.print("---------\n" +
  65.                          "| " + field[0][0] + " " + field[0][1] + " " + field[0][2] + " |\n" +
  66.                          "| " + field[1][0] + " " + field[1][1] + " " + field[1][2] + " |\n" +
  67.                          "| " + field[2][0] + " " + field[2][1] + " " + field[2][2] + " |\n" +
  68.                          "---------\n");
  69.     }
  70.    
  71.    
  72.     private boolean isPlayerWin (char player) {
  73.         return (field[0][0] == player && field[1][1] == player && field[2][2] == player) ||
  74.                 ((field[0][2] == player && field[1][1] == player && field[2][0] == player) ||
  75.            
  76.             (field[0][0] == player && field[1][0] == player && field[2][0] == player) ||
  77.             (field[0][1] == player && field[1][1] == player && field[2][1] == player) ||
  78.             (field[0][2] == player && field[1][2] == player && field[2][2] == player) ||
  79.            
  80.             (field[0][0] == player && field[0][1] == player && field[0][2] == player) ||
  81.             (field[1][0] == player && field[1][1] == player && field[1][2] == player) ||
  82.             (field[2][0] == player && field[2][1] == player && field[2][2] == player));
  83.     }
  84.    
  85.    
  86.     public void checkGameState() {
  87.        
  88.         int xCnt = 0;
  89.         int oCnt = 0;
  90.        
  91.         for (int i = 0; i < field.length; i++) {
  92.             for (int j = 0; j < field[i].length; j++) {
  93.                 if (field[i][j] == X_CELL) {
  94.                     xCnt++;
  95.                 } else if (field[i][j] == Grid.O_CELL) {
  96.                     oCnt++;
  97.                 }
  98.             }
  99.         }
  100.         int moveCnt = xCnt + oCnt;
  101.         boolean isImpossQntDiff = xCnt > oCnt + 1 || xCnt < oCnt - 1 || oCnt > xCnt + 1 || oCnt < xCnt - 1;
  102.        
  103.         char player = (moveCounter - 1) % 2 == 0 ? 'X' : 'O';
  104.         for (;;) {
  105.             if (isPlayerWin(player)) {
  106.                 System.out.println(player + " wins");
  107.                 Main.isGameRunning = false;
  108.                 return;
  109.             } else if (isPlayerWin(player) || isImpossQntDiff) {
  110.                 System.out.println("Impossible");
  111.                 return;
  112.             } else if (9 == moveCnt){
  113.                 System.out.println("Draw");
  114.                 Main.isGameRunning = false;
  115.                 return;
  116.             } else {
  117.                 return;
  118.             }
  119.         }
  120.     }
  121. }
  122. //    public void gameStartInput () {
  123. //        final Scanner s = new Scanner(System.in);
  124. //        Grid.input = s.nextLine().replaceAll("_", " ");
  125. //        int counter = 0;
  126. //        for (int i = 0; i < field.length; i++) {
  127. //            for (int j = 0; j < field[i].length; j++) {
  128. //                field[i][j] = Grid.input.charAt(counter);
  129. //                counter++;
  130. //            }
  131. //        }
  132. //        showGrid();
  133. //        checkGameState();
  134. //    }
Advertisement
Comments
  • keevosp
    334 days (edited)
    # text 0.27 KB | 0 0
    1. https://hyperskill.org/projects/48?track=8
    2. Learning outcomes
    3. After finishing this project, you'll get to know a lot about planning and developing a complex program from scratch, using methods, nested lists, list comprehension, handling errors, and processing user input.
    4.  
    5.  
Add Comment
Please, Sign In to add comment
Advertisement