added some gameTests and reformatted ServerStateTests

This commit is contained in:
Benjamin Feyer
2024-12-13 04:07:00 +01:00
parent 702154c018
commit dc7dae5db9
3 changed files with 205 additions and 41 deletions

View File

@@ -2,25 +2,169 @@
import org.junit.Before;
import org.junit.Test;
import pp.mdga.game.card.PowerCard;
import pp.mdga.game.card.ShieldCard;
import pp.mdga.game.card.SwapCard;
import pp.mdga.game.card.TurboCard;
import pp.mdga.message.client.LobbyReadyMessage;
import pp.mdga.message.client.SelectTSKMessage;
import pp.mdga.message.client.StartGameMessage;
import pp.mdga.message.server.PlayCardMessage;
import pp.mdga.message.server.ServerMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.ServerSender;
import java.util.ArrayList;
import static org.junit.Assert.*;
/**
* this test-class tests the testcases T001-T016
*/
public class GameTest {
private ServerGameLogic logic;
private Game game;
private Player playerHost;
private int IDHost;
private Player playerClient;
private int IDClient;
/**
* this method is used to set the variables for this test-class
*/
@Before
public void setup() {
game = new Game();
logic = new ServerGameLogic(new ServerSender() {
@Override
public void send(int id, ServerMessage message) {
}
@Override
public void broadcast(ServerMessage message) {
}
@Override
public void disconnectClient(int id) {
}
@Override
public void shutdown() {
}
}, game);
playerHost = new Player("Host");
IDHost = 1;
playerClient = new Player("Client");
IDClient = 2;
game.addPlayer(IDHost, playerHost);
game.setHost(IDHost);
game.addPlayer(IDClient, playerClient);
}
/**
* this method tests, that, at the beginning of the game, all players have one piece on the start-field and one powerCard
* <p>
* T001
*/
@Test
public void testStartLineUp() {
// TODO: Implement test logic for starting line-up
//tests if both player have no color and are not ready
assertFalse(playerClient.isReady());
assertFalse(playerHost.isReady());
assertEquals(playerClient.getColor(), Color.NONE);
assertEquals(playerHost.getColor(), Color.NONE);
//send the selectTSK-message
logic.received(new SelectTSKMessage(Color.CYBER), IDClient);
logic.received(new SelectTSKMessage(Color.ARMY), IDHost);
//tests if the Tsk's are set correctly
assertFalse(playerClient.isReady());
assertFalse(playerHost.isReady());
assertEquals(playerClient.getColor(), Color.CYBER);
assertEquals(playerHost.getColor(), Color.ARMY);
//sends and tests the readyMessage for the client
logic.received(new LobbyReadyMessage(), IDClient);
assertTrue(playerClient.isReady());
assertFalse(playerHost.isReady());
//try to start the game, which should fail
logic.received(new StartGameMessage(), IDHost);
assertEquals(logic.getCurrentState(), logic.getLobbyState());
//sends and tests the readyMessage for the host
logic.received(new LobbyReadyMessage(), IDHost);
assertTrue(playerClient.isReady());
assertTrue(playerHost.isReady());
//tests if the game has started after the gameStartMessage
logic.received(new StartGameMessage(), IDHost);
assertEquals(logic.getCurrentState(), logic.getGameState());
//tests the start positions for the pieces
assertTrue(game.getBoard().getInfield()[playerHost.getStartNodeIndex()].isOccupied());
assertTrue(game.getBoard().getInfield()[playerHost.getStartNodeIndex()].isOccupied(playerHost.getColor()));
assertTrue(game.getBoard().getInfield()[playerClient.getStartNodeIndex()].isOccupied());
assertTrue(game.getBoard().getInfield()[playerClient.getStartNodeIndex()].isOccupied(playerClient.getColor()));
//tests if the players have no handCards
assertFalse(playerHost.getHandCards().isEmpty());
assertFalse(playerClient.getHandCards().isEmpty());
assertEquals(1, playerHost.getHandCards().size());
assertEquals(1, playerClient.getHandCards().size());
}
/**
* this method tests the drawPile
* <p>
* T002
*/
@Test
public void testCreatePowerCardDeck() {
// TODO: Implement test logic for creating power card deck
//tests if both player have no color and are not ready
assertFalse(playerClient.isReady());
assertFalse(playerHost.isReady());
assertEquals(playerClient.getColor(), Color.NONE);
assertEquals(playerHost.getColor(), Color.NONE);
//send the selectTSK-message
logic.received(new SelectTSKMessage(Color.CYBER), IDClient);
logic.received(new SelectTSKMessage(Color.ARMY), IDHost);
//sends and tests the readyMessage for the client
logic.received(new LobbyReadyMessage(), IDClient);
logic.received(new LobbyReadyMessage(), IDHost);
assertTrue(playerClient.isReady());
assertTrue(playerHost.isReady());
//tests if the game has started after the gameStartMessage
logic.received(new StartGameMessage(), IDHost);
assertEquals(logic.getCurrentState(), logic.getGameState());
//tests if the players have no handCards
assertFalse(playerHost.getHandCards().isEmpty());
assertFalse(playerClient.getHandCards().isEmpty());
assertEquals(1, playerHost.getHandCards().size());
assertEquals(1, playerClient.getHandCards().size());
assertEquals(38, game.getDrawPile().size());
ArrayList<PowerCard> cards = new ArrayList<>(game.getDrawPile());
cards.add(playerClient.getHandCards().get(0));
cards.add(playerHost.getHandCards().get(0));
//tests if the right amount of PowerCards are in the DrawPile
assertEquals(12, cards.stream().filter((powerCard -> powerCard instanceof ShieldCard)).toArray().length);
assertEquals(12, cards.stream().filter((powerCard -> powerCard instanceof SwapCard)).toArray().length);
assertEquals(16, cards.stream().filter((powerCard -> powerCard instanceof TurboCard)).toArray().length);
}
// UC-Game-02
@@ -70,10 +214,32 @@ public void testChangeActivePlayer() {
// TODO: Implement test logic for changing the active player
}
// UC-Game-09
/**
* this test-method tests the chances fo the turbo-card
* <p>
* T012
*/
@Test
public void testUseTurbo() {
// TODO: Implement test logic for using a turbo power-up
Die die = new Die();
ArrayList<Integer> modifier0 = new ArrayList<>();
ArrayList<Integer> modifier1 = new ArrayList<>();
ArrayList<Integer> modifier2 = new ArrayList<>();
for (int i = 0; i < 10000000; i++) {
die.modify();
switch (die.getDieModifier()) {
case 0 -> modifier0.add(0);
case 1 -> modifier1.add(1);
default -> modifier2.add(2);
}
}
//System.out.println("modifier2.size(): " + modifier2.size() + " modifier1.size(): " + modifier1.size()+" modifier0.size(): " + modifier0.size());
//test with 5% range for the correct changes
assertTrue(5700000 < modifier2.size() && modifier2.size() < 6300000);
assertTrue(1950000 < modifier1.size() && modifier1.size() < 2050000);
assertTrue(1950000 < modifier0.size() && modifier0.size() < 2050000);
}
@Test

View File

@@ -224,7 +224,6 @@ public void shutdown() {
pieceClient1.setState(home);
game.getBoard().setPieceOnBoard(19, pieceClient2); //for UC 13, 15
pieceClient2.setState(active);
//game.getBoard().setPieceOnBoard(); //todo
//set the host-pieces here
game.getBoard().setPieceOnBoard(28, pieceHost0); //for UC 02,14 ,15, 03.01,4
@@ -242,7 +241,6 @@ public void shutdown() {
game.getBoard().setPieceOnBoard(12,pieceCyber1); //
pieceCyber1.setState(active);
game.getPlayerByColor(cyberColor).addWaitingPiece(pieceClient3); //for uc 7
//game..... todo
//initializes the states
gameState= serverGameLogic.getGameState();