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.

178 lines
6.4 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. 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));
  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. * @return true if Player can roll the dice another time
  63. */
  64. public boolean checkDice(int dice, Player p, int countRolls) {
  65. int figuresInBase = p.checkFigureInBase(p.figures);
  66. if(figuresInBase == 4) {
  67. return countRolls < 3;
  68. } else return dice == 6;
  69. }
  70. /**
  71. * Check if a target field is free or occupied by another figure
  72. * @param posToCheck Target position to check
  73. * @param p active Player
  74. * @param g game instance
  75. * @return 2 - own Player 1 - another Player 0 - Field clear
  76. */
  77. public int checkFieldClear(int posToCheck, Player p, Game g) {
  78. int mode;
  79. for (Player currentPlayer : g.players) {
  80. if (currentPlayer.name.equals(p.name)) {
  81. mode = 2;
  82. } else {
  83. mode = 1;
  84. }
  85. for (Figure f : currentPlayer.figures) {
  86. if (posToCheck == f.getPosition()) {
  87. return mode;
  88. }
  89. }
  90. }
  91. return 0;
  92. }
  93. /**
  94. * Get List of all usable Figures in the current Play for one Player
  95. * @param dice Value of dice got from rolldice()
  96. * @param p active Player
  97. * @param g game instance
  98. * @return List of int with figIds the Player can choose from
  99. */
  100. public ArrayList<Integer> getUsableFigures(int dice, Player p, Game g) {
  101. ArrayList<Integer> result = new ArrayList<>();
  102. for(int i = 0; i < p.figures.size(); i++) {
  103. if(figureIsUsable(dice, i, p, g)) {
  104. result.add(i);
  105. }
  106. }
  107. return result;
  108. }
  109. /**
  110. * Check if a figure is usable
  111. * @param dice Value of dice got from rolldice()
  112. * @param figId id of figure to check
  113. * @param p active Player
  114. * @param g game instance
  115. * @return true if figure is usable
  116. */
  117. public boolean figureIsUsable(int dice, int figId, Player p, Game g) {
  118. Figure f = p.figures.get(figId);
  119. if(p.checkFigureInBase(p.figures) > 0 && dice == 6){
  120. if(g.checkFieldClear(p.startPos, p, g) == 2) {
  121. return f.getPosition() == p.startPos;
  122. }
  123. return f.getPosition() == -1;
  124. } else if (f.getPosition() <= p.jumpToHome && (f.getPosition() + dice) > p.jumpToHome) {
  125. if((f.getPosition() + dice) <= p.jumpToHome + 4) {
  126. return g.checkFieldClear(f.getPosition() + dice - p.jumpToHome + p.startHome - 1, p, g) != 2;
  127. } else {
  128. return false;
  129. }
  130. } else if(f.getPosition() >= p.startHome && (f.getPosition() + dice) < p.startHome + 4) {
  131. return g.checkFieldClear(f.getPosition() + dice, p, g) == 0;
  132. } else if(f.getPosition() != -1 && f.getPosition() < 40) {
  133. return g.checkFieldClear((f.getPosition() + dice) % 40, p, g) != 2;
  134. }
  135. return false;
  136. }
  137. /**
  138. * Set a figure on a field according
  139. * @param figId id of figure to check
  140. * @param dice Value of dice got from rolldice()
  141. * @param p active Player
  142. * @param g game instance
  143. * @return 0 if no figure kicked. 1 if another figure was kicked
  144. */
  145. public int setFigure(int figId, int dice, Player p, Game g) {
  146. int preCalculated;
  147. if (p.figures.get(figId).getPosition() == -1) {
  148. preCalculated = p.startPos;
  149. } else if (p.figures.get(figId).getPosition() <= p.jumpToHome && (p.figures.get(figId).getPosition() + dice) > p.jumpToHome) {
  150. preCalculated = p.startHome + (dice - (p.jumpToHome - p.figures.get(figId).getPosition()) - 1);
  151. } else if (p.figures.get(figId).getPosition() >= p.startHome) {
  152. preCalculated = p.figures.get(figId).getPosition() + dice;
  153. }else {
  154. preCalculated = (p.figures.get(figId).getPosition() + dice) % 40;
  155. }
  156. for(Player currentPlayer : g.players) {
  157. for(Figure currentFigure : currentPlayer.figures) {
  158. if(currentFigure.getPosition() == preCalculated) {
  159. currentFigure.setPosition(-1);
  160. }
  161. }
  162. }
  163. p.figures.get(figId).setPosition(preCalculated);
  164. return preCalculated;
  165. }
  166. }