Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class App {
- public static void main(String[] args) {
- TicTacToe game = new TicTacToe();
- game.start();
- }
- }
- -----------------------------------------------
- enum Cell {
- HUMAN("X"),
- COMPUTER("O");
- private final String symbol;
- Cell(String symbol) {
- this.symbol = symbol;
- }
- String getSymbol() {
- return symbol;
- }
- }
- enum State {
- HUMAN_WINNER,
- COMPUTER_WINNER,
- TIE
- }
- public class TicTacToe extends Winner {
- private final Scanner input = new Scanner(System.in);
- public TicTacToe() {
- displayGrid();
- }
- public void start() {
- game();
- }
- private void game() {
- while( true ) {
- runHumansTurn();
- if ( isGameDraw() ) {
- System.out.println(State.TIE);
- return;
- }
- if ( hasWinner() ) {
- break;
- }
- runComputerTurn();
- if ( hasWinner() ) {
- break;
- }
- displayGrid();
- }
- displayGrid();
- getWinner();
- }
- private boolean isGameDraw() {
- for ( Cell[] players: getGrid() ) {
- for ( int i = 0; i < getGrid().length; i++ ) {
- if ( players[i] == null || players[i].getSymbol().equals(" ") || isWinner(Cell.HUMAN) ) {
- return false;
- }
- }
- }
- return true;
- }
- private void runHumansTurn() {
- System.out.println("Pick a number from 1 - 9 ");
- int humanSelectedPosition = input.nextInt();
- while( isCellTaken(getCellPositioning(humanSelectedPosition)) || isRightInput(humanSelectedPosition) ) {
- System.out.println("Try again ");
- humanSelectedPosition = input.nextInt();
- }
- getGrid()[(humanSelectedPosition - 1) / 3][(humanSelectedPosition - 1) % 3] = Cell.HUMAN;
- }
- private Cell getCellPositioning(int humanSelectedPosition) {
- return getGrid()[(humanSelectedPosition - 1) / 3][(humanSelectedPosition - 1) % 3];
- }
- private boolean isRightInput(int humanSelectedPosition) {
- return humanSelectedPosition > 9 || humanSelectedPosition < 1;
- }
- private void displayGrid() {
- for ( Cell[] players: getGrid() ) {
- for ( int index = 0; index < getGrid().length; index++ ) {
- String symbolText = players[index] == null ? " " : players[index].getSymbol();
- String delimiter = isLastRowIndex(index % getCols()) ? "\n" : "|";
- System.out.print(symbolText + delimiter);
- }
- }
- }
- private void runComputerTurn() {
- int computerSelectedPosition = (int) (Math.random() * 9 + 1);
- while( isCellTaken(getCellPositioning(computerSelectedPosition)) ) {
- computerSelectedPosition = (int) (Math.random() * 9 + 1);
- }
- getGrid()[(computerSelectedPosition - 1) / 3][(computerSelectedPosition - 1) % 3] = Cell.COMPUTER;
- }
- private boolean isCellTaken(Cell playerCell) {
- return playerCell != null;
- }
- private boolean isLastRowIndex(int col) {
- return col == 2;
- }
- }
- ------------------------------------------------------------------------------------------------
- public class Winner {
- private static final int COLS = 3;
- private static final int ROWS = 3;
- private final Cell[][] grid = new Cell[ROWS][COLS];
- Cell[][] getGrid() {
- return grid;
- }
- boolean hasWinner() {
- if ( isWinner(Cell.HUMAN) ) {
- return true;
- } else {
- return isWinner(Cell.COMPUTER);
- }
- }
- int getCols() {
- return COLS;
- }
- void getWinner() {
- if ( isWinner(Cell.HUMAN) ) {
- System.out.println(State.HUMAN_WINNER);
- } else {
- System.out.println(State.COMPUTER_WINNER);
- }
- }
- boolean isWinner(Cell userCharacter) {
- return hasWonHorizontally(userCharacter) || hasWonVertically(userCharacter) || hasWonDiagonally(userCharacter);
- }
- private boolean hasWonDiagonally(Cell userCharacter) {
- return leftToRightDiagonalWin(userCharacter) || rightToLeftDiagonalWin(userCharacter);
- }
- private boolean rightToLeftDiagonalWin(Cell userCharacter) {
- for ( int rows = 0; rows < ROWS; rows++ ) {
- Cell cell = grid[COLS - rows - 1][rows];
- if ( cell != userCharacter ) {
- return false;
- }
- }
- return true;
- }
- private boolean leftToRightDiagonalWin(Cell userCharacter) {
- for ( int i = 0; i < ROWS; i++ ) {
- Cell cell = grid[i][i];
- if ( cell != userCharacter ) {
- return false;
- }
- }
- return true;
- }
- private boolean hasWonVertically(Cell userCharacter) {
- for ( int col = 0; col < COLS; col++ ) {
- boolean isColWon = true;
- for ( int row = 0; row < ROWS; row++ ) {
- Cell cellIndex = grid[row][col];
- if ( cellIndex != userCharacter ) {
- isColWon = false;
- break;
- }
- }
- if ( isColWon ) {
- return true;
- }
- }
- return false;
- }
- private boolean hasWonHorizontally(Cell userCharacter) {
- for ( int row = 0; row < ROWS; row++ ) {
- boolean isRowWon = true;
- for ( int col = 0; col < COLS; col++ ) {
- Cell cellIndex = grid[row][col];
- if ( cellIndex != userCharacter ) {
- isRowWon = false;
- break;
- }
- }
- if ( isRowWon ) {
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement