Monopoly/src/main/java/de/joethei/monopoly/Monopoly.java

263 lines
13 KiB
Java

package de.joethei.monopoly;
import de.joethei.monopoly.action.Action;
import de.joethei.monopoly.action.Card;
import de.joethei.monopoly.colors.BrightColor;
import de.joethei.monopoly.colors.ConsoleColor;
import de.joethei.monopoly.colors.NormalColor;
import de.joethei.monopoly.fields.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class Monopoly {
private static List<Player> players = new LinkedList<>();
private static Map<Player, Integer> playersInJail = new LinkedHashMap<>();
private static List<Field> fields = new CircularLinkedList<>();
private static List<Card> cards = new LinkedList<>();
private static Random random;
private static ConcurrentMap<String, AtomicLong> statistics = new ConcurrentHashMap<>();
public static void main(String[] args) {
random = new Random();
Scanner scanner = new Scanner(System.in);
//Anlegen des Spielfeldes
fields.add(new Field("Start"));
fields.add(new Street("Badstraße",40, NormalColor.PURPLE));
fields.add(new Event());
fields.add(new Street("Turmstraße", 80, NormalColor.PURPLE));
fields.add(new Tax("Einkommensteuer", 20));
fields.add(new TrainStation("Südbahnhof", 500));
fields.add(new Street("Chauseestraße", 120, BrightColor.BLUE));
fields.add(new Event());
fields.add(new Street("Elisenstraße", 120, BrightColor.BLUE));
fields.add(new Street("Poststraße", 160, BrightColor.BLUE));
fields.add(new Jail());
fields.add(new Street("Seestraße", 200, BrightColor.PURPLE));
fields.add(new Factory("Elektrizitätswerk", 80));
fields.add(new Street("Hafenstraße", 200, BrightColor.PURPLE));
fields.add(new Street("Neue Straße", 240, BrightColor.PURPLE));
fields.add(new TrainStation("Westbahnhof", 500));
fields.add(new Street("Münchener Straße", 280, NormalColor.YELLOW));
fields.add(new Event());
fields.add(new Street("Wiener Straße", 280, NormalColor.YELLOW));
fields.add(new Street("Berliner Straße",320, NormalColor.YELLOW));
fields.add(new FreeParking());
fields.add(new Street("Theaterstraße", 360, NormalColor.RED));
fields.add(new Event());
fields.add(new Street("Museumsstraße", 360, NormalColor.RED));
fields.add(new Street("Opernplatz", 400, NormalColor.RED));
fields.add(new TrainStation("Nordbahnhof", 500));
fields.add(new Street("Lessingstraße", 440, BrightColor.YELLOW));
fields.add(new Street("Schillerstraße", 440, BrightColor.YELLOW));
fields.add(new Factory("Wasserwerk", 80));
fields.add(new Street("Goethestraße", 480, BrightColor.YELLOW));
fields.add(new Jail());
fields.add(new Street("Rathausplatz", 520, NormalColor.GREEN));
fields.add(new Street("Hauptstraße", 520, NormalColor.GREEN));
fields.add(new Event());
fields.add(new Street("Bahnhofsstraße", 560, NormalColor.GREEN));
fields.add(new TrainStation("Hauptbahnhof", 500));
fields.add(new Event());
fields.add(new Street("Parkstraße", 700, NormalColor.BLUE));
fields.add(new Tax("Zusatzsteuer", 300));
fields.add(new Street("Schloßalle", 1000, NormalColor.BLUE));
//anlegen der Karten
cards.add(new Card("Eins deiner Häuser braucht eine Renovierung", 500, false, false));
cards.add(new Card("Wasserschaden, die Versicherung will nicht zahlen", 1000, false, false));
cards.add(new Card("Du bist zu schnell gefahren", 50, false, false));
cards.add(new Card("Eine deiner Straßen muss ausgebessert werden", 800, false, false));
cards.add(new Card("Du bist betrunken Auto gefahren", 100, false, false));
cards.add(new Card("Einer deiner Mieter hat einen Handwerker benötigt", 500, false, false));
cards.add(new Card("Du wurdest zum Vorstand gewählt", 5, false, true));
cards.add(new Card("3 Felder zurück", 0, false, false) {
@Override
public void execute(Player player) {
System.out.println("Du wurdest 3 Felder nach hinten geworfen");
player.setField(player.getField() - 3);
}
});
cards.add(new Card("Steuerrückzahlung", 1000, true, false));
cards.add(new Card("Du hast im Lotto gewonnen", 2000, true, false));
cards.add(new Card("Zinsen sind fällig", 30, true, false));
cards.add(new Card("Dividende ist fällig", 1000, true, false));
cards.add(new Card("Du hast von einem unbekannten Verwandten geerbt", 2000, true, false));
cards.add(new Card("Du hast Geburtstag, jeder schenkt dir", 50, true, true));
cards.add(new Card("Die Bank hat einen Fehler gemacht", 400, true, false));
cards.add(new Card("Gefängnisfreikarte", 0, true, false) {
@Override
public void execute(Player player) {
System.out.println("Du hast eine Gefängnisfreikarte erhalten");
player.setJailFreeCards(player.getJailFreeCards() + 1);
}
});
//
statistics.put("rounds", new AtomicLong(0));
statistics.put("buildHouses", new AtomicLong(0));
statistics.put("jailRounds", new AtomicLong(0));
statistics.put("jailFreeCards", new AtomicLong(0));
statistics.put("movedFields", new AtomicLong(0));
statistics.put("eventCards", new AtomicLong(0));
statistics.put("tax", new AtomicLong(0));
System.out.println("Spieleranzahl eingeben");
int playerNumber = scanner.nextInt();
if(playerNumber <= 1) {
System.out.println("Zu wenige Spieler");
System.exit(1);
}
else if(playerNumber >= 6) {
System.out.println("Zu viele Spieler");
System.exit(1);
}
for (int i = 0; i < playerNumber; i++) {
System.out.println("Name des " + (players.size() + 1) + ". Spielers eingeben");
players.add(new Player(scanner.next(), NormalColor.values()[i]));
System.out.println("Du hast die Farbe " + NormalColor.values()[i].getColorCode() + NormalColor.values()[i].name() + NormalColor.RESET.getColorCode());
}
System.out.println("Spiel wird gestartet.");
statistics.put("time", new AtomicLong(System.currentTimeMillis()));
List<Player> toRemove = new LinkedList<>();
while(true) {
statistics.get("rounds").incrementAndGet();
for(Player player : players) {
if(player.getMoney() <= 0) {
System.out.println(player.getColor().getColorCode() + player.getName() + " ist pleite");
toRemove.add(player);
}
if(players.size() <= 1) {
Player p = players.get(0);
System.out.println(p.getColor().getColorCode() + p.getName() + " hat gewonnen" + NormalColor.RESET.getColorCode());
System.out.println("========= Ein paar Statistiken ==========");
System.out.println("Gespielte Runden: " + statistics.get("rounds"));
System.out.println("Davon im Gefängnis verbracht: " + statistics.get("jailRounds"));
long time = System.currentTimeMillis() - statistics.get("time").longValue();
System.out.println("Spieldauer: " + String.format("%d Minuten, %d Sekunden", TimeUnit.MILLISECONDS.toMinutes(time), TimeUnit.MILLISECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(time))));
System.out.println("Zurückgelegte Schritte: " + statistics.get("movedFields"));
System.out.println("Gebaute Häuser:" + statistics.get("buildHouses"));
System.out.println("Bezahlte Steuern: " + statistics.get("tax") + "");
System.out.println("Verwendete Ereigniskarten: " + statistics.get("eventCards"));
System.out.println("Davon vewendete Gefängnisfreikarten: " + statistics.get("jailFreeCards"));
System.out.println("=========================================");
System.exit(0);
}
else if(playersInJail.containsKey(player)) {
System.out.println(player.getColor().getColorCode() + "======================" + NormalColor.RESET.getColorCode());
if(playersInJail.get(player) == 1) {
System.out.println("Du wurdest frei gelassen");
playersInJail.remove(player);
}
else if(player.getJailFreeCards() > 0) {
player.setJailFreeCards(player.getJailFreeCards() - 1);
playersInJail.remove(player);
System.out.println("Du hast eine Gefängnisfreikarte verwendet um wieder mitspielen zu dürfen");
statistics.get("jailFreeCards").incrementAndGet();
}else {
playersInJail.replace(player, playersInJail.get(player) - 1);
Monopoly.getStatistics().get("jailRounds").incrementAndGet();
System.out.println("Du musst noch " + playersInJail.get(player) + " Runden warten um wieder mitspielen zu dürfen");
}
System.out.println(player.getColor().getColorCode() + "======================" + NormalColor.RESET.getColorCode());
}else if(!toRemove.contains(player)){
int r = random.nextInt(10) + 2;
int id = player.getField() + r;
statistics.get("movedFields").addAndGet(r);
//Start überschritten oder genau getroffen
if((id + player.getField()) >= fields.size()) {
player.setMoney(player.getMoney() + 200);
}
Field field = fields.get(id);
player.setField(id);
System.out.println(player.getColor().getColorCode() + "======================" + NormalColor.RESET.getColorCode());
System.out.println("Kontostand: " + player.getMoney() + "");
System.out.println("Gewürfelt: " + r);
System.out.println("Feld: " + field.getName());
if(field instanceof OwnableField) {
OwnableField ownableField = (OwnableField) field;
if(ownableField.getOwner() != null) System.out.println("Besitzer: " + ownableField.getOwner().getColor().getColorCode() + ownableField.getOwner().getName() + NormalColor.RESET.getColorCode());
else System.out.println("Preis: " + ((OwnableField) field).getPrice() + "");
}
field.getActions().forEach(action -> {
if(action.canExecute(player) && action.isRequired()) action.execute(player);
});
if(field.getActions().size() > 0) {
System.out.println("Mögliche Aktionen:");
for (Action action : field.getActions()) {
if(action.canExecute(player) && !action.isRequired()) {
System.out.println(field.getActions().indexOf(action) + ": " + action.getName());
}
}
System.out.println("n: nächster Spieler");
String input = scanner.next();
if(!input.equalsIgnoreCase("n")) {
field.getActions().get(Integer.parseInt(input)).execute(player);
}
}
System.out.println(player.getColor().getColorCode() + "======================" + NormalColor.RESET.getColorCode());
}
try {
//ein bisschen warten nach jedem Durchlauf
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(Player player : toRemove) {
players.remove(player);
}
toRemove.clear();
}
}
public static long getFieldsByColor(ConsoleColor color) {
return fields.stream().filter(field -> field instanceof Street && ((Street) field).getColor().equals(color)).count();
}
public static List<Player> getPlayers() {
return Monopoly.players;
}
public static Map<Player, Integer> getPlayersInJail() {
return Monopoly.playersInJail;
}
public static List<Field> getFields() {
return Monopoly.fields;
}
public static List<Card> getCards() {
return Monopoly.cards;
}
public static ConcurrentMap<String, AtomicLong> getStatistics() {
return Monopoly.statistics;
}
public static Random getRandom() {
return Monopoly.random;
}
}