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.

176 lines
5.9 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
  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. System.out.println("Spieler " + p.name + " an der Reihe.");
  15. do {
  16. int figId;
  17. dice = p.rollDice();
  18. TimeUnit.SECONDS.sleep(1L);
  19. System.out.println("Würfel: " + dice);
  20. c++;
  21. ArrayList<Integer> usableFigures = g.getUsableFigures(dice, p, g);
  22. if(usableFigures.size() > 0) {
  23. do {
  24. figId = p.choose(usableFigures);
  25. } while(figId == -1);
  26. g.setFigure(figId, dice, p, g);
  27. }
  28. } while (g.checkDice(dice, p, c));
  29. if(p.checkGameWin(p.figures)) {
  30. winner = p;
  31. System.out.println("Spieler " + winner.name + " gewinnt!");
  32. exit(42);
  33. }
  34. }
  35. }
  36. }
  37. public Game() {
  38. this.gb = new Gameboard();
  39. gb.initGameboard();
  40. players = new ArrayList<>();
  41. players.add(new Player("Rot",0, 40, 39));
  42. players.add(new Player("Blau",10, 44, 9));
  43. players.add(new Player("Gelb",20, 48, 19));
  44. players.add(new Player("Grün",30, 52, 29));
  45. }
  46. public String printGameboard(Game g, Player p) {
  47. return gb.printBoard(g, p);
  48. }
  49. public static void clearScreen() {
  50. System.out.print("\033[H\033[2J");
  51. System.out.flush();
  52. }
  53. /**
  54. * Check if the Player is allowed to roll the dice again
  55. * @param dice Value of dice got from rolldice()
  56. * @param p active Player
  57. * @param countRolls Counter how often the Player already rolled the dice
  58. * @return true if Player can roll the dice another time
  59. */
  60. public boolean checkDice(int dice, Player p, int countRolls) {
  61. boolean figuresOnBoard = false;
  62. for(Figure f : p.figures) {
  63. if (f.getPosition() > -1 && f.getPosition() < 40) {
  64. figuresOnBoard = true;
  65. break;
  66. }
  67. }
  68. if(!figuresOnBoard) {
  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; //TODO Wenn Figur auf Startpos von anderer Figur blockiert ist
  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() != -1) {
  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 {
  150. preCalculated = (p.figures.get(figId).getPosition() + dice) % 40;
  151. }
  152. int kicked = 0;
  153. for(Player currentPlayer : g.players) {
  154. for(Figure currentFigure : currentPlayer.figures) {
  155. if(currentFigure.getPosition() == preCalculated) {
  156. currentFigure.setPosition(-1);
  157. kicked = 1;
  158. }
  159. }
  160. }
  161. p.figures.get(figId).setPosition(preCalculated);
  162. return kicked;
  163. }
  164. }