commit 967abdba8fb0ef07d061ec684c445e520e607913 Author: Johannes Theiner Date: Fri Mar 9 09:20:11 2018 +0100 first, and last commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5b1139 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +Monopoly.iml \ No newline at end of file diff --git a/monopoly.png b/monopoly.png new file mode 100644 index 0000000..dfc194a Binary files /dev/null and b/monopoly.png differ diff --git a/monopoly.uml b/monopoly.uml new file mode 100644 index 0000000..db20b70 --- /dev/null +++ b/monopoly.uml @@ -0,0 +1,699 @@ + + + JAVA + + + de.joethei.monopoly.colors.ConsoleColor + de.joethei.monopoly.colors.UnderlinedColor + de.joethei.monopoly.colors.BackgroundBrightColor + de.joethei.monopoly.colors.BoldBrightColor + de.joethei.monopoly.fields.TrainStation + de.joethei.monopoly.Player + de.joethei.monopoly.actions.BuildHouseAction + de.joethei.monopoly.fields.Factory + de.joethei.monopoly.action.Card + de.joethei.monopoly.fields.Event + de.joethei.monopoly.fields.Street + de.joethei.monopoly.fields.Tax + de.joethei.monopoly.colors.NormalColor + de.joethei.monopoly.Monopoly + de.joethei.monopoly.action.Action + de.joethei.monopoly.action.ActionType + de.joethei.monopoly.colors.BoldColor + de.joethei.monopoly.fields.Field + de.joethei.monopoly.actions.BuyFieldAction + de.joethei.monopoly.fields.OwnableField + de.joethei.monopoly.CircularLinkedList + de.joethei.monopoly.fields.FreeParking + de.joethei.monopoly.actions.SellFieldAction + de.joethei.monopoly.actions.PayRentAction + de.joethei.monopoly.colors.BackgroundColor + de.joethei.monopoly.colors.BrightColor + de.joethei.monopoly.fields.Jail + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + de.joethei.monopoly.colors.UnderlinedColor + + + Fields + Inner Classes + Constructors + Properties + Methods + + All + private + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c045855 --- /dev/null +++ b/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + de.joethei + Monopoly + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-jar-plugin + + + + de.joethei.monopoly.Monopoly + + + + + + + + + + org.projectlombok + lombok + 1.16.20 + + + + \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/Action.java b/src/main/java/de/joethei/monopoly/Action.java new file mode 100644 index 0000000..5e3e881 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/Action.java @@ -0,0 +1,16 @@ +package de.joethei.monopoly; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter @AllArgsConstructor +public abstract class Action { + + private String name; + public abstract void execute(Player player); + + @Override + public String toString() { + return name; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/CircularLinkedList.java b/src/main/java/de/joethei/monopoly/CircularLinkedList.java new file mode 100644 index 0000000..6d65aeb --- /dev/null +++ b/src/main/java/de/joethei/monopoly/CircularLinkedList.java @@ -0,0 +1,14 @@ +package de.joethei.monopoly; + +import java.util.LinkedList; + +public class CircularLinkedList extends LinkedList { + + @Override + public E get(int index) { + while (index >= size()) { + index = index - size(); + } + return super.get(index); + } +} diff --git a/src/main/java/de/joethei/monopoly/Main.java b/src/main/java/de/joethei/monopoly/Main.java new file mode 100644 index 0000000..91a0855 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/Main.java @@ -0,0 +1,86 @@ +package de.joethei.monopoly; + +import de.joethei.monopoly.colors.BoldColor; +import de.joethei.monopoly.colors.ConsoleColor; +import de.joethei.monopoly.colors.NormalColor; +import de.joethei.monopoly.fields.Field; +import de.joethei.monopoly.fields.Street; +import lombok.Getter; + +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class Main { + @Getter private static List players = new LinkedList<>(); + @Getter private static CircularLinkedList fields = new CircularLinkedList<>(); + + public static void main(String[] args) { + Random random = new Random(); + + fields.add(new Street("Hausweg", BoldColor.RED)); + fields.add(new Street("Torstraße", BoldColor.RED)); + fields.add(new Street("Baumstraße", BoldColor.BLUE)); + fields.add(new Street("Hotelstraße", BoldColor.BLUE)); + fields.add(new Street("Straßenweg", BoldColor.GREEN)); + fields.add(new Street("Hauptstraße", BoldColor.GREEN)); + fields.add(new Street("Mauerstraße", BoldColor.CYAN)); + fields.add(new Street("TesterStraße", BoldColor.CYAN)); + + + Scanner scanner = new Scanner(System.in); + + System.out.println("Spieleranzahl eingeben"); + int playerNumber = scanner.nextInt(); + + 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])); + } + + boolean running = true; + while(running) { + if(players.size() <= 0) { + Player player = players.get(0); + System.out.println(player.getColor().getColorCode() + player.getName() + " hat gewonnen"); + running = false; + } + for(Player player : players) { + if(player.getMoney() <= 0) { + System.out.println(player.getColor().getColorCode() + player.getName() + " ist ausgeschieden"); + players.remove(player); + } + Field field = fields.get(player.getField() + (random.nextInt(10) + 2)); + + if(field instanceof Street) { + Street street = (Street) field; + if(street.getOwner() == null) { + System.out.println(player.getColor().getColorCode() + + "Möchtest du das Feld " + street.getColor().getColorCode() + field.getName() + + player.getColor().getColorCode() + " kaufen ?"); + if(scanner.next().equalsIgnoreCase("ja")) { + player.buy(street); + } + }else { + if(street.getOwner() == player && street.getHouses() < 5) { + System.out.println(player.getColor().getColorCode() + "Möchtest du auf dem Feld " + street.getColor().getColorCode() + + street.getName() + player.getColor().getColorCode() + " ein Haus bauen ?"); + if(scanner.next().equalsIgnoreCase("ja")) field.getOptionalAction().execute(player); + } + field.getRequiredAction().execute(player); + } + } + try { + Thread.sleep(1000L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + + public static long getFieldsByColor(ConsoleColor color) { + return fields.stream().filter(field -> field instanceof Street && ((Street) field).getColor().equals(color)).count(); + } +} diff --git a/src/main/java/de/joethei/monopoly/Monopoly.java b/src/main/java/de/joethei/monopoly/Monopoly.java new file mode 100644 index 0000000..35de85d --- /dev/null +++ b/src/main/java/de/joethei/monopoly/Monopoly.java @@ -0,0 +1,263 @@ +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 players = new LinkedList<>(); + private static Map playersInJail = new LinkedHashMap<>(); + private static List fields = new CircularLinkedList<>(); + private static List cards = new LinkedList<>(); + private static Random random; + private static ConcurrentMap 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 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 getPlayers() { + return Monopoly.players; + } + + public static Map getPlayersInJail() { + return Monopoly.playersInJail; + } + + public static List getFields() { + return Monopoly.fields; + } + + public static List getCards() { + return Monopoly.cards; + } + + public static ConcurrentMap getStatistics() { + return Monopoly.statistics; + } + + public static Random getRandom() { + return Monopoly.random; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/Player.java b/src/main/java/de/joethei/monopoly/Player.java new file mode 100644 index 0000000..fdfb612 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/Player.java @@ -0,0 +1,63 @@ +package de.joethei.monopoly; + +import de.joethei.monopoly.colors.ConsoleColor; +import de.joethei.monopoly.colors.NormalColor; +import de.joethei.monopoly.fields.OwnableField; + +import java.util.LinkedList; +import java.util.List; + +public class Player { + private List owning = new LinkedList<>(); + private String name; + private int field; + private int jailFreeCards = 0; + private long money = 1000; + private ConsoleColor color; + + Player(String name, ConsoleColor color) { + this.name = name; + this.color = color; + } + + @Override + public String toString() { + return color.getColorCode() + name + ": " + " | " + money + NormalColor.RESET.getColorCode(); + } + + public List getOwning() { + return this.owning; + } + + public String getName() { + return this.name; + } + + public int getField() { + return this.field; + } + + public int getJailFreeCards() { + return this.jailFreeCards; + } + + public long getMoney() { + return this.money; + } + + public ConsoleColor getColor() { + return this.color; + } + + public void setField(int field) { + this.field = field; + } + + public void setJailFreeCards(int jailFreeCards) { + this.jailFreeCards = jailFreeCards; + } + + public void setMoney(long money) { + this.money = money; + } +} diff --git a/src/main/java/de/joethei/monopoly/action/Action.java b/src/main/java/de/joethei/monopoly/action/Action.java new file mode 100644 index 0000000..b78f2a3 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/action/Action.java @@ -0,0 +1,53 @@ +package de.joethei.monopoly.action; + +import de.joethei.monopoly.Monopoly; +import de.joethei.monopoly.Player; +import de.joethei.monopoly.fields.Field; +import de.joethei.monopoly.fields.OwnableField; + +public abstract class Action { + + private String name; + private Field field; + private ActionType actionType; + + public Action(String name, Field field, ActionType actionType) { + this.name = name; + this.field = field; + this.actionType = actionType; + } + + public abstract void execute(Player player); + + public boolean isRequired() { + return actionType == ActionType.REQUIRED || actionType == ActionType.REQUIRED_OWNER; + } + + + public boolean canExecute(Player player) { + if(Monopoly.getFields().get(player.getField()).equals(field)) { + + if(field instanceof OwnableField) { + OwnableField ownableField = (OwnableField) field; + if(actionType == ActionType.OPTIONAL && ownableField.getOwner() == null) return true; + if(actionType == ActionType.OPTIONAL_OWNER && ownableField.getOwner() == player) return true; + if(actionType == ActionType.REQUIRED && ownableField.getOwner() != player) return true; + return false; + } + return true; + } + return false; + } + + public String getName() { + return name; + } + + public Field getField() { + return field; + } + + public ActionType getActionType() { + return actionType; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/action/ActionType.java b/src/main/java/de/joethei/monopoly/action/ActionType.java new file mode 100644 index 0000000..05ce40d --- /dev/null +++ b/src/main/java/de/joethei/monopoly/action/ActionType.java @@ -0,0 +1,9 @@ +package de.joethei.monopoly.action; + +public enum ActionType { + + REQUIRED, + OPTIONAL, + REQUIRED_OWNER, + OPTIONAL_OWNER +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/action/Card.java b/src/main/java/de/joethei/monopoly/action/Card.java new file mode 100644 index 0000000..b7ea798 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/action/Card.java @@ -0,0 +1,54 @@ +package de.joethei.monopoly.action; + +import de.joethei.monopoly.Monopoly; +import de.joethei.monopoly.Player; +import de.joethei.monopoly.colors.BrightColor; +import de.joethei.monopoly.colors.NormalColor; + +public class Card { + + private String name; + private int money; + private boolean positive; + private boolean otherPlayers; + + public Card(String name, int money, boolean positive, boolean otherPlayers) { + this.name = name; + this.money = money; + this.positive = positive; + this.otherPlayers = otherPlayers; + } + + public void execute(Player player) { + if(otherPlayers) { + if(positive) { + System.out.println(name + " " + BrightColor.GREEN.getColorCode() + "+" + (money * (Monopoly.getPlayers().size() -1)) + "€" + NormalColor.RESET.getColorCode()); + for(Player players : Monopoly.getPlayers()) { + if(players != player) { + player.setMoney(player.getMoney() + money); + players.setMoney(players.getMoney() - money); + } + } + }else { + System.out.println(name + " " + NormalColor.RED.getColorCode() + "-" + (money * (Monopoly.getPlayers().size() -1)) + "€" + NormalColor.RESET.getColorCode()); + for(Player players : Monopoly.getPlayers()) { + if(players != player) { + player.setMoney(player.getMoney() - money); + players.setMoney(players.getMoney() + money); + } + } + } + + }else { + if(positive) { + System.out.println(name + " " + BrightColor.GREEN.getColorCode() + "+" + money + "€" + NormalColor.RESET.getColorCode()); + player.setMoney(player.getMoney() + money); + } + else { + System.out.println(name + " " + NormalColor.RED.getColorCode() + "-" + money + "€" + NormalColor.RESET.getColorCode()); + player.setMoney(player.getMoney() - money); + } + } + + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/actions/BuildHouseAction.java b/src/main/java/de/joethei/monopoly/actions/BuildHouseAction.java new file mode 100644 index 0000000..f698e19 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/actions/BuildHouseAction.java @@ -0,0 +1,47 @@ +package de.joethei.monopoly.actions; + +import de.joethei.monopoly.Monopoly; +import de.joethei.monopoly.Player; +import de.joethei.monopoly.action.Action; +import de.joethei.monopoly.action.ActionType; +import de.joethei.monopoly.fields.Field; +import de.joethei.monopoly.fields.OwnableField; +import de.joethei.monopoly.fields.Street; + +public class BuildHouseAction extends Action { + + public BuildHouseAction(Field field) { + super("ein Haus bauen", field, ActionType.OPTIONAL_OWNER); + } + + @Override + public void execute(Player player) { + if (player.getMoney() <= 100) { + System.out.println(player.getColor().getColorCode() + "Du hast nicht genug Geld"); + }else { + Street street = (Street) getField(); + int i = 0; + for(OwnableField field : street.getOwner().getOwning()) { + if(field instanceof Street) { + Street s = (Street) field; + if(s.getColor().equals(street.getColor())) { + i++; + } + } + } + if (i >= Monopoly.getFieldsByColor(street.getColor())) { + if (street.getHouses() <= 4) { + street.setHouses(street.getHouses() + 1); + System.out.println("Haus gebaut"); + player.setMoney(player.getMoney() - 100); + Monopoly.getStatistics().get("buildHouses").incrementAndGet(); + } else if (street.getHouses() == 5) { + System.out.println("Du hast bereits ein Hotel in dieser Straße"); + } + }else { + System.out.println("Du besitzt nicht alle Straßen dieser Farbe"); + } + } + + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/actions/BuyFieldAction.java b/src/main/java/de/joethei/monopoly/actions/BuyFieldAction.java new file mode 100644 index 0000000..7ea02ed --- /dev/null +++ b/src/main/java/de/joethei/monopoly/actions/BuyFieldAction.java @@ -0,0 +1,27 @@ +package de.joethei.monopoly.actions; + +import de.joethei.monopoly.Player; +import de.joethei.monopoly.action.Action; +import de.joethei.monopoly.action.ActionType; +import de.joethei.monopoly.fields.Field; +import de.joethei.monopoly.fields.OwnableField; + +public class BuyFieldAction extends Action{ + + public BuyFieldAction(Field field) { + super("das Feld kaufen", field, ActionType.OPTIONAL); + } + + @Override + public void execute(Player player) { + OwnableField field = (OwnableField) getField(); + if(field.getOwner() == null) { + if(player.getMoney() >= field.getPrice()) { + player.setMoney(player.getMoney() - field.getPrice()); + player.getOwning().add(field); + field.setOwner(player); + System.out.println(player.getColor().getColorCode() + "Gekauft!"); + }else System.out.println(player.getColor().getColorCode() + "Du hast nicht genug Geld"); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/actions/PayRentAction.java b/src/main/java/de/joethei/monopoly/actions/PayRentAction.java new file mode 100644 index 0000000..1b9e92b --- /dev/null +++ b/src/main/java/de/joethei/monopoly/actions/PayRentAction.java @@ -0,0 +1,32 @@ +package de.joethei.monopoly.actions; + +import de.joethei.monopoly.Player; +import de.joethei.monopoly.action.Action; +import de.joethei.monopoly.action.ActionType; +import de.joethei.monopoly.fields.Field; +import de.joethei.monopoly.fields.OwnableField; +import de.joethei.monopoly.fields.Street; + +public class PayRentAction extends Action{ + + private int rent; + + public PayRentAction(Field field, int rent) { + super("Miete bezahlt", field, ActionType.REQUIRED); + this.rent = rent; + } + + @Override + public void execute(Player player) { + OwnableField field = (OwnableField) getField(); + if(field.getOwner() != null) { + int money = rent; + if(field instanceof Street) { + Street street = (Street) field; + money = rent * street.getHouses(); + } + field.getOwner().setMoney(field.getOwner().getMoney() + money); + player.setMoney(player.getMoney() - money); + } + } +} diff --git a/src/main/java/de/joethei/monopoly/actions/SellFieldAction.java b/src/main/java/de/joethei/monopoly/actions/SellFieldAction.java new file mode 100644 index 0000000..374a9f3 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/actions/SellFieldAction.java @@ -0,0 +1,30 @@ +package de.joethei.monopoly.actions; + +import de.joethei.monopoly.Player; +import de.joethei.monopoly.action.Action; +import de.joethei.monopoly.action.ActionType; +import de.joethei.monopoly.fields.Field; +import de.joethei.monopoly.fields.OwnableField; +import de.joethei.monopoly.fields.Street; + +public class SellFieldAction extends Action{ + + public SellFieldAction(Field field) { + super("das Feld verkaufen", field, ActionType.OPTIONAL_OWNER); + } + + @Override + public void execute(Player player) { + OwnableField field = (OwnableField) getField(); + if(field.getOwner() == player) { + int price; + if(field instanceof Street) { + Street street = (Street) field; + price = field.getPrice() + (street.getHouses() * 100); + }else price = field.getPrice(); + player.setMoney(player.getMoney() + price); + player.getOwning().remove(field); + field.setOwner(null); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/colors/BackgroundBrightColor.java b/src/main/java/de/joethei/monopoly/colors/BackgroundBrightColor.java new file mode 100644 index 0000000..17c0ca9 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/colors/BackgroundBrightColor.java @@ -0,0 +1,24 @@ +package de.joethei.monopoly.colors; + +public enum BackgroundBrightColor implements ConsoleColor{ + + BLACK("\033[0;100m"), + RED("\033[0;101m"), + GREEN("\033[0;102m"), + YELLOW("\033[0;103m"), + BLUE("\033[0;104m"), + PURPLE("\033[0;105m"), + CYAN("\033[0;106m"), + WHITE("\033[0;107m"); + + private String colorCode; + + BackgroundBrightColor(String colorCode) { + this.colorCode = colorCode; + } + + @Override + public String getColorCode() { + return colorCode; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/colors/BackgroundColor.java b/src/main/java/de/joethei/monopoly/colors/BackgroundColor.java new file mode 100644 index 0000000..f73c4e2 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/colors/BackgroundColor.java @@ -0,0 +1,23 @@ +package de.joethei.monopoly.colors; + +public enum BackgroundColor implements ConsoleColor{ + + BLACK_BACKGROUND("\033[40m"), + RED_BACKGROUND("\033[41m"), + GREEN_BACKGROUND("\033[42m"), + YELLOW_BACKGROUND("\033[43m"), + BLUE_BACKGROUND("\033[44m"), + PURPLE_BACKGROUND("\033[45m"), + CYAN_BACKGROUND("\033[46m"), + WHITE_BACKGROUND("\033[47m"); + + private String colorCode; + + BackgroundColor(String colorCode) { + this.colorCode = colorCode; + } + + public String getColorCode() { + return this.colorCode; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/colors/BoldBrightColor.java b/src/main/java/de/joethei/monopoly/colors/BoldBrightColor.java new file mode 100644 index 0000000..17ef796 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/colors/BoldBrightColor.java @@ -0,0 +1,23 @@ +package de.joethei.monopoly.colors; + +public enum BoldBrightColor { + + BLACK("\033[1;90m"), + RED("\033[1;91m"), + GREEN("\033[1;92m"), + YELLOW("\033[1;93m"), + BLUE("\033[1;94m"), + PURPLE("\033[1;95m"), + CYAN("\033[1;96m"), + WHITE("\033[1;97m"); + + private String colorCode; + + BoldBrightColor(String colorCode) { + this.colorCode = colorCode; + } + + public String getColorCode() { + return this.colorCode; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/colors/BoldColor.java b/src/main/java/de/joethei/monopoly/colors/BoldColor.java new file mode 100644 index 0000000..a5b7f45 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/colors/BoldColor.java @@ -0,0 +1,23 @@ +package de.joethei.monopoly.colors; + +public enum BoldColor implements ConsoleColor{ + + BLACK("\033[1;30m"), + RED("\033[1;31m"), + GREEN("\033[1;32m"), + YELLOW("\033[1;33m"), + BLUE("\033[1;34m"), + PURPLE("\033[1;35m"), + CYAN("\033[1;36m"), + WHITE("\033[1;37m"); + + private String colorCode; + + BoldColor(String colorCode) { + this.colorCode = colorCode; + } + + public String getColorCode() { + return this.colorCode; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/colors/BrightColor.java b/src/main/java/de/joethei/monopoly/colors/BrightColor.java new file mode 100644 index 0000000..28ecb93 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/colors/BrightColor.java @@ -0,0 +1,24 @@ +package de.joethei.monopoly.colors; + +public enum BrightColor implements ConsoleColor{ + + BLACK("\033[0;90m"), + RED("\033[0;91m"), + GREEN("\033[0;92m"), + YELLOW("\033[0;93m"), + BLUE("\033[0;94m"), + PURPLE("\033[0;95m"), + CYAN("\033[0;96m"), + WHITE("\033[0;97m"); + + private String colorCode; + + BrightColor(String colorCode) { + this.colorCode = colorCode; + } + + @Override + public String getColorCode() { + return colorCode; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/colors/ConsoleColor.java b/src/main/java/de/joethei/monopoly/colors/ConsoleColor.java new file mode 100644 index 0000000..3db3709 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/colors/ConsoleColor.java @@ -0,0 +1,11 @@ +package de.joethei.monopoly.colors; + + +/** + * Sorgt für Farbausgaben in der Konsole + * Funktioniert nur in Konsolen die ANSI unterstützen(also nicht unter Windows) + */ +public interface ConsoleColor { + + public String getColorCode(); +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/colors/NormalColor.java b/src/main/java/de/joethei/monopoly/colors/NormalColor.java new file mode 100644 index 0000000..c52d71c --- /dev/null +++ b/src/main/java/de/joethei/monopoly/colors/NormalColor.java @@ -0,0 +1,23 @@ +package de.joethei.monopoly.colors; + +public enum NormalColor implements ConsoleColor{ + + RED("\033[0;31m"), + GREEN("\033[0;32m"), + YELLOW("\033[0;33m"), + BLUE("\033[0;34m"), + PURPLE("\033[0;35m"), + CYAN("\033[0;36m"), + WHITE("\033[0;37m"), + RESET("\033[0m"); + + private String colorCode; + + NormalColor(String colorCode) { + this.colorCode = colorCode; + } + + public String getColorCode() { + return this.colorCode; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/colors/UnderlinedColor.java b/src/main/java/de/joethei/monopoly/colors/UnderlinedColor.java new file mode 100644 index 0000000..1049541 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/colors/UnderlinedColor.java @@ -0,0 +1,22 @@ +package de.joethei.monopoly.colors; + +public enum UnderlinedColor implements ConsoleColor{ + + BLACK("\033[4;30m"), + RED("\033[4;31m"), + GREEN("\033[4;32m"), + YELLOW("\033[4;33m"), + BLUE("\033[4;34m"), + PURPLE("\033[4;35m"), + CYAN("\033[4;36m"); + + private String colorCode; + + UnderlinedColor(String colorCode) { + this.colorCode = colorCode; + } + + public String getColorCode() { + return this.colorCode; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/fields/Event.java b/src/main/java/de/joethei/monopoly/fields/Event.java new file mode 100644 index 0000000..c4e4d28 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/fields/Event.java @@ -0,0 +1,21 @@ +package de.joethei.monopoly.fields; + +import de.joethei.monopoly.Monopoly; +import de.joethei.monopoly.Player; +import de.joethei.monopoly.action.Action; +import de.joethei.monopoly.action.ActionType; + +public class Event extends Field{ + + public Event() { + super("Ereignisfeld"); + getActions().add(new Action("Karte erhalten und ausführen", this, ActionType.REQUIRED) { + @Override + public void execute(Player player) { + Monopoly.getStatistics().get("eventCards").incrementAndGet(); + int random = Monopoly.getRandom().nextInt(Monopoly.getCards().size()); + Monopoly.getCards().get(random).execute(player); + } + }); + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/fields/Factory.java b/src/main/java/de/joethei/monopoly/fields/Factory.java new file mode 100644 index 0000000..3fe54f4 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/fields/Factory.java @@ -0,0 +1,15 @@ +package de.joethei.monopoly.fields; + +import de.joethei.monopoly.actions.BuyFieldAction; +import de.joethei.monopoly.actions.PayRentAction; +import de.joethei.monopoly.actions.SellFieldAction; + +public class Factory extends OwnableField{ + + public Factory(String name, int price) { + super(name, price); + getActions().add(new BuyFieldAction(this)); + getActions().add(new SellFieldAction(this)); + getActions().add(new PayRentAction(this, 100)); + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/fields/Field.java b/src/main/java/de/joethei/monopoly/fields/Field.java new file mode 100644 index 0000000..0eaaa21 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/fields/Field.java @@ -0,0 +1,23 @@ +package de.joethei.monopoly.fields; + +import de.joethei.monopoly.action.Action; + +import java.util.ArrayList; + +public class Field { + + private ArrayList actions = new ArrayList<>(); + private String name; + + public Field(String name) { + this.name = name; + } + + public ArrayList getActions() { + return this.actions; + } + + public String getName() { + return this.name; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/fields/FreeParking.java b/src/main/java/de/joethei/monopoly/fields/FreeParking.java new file mode 100644 index 0000000..19151a3 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/fields/FreeParking.java @@ -0,0 +1,7 @@ +package de.joethei.monopoly.fields; + +public class FreeParking extends Field{ + public FreeParking() { + super("Frei parken"); + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/fields/Jail.java b/src/main/java/de/joethei/monopoly/fields/Jail.java new file mode 100644 index 0000000..df80d26 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/fields/Jail.java @@ -0,0 +1,19 @@ +package de.joethei.monopoly.fields; + +import de.joethei.monopoly.Monopoly; +import de.joethei.monopoly.Player; +import de.joethei.monopoly.action.Action; +import de.joethei.monopoly.action.ActionType; + +public class Jail extends Field { + + public Jail() { + super("Gefängnis"); + getActions().add(new Action("eingebuchtet", this, ActionType.REQUIRED) { + @Override + public void execute(Player player) { + Monopoly.getPlayersInJail().put(player, 3); + } + }); + } +} diff --git a/src/main/java/de/joethei/monopoly/fields/OwnableField.java b/src/main/java/de/joethei/monopoly/fields/OwnableField.java new file mode 100644 index 0000000..a684299 --- /dev/null +++ b/src/main/java/de/joethei/monopoly/fields/OwnableField.java @@ -0,0 +1,27 @@ +package de.joethei.monopoly.fields; + +import de.joethei.monopoly.Player; + +public class OwnableField extends Field { + + private Player owner; + private int price; + + OwnableField(String name, int price) { + super(name); + this.price = price; + } + + public Player getOwner() { + return this.owner; + } + + public int getPrice() { + return this.price; + } + + public void setOwner(Player owner) { + this.owner = owner; + } + +} diff --git a/src/main/java/de/joethei/monopoly/fields/Street.java b/src/main/java/de/joethei/monopoly/fields/Street.java new file mode 100644 index 0000000..bf0561d --- /dev/null +++ b/src/main/java/de/joethei/monopoly/fields/Street.java @@ -0,0 +1,40 @@ +package de.joethei.monopoly.fields; + +import de.joethei.monopoly.actions.BuildHouseAction; +import de.joethei.monopoly.actions.BuyFieldAction; +import de.joethei.monopoly.actions.PayRentAction; +import de.joethei.monopoly.actions.SellFieldAction; +import de.joethei.monopoly.colors.ConsoleColor; +import de.joethei.monopoly.colors.NormalColor; +public class Street extends OwnableField { + + private ConsoleColor color; + private int houses = 0; + + public Street(String name, int price, ConsoleColor color) { + super(name, price); + this.color = color; + + getActions().add(new BuyFieldAction(this)); + getActions().add(new SellFieldAction(this)); + getActions().add(new BuildHouseAction(this)); + getActions().add(new PayRentAction(this, 50)); + } + + public ConsoleColor getColor() { + return color; + } + + public int getHouses() { + return houses; + } + + public void setHouses(int houses) { + this.houses = houses; + } + + @Override + public String getName() { + return color.getColorCode() + super.getName() + NormalColor.RESET.getColorCode(); + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/fields/Tax.java b/src/main/java/de/joethei/monopoly/fields/Tax.java new file mode 100644 index 0000000..5a5e42c --- /dev/null +++ b/src/main/java/de/joethei/monopoly/fields/Tax.java @@ -0,0 +1,20 @@ +package de.joethei.monopoly.fields; + +import de.joethei.monopoly.Monopoly; +import de.joethei.monopoly.Player; +import de.joethei.monopoly.action.Action; +import de.joethei.monopoly.action.ActionType; + +public class Tax extends Field{ + + public Tax(String name, int tax) { + super(name); + getActions().add(new Action("Steuer zahlen", this, ActionType.REQUIRED) { + @Override + public void execute(Player player) { + Monopoly.getStatistics().get("tax").addAndGet(tax); + player.setMoney(player.getMoney() - tax); + } + }); + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/monopoly/fields/TrainStation.java b/src/main/java/de/joethei/monopoly/fields/TrainStation.java new file mode 100644 index 0000000..302e3ac --- /dev/null +++ b/src/main/java/de/joethei/monopoly/fields/TrainStation.java @@ -0,0 +1,15 @@ +package de.joethei.monopoly.fields; + +import de.joethei.monopoly.actions.BuyFieldAction; +import de.joethei.monopoly.actions.PayRentAction; +import de.joethei.monopoly.actions.SellFieldAction; + +public class TrainStation extends OwnableField{ + + public TrainStation(String name, int price) { + super(name, price); + getActions().add(new BuyFieldAction(this)); + getActions().add(new SellFieldAction(this)); + getActions().add(new PayRentAction(this, 25)); + } +}