package Game.TicTacToe; import java.util.ArrayList; import java.util.Arrays; public class Board { enum State { CIRCLE, CROSS, EMPTY } public enum CurrentState { DRAW, CIRCLEWIN, CROSSWIN, NOTFINISHED } private ArrayList winPatterns; private State[] states; public Board() { states = new State[9]; for (int i = 0; i < states.length; i++) { states[i] = State.EMPTY; } winPatterns = new ArrayList<>(); winPatterns.add(new int[]{1,1,1,0,0,0,0,0,0}); winPatterns.add(new int[]{0,0,0,1,1,1,0,0,0}); winPatterns.add(new int[]{0,0,0,0,0,0,1,1,1}); winPatterns.add(new int[]{1,0,0,1,0,0,1,0,0}); winPatterns.add(new int[]{0,1,0,0,1,0,0,1,0}); winPatterns.add(new int[]{0,0,1,0,0,1,0,0,1}); winPatterns.add(new int[]{0,0,1,0,1,0,1,0,0}); winPatterns.add(new int[]{1,0,0,0,1,0,0,0,1}); } public static char getStatedChar(State state) { switch (state) { case CIRCLE: return 'O'; case CROSS: return 'X'; case EMPTY: return ' '; default: return '-'; } } public boolean setCellState(int cell, boolean cross) { if (cell <= 9 && cell >= 1) { if (this.states[cell-1] != State.EMPTY) { return false; } if (cross) { this.states[cell-1] = State.CROSS; } else { this.states[cell-1] = State.CIRCLE; } }else { return false; } return true; } /* 1 ║2 ║3 o ║ x ║ o ═════╬═════╬═════ 4 ║5 ║6 o ║ x ║ o ═════╬═════╬═════ 7 ║8 ║9 o ║ x ║ o */ public ArrayList getOutputBoard() { ArrayList outputBoard = new ArrayList<>(); outputBoard.add("1 ║2 ║3"); outputBoard.add(" " + getStatedChar(states[0]) + " ║ " + getStatedChar(states[1]) + " ║ " + getStatedChar(states[2]) +" "); outputBoard.add("═════╬═════╬═════"); outputBoard.add("4 ║5 ║6"); outputBoard.add(" " + getStatedChar(states[3]) + " ║ " + getStatedChar(states[4]) + " ║ " + getStatedChar(states[5]) +" "); outputBoard.add("═════╬═════╬═════"); outputBoard.add("7 ║8 ║9"); outputBoard.add(" " + getStatedChar(states[6]) + " ║ " + getStatedChar(states[7]) + " ║ " + getStatedChar(states[8]) +" "); return outputBoard; } public State[] getStates() { return this.states; } @Override public boolean equals(Object o) { if (!(o instanceof Board)) { return false; } Board x = (Board)o; for (int i = 0; i < x.getStates().length; i++) { if (this.getStates()[i] != x.getStates()[i]) { return false; } } return true; } //Checkfull(draw), Checkwinner clear,neue Runde und Scoreboard public CurrentState getCurrentState() { int counterCross; int counterCircle; for (int[] pattern : winPatterns) { counterCircle = 0; counterCross = 0; for (int i = 0; i < pattern.length; i++) { if (pattern[i] == 1) { if (getStates()[i] == State.CIRCLE) counterCircle++; if (getStates()[i] == State.CROSS) counterCross++; } } if (counterCircle >= 3) return CurrentState.CIRCLEWIN; if (counterCross >= 3) return CurrentState.CROSSWIN; } //Not finished for (int i = 0; i < getStates().length; i++) { if (getStates()[i] == State.EMPTY) { return CurrentState.NOTFINISHED; } } //Draw return CurrentState.DRAW; } public static State[] convertSimpleToState(int[] temp) { if (temp.length != 9) return null; State[] stateArray = new State[9]; for (int i = 0; i < temp.length; i++) { switch(temp[i]) { case 1: stateArray[i] = State.CIRCLE; break; case 2: stateArray[i] = State.CROSS; break; default: stateArray[i] = State.EMPTY; } } return stateArray; } }