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.

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