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.

105 lines
3.4 KiB

import java.util.ArrayList;
public class Game {
Gameboard gb;
ArrayList<Player> players;
public static void main(String[] args) {
Game g = new Game();
while(true){
for (Player p : g.players) {
int c = 0;
int dice;
do {
dice = p.rollDice();
c++;
ArrayList<Integer> usableFigures = g.getUsableFigures(dice, p, g);
int figId = p.choose(usableFigures);
} while (g.checkDice(dice, p, c));
p.checkGameWin(p.figures);
}
}
}
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 boolean checkDice(int dice, Player p, int countRolls) {
int figuresInBase = p.checkFigureInBase(p.figures);
if(figuresInBase == 4) {
return countRolls <= 3;
} else return dice == 6;
}
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;
}
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;
}
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;
}
public int setFigure(int figId, int dice, Player p, Game g) {
int 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;
}
}