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.

99 lines
3.2 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
2 years ago
2 years ago
  1. import java.util.*;
  2. public class Player {
  3. String name;
  4. ArrayList<Figure> figures;
  5. int startPos;
  6. int startHome;
  7. int jumpToHome;
  8. Scanner scanner;
  9. public Player(String name, int startPos, int startHome, int jumpToHome) {
  10. this.name = name;
  11. this.startPos = startPos;
  12. this.startHome = startHome;
  13. this.jumpToHome = jumpToHome;
  14. figures = new ArrayList<>();
  15. for(int i = 0; i < 4; i++) {
  16. figures.add(new Figure());
  17. }
  18. }
  19. /**
  20. * Rolls the dice - Generates a random Number in the range of 1 - 6
  21. * @return value of the dice
  22. */
  23. public int rollDice() {
  24. return (int) (Math.random() * 6 + 1);
  25. }
  26. /**
  27. * Checks if a Player has won the game with the current play
  28. * @param figures figures of the current Player
  29. * @return true if Player has won
  30. */
  31. public boolean checkGameWin(ArrayList<Figure> figures) {
  32. Iterator<Figure> it = figures.iterator();
  33. Figure f;
  34. while(it.hasNext()) {
  35. f = it.next();
  36. if(!(f.getPosition() >= startHome && f.getPosition() <= startHome+3)) {
  37. return false;
  38. }
  39. }
  40. return true;
  41. }
  42. /**
  43. * Checks if the Player has figures in the base (off gameboard)
  44. * @param figures figures of the current Player
  45. * @return true if there is at least one figure in the base (off gameboard)
  46. */
  47. public int checkFigureInBase(ArrayList<Figure> figures) {
  48. Iterator<Figure> it = figures.iterator();
  49. Figure f;
  50. int count = 0;
  51. while(it.hasNext()) {
  52. f = it.next();
  53. if(f.getPosition() == -1) {
  54. count++;
  55. }
  56. }
  57. return count;
  58. }
  59. /**
  60. * Get the user input on witch figure the user wants to move
  61. * @param usableFigures list of usable Figures to choose from
  62. * @return int figid of the figure chosen
  63. */
  64. public int choose(ArrayList<Integer> usableFigures) {
  65. StringBuilder out = new StringBuilder("[");
  66. for(int i = 0; i<usableFigures.size(); i++) {
  67. out.append(usableFigures.get(i) + 1);
  68. if(i < usableFigures.size()-1) {
  69. out.append(",");
  70. }
  71. }
  72. out.append("]");
  73. scanner = new Scanner(System.in);
  74. System.out.print("Wählen Sie eine Figur " + out + ": ");
  75. try{
  76. int input = scanner.nextInt();
  77. if(input == -1) {
  78. System.exit(0);
  79. }
  80. if (input > Collections.max(usableFigures) + 1 || input < Collections.min(usableFigures) + 1) {
  81. System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" +
  82. "Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1) + " eingeben.");
  83. return -1;
  84. }
  85. return input - 1;
  86. } catch (Exception e) {
  87. System.out.println("Die Eingabe hat keine Zahl bekommen.\n" +
  88. "Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1)+ " eingeben.");
  89. return -1;
  90. }
  91. }
  92. }