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"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <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" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

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