refactoring in Siedler.java

This commit is contained in:
schrom01 2021-12-07 08:30:14 +01:00
parent 21f144b294
commit d35c7299ae
2 changed files with 46 additions and 24 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -10,6 +10,10 @@ public class Siedler {
//Spiel erstellen
Parser parser = new Parser();
SiedlerGame game = foundingPhase(parser);
gamePhase(parser, game);
}
private static void gamePhase(Parser parser, SiedlerGame game) {
boolean running = true;
boolean diceThrown = false;
while (running){
@ -23,39 +27,23 @@ public class Siedler {
parser.displayPlayerInfo(game.getCurruntPlayerResource(), game.getCurrentPlayerWinpoints());
switch (parser.getAction()) {
case NEXTPLAYER:
Config.Faction winner = game.getWinner();
if(winner == null) {
game.switchToNextPlayer();
diceThrown = false;
} else {
parser.displayWinnertext(winner);
if(caseNextPlayer(parser, game)){
running = false;
} else {
diceThrown = false;
}
break;
case BUILDSETTLEMENT:
parser.giveCoordinatesForStructures(Config.Structure.SETTLEMENT);
if(!game.buildSettlement(parser.getPoint())){
parser.errorMessage();
}
caseBuildStructure(parser, game, Config.Structure.SETTLEMENT);
break;
case BUILDCITY:
parser.giveCoordinatesForStructures(Config.Structure.CITY);
if(!game.buildCity(parser.getPoint())){
parser.errorMessage();
}
caseBuildStructure(parser, game, Config.Structure.CITY);
break;
case BUILDROAD:
parser.giveCoordinatesForStructures(Config.Structure.ROAD);
if(game.buildRoad(parser.getPoint(), parser.getPoint())){
parser.errorMessage();
}
caseBuildStructure(parser, game, Config.Structure.ROAD);
break;
case TRADEWITHBANK:
Config.Resource offer = parser.trade(true);
Config.Resource want = parser.trade(false);
if(!game.tradeWithBankFourToOne(offer, want)){
parser.errorMessage();
}
caseTradeWithBank(parser, game);
break;
case QUIT:
running = false;
@ -67,6 +55,40 @@ public class Siedler {
}
}
private static boolean caseNextPlayer(Parser parser, SiedlerGame game){
Config.Faction winner = game.getWinner();
if(winner == null) {
game.switchToNextPlayer();
return false;
} else {
parser.displayWinnertext(winner);
return true;
}
}
private static void caseTradeWithBank(Parser parser, SiedlerGame game) {
Config.Resource offer = parser.trade(true);
Config.Resource want = parser.trade(false);
if(!game.tradeWithBankFourToOne(offer, want)){
parser.errorMessage();
}
}
private static void caseBuildStructure(Parser parser, SiedlerGame game, Config.Structure structure) {
parser.giveCoordinatesForStructures(structure);
boolean successfully = false;
if(structure == Config.Structure.SETTLEMENT){
successfully = game.buildSettlement(parser.getPoint());
} else if(structure == Config.Structure.CITY) {
successfully = game.buildCity(parser.getPoint());
} else if(structure == Config.Structure.ROAD){
successfully = game.buildRoad(parser.getPoint(), parser.getPoint());
}
if(!successfully){
parser.errorMessage();
}
}
private static void throwDice(SiedlerGame game, Parser parser) {
Random random = new Random();
//sum of two integers from 0-5 + 2 --> sum of two integers from 1-6