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.

173 lines
4.8 KiB

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