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.

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