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.

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