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.

82 lines
2.1 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. private State[] states;
  10. public Board() {
  11. states = new State[9];
  12. for (int i = 0; i < states.length; i++) {
  13. states[i] = State.EMPTY;
  14. }
  15. }
  16. public static char getStatedChar(State state) {
  17. switch (state) {
  18. case CIRCLE:
  19. return 'O';
  20. case CROSS:
  21. return 'X';
  22. case EMPTY:
  23. return ' ';
  24. default:
  25. return '-';
  26. }
  27. }
  28. /*
  29. 1 2 3
  30. o x o
  31. 4 5 6
  32. o x o
  33. 7 8 9
  34. o x o
  35. */
  36. public ArrayList<String> getOutputBoard() {
  37. ArrayList<String> outputBoard = new ArrayList<>();
  38. outputBoard.add("1 ║2 ║3");
  39. outputBoard.add(" " + getStatedChar(states[0]) + " ║ " + getStatedChar(states[1]) + " ║ " + getStatedChar(states[2]) +" ");
  40. outputBoard.add("═════╬═════╬═════");
  41. outputBoard.add("4 ║5 ║6");
  42. outputBoard.add(" " + getStatedChar(states[3]) + " ║ " + getStatedChar(states[4]) + " ║ " + getStatedChar(states[5]) +" ");
  43. outputBoard.add("═════╬═════╬═════");
  44. outputBoard.add("7 ║8 ║9");
  45. outputBoard.add(" " + getStatedChar(states[6]) + " ║ " + getStatedChar(states[7]) + " ║ " + getStatedChar(states[8]) +" ");
  46. return outputBoard;
  47. }
  48. public State[] getStates() {
  49. return this.states;
  50. }
  51. @Override
  52. public boolean equals(Object o) {
  53. if (!(o instanceof Board)) {
  54. return false;
  55. }
  56. Board x = (Board)o;
  57. for (int i = 0; i < x.getStates().length; i++) {
  58. if (this.getStates()[i] != x.getStates()[i]) {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. }