Merge remote-tracking branch 'origin/main'

This commit is contained in:
Leonardo Brandenberger 2021-12-10 20:19:30 +01:00
commit 6730fd722b
2 changed files with 61 additions and 111 deletions

View File

@ -32,6 +32,9 @@ public class SiedlerGame {
* or players is not between two and four * or players is not between two and four
*/ */
public SiedlerGame(int winPoints, int numberOfPlayers) { public SiedlerGame(int winPoints, int numberOfPlayers) {
if (winPoints < 3 || numberOfPlayers < Config.MIN_NUMBER_OF_PLAYERS || numberOfPlayers > 4) {
throw new IllegalArgumentException();
}
bank = new Bank(); bank = new Bank();
board = new SiedlerBoard(); board = new SiedlerBoard();
board.createFixGameField(); board.createFixGameField();

View File

@ -1,38 +1,34 @@
package ch.zhaw.catan; package ch.zhaw.catan;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
import java.awt.*; import java.awt.Point;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertThrows;
/***
* TODO Write your own tests in this class.
*
* Note: Have a look at {@link ch.zhaw.catan.games.ThreePlayerStandard}. It can be used
* to get several different game states.
*
*/
/** /**
* @class SiedlerGameTest * @class SiedlerGameTest
* * <p>
* contains all of the test cases for SiedlerGame class. * contains all of the test cases for SiedlerGame class.
* The Test cases are categorized into * The Test cases are categorized into
* - Positive TestCases * - Positive TestCases
* Tests the methods of SiedlerGame with the intended values * Tests the methods of SiedlerGame with the intended values
* * <p>
* - Negative TestCases * - Negative TestCases
* Tests the methods of SiedlerGame with values, that results to errors and failure * Tests the methods of SiedlerGame with values, that results to errors and failure
* * <p>
* - SystemTestCases * - SystemTestCases
* Tests the coordination of all methods and simulates a game with all moves programmed * Tests the coordination of all methods and simulates a game with programmed moves
* Checks if the methods are being executed and the values are set correctly * Checks if the methods are being executed and the values are set correctly
*/ */
public class SiedlerGameTest { public class SiedlerGameTest {
@ -42,7 +38,7 @@ public class SiedlerGameTest {
/** /**
* Property START_SETTLEMENT_POSITIONS * Property START_SETTLEMENT_POSITIONS
* * <p>
* Lists all positions of Settlement for every faction in the initialization phase. * Lists all positions of Settlement for every faction in the initialization phase.
* Each faction is assigned to a list of specific settlements, which represents as a list of points * Each faction is assigned to a list of specific settlements, which represents as a list of points
*/ */
@ -55,7 +51,7 @@ public class SiedlerGameTest {
/** /**
* Property START_ROADS_POSITION * Property START_ROADS_POSITION
* * <p>
* Lists all endpoints of roads for every faction in the initialization phase. * Lists all endpoints of roads for every faction in the initialization phase.
* Each faction is assigned to a specific Road, which represents as a tuple of 2 points * Each faction is assigned to a specific Road, which represents as a tuple of 2 points
*/ */
@ -87,7 +83,7 @@ public class SiedlerGameTest {
public void TestGameInitializationWithAllPlayercounts(int playerAmount) { public void TestGameInitializationWithAllPlayercounts(int playerAmount) {
SiedlerGame game = new SiedlerGame(DEFAULT_WINPOINTS, playerAmount); SiedlerGame game = new SiedlerGame(DEFAULT_WINPOINTS, playerAmount);
for (Config.Faction faction: game.getPlayerFactions()) { for (Config.Faction faction : game.getPlayerFactions()) {
HashMap<Config.Resource, Integer> resources = game.getCurrentPlayerResource(); HashMap<Config.Resource, Integer> resources = game.getCurrentPlayerResource();
Assertions.assertEquals(0, resources.get(Config.Resource.BRICK)); Assertions.assertEquals(0, resources.get(Config.Resource.BRICK));
@ -101,19 +97,38 @@ public class SiedlerGameTest {
} }
/** /**
* * Tests if the method throwDice halves the resources of the player
* 7 will be passed to the throwDice Method when called
* <p>
* Method does not halve a resource, when the amount is below 7.
* Player has gotten 8 wool resources and throws a 7 with the dice
* <p>
* expected: throwDice with a 7 will halve wool resource, the amount should be 4
*/ */
@Test @Test
@DisplayName("Test") @DisplayName("Test Throw Dice with value 7, expected half the resources")
public void TestHandle7() { public void TestThrowDiceWith7() {
SiedlerGame game = startGame(); SiedlerGame game = gameAfterSetupPhase();
//todo
throwDiceSeveralTimes(game, 8, 1);
throwDiceSeveralTimes(game, 7, 1);
Assertions.assertEquals(2, game.getCurrentPlayerResource().get(Config.Resource.WOOL));
throwDiceSeveralTimes(game, 8, 1);
throwDiceSeveralTimes(game, 8, 1);
throwDiceSeveralTimes(game, 8, 1);
Assertions.assertEquals(8, game.getCurrentPlayerResource().get(Config.Resource.WOOL));
throwDiceSeveralTimes(game, 7, 1);
Assertions.assertEquals(4, game.getCurrentPlayerResource().get(Config.Resource.WOOL));
} }
} }
/** /**
* @Class NegAtiveTestcases * @Class NegAtiveTestcases
* * <p>
* contains all negative test cases * contains all negative test cases
*/ */
@Nested @Nested
@ -142,7 +157,6 @@ public class SiedlerGameTest {
@Test @Test
@DisplayName("Starting Siedler game with one player, expects fail") @DisplayName("Starting Siedler game with one player, expects fail")
public void startSiedlerGameWithLowerThanMinimumWinPoints() { public void startSiedlerGameWithLowerThanMinimumWinPoints() {
Exception exc = assertThrows(IllegalArgumentException.class, () -> { Exception exc = assertThrows(IllegalArgumentException.class, () -> {
SiedlerGame game = new SiedlerGame(1, 4); SiedlerGame game = new SiedlerGame(1, 4);
}); });
@ -154,7 +168,7 @@ public class SiedlerGameTest {
/** /**
* This testcase will test, if the methods placeInitialRoad and placeInitialSettlement are overwritting * This testcase will test, if the methods placeInitialRoad and placeInitialSettlement are overwritting
* already occupied positions if the same faction or any other faction are calling the method with the same positions * already occupied positions if the same faction or any other faction are calling the method with the same positions
* * <p>
* Expected: Method placeInitialRoad placeInitialSettlement should return false, independent of the current faction playing * Expected: Method placeInitialRoad placeInitialSettlement should return false, independent of the current faction playing
*/ */
@Test @Test
@ -179,23 +193,11 @@ public class SiedlerGameTest {
Assertions.assertFalse(game.placeInitialSettlement(settlementPoint, false)); Assertions.assertFalse(game.placeInitialSettlement(settlementPoint, false));
} }
/**
* This testcase will test, if the methods buildRoad, buildCity buildSettlement are overwritting
* already occupied positions if the same faction or any other faction are calling the method with the same positions
*
* Expected: Method placeInitialRoad placeInitialSettlement should return false, independent of the current faction playing
*/
@Test
@DisplayName("Test buildRoad, buildCity and buildSettlement with already occupied positions")
public void testBuildStructuresOnOccupiedFields() {
}
} }
/** /**
* @Class SystemTestCases * @Class SystemTestCases
* * <p>
* This class simulates a running game and tests multiple sequences of this game. * This class simulates a running game and tests multiple sequences of this game.
*/ */
@Nested @Nested
@ -229,50 +231,12 @@ public class SiedlerGameTest {
public void TestGameAfterSetupPhase() { public void TestGameAfterSetupPhase() {
SiedlerGame game = gameAfterSetupPhase(); SiedlerGame game = gameAfterSetupPhase();
System.out.println(game.getBoard().toString()); throwDiceSeveralTimes(game, 8, 1);
Assertions.assertEquals(2, game.getCurrentPlayerResourceStock(Config.Resource.WOOL));
throwDiceSeveralTimes(game, 5, 5);
throwDiceSeveralTimes(game, 5, 5);
throwDiceSeveralTimes(game, 5, 5);
throwDiceSeveralTimes(game, 5, 5);
throwDiceSeveralTimes(game, 5, 5);
//game.switchToNextPlayer();
//throwDiceSeveralTimes(game, 6, 5);
//throwDiceSeveralTimes(game, 6, 5);
//throwDiceSeveralTimes(game, 6, 5);
//throwDiceSeveralTimes(game, 6, 5);
//throwDiceSeveralTimes(game, 6, 5);
soutDistribution(game);
}
/**
* tests, if a player wins when reached the amount of points.
*/
@Test
@DisplayName("End of Game test")
public void TestGameEnd() {
} }
//TEMPORARY METHOD, WILL BE REMOVED SOON
private static void soutDistribution(SiedlerGame game) {
System.out.println("\n\nVerteilung \n\n");
for (int i = 0 ; i < game.getPlayerFactions().size(); i++) {
HashMap<Config.Resource, Integer> resources = game.getCurrentPlayerResource();
System.out.println(game.getCurrentPlayerFaction().toString() + "\n");
System.out.println(" BRICK " + resources.get(Config.Resource.BRICK).toString());
System.out.println(" GRAIN " + resources.get(Config.Resource.GRAIN));
System.out.println(" LUMBER " + resources.get(Config.Resource.LUMBER));
System.out.println(" ORE " + resources.get(Config.Resource.ORE));
System.out.println(" WOOL " + resources.get(Config.Resource.WOOL));
game.switchToNextPlayer();
}
}
} }
/** /**
@ -311,8 +275,6 @@ public class SiedlerGameTest {
game.placeInitialRoad(secondRoad.first, secondRoad.second); game.placeInitialRoad(secondRoad.first, secondRoad.second);
} }
System.out.println(game.getBoard().toString());
return game; return game;
} }
@ -324,24 +286,9 @@ public class SiedlerGameTest {
* @param amountDiceThrows Type int, The amount of dice throws * @param amountDiceThrows Type int, The amount of dice throws
*/ */
private static void throwDiceSeveralTimes(SiedlerGame game, int dice, int amountDiceThrows) { private static void throwDiceSeveralTimes(SiedlerGame game, int dice, int amountDiceThrows) {
//System.out.println(game.getCurrentPlayerFaction().toString() + " got " + dice + " and throw " + amountDiceThrows + " times");
for (int i = 0; i < amountDiceThrows; i++) { for (int i = 0; i < amountDiceThrows; i++) {
game.throwDice(dice); game.throwDice(dice);
} }
} }
/**
* This method will setup a game to a state before a player has the points
* for being able to win the game
*
* @return SiedlerGame which is near the
*/
private static SiedlerGame gameNearEnd() {
SiedlerGame game = gameAfterSetupPhase();
return game;
}
} }