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.

124 lines
4.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 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 boolean checkDice(int dice, Player p, int countRolls) {
  47. int figuresInBase = p.checkFigureInBase(p.figures);
  48. if(figuresInBase == 4) {
  49. return countRolls < 3;
  50. } else return dice == 6;
  51. }
  52. public int checkFieldClear(int posToCheck, Player p, Game g) {
  53. int mode;
  54. for (Player currentPlayer : g.players) {
  55. if (currentPlayer.name.equals(p.name)) {
  56. mode = 2;
  57. } else {
  58. mode = 1;
  59. }
  60. for (Figure f : currentPlayer.figures) {
  61. if (posToCheck == f.getPosition()) {
  62. return mode;
  63. }
  64. }
  65. }
  66. return 0;
  67. }
  68. public ArrayList<Integer> getUsableFigures(int dice, Player p, Game g) {
  69. ArrayList<Integer> result = new ArrayList<>();
  70. for(int i = 0; i < p.figures.size(); i++) {
  71. if(figureIsUsable(dice, i, p, g)) {
  72. result.add(i);
  73. }
  74. }
  75. return result;
  76. }
  77. public boolean figureIsUsable(int dice, int figId, Player p, Game g) {
  78. Figure f = p.figures.get(figId);
  79. if(p.checkFigureInBase(p.figures) > 0 && dice == 6){
  80. if(g.checkFieldClear(p.startPos, p, g) == 2) {
  81. return f.getPosition() == p.startPos; //TODO Wenn Figur auf Startpos von anderer Figur blockiert ist
  82. }
  83. return f.getPosition() == -1;
  84. } else if (f.getPosition() <= p.jumpToHome && (f.getPosition() + dice) > p.jumpToHome) {
  85. if((f.getPosition() + dice) <= p.jumpToHome + 4) {
  86. return g.checkFieldClear(f.getPosition() + dice - p.jumpToHome + p.startHome - 1, p, g) != 2;
  87. } else {
  88. return false;
  89. }
  90. } else if(f.getPosition() != -1) {
  91. return g.checkFieldClear((f.getPosition() + dice) % 40, p, g) != 2;
  92. }
  93. return false;
  94. }
  95. public int setFigure(int figId, int dice, Player p, Game g) {
  96. int preCalculated;
  97. if(p.figures.get(figId).getPosition() == -1) {
  98. preCalculated = p.startPos;
  99. } else {
  100. preCalculated = (p.figures.get(figId).getPosition() + dice) % 40;
  101. }
  102. int kicked = 0;
  103. for(Player currentPlayer : g.players) {
  104. for(Figure currentFigure : currentPlayer.figures) {
  105. if(currentFigure.getPosition() == preCalculated) {
  106. currentFigure.setPosition(-1);
  107. kicked = 1;
  108. }
  109. }
  110. }
  111. p.figures.get(figId).setPosition(preCalculated);
  112. return kicked;
  113. }
  114. }