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.

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