You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.8 KiB

  1. package Game.TicTacToe;
  2. import java.util.ArrayList;
  3. public class Board {
  4. enum State {
  5. CIRCLE,
  6. CROSS,
  7. EMPTY
  8. }
  9. public enum CurrentState {
  10. DRAW,
  11. CIRCLEWIN,
  12. CROSSWIN,
  13. NOTFINISHED
  14. }
  15. private ArrayList<int[]> winPatterns;
  16. private State[] states;
  17. public Board() {
  18. states = new State[9];
  19. for (int i = 0; i < states.length; i++) {
  20. states[i] = State.EMPTY;
  21. }
  22. winPatterns = new ArrayList<>();
  23. winPatterns.add(new int[]{1, 1, 1, 0, 0, 0, 0, 0, 0});
  24. winPatterns.add(new int[]{0, 0, 0, 1, 1, 1, 0, 0, 0});
  25. winPatterns.add(new int[]{0, 0, 0, 0, 0, 0, 1, 1, 1});
  26. winPatterns.add(new int[]{1, 0, 0, 1, 0, 0, 1, 0, 0});
  27. winPatterns.add(new int[]{0, 1, 0, 0, 1, 0, 0, 1, 0});
  28. winPatterns.add(new int[]{0, 0, 1, 0, 0, 1, 0, 0, 1});
  29. winPatterns.add(new int[]{0, 0, 1, 0, 1, 0, 1, 0, 0});
  30. winPatterns.add(new int[]{1, 0, 0, 0, 1, 0, 0, 0, 1});
  31. }
  32. public static char getStatedChar(State state) {
  33. switch (state) {
  34. case CIRCLE:
  35. return 'O';
  36. case CROSS:
  37. return 'X';
  38. case EMPTY:
  39. return ' ';
  40. default:
  41. return '-';
  42. }
  43. }
  44. public boolean setCellState(int cell, boolean cross) {
  45. if (cell <= 9 && cell >= 1) {
  46. if (this.states[cell - 1] != State.EMPTY) {
  47. return false;
  48. }
  49. if (cross) {
  50. this.states[cell - 1] = State.CROSS;
  51. } else {
  52. this.states[cell - 1] = State.CIRCLE;
  53. }
  54. } else {
  55. return false;
  56. }
  57. return true;
  58. }
  59. /*
  60. 1 2 3
  61. o x o
  62. 4 5 6
  63. o x o
  64. 7 8 9
  65. o x o
  66. */
  67. public ArrayList<String> getOutputBoard() {
  68. ArrayList<String> outputBoard = new ArrayList<>();
  69. outputBoard.add("1 ║2 ║3");
  70. outputBoard.add(" " + getStatedChar(states[0]) + " ║ " + getStatedChar(states[1]) + " ║ " + getStatedChar(states[2]) + " ");
  71. outputBoard.add("═════╬═════╬═════");
  72. outputBoard.add("4 ║5 ║6");
  73. outputBoard.add(" " + getStatedChar(states[3]) + " ║ " + getStatedChar(states[4]) + " ║ " + getStatedChar(states[5]) + " ");
  74. outputBoard.add("═════╬═════╬═════");
  75. outputBoard.add("7 ║8 ║9");
  76. outputBoard.add(" " + getStatedChar(states[6]) + " ║ " + getStatedChar(states[7]) + " ║ " + getStatedChar(states[8]) + " ");
  77. return outputBoard;
  78. }
  79. public State[] getStates() {
  80. return this.states;
  81. }
  82. @Override
  83. public boolean equals(Object o) {
  84. if (!(o instanceof Board)) {
  85. return false;
  86. }
  87. Board x = (Board) o;
  88. for (int i = 0; i < x.getStates().length; i++) {
  89. if (this.getStates()[i] != x.getStates()[i]) {
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. //Checkfull(draw), Checkwinner clear,neue Runde und Scoreboard
  96. public CurrentState getCurrentState() {
  97. int counterCross;
  98. int counterCircle;
  99. for (int[] pattern : winPatterns) {
  100. counterCircle = 0;
  101. counterCross = 0;
  102. for (int i = 0; i < pattern.length; i++) {
  103. if (pattern[i] == 1) {
  104. if (getStates()[i] == State.CIRCLE)
  105. counterCircle++;
  106. if (getStates()[i] == State.CROSS)
  107. counterCross++;
  108. }
  109. }
  110. if (counterCircle >= 3)
  111. return CurrentState.CIRCLEWIN;
  112. if (counterCross >= 3)
  113. return CurrentState.CROSSWIN;
  114. }
  115. //Not finished
  116. for (int i = 0; i < getStates().length; i++) {
  117. if (getStates()[i] == State.EMPTY) {
  118. return CurrentState.NOTFINISHED;
  119. }
  120. }
  121. //Draw
  122. return CurrentState.DRAW;
  123. }
  124. public static State[] convertSimpleToState(int[] temp) {
  125. if (temp.length != 9)
  126. return null;
  127. State[] stateArray = new State[9];
  128. for (int i = 0; i < temp.length; i++) {
  129. switch (temp[i]) {
  130. case 1:
  131. stateArray[i] = State.CIRCLE;
  132. break;
  133. case 2:
  134. stateArray[i] = State.CROSS;
  135. break;
  136. default:
  137. stateArray[i] = State.EMPTY;
  138. }
  139. }
  140. return stateArray;
  141. }
  142. }