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.

88 lines
2.8 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. scanner = new Scanner(System.in);
  66. System.out.print("Wählen Sie eine Figur " + usableFigures.toString() + ": ");
  67. try{
  68. int input = scanner.nextInt();
  69. if (input > Collections.max(usableFigures) || input < Collections.min(usableFigures)) {
  70. System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" +
  71. "Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
  72. return -1;
  73. }
  74. return input;
  75. } catch (Exception e) {
  76. System.out.println("Die Eingabe hat keine Zahl bekommen.\n" +
  77. "Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
  78. return -1;
  79. }
  80. }
  81. }