Advertisement
TheRightGuy

Tic Tac Toe Review

Sep 1st, 2022
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.91 KB | None | 0 0
  1. public class App {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         TicTacToe game = new TicTacToe();
  6.         game.start();
  7.     }
  8. }
  9. -----------------------------------------------
  10. enum Cell {
  11.     HUMAN("X"),
  12.     COMPUTER("O");
  13.  
  14.     private final String symbol;
  15.  
  16.     Cell(String symbol) {
  17.         this.symbol = symbol;
  18.     }
  19.  
  20.     String getSymbol() {
  21.         return symbol;
  22.     }
  23. }
  24.  
  25. enum State {
  26.     HUMAN_WINNER,
  27.     COMPUTER_WINNER,
  28.     TIE
  29. }
  30.  
  31. public class TicTacToe extends Winner {
  32.  
  33.     private final Scanner input = new Scanner(System.in);
  34.  
  35.     public TicTacToe() {
  36.         displayGrid();
  37.     }
  38.  
  39.     public void start() {
  40.         game();
  41.     }
  42.  
  43.     private void game() {
  44.         while( true ) {
  45.             runHumansTurn();
  46.             if ( isGameDraw() ) {
  47.                 System.out.println(State.TIE);
  48.                 return;
  49.             }
  50.             if ( hasWinner() ) {
  51.                 break;
  52.             }
  53.             runComputerTurn();
  54.             if ( hasWinner() ) {
  55.                 break;
  56.             }
  57.             displayGrid();
  58.         }
  59.         displayGrid();
  60.         getWinner();
  61.     }
  62.  
  63.     private boolean isGameDraw() {
  64.         for ( Cell[] players: getGrid() ) {
  65.             for ( int i = 0; i < getGrid().length; i++ ) {
  66.                 if ( players[i] == null || players[i].getSymbol().equals(" ") || isWinner(Cell.HUMAN) ) {
  67.                     return false;
  68.                 }
  69.             }
  70.         }
  71.         return true;
  72.     }
  73.  
  74.     private void runHumansTurn() {
  75.         System.out.println("Pick a number from 1 - 9 ");
  76.         int humanSelectedPosition = input.nextInt();
  77.         while( isCellTaken(getCellPositioning(humanSelectedPosition)) || isRightInput(humanSelectedPosition) ) {
  78.             System.out.println("Try again ");
  79.             humanSelectedPosition = input.nextInt();
  80.         }
  81.         getGrid()[(humanSelectedPosition - 1) / 3][(humanSelectedPosition - 1) % 3] = Cell.HUMAN;
  82.     }
  83.  
  84.     private Cell getCellPositioning(int humanSelectedPosition) {
  85.         return getGrid()[(humanSelectedPosition - 1) / 3][(humanSelectedPosition - 1) % 3];
  86.     }
  87.  
  88.     private boolean isRightInput(int humanSelectedPosition) {
  89.         return humanSelectedPosition > 9 || humanSelectedPosition < 1;
  90.     }
  91.  
  92.     private void displayGrid() {
  93.         for ( Cell[] players: getGrid() ) {
  94.             for ( int index = 0; index < getGrid().length; index++ ) {
  95.                 String symbolText = players[index] == null ? " " : players[index].getSymbol();
  96.  
  97.                 String delimiter = isLastRowIndex(index % getCols()) ? "\n" : "|";
  98.  
  99.                 System.out.print(symbolText + delimiter);
  100.             }
  101.         }
  102.     }
  103.  
  104.     private void runComputerTurn() {
  105.         int computerSelectedPosition = (int) (Math.random() * 9 + 1);
  106.         while( isCellTaken(getCellPositioning(computerSelectedPosition)) ) {
  107.             computerSelectedPosition = (int) (Math.random() * 9 + 1);
  108.         }
  109.         getGrid()[(computerSelectedPosition - 1) / 3][(computerSelectedPosition - 1) % 3] = Cell.COMPUTER;
  110.     }
  111.  
  112.     private boolean isCellTaken(Cell playerCell) {
  113.         return playerCell != null;
  114.     }
  115.  
  116.     private boolean isLastRowIndex(int col) {
  117.         return col == 2;
  118.     }
  119. }
  120. ------------------------------------------------------------------------------------------------
  121. public class Winner {
  122.     private static final int COLS = 3;
  123.     private static final int ROWS = 3;
  124.     private final Cell[][] grid = new Cell[ROWS][COLS];
  125.  
  126.     Cell[][] getGrid() {
  127.         return grid;
  128.     }
  129.  
  130.     boolean hasWinner() {
  131.         if ( isWinner(Cell.HUMAN) ) {
  132.             return true;
  133.         } else {
  134.             return isWinner(Cell.COMPUTER);
  135.         }
  136.     }
  137.  
  138.     int getCols() {
  139.         return COLS;
  140.     }
  141.  
  142.     void getWinner() {
  143.         if ( isWinner(Cell.HUMAN) ) {
  144.             System.out.println(State.HUMAN_WINNER);
  145.         } else {
  146.             System.out.println(State.COMPUTER_WINNER);
  147.         }
  148.     }
  149.  
  150.     boolean isWinner(Cell userCharacter) {
  151.         return hasWonHorizontally(userCharacter) || hasWonVertically(userCharacter) || hasWonDiagonally(userCharacter);
  152.     }
  153.  
  154.     private boolean hasWonDiagonally(Cell userCharacter) {
  155.         return leftToRightDiagonalWin(userCharacter) || rightToLeftDiagonalWin(userCharacter);
  156.     }
  157.  
  158.     private boolean rightToLeftDiagonalWin(Cell userCharacter) {
  159.         for ( int rows = 0; rows < ROWS; rows++ ) {
  160.             Cell cell = grid[COLS - rows - 1][rows];
  161.             if ( cell != userCharacter ) {
  162.                 return false;
  163.             }
  164.         }
  165.         return true;
  166.     }
  167.  
  168.     private boolean leftToRightDiagonalWin(Cell userCharacter) {
  169.         for ( int i = 0; i < ROWS; i++ ) {
  170.             Cell cell = grid[i][i];
  171.             if ( cell != userCharacter ) {
  172.                 return false;
  173.             }
  174.         }
  175.         return true;
  176.     }
  177.  
  178.     private boolean hasWonVertically(Cell userCharacter) {
  179.         for ( int col = 0; col < COLS; col++ ) {
  180.             boolean isColWon = true;
  181.             for ( int row = 0; row < ROWS; row++ ) {
  182.                 Cell cellIndex = grid[row][col];
  183.                 if ( cellIndex != userCharacter ) {
  184.                     isColWon = false;
  185.                     break;
  186.                 }
  187.             }
  188.  
  189.             if ( isColWon ) {
  190.                 return true;
  191.             }
  192.         }
  193.         return false;
  194.     }
  195.  
  196.     private boolean hasWonHorizontally(Cell userCharacter) {
  197.         for ( int row = 0; row < ROWS; row++ ) {
  198.             boolean isRowWon = true;
  199.             for ( int col = 0; col < COLS; col++ ) {
  200.                 Cell cellIndex = grid[row][col];
  201.                 if ( cellIndex != userCharacter ) {
  202.                     isRowWon = false;
  203.                     break;
  204.                 }
  205.             }
  206.             if ( isRowWon ) {
  207.                 return true;
  208.             }
  209.         }
  210.         return false;
  211.     }
  212. }
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement