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

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) {
StringBuilder out = new StringBuilder("[");
for(int i = 0; i<usableFigures.size(); i++) {
out.append(usableFigures.get(i) + 1);
if(i < usableFigures.size()-1) {
out.append(",");
}
}
out.append("]");
scanner = new Scanner(System.in);
System.out.print("Wählen Sie eine Figur " + out + ": ");
try{
int input = scanner.nextInt();
if(input == -1) {
System.exit(0);
}
if (input > Collections.max(usableFigures) + 1 || input < Collections.min(usableFigures) + 1) {
System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" +
"Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1) + " eingeben.");
return -1;
}
return input - 1;
} catch (Exception e) {
System.out.println("Die Eingabe hat keine Zahl bekommen.\n" +
"Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1)+ " eingeben.");
return -1;
}
}
}