import java.util.ArrayList; import java.util.concurrent.TimeUnit; import static java.lang.System.exit; public class Game { Gameboard gb; ArrayList players; public static void main(String[] args) throws InterruptedException { Game g = new Game(); Player winner; clearScreen(); while(true){ for (Player p : g.players) { int c = 0; int dice; System.out.println(g.printGameboard(g, p)); do { int figId; dice = p.rollDice(); TimeUnit.SECONDS.sleep(1L); System.out.println("Würfel: " + dice); c++; ArrayList usableFigures = g.getUsableFigures(dice, p, g); if(usableFigures.size() > 0) { do { figId = p.choose(usableFigures); } while(figId == -1); g.setFigure(figId, dice, p, g); clearScreen(); System.out.println(g.printGameboard(g, p)); } } while (g.checkDice(dice, p, c)); if(p.checkGameWin(p.figures)) { winner = p; System.out.println("Spieler " + winner.name + " gewinnt!"); exit(42); } TimeUnit.SECONDS.sleep(1L); clearScreen(); } } } 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 getUsableFigures(int dice, Player p, Game g) { ArrayList 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; } 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() >= p.startHome && (f.getPosition() + dice) < p.startHome + 4) { return g.checkFieldClear(f.getPosition() + dice, p, g) == 0; } else if(f.getPosition() != -1 && f.getPosition() < 40) { 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 if (p.figures.get(figId).getPosition() <= p.jumpToHome && (p.figures.get(figId).getPosition() + dice) > p.jumpToHome) { preCalculated = p.startHome + (dice - (p.jumpToHome - p.figures.get(figId).getPosition()) - 1); } else if (p.figures.get(figId).getPosition() >= p.startHome) { preCalculated = p.figures.get(figId).getPosition() + dice; }else { preCalculated = (p.figures.get(figId).getPosition() + dice) % 40; } for(Player currentPlayer : g.players) { for(Figure currentFigure : currentPlayer.figures) { if(currentFigure.getPosition() == preCalculated) { currentFigure.setPosition(-1); } } } p.figures.get(figId).setPosition(preCalculated); return preCalculated; } }