Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class HomeAssignment {
- private char[] inputArray;
- private boolean isWinnerX = false;
- private boolean isWinnerO = false;
- private int xCounter = 0;
- private int oCounter = 0;
- private boolean isFinished = false;
- private void ticTacToe(String input) {
- validateInput(input);
- if (!isFinished) {
- validateState(inputArray);
- }
- if (!isFinished) {
- isWinnerX = findWinner(inputArray, 'X');
- isWinnerO = findWinner(inputArray, 'O');
- }
- if (!isFinished) {
- validateWinner();
- }
- if (!isFinished) {
- whoseTurnToPlay();
- }
- resetResults();
- }
- /**
- * Description: Validation of input string allowed symbols: X O - allowed length 9.
- *
- * @param input string
- */
- private void validateInput(String input) {
- Pattern pattern = Pattern.compile("^[\\-XO]{9}$");
- Matcher matcher = pattern.matcher(input);
- if (matcher.matches()) {
- inputArray = input.toCharArray();
- } else {
- System.out.println("Invalid input");
- isFinished = true;
- }
- }
- /**
- * Description: Validation of the state. X counter must be equal or higher +1 then O counter.
- *
- * @param array input string converted to array
- */
- private void validateState(char[] array) {
- for (char element : array) {
- if (element == 'X') xCounter++;
- if (element == 'O') oCounter++;
- }
- if (!(xCounter == oCounter || xCounter == oCounter + 1)) {
- System.out.println("Invalid state");
- isFinished = true;
- }
- }
- /**
- * Description: Check all states to find a winner.
- *
- * @param array input string parsed to array.
- * @param tic X or O
- * @return true if winner is found
- */
- private boolean findWinner(char[] array, char tic) {
- boolean winner = false;
- if (array[0] == tic && array[1] == tic && array[2] == tic ||
- array[3] == tic && array[4] == tic && array[5] == tic ||
- array[6] == tic && array[7] == tic && array[8] == tic ||
- array[0] == tic && array[3] == tic && array[6] == tic ||
- array[1] == tic && array[4] == tic && array[7] == tic ||
- array[2] == tic && array[5] == tic && array[8] == tic ||
- array[0] == tic && array[4] == tic && array[8] == tic ||
- array[2] == tic && array[4] == tic && array[6] == tic) {
- winner = true;
- }
- return winner;
- }
- /**
- * Description: Check if there is tiy or possibility for 2 winners.
- */
- private void validateWinner() {
- // if there is no winner and all moves are done it is tie
- if (!isWinnerX && !isWinnerO && xCounter == 5 && oCounter == 4) {
- System.out.println("Valid state, game is over, result is a tie");
- isFinished = true;
- }
- // if there are 2 winners it is invalid state.
- if (isWinnerX && isWinnerO) {
- System.out.println("invalid state. There is can be only one winner");
- isFinished = true;
- }
- // if X is winner and game still not finished X win.
- if (isWinnerX && !isFinished) {
- System.out.println("Valid state, game is over, “X” won");
- isFinished = true;
- } else if (isWinnerO && !isFinished) {
- System.out.println("Valid state, game is over, “O” won");
- isFinished = true;
- }
- }
- private void whoseTurnToPlay() {
- if (xCounter < 5) {
- if (xCounter == oCounter) {
- System.out.println("Valid state, game is still going, “X” to play");
- } else System.out.println("Valid state, game is still going, “O” to play");
- }
- }
- // reset all fields to default values.
- private void resetResults() {
- isFinished = false;
- isWinnerX = false;
- isWinnerO = false;
- xCounter = 0;
- oCounter = 0;
- inputArray = null;
- }
- public static void main(String[] args) {
- HomeAssignment result = new HomeAssignment();
- System.out.println("----------Invalid input testing---------");
- result.ticTacToe("XPLXX"); // invalid symbol
- result.ticTacToe("XOXOXOXO"); // 8 symbols
- result.ticTacToe("XOXOXOXOXO"); // 10 symbols
- result.ticTacToe(""); // empty input
- result.ticTacToe("xO-------"); // case sensitive
- result.ticTacToe("О--------"); // different language letter
- System.out.println();
- System.out.println("----------Invalid state testing---------");
- result.ticTacToe("O--------"); // invalid state X should start first
- result.ticTacToe("OOOXX----"); // invalid state X should be more than O
- result.ticTacToe("OOXXXX---"); // invalid state too many X
- result.ticTacToe("OOOXXX---"); // invalid state 2 winners at the same time.
- System.out.println();
- System.out.println("----------Tie---------");
- result.ticTacToe("XOXOOXXXO");
- result.ticTacToe("OXOXOXXOX");
- System.out.println();
- System.out.println("----------X won---------");
- result.ticTacToe("XXXOO-O--"); // 012
- result.ticTacToe("OO-XXXOOX"); // 345
- result.ticTacToe("OOXOXOXXX"); // 678
- result.ticTacToe("XOOXOXXXO"); // 036
- System.out.println();
- System.out.println("----------O won---------");
- result.ticTacToe("XOXXO--OX"); // 147
- result.ticTacToe("XXOXOO-XO"); // 258
- result.ticTacToe("OXXXOOXXO"); // 048
- result.ticTacToe("XXOXOXO--"); // 246
- System.out.println();
- System.out.println("----------X to play---------");
- result.ticTacToe("XO-------");
- result.ticTacToe("XOXO-----");
- result.ticTacToe("XOXOXO---");
- result.ticTacToe("XOXOXOOX-");
- result.ticTacToe("XOXOXO-XO");
- System.out.println();
- System.out.println("----------O to play---------");
- result.ticTacToe("X--------");
- result.ticTacToe("XO------X");
- result.ticTacToe("XO----XOX");
- result.ticTacToe("--XXOXOXO");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement