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.

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