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.

187 lines
6.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import java.util.ArrayList;
  2. import java.util.concurrent.TimeUnit;
  3. import static java.lang.System.exit;
  4. public class Game {
  5. Gameboard gb;
  6. ArrayList<Player> players;
  7. public static void main(String[] args) throws InterruptedException {
  8. Game g = new Game();
  9. Player winner;
  10. while(true){
  11. for (Player p : g.players) {
  12. int c = 0;
  13. int dice;
  14. do {
  15. clearScreen();
  16. int figId;
  17. dice = p.rollDice();
  18. System.out.println(g.printGameboard(g,p,dice));
  19. TimeUnit.MILLISECONDS.sleep(600L);
  20. System.out.println("Würfel: "+ dice);
  21. TimeUnit.MILLISECONDS.sleep(600L);
  22. c++;
  23. ArrayList<Integer> usableFigures = g.getUsableFigures(dice, p, g);
  24. if(usableFigures.size() > 0) {
  25. do {
  26. figId = p.choose(usableFigures);
  27. } while(figId == -1);
  28. g.setFigure(figId, dice, p, g);
  29. clearScreen();
  30. System.out.println(g.printGameboard(g,p,dice));
  31. }
  32. } while (g.checkDice(dice, p, c, g));
  33. if(p.checkGameWin(p.figures)) {
  34. winner = p;
  35. System.out.println("Spieler " + winner.name + " gewinnt!");
  36. exit(42);
  37. }
  38. }
  39. }
  40. }
  41. public Game() {
  42. this.gb = new Gameboard();
  43. gb.initGameboard();
  44. players = new ArrayList<>();
  45. players.add(new Player("Rot",0, 40, 39));
  46. players.add(new Player("Blau",10, 44, 9));
  47. players.add(new Player("Gelb",20, 48, 19));
  48. players.add(new Player("Grün",30, 52, 29));
  49. }
  50. public String printGameboard(Game g, Player p, int dice) {
  51. return gb.printBoard(g, p, dice);
  52. }
  53. public static void clearScreen() {
  54. System.out.print("\033[H\033[2J");
  55. System.out.flush();
  56. }
  57. /**
  58. * Check if the Player is allowed to roll the dice again
  59. * @param dice Value of dice got from rolldice()
  60. * @param p active Player
  61. * @param countRolls Counter how often the Player already rolled the dice
  62. * @param g game instance
  63. * @return true if Player can roll the dice another time
  64. */
  65. public boolean checkDice(int dice, Player p, int countRolls, Game g) {
  66. int figuresInBase = p.checkFigureInBase(p.figures);
  67. if(countRolls >= 3) return dice == 6;
  68. if(figuresInBase == 4) {
  69. return true;
  70. } else if (figuresInBase < 4) {
  71. for(int i = 0; i < 4 - figuresInBase; i++) {
  72. if(checkFieldClear(p.startHome+3-i,p,g) == 0) {
  73. return dice == 6;
  74. }
  75. }
  76. return true;
  77. } else return dice == 6;
  78. }
  79. /**
  80. * Check if a target field is free or occupied by another figure
  81. * @param posToCheck Target position to check
  82. * @param p active Player
  83. * @param g game instance
  84. * @return 2 - own Player 1 - another Player 0 - Field clear
  85. */
  86. public int checkFieldClear(int posToCheck, Player p, Game g) {
  87. int mode;
  88. for (Player currentPlayer : g.players) {
  89. if (currentPlayer.name.equals(p.name)) {
  90. mode = 2;
  91. } else {
  92. mode = 1;
  93. }
  94. for (Figure f : currentPlayer.figures) {
  95. if (posToCheck == f.getPosition()) {
  96. return mode;
  97. }
  98. }
  99. }
  100. return 0;
  101. }
  102. /**
  103. * Get List of all usable Figures in the current Play for one Player
  104. * @param dice Value of dice got from rolldice()
  105. * @param p active Player
  106. * @param g game instance
  107. * @return List of int with figIds the Player can choose from
  108. */
  109. public ArrayList<Integer> getUsableFigures(int dice, Player p, Game g) {
  110. ArrayList<Integer> result = new ArrayList<>();
  111. for(int i = 0; i < p.figures.size(); i++) {
  112. if(figureIsUsable(dice, i, p, g)) {
  113. result.add(i);
  114. }
  115. }
  116. return result;
  117. }
  118. /**
  119. * Check if a figure is usable
  120. * @param dice Value of dice got from rolldice()
  121. * @param figId id of figure to check
  122. * @param p active Player
  123. * @param g game instance
  124. * @return true if figure is usable
  125. */
  126. public boolean figureIsUsable(int dice, int figId, Player p, Game g) {
  127. Figure f = p.figures.get(figId);
  128. if(p.checkFigureInBase(p.figures) > 0 && dice == 6){
  129. if(g.checkFieldClear(p.startPos, p, g) == 2) {
  130. return f.getPosition() == p.startPos;
  131. }
  132. return f.getPosition() == -1;
  133. } else if (f.getPosition() <= p.jumpToHome && (f.getPosition() + dice) > p.jumpToHome) {
  134. if((f.getPosition() + dice) <= p.jumpToHome + 4) {
  135. return g.checkFieldClear(f.getPosition() + dice - p.jumpToHome + p.startHome - 1, p, g) != 2;
  136. } else {
  137. return false;
  138. }
  139. } else if(f.getPosition() >= p.startHome && (f.getPosition() + dice) < p.startHome + 4) {
  140. return g.checkFieldClear(f.getPosition() + dice, p, g) == 0;
  141. } else if(f.getPosition() != -1 && f.getPosition() < 40) {
  142. return g.checkFieldClear((f.getPosition() + dice) % 40, p, g) != 2;
  143. }
  144. return false;
  145. }
  146. /**
  147. * Set a figure on a field according
  148. * @param figId id of figure to check
  149. * @param dice Value of dice got from rolldice()
  150. * @param p active Player
  151. * @param g game instance
  152. * @return 0 if no figure kicked. 1 if another figure was kicked
  153. */
  154. public int setFigure(int figId, int dice, Player p, Game g) {
  155. int preCalculated;
  156. if (p.figures.get(figId).getPosition() == -1) {
  157. preCalculated = p.startPos;
  158. } else if (p.figures.get(figId).getPosition() <= p.jumpToHome && (p.figures.get(figId).getPosition() + dice) > p.jumpToHome) {
  159. preCalculated = p.startHome + (dice - (p.jumpToHome - p.figures.get(figId).getPosition()) - 1);
  160. } else if (p.figures.get(figId).getPosition() >= p.startHome) {
  161. preCalculated = p.figures.get(figId).getPosition() + dice;
  162. }else {
  163. preCalculated = (p.figures.get(figId).getPosition() + dice) % 40;
  164. }
  165. for(Player currentPlayer : g.players) {
  166. for(Figure currentFigure : currentPlayer.figures) {
  167. if(currentFigure.getPosition() == preCalculated) {
  168. currentFigure.setPosition(-1);
  169. }
  170. }
  171. }
  172. p.figures.get(figId).setPosition(preCalculated);
  173. return preCalculated;
  174. }
  175. }