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

import java.util.*;
public class Player {
String name;
ArrayList<Figure> figures;
int startPos;
int startHome;
int jumpToHome;
Scanner scanner;
public Player(String name, int startPos, int startHome, int jumpToHome) {
this.name = name;
this.startPos = startPos;
this.startHome = startHome;
this.jumpToHome = jumpToHome;
figures = new ArrayList<>();
for(int i = 0; i < 4; i++) {
figures.add(new Figure());
}
}
/**
* Rolls the dice - Generates a random Number in the range of 1 - 6
* @return value of the dice
*/
public int rollDice() {
return (int) (Math.random() * 6 + 1);
}
/**
* Checks if a Player has won the game with the current play
* @param figures figures of the current Player
* @return true if Player has won
*/
public boolean checkGameWin(ArrayList<Figure> figures) {
Iterator<Figure> it = figures.iterator();
Figure f;
while(it.hasNext()) {
f = it.next();
if(!(f.getPosition() >= startHome && f.getPosition() <= startHome+3)) {
return false;
}
}
return true;
}
/**
* Checks if the Player has figures in the base (off gameboard)
* @param figures figures of the current Player
* @return true if there is at least one figure in the base (off gameboard)
*/
public int checkFigureInBase(ArrayList<Figure> figures) {
Iterator<Figure> it = figures.iterator();
Figure f;
int count = 0;
while(it.hasNext()) {
f = it.next();
if(f.getPosition() == -1) {
count++;
}
}
return count;
}
/**
* Get the user input on witch figure the user wants to move
* @param usableFigures list of usable Figures to choose from
* @return int figid of the figure chosen
*/
public int choose(ArrayList<Integer> usableFigures) {
scanner = new Scanner(System.in);
System.out.print("Wählen Sie eine Figur " + usableFigures.toString() + ": ");
try{
int input = scanner.nextInt();
if (input > Collections.max(usableFigures) || input < Collections.min(usableFigures)) {
System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" +
"Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
return -1;
}
return input;
} catch (Exception e) {
System.out.println("Die Eingabe hat keine Zahl bekommen.\n" +
"Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
return -1;
}
}
}