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.

100 lines
3.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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. Iterator<Integer> it = usableFigures.iterator();
  67. for(int i = 0; i<usableFigures.size(); i++) {
  68. out.append(usableFigures.get(i) + 1);
  69. if(i < usableFigures.size()-1) {
  70. out.append(",");
  71. }
  72. }
  73. out.append("]");
  74. scanner = new Scanner(System.in);
  75. System.out.print("Wählen Sie eine Figur " + out + ": ");
  76. try{
  77. int input = scanner.nextInt();
  78. if(input == -1) {
  79. System.exit(0);
  80. }
  81. if (input > Collections.max(usableFigures) + 1 || input < Collections.min(usableFigures) + 1) {
  82. System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" +
  83. "Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1) + " eingeben.");
  84. return -1;
  85. }
  86. return input - 1;
  87. } catch (Exception e) {
  88. System.out.println("Die Eingabe hat keine Zahl bekommen.\n" +
  89. "Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1)+ " eingeben.");
  90. return -1;
  91. }
  92. }
  93. }