Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. package ticTacToe;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Map;
  5.  
  6. public class MNKBoard implements Board, Position {
  7. private static final Map<Cell, Character> SYMBOLS = Map.of(
  8. Cell.X, 'X',
  9. Cell.O, 'O',
  10. Cell.E, '.'
  11. );
  12.  
  13. private final Cell[][] cells;
  14. private Cell turn;
  15. private final int n;
  16. private final int m;
  17. private final int k;
  18. private int moveCounter;
  19.  
  20. public MNKBoard(int m, int n, int k) {
  21. this.n = n;
  22. this.m = m;
  23. this.k = k;
  24. this.moveCounter = m * n;
  25. this.cells = new Cell[n][m];
  26. for (Cell[] row : cells) {
  27. Arrays.fill(row, Cell.E);
  28. }
  29. turn = Cell.X;
  30. }
  31. @Override
  32. public Position getPosition() {
  33. return this;
  34. }
  35.  
  36. @Override
  37. public Cell getCell() {
  38. return turn;
  39. }
  40.  
  41. @Override
  42. public Result makeMove(Move move) {
  43. if (!isValid(move)) {
  44. return Result.LOSE;
  45. }
  46. cells[move.getRow()][move.getColumn()] = move.getValue();
  47. moveCounter--;
  48. turn = turn == Cell.X ? Cell.O : Cell.X;
  49. if (moveCounter == 0) {
  50. return Result.DRAW;
  51. } else {
  52. return check(move);
  53. }
  54.  
  55. }
  56.  
  57. private Result check(Move move) {
  58. // System.out.println("- " + (count(1, 0, move) + count(-1,0, move) + 1));
  59. // System.out.println("| " + (count(0, 1, move) + count(0,-1, move) + 1));
  60. // System.out.println("/ " + (count(1, 1, move) + count(-1,-1, move) + 1));
  61. // System.out.println("\\ " + (count(-1, 1, move) + count(1,-1, move) + 1));
  62. if (count(1, 0, move) + count(-1,0, move) + 1 >= k) {
  63. return Result.WIN;
  64. } else if (count(0, 1, move) + count(0,-1, move) + 1 >= k) {
  65. return Result.WIN;
  66. } else if (count(1, 1, move) + count(-1,-1, move) + 1 >= k) {
  67. return Result.WIN;
  68. } else if (count(-1, 1, move) + count(1,-1, move) + 1 >= k) {
  69. return Result.WIN;
  70. } else {
  71. return Result.UNKNOWN;
  72. }
  73. }
  74.  
  75. private int count(int x, int y, Move move) {
  76. int cnt = 0;
  77.  
  78. cycle:
  79. for (int i = 1; i < k; i += x) {
  80. for (int j = 1; j < k; j += y) {
  81. if (move.getRow() + i < n && move.getRow() + i >= 0 &&
  82. move.getColumn() + j < m && move.getColumn() + j >= 0) {
  83. if (cells[move.getRow() + i][move.getColumn() + j] == move.getValue()) {
  84. cnt++;
  85. } else {
  86. //System.out.println(cnt);
  87. break cycle;
  88. }
  89. } else {
  90. //System.out.println(cnt);
  91. break cycle;
  92. }
  93. }
  94. }
  95. //System.out.println(cnt);
  96. return cnt;
  97. }
  98.  
  99. @Override
  100. public boolean isValid(Move move) {
  101. return 0 <= move.getRow() && move.getRow() < n
  102. && 0 <= move.getColumn() && move.getColumn() < m
  103. && cells[move.getRow()][move.getColumn()] == Cell.E
  104. && turn == getCell();
  105. }
  106.  
  107. @Override
  108. public Cell getCell(int r, int c) {
  109. return cells[r][c];
  110. }
  111.  
  112. @Override
  113. public String toString() {
  114. final StringBuilder sb = new StringBuilder(" ");
  115. for (int i = 0; i < m; i++) {
  116. sb.append(i);
  117. }
  118. for (int r = 0; r < n; r++) {
  119. sb.append("\n");
  120. sb.append(r);
  121. sb.append(" ");
  122. for (int c = 0; c < m; c++) {
  123. sb.append(SYMBOLS.get(cells[r][c]));
  124. }
  125. }
  126. return sb.toString();
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement