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.

164 lines
5.7 KiB

  1. package Game;
  2. import Game.ChessObj.ChessBoard;
  3. import Game.ChessObj.ChessFigure;
  4. import java.util.ArrayList;
  5. public class Chess extends Game {
  6. private ChessFigure.Team currentTeam;
  7. private ArrayList<ChessFigure> destroyedWhiteFigures;
  8. private ArrayList<ChessFigure> destroyedBlackFigures;
  9. private ChessBoard chessBoard;
  10. public Chess() {
  11. init();
  12. }
  13. private void init() {
  14. chessBoard = new ChessBoard();
  15. currentTeam = ChessFigure.Team.WHITE;
  16. destroyedWhiteFigures = new ArrayList<>();
  17. destroyedBlackFigures = new ArrayList<>();
  18. outputBuffer.addAll(chessBoard.getOutputBoard());
  19. }
  20. @Override
  21. public void update(String input) {
  22. }
  23. //int[0] = x, int[1] = y;
  24. public int[] convertInput(String input) {
  25. int[] temp = new int[2];
  26. //input is not 2 chars big
  27. if (input.length() != 2)
  28. return null;
  29. char[] symbols = input.toCharArray();
  30. int counterDigit = 0;
  31. int counterChar = 0;
  32. //input contains one number and one char
  33. for (char x : symbols) {
  34. if (Character.isDigit(x)) {
  35. counterDigit++;
  36. temp[1] = Character.getNumericValue(x) - 1;
  37. }
  38. if (Character.isAlphabetic(x)) {
  39. counterChar++;
  40. temp[0] = x;
  41. }
  42. }
  43. if (counterChar != 1 || counterDigit != 1)
  44. return null;
  45. temp[0] = Character.toLowerCase(temp[0]) - 97;
  46. if (!chessBoard.isCellInBoard(temp[0], temp[1]))
  47. return null;
  48. else
  49. return temp;
  50. }
  51. public ArrayList<String> makeMove(int[] source, int[] target) {
  52. ArrayList<String> result = new ArrayList<>();
  53. ChessFigure sourceFigure = chessBoard.getBoard()[source[1]][source[0]];
  54. ChessFigure targetFigure = chessBoard.getBoard()[target[1]][target[0]];
  55. String sourceFigureName = "<NOT FOUND>";
  56. String targetFigureName = "<NOT FOUND>";
  57. String sourceCoords = (char) (source[0] + 97) + "" + (source[1] + 1);
  58. String targetCoords = (char) (target[0] + 97) + "" + (target[1] + 1);
  59. ChessBoard.MoveFeedback moveFeedback = chessBoard.moveFigure(source[0], source[1], target[0], target[1]);
  60. if (sourceFigure != null)
  61. sourceFigureName = sourceFigure.getTeam().name().toCharArray()[0] + sourceFigure.getTeam().name().substring(1).toLowerCase() + " " + sourceFigure.getType().name().toLowerCase();
  62. if (targetFigure != null)
  63. targetFigureName = targetFigure.getTeam().name().toLowerCase() + " " + targetFigure.getType().name().toLowerCase();
  64. if (moveFeedback == ChessBoard.MoveFeedback.MOVE) {
  65. result.add("Successfully moved " + sourceFigureName.toLowerCase() + " from " + sourceCoords + " to " + targetCoords);
  66. } else if (moveFeedback == ChessBoard.MoveFeedback.ENEMYBEATEN) {
  67. result.add(sourceFigureName + " successfully beat " + targetFigureName.toLowerCase() + " at " + targetCoords);
  68. if (targetFigure.getTeam() == ChessFigure.Team.WHITE)
  69. getDestroyedWhiteFigures().add(targetFigure);
  70. else
  71. getDestroyedBlackFigures().add(targetFigure);
  72. } else {
  73. result.add("Invalid input!");
  74. switch (moveFeedback) {
  75. case INVALID:
  76. if (chessBoard.getBoard()[target[0]][target[1]].getTeam() == getCurrentTeam())
  77. result.add("You are on the same Team! [" + getCurrentTeam().name() + "]");
  78. break;
  79. case OUTSIDEOFBOARD:
  80. result.add("Input is not inside the board!");
  81. break;
  82. default:
  83. }
  84. }
  85. return result;
  86. }
  87. private ArrayList<String> getUpdatedOutputBoard(ArrayList<ChessFigure> whiteFigures, ArrayList<ChessFigure> blackFigures) {
  88. ArrayList<String> updatedOutputBoard = chessBoard.getOutputBoard();
  89. ArrayList<String> sidebarWhiteFigures = getSidebarFigures(whiteFigures, 5);
  90. ArrayList<String> sidebarBlackFigures = getSidebarFigures(blackFigures, 5);
  91. for (int i = 0; i < sidebarWhiteFigures.size(); i++) {
  92. int index = i + 3;
  93. updatedOutputBoard.set(index, updatedOutputBoard.get(index) + sidebarWhiteFigures.get(i));
  94. }
  95. for (int i = 0; i < sidebarBlackFigures.size(); i++) {
  96. int index = updatedOutputBoard.size() - (i + 3);
  97. updatedOutputBoard.set(index, updatedOutputBoard.get(index) + sidebarBlackFigures.get(i));
  98. }
  99. return updatedOutputBoard;
  100. }
  101. public ArrayList<String> getSidebarFigures(ArrayList<ChessFigure> chessFigureArrayList, int maxPerLine) {
  102. ArrayList<String> result = new ArrayList<>();
  103. String line = "";
  104. int counter = 0;
  105. for (int i = 0; i < chessFigureArrayList.size(); i++) {
  106. if (i == chessFigureArrayList.size() - 1) {
  107. line += chessFigureArrayList.get(i).getSymbol() + "";
  108. result.add(line);
  109. return result;
  110. }
  111. line += chessFigureArrayList.get(i).getSymbol() + ",";
  112. counter++;
  113. if (counter >= maxPerLine) {
  114. result.add(line);
  115. line = "";
  116. counter = 0;
  117. }
  118. }
  119. return result;
  120. }
  121. public ChessBoard getChessBoard() {
  122. return this.chessBoard;
  123. }
  124. public ChessFigure.Team getCurrentTeam() {
  125. return this.currentTeam;
  126. }
  127. public ArrayList<ChessFigure> getDestroyedWhiteFigures() {
  128. return destroyedWhiteFigures;
  129. }
  130. public ArrayList<ChessFigure> getDestroyedBlackFigures() {
  131. return destroyedBlackFigures;
  132. }
  133. }