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.

171 lines
5.8 KiB

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import static java.lang.System.exit;
public class Game {
Gameboard gb;
ArrayList<Player> players;
public static void main(String[] args) throws InterruptedException {
Game g = new Game();
Player winner;
while(true){
for (Player p : g.players) {
int c = 0;
int dice;
System.out.println("Spieler " + p.name + " an der Reihe.");
do {
int figId;
dice = p.rollDice();
TimeUnit.SECONDS.sleep(1L);
System.out.println("Würfel: " + dice);
c++;
ArrayList<Integer> usableFigures = g.getUsableFigures(dice, p, g);
if(usableFigures.size() > 0) {
do {
figId = p.choose(usableFigures);
} while(figId == -1);
g.setFigure(figId, dice, p, g);
}
} while (g.checkDice(dice, p, c));
if(p.checkGameWin(p.figures)) {
winner = p;
System.out.println("Spieler " + winner.name + " gewinnt!");
exit(42);
}
}
}
}
public Game() {
this.gb = new Gameboard();
gb.initGameboard();
players = new ArrayList<>();
players.add(new Player("Rot",0, 40, 39));
players.add(new Player("Blau",10, 44, 9));
players.add(new Player("Gelb",20, 48, 19));
players.add(new Player("Grün",30, 52, 29));
}
public String printGameboard(Game g, Player p) {
return gb.printBoard(g, p);
}
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
/**
* Check if the Player is allowed to roll the dice again
* @param dice Value of dice got from rolldice()
* @param p active Player
* @param countRolls Counter how often the Player already rolled the dice
* @return true if Player can roll the dice another time
*/
public boolean checkDice(int dice, Player p, int countRolls) {
int figuresInBase = p.checkFigureInBase(p.figures);
if(figuresInBase == 4) {
return countRolls < 3;
} else return dice == 6;
}
/**
* Check if a target field is free or occupied by another figure
* @param posToCheck Target position to check
* @param p active Player
* @param g game instance
* @return 2 - own Player 1 - another Player 0 - Field clear
*/
public int checkFieldClear(int posToCheck, Player p, Game g) {
int mode;
for (Player currentPlayer : g.players) {
if (currentPlayer.name.equals(p.name)) {
mode = 2;
} else {
mode = 1;
}
for (Figure f : currentPlayer.figures) {
if (posToCheck == f.getPosition()) {
return mode;
}
}
}
return 0;
}
/**
* Get List of all usable Figures in the current Play for one Player
* @param dice Value of dice got from rolldice()
* @param p active Player
* @param g game instance
* @return List of int with figIds the Player can choose from
*/
public ArrayList<Integer> getUsableFigures(int dice, Player p, Game g) {
ArrayList<Integer> result = new ArrayList<>();
for(int i = 0; i < p.figures.size(); i++) {
if(figureIsUsable(dice, i, p, g)) {
result.add(i);
}
}
return result;
}
/**
* Check if a figure is usable
* @param dice Value of dice got from rolldice()
* @param figId id of figure to check
* @param p active Player
* @param g game instance
* @return true if figure is usable
*/
public boolean figureIsUsable(int dice, int figId, Player p, Game g) {
Figure f = p.figures.get(figId);
if(p.checkFigureInBase(p.figures) > 0 && dice == 6){
if(g.checkFieldClear(p.startPos, p, g) == 2) {
return f.getPosition() == p.startPos; //TODO Wenn Figur auf Startpos von anderer Figur blockiert ist
}
return f.getPosition() == -1;
} else if (f.getPosition() <= p.jumpToHome && (f.getPosition() + dice) > p.jumpToHome) {
if((f.getPosition() + dice) <= p.jumpToHome + 4) {
return g.checkFieldClear(f.getPosition() + dice - p.jumpToHome + p.startHome - 1, p, g) != 2;
} else {
return false;
}
} else if(f.getPosition() != -1) {
return g.checkFieldClear((f.getPosition() + dice) % 40, p, g) != 2;
}
return false;
}
/**
* Set a figure on a field according
* @param figId id of figure to check
* @param dice Value of dice got from rolldice()
* @param p active Player
* @param g game instance
* @return 0 if no figure kicked. 1 if another figure was kicked
*/
public int setFigure(int figId, int dice, Player p, Game g) {
int preCalculated;
if(p.figures.get(figId).getPosition() == -1) {
preCalculated = p.startPos;
} else {
preCalculated = (p.figures.get(figId).getPosition() + dice) % 40;
}
int kicked = 0;
for(Player currentPlayer : g.players) {
for(Figure currentFigure : currentPlayer.figures) {
if(currentFigure.getPosition() == preCalculated) {
currentFigure.setPosition(-1);
kicked = 1;
}
}
}
p.figures.get(figId).setPosition(preCalculated);
return kicked;
}
}