Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.40 KB | None | 0 0
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class HomeAssignment {
  5.  
  6.     private char[] inputArray;
  7.     private boolean isWinnerX = false;
  8.     private boolean isWinnerO = false;
  9.     private int xCounter = 0;
  10.     private int oCounter = 0;
  11.     private boolean isFinished = false;
  12.  
  13.     private void ticTacToe(String input) {
  14.  
  15.         validateInput(input);
  16.         if (!isFinished) {
  17.             validateState(inputArray);
  18.         }
  19.         if (!isFinished) {
  20.             isWinnerX = findWinner(inputArray, 'X');
  21.             isWinnerO = findWinner(inputArray, 'O');
  22.         }
  23.         if (!isFinished) {
  24.             validateWinner();
  25.         }
  26.         if (!isFinished) {
  27.             whoseTurnToPlay();
  28.         }
  29.         resetResults();
  30.  
  31.     }
  32.  
  33.     /**
  34.      * Description: Validation of input string allowed symbols: X O - allowed length 9.
  35.      *
  36.      * @param input string
  37.      */
  38.     private void validateInput(String input) {
  39.         Pattern pattern = Pattern.compile("^[\\-XO]{9}$");
  40.         Matcher matcher = pattern.matcher(input);
  41.  
  42.         if (matcher.matches()) {
  43.             inputArray = input.toCharArray();
  44.         } else {
  45.             System.out.println("Invalid input");
  46.             isFinished = true;
  47.         }
  48.     }
  49.  
  50.     /**
  51.      * Description: Validation of the state. X counter must be equal or higher +1 then O counter.
  52.      *
  53.      * @param array input string converted to array
  54.      */
  55.     private void validateState(char[] array) {
  56.         for (char element : array) {
  57.             if (element == 'X') xCounter++;
  58.             if (element == 'O') oCounter++;
  59.         }
  60.         if (!(xCounter == oCounter || xCounter == oCounter + 1)) {
  61.             System.out.println("Invalid state");
  62.             isFinished = true;
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * Description: Check all states to find a winner.
  68.      *
  69.      * @param array input string parsed to array.
  70.      * @param tic   X or O
  71.      * @return true if winner is found
  72.      */
  73.     private boolean findWinner(char[] array, char tic) {
  74.         boolean winner = false;
  75.         if (array[0] == tic && array[1] == tic && array[2] == tic ||
  76.                 array[3] == tic && array[4] == tic && array[5] == tic ||
  77.                 array[6] == tic && array[7] == tic && array[8] == tic ||
  78.                 array[0] == tic && array[3] == tic && array[6] == tic ||
  79.                 array[1] == tic && array[4] == tic && array[7] == tic ||
  80.                 array[2] == tic && array[5] == tic && array[8] == tic ||
  81.                 array[0] == tic && array[4] == tic && array[8] == tic ||
  82.                 array[2] == tic && array[4] == tic && array[6] == tic) {
  83.             winner = true;
  84.         }
  85.         return winner;
  86.     }
  87.  
  88.     /**
  89.      * Description: Check if there is tiy or possibility for 2 winners.
  90.      */
  91.     private void validateWinner() {
  92.         // if there is no winner and all moves are done it is tie
  93.         if (!isWinnerX && !isWinnerO && xCounter == 5 && oCounter == 4) {
  94.             System.out.println("Valid state, game is over, result is a tie");
  95.             isFinished = true;
  96.         }
  97.         // if there are 2 winners it is invalid state.
  98.         if (isWinnerX && isWinnerO) {
  99.             System.out.println("invalid state. There is can be only one winner");
  100.             isFinished = true;
  101.         }
  102.         // if X is winner and game still not finished X win.
  103.         if (isWinnerX && !isFinished) {
  104.             System.out.println("Valid state, game is over, “X” won");
  105.             isFinished = true;
  106.         } else if (isWinnerO && !isFinished) {
  107.             System.out.println("Valid state, game is over, “O” won");
  108.             isFinished = true;
  109.         }
  110.     }
  111.  
  112.     private void whoseTurnToPlay() {
  113.         if (xCounter < 5) {
  114.             if (xCounter == oCounter) {
  115.                 System.out.println("Valid state, game is still going, “X” to play");
  116.             } else System.out.println("Valid state, game is still going, “O” to play");
  117.         }
  118.     }
  119.  
  120.     // reset all fields to default values.
  121.     private void resetResults() {
  122.         isFinished = false;
  123.         isWinnerX = false;
  124.         isWinnerO = false;
  125.         xCounter = 0;
  126.         oCounter = 0;
  127.         inputArray = null;
  128.     }
  129.  
  130.     public static void main(String[] args) {
  131.         HomeAssignment result = new HomeAssignment();
  132.  
  133.         System.out.println("----------Invalid input testing---------");
  134.         result.ticTacToe("XPLXX"); // invalid symbol
  135.         result.ticTacToe("XOXOXOXO"); // 8 symbols
  136.         result.ticTacToe("XOXOXOXOXO"); // 10 symbols
  137.         result.ticTacToe(""); // empty input
  138.         result.ticTacToe("xO-------"); // case sensitive
  139.         result.ticTacToe("О--------"); // different language letter
  140.  
  141.         System.out.println();
  142.         System.out.println("----------Invalid state testing---------");
  143.         result.ticTacToe("O--------"); // invalid state X should start first
  144.         result.ticTacToe("OOOXX----"); // invalid state X should be more than O
  145.         result.ticTacToe("OOXXXX---"); // invalid state too many X
  146.         result.ticTacToe("OOOXXX---"); // invalid state 2 winners at the same time.
  147.  
  148.         System.out.println();
  149.         System.out.println("----------Tie---------");
  150.         result.ticTacToe("XOXOOXXXO");
  151.         result.ticTacToe("OXOXOXXOX");
  152.  
  153.         System.out.println();
  154.         System.out.println("----------X won---------");
  155.         result.ticTacToe("XXXOO-O--"); // 012
  156.         result.ticTacToe("OO-XXXOOX"); // 345
  157.         result.ticTacToe("OOXOXOXXX"); // 678
  158.         result.ticTacToe("XOOXOXXXO"); // 036
  159.  
  160.         System.out.println();
  161.         System.out.println("----------O won---------");
  162.         result.ticTacToe("XOXXO--OX"); // 147
  163.         result.ticTacToe("XXOXOO-XO"); // 258
  164.         result.ticTacToe("OXXXOOXXO"); // 048
  165.         result.ticTacToe("XXOXOXO--"); // 246
  166.  
  167.         System.out.println();
  168.         System.out.println("----------X to play---------");
  169.         result.ticTacToe("XO-------");
  170.         result.ticTacToe("XOXO-----");
  171.         result.ticTacToe("XOXOXO---");
  172.         result.ticTacToe("XOXOXOOX-");
  173.         result.ticTacToe("XOXOXO-XO");
  174.  
  175.         System.out.println();
  176.         System.out.println("----------O to play---------");
  177.         result.ticTacToe("X--------");
  178.         result.ticTacToe("XO------X");
  179.         result.ticTacToe("XO----XOX");
  180.         result.ticTacToe("--XXOXOXO");
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement