added some tests and corrected some tests and added logic for the tests like getter and setter

This commit is contained in:
Benjamin Feyer
2024-12-12 13:46:23 +01:00
parent d12b0b6a77
commit 0d4685f3c2
6 changed files with 134 additions and 59 deletions

View File

@@ -116,6 +116,14 @@ public PowerCard draw() {
}
return this.drawPile.remove(0);
}
else{
Collections.shuffle(this.discardPile);
this.drawPile.addAll(this.discardPile);
discardPile.clear();
if(!this.drawPile.isEmpty()){
return this.drawPile.remove(0);
}
}
return null;
}

View File

@@ -358,6 +358,15 @@ public void setPieceInHome(int index, Piece piece) {
this.homeNodes[index].setOccupant(piece);
}
/**
* this method is used to set the homeIndex
*
* @param index to be set
*/
public void setStartNodeIndex(int index){
this.startNodeIndex=index;
}
/**
* The string representation of the player
*

View File

@@ -169,4 +169,13 @@ public void exit() {
isHomeMove.clear();
targetIndex.clear();
}
/**
* getter for movables pieces
*
* @return ArrayList of movables Pieces
*/
public ArrayList<Piece> getMoveablePieces() {
return moveablePieces;
}
}

View File

@@ -102,4 +102,13 @@ public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Entered StartPieceState state.");
piece = null;
}
/**
* this is a setter for the piece
*
* @param piece to be set
*/
public void setPiece(Piece piece){
this.piece = piece;
}
}

View File

@@ -62,4 +62,13 @@ public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Exited WaitingPieceState state.");
piece = null;
}
/**
* this is a setter for the piece
*
* @param piece to be set
*/
public void setPiece(Piece piece){
this.piece = piece;
}
}

View File

@@ -3,7 +3,9 @@
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.AnimationEndMessage;
import pp.mdga.message.client.RequestDieMessage;
import pp.mdga.message.client.RequestMoveMessage;
@@ -270,7 +272,7 @@ public void shutdown() {
* Use Case UC-Piece-01: Ensure that a piece moves in the right direction
* </p>
*/
@Test
@Test (expected = RuntimeException.class)
public void testMove() {
//sets the active Player to host
game.setActiveColor(hostColor);
@@ -382,6 +384,7 @@ public void testThrow() {
//set active player to host
game.setActiveColor(hostColor);
playerHost.setStartNodeIndex(10);
//sends the server in firstRoll
serverGameLogic.setCurrentState(gameState);
@@ -400,18 +403,19 @@ public void testThrow() {
//send requestDice
serverGameLogic.received(new RequestDieMessage(),IDHost);
serverGameLogic.received(new AnimationEndMessage(),IDHost);
//send requestMove-message
serverGameLogic.received(new RequestMoveMessage(pieceHost1),IDHost);
//tests if the idx is unoccupied
assertFalse(game.getBoard().getInfield()[19].isOccupied());
assertFalse(game.getBoard().getInfield()[18].isOccupied());
//tests if the idx 20 is occupied
assertTrue(game.getBoard().getInfield()[20].isOccupied());
assertTrue(game.getBoard().getInfield()[19].isOccupied());
//tests if the piece on idx 20 is pieceHost1
assertEquals(game.getBoard().getInfield()[20].getOccupant(),pieceHost1);
assertEquals(game.getBoard().getInfield()[19].getOccupant(),pieceHost1);
}
/**
@@ -442,6 +446,7 @@ public void testGetThrown() {
//send requestDice
serverGameLogic.received(new RequestDieMessage(),IDHost);
serverGameLogic.received(new AnimationEndMessage(),IDHost);
//send requestMove-message
serverGameLogic.received(new RequestMoveMessage(pieceHost1),IDHost);
@@ -462,6 +467,7 @@ public void testLeaveWaitingArea() {
//set active player to host
game.setActiveColor(hostColor);
playerHost.setStartNodeIndex(20);
//sends the server in firstRoll
serverGameLogic.setCurrentState(gameState);
@@ -480,11 +486,13 @@ public void testLeaveWaitingArea() {
//send requestDice
serverGameLogic.received(new RequestDieMessage(),IDHost);
serverGameLogic.received(new AnimationEndMessage(),IDHost);
//tests if the sever is in selectPiece
assertEquals(serverGameLogic.getCurrentState(),gameState);
assertEquals(gameState.getCurrentState(), turnState);
assertEquals(turnState.getCurrentState(),choosePieceState);
waitingPieceState.setPiece(pieceHost2);
assertEquals(choosePieceState.getCurrentState(),waitingPieceState);
//send requestMove-message
@@ -492,10 +500,9 @@ public void testLeaveWaitingArea() {
//tests if the waitingArea does not include the piece anymore
assertFalse(Arrays.stream(game.getPlayerByColor(hostColor).getWaitingArea()).toList().contains(pieceHost2));
//tests if the pieceHost3 is at idx 30
assertTrue(game.getBoard().getInfield()[30].isOccupied());
assertEquals(game.getBoard().getInfield()[30].getOccupant(),pieceHost2);
assertTrue(game.getBoard().getInfield()[20].isOccupied());
assertEquals(game.getBoard().getInfield()[20].getOccupant(),pieceHost2);
}
/**
@@ -527,6 +534,7 @@ public void testMustLeaveStartingField() {
//send requestDiceMessage
serverGameLogic.received(new RequestDieMessage(),IDCyber);
serverGameLogic.received(new AnimationEndMessage(),IDCyber);
System.out.println(game.getBoard().getInfieldIndexOfPiece(pieceCyber0));
//tests if the sever is in startPiece
assertEquals(serverGameLogic.getCurrentState(),gameState);
@@ -535,25 +543,12 @@ public void testMustLeaveStartingField() {
assertEquals(choosePieceState.getCurrentState(),startPieceState);
//send requestMoveMessage
serverGameLogic.received(new RequestMoveMessage(pieceHost1),IDCyber);
serverGameLogic.received(new RequestMoveMessage(pieceCyber0),IDCyber);
//tests if the cyberPiece2 has moved
assertTrue(game.getBoard().getInfield()[12].isOccupied());
assertEquals(game.getBoard().getInfield()[12].getOccupant(),pieceCyber1);
//tests if the sever is in startPiece
serverGameLogic.setCurrentState(gameState);
gameState.setCurrentState(turnState);
turnState.setCurrentState(choosePieceState);
choosePieceState.setCurrentState(startPieceState);
//send requestMoveMessage
serverGameLogic.received(new RequestMoveMessage(pieceHost0),IDCyber);
//tests if the piece is moved
assertTrue(game.getBoard().getInfield()[14].isOccupied());
assertEquals(game.getBoard().getInfield()[14].getOccupant(),pieceHost0);
assertFalse(game.getBoard().getInfield()[10].isOccupied());
assertEquals(game.getBoard().getInfield()[14].getOccupant(),pieceCyber0);
System.out.println(game.getBoard().getInfieldIndexOfPiece(pieceCyber0));
}
/**
@@ -564,6 +559,9 @@ public void testMustLeaveStartingField() {
*/
@Test
public void testDontHaveToLeaveStartingField() {
//set the active player to cyberPlayer
game.setActiveColor(cyberColor);
//sends the server in firstRoll
serverGameLogic.setCurrentState(gameState);
gameState.setCurrentState(turnState);
@@ -576,9 +574,6 @@ public void testDontHaveToLeaveStartingField() {
assertEquals(turnState.getCurrentState(),rollDiceState);
assertEquals(rollDiceState.getCurrentState(),firstRollState);
//set the active player to cyberPlayer
game.setActiveColor(cyberColor);
//remove piece from cyberPlayer
game.getPlayerByColor(cyberColor).removeWaitingPiece(game.getPlayerByColor(cyberColor).getWaitingPiece());
@@ -588,11 +583,12 @@ public void testDontHaveToLeaveStartingField() {
//tests if the server is in selectPieceState
assertEquals(serverGameLogic.getCurrentState(),gameState);
assertEquals(gameState.getCurrentState(), turnState);
assertEquals(turnState.getCurrentState(),choosePieceState);
assertEquals(choosePieceState.getCurrentState(),selectPieceState);
assertEquals(turnState.getCurrentState(),rollDiceState);
assertEquals(rollDiceState.getCurrentState(),firstRollState);
//send requestDice-Message
serverGameLogic.received(new RequestDieMessage(),IDCyber);
serverGameLogic.received(new AnimationEndMessage(),IDCyber);
//send requestMoveMessage for CyberPiece1
serverGameLogic.received(new RequestMoveMessage(pieceCyber1),IDCyber);
@@ -609,8 +605,11 @@ public void testDontHaveToLeaveStartingField() {
* Use Case UC-Piece-08: Verify conditions preventing a piece from leaving its starting field.
* </p>
*/
@Test
@Test (expected = RuntimeException.class)
public void testCantLeaveStartingField() {
//sets the activePlayer to cyber
game.setActiveColor(cyberColor);
//sends the server in firstRoll
serverGameLogic.setCurrentState(gameState);
gameState.setCurrentState(turnState);
@@ -623,20 +622,19 @@ public void testCantLeaveStartingField() {
assertEquals(turnState.getCurrentState(),rollDiceState);
assertEquals(rollDiceState.getCurrentState(),firstRollState);
//sets the activePlayer to cyber
game.setActiveColor(cyberColor);
//set Dice to two
game.setDie(die2);
//send requestDiceMessage
serverGameLogic.received(new RequestDieMessage(),IDCyber);
serverGameLogic.received(new AnimationEndMessage(),IDCyber);
//tests if the sever is in selectPiece
assertEquals(serverGameLogic.getCurrentState(),gameState);
assertEquals(gameState.getCurrentState(), turnState);
assertEquals(turnState.getCurrentState(),choosePieceState);
assertEquals(choosePieceState.getCurrentState(),selectPieceState);
//selectPieceState.setMoveablePieces(new ArrayList<>(List.of(pieceCyber0)));
//send requestMoveMessage
serverGameLogic.received(new RequestMoveMessage(pieceCyber0),IDCyber);
@@ -670,7 +668,6 @@ public void testCantLeaveStartingField() {
*/
@Test
public void testReachBonusField() {
//sets the active Player to Host
game.setActiveColor(hostColor);
@@ -694,6 +691,7 @@ public void testReachBonusField() {
//sends the requestDice-Message
serverGameLogic.received(new RequestDieMessage(),IDHost);
serverGameLogic.received(new AnimationEndMessage(),IDHost);
//requestMove of hostPiece3
serverGameLogic.received(new RequestMoveMessage(pieceHost3),IDHost);
@@ -715,6 +713,9 @@ public void testReachBonusField() {
*/
@Test
public void testNoPowerCards() {
//sets the active Player to Host
game.setActiveColor(hostColor);
//sends the server in firstRoll
serverGameLogic.setCurrentState(gameState);
gameState.setCurrentState(turnState);
@@ -727,9 +728,6 @@ public void testNoPowerCards() {
assertEquals(turnState.getCurrentState(),rollDiceState);
assertEquals(rollDiceState.getCurrentState(),firstRollState);
//sets the active Player to Host
game.setActiveColor(hostColor);
//sets the dice-seed to 4
game.setDie(die4);
@@ -742,7 +740,8 @@ public void testNoPowerCards() {
assertTrue(game.getDiscardPile().isEmpty());
//sends the requestDice-Message
serverGameLogic.received(new RequestMoveMessage(pieceHost1),IDHost);
serverGameLogic.received(new RequestDieMessage(),IDHost);
serverGameLogic.received(new AnimationEndMessage(),IDHost);
// requestMOve of hostPiece3
serverGameLogic.received(new RequestMoveMessage(pieceHost3),IDHost);
@@ -781,10 +780,9 @@ public void testShufflePile() {
//sets the dice-seed to 4
game.setDie(die4);
firstRollState.setIsDied(true);
//sets the discard pile empty
game.setDiscardPile(game.getDrawPile());
game.setDiscardPile(new ArrayList<>(List.of(new TurboCard(),new TurboCard())));
assertFalse(game.getDiscardPile().isEmpty());
//sets the pile empty
@@ -792,16 +790,19 @@ public void testShufflePile() {
assertTrue(game.getDrawPile().isEmpty());
//sends the requestDice-Message
serverGameLogic.received(new RequestMoveMessage(pieceHost1),IDHost);
serverGameLogic.received(new RequestDieMessage(),IDHost);
serverGameLogic.received(new AnimationEndMessage(),IDHost);
// requestMOve of hostPiece3
serverGameLogic.received(new RequestMoveMessage(pieceHost3),IDHost);
assertTrue(game.getBoard().getInfield()[4].isOccupied());
assertEquals(game.getBoard().getInfield()[4].getOccupant(),pieceHost3);
System.out.println(game.getBoard().getInfield()[4].isBonus());
//tests if the player has received a bonusCard
assertFalse(game.getDrawPile().isEmpty());
assertTrue(game.getDiscardPile().isEmpty());
//tests if the drawPile is not Empty
assertFalse(game.getDrawPile().isEmpty());
@@ -817,7 +818,8 @@ public void testShufflePile() {
*/
@Test
public void testEnterHouse() {
pieceClient2.setState(PieceState.ACTIVE);
playerClient.setStartNodeIndex(20);
//set activePlayer to client
game.setActiveColor(clientColor);
@@ -838,11 +840,16 @@ public void testEnterHouse() {
//sends the request-Dice-message
serverGameLogic.received(new RequestDieMessage(),IDClient);
serverGameLogic.received(new AnimationEndMessage(),IDClient);
//tests if the server is in firstRoll
assertEquals(serverGameLogic.getCurrentState(),gameState);
assertEquals(gameState.getCurrentState(), turnState);
assertEquals(turnState.getCurrentState(),choosePieceState);
assertEquals(choosePieceState.getCurrentState(),selectPieceState);
//sends the requestMove-message for pieceClient1
System.out.println(game.getBoard().getInfieldIndexOfPiece(pieceClient2));
serverGameLogic.received(new RequestMoveMessage(pieceClient2),IDClient);
System.out.println(game.getBoard().getInfieldIndexOfPiece(pieceClient2));
//tests, if the piece is in the first slot of the home
assertFalse(game.getBoard().getInfield()[19].isOccupied());
@@ -860,12 +867,16 @@ public void testEnterHouse() {
public void testOnlyEnterOwnHouse() {
//set activePlayer to host
game.setActiveColor(hostColor);
System.out.println(playerHost.getStartNodeIndex());
game.getBoard().setPieceOnBoard(28,pieceHost1);
game.getBoard().getInfield()[18].clearOccupant();
//sends the server in firstRoll
serverGameLogic.setCurrentState(gameState);
gameState.setCurrentState(turnState);
turnState.setCurrentState(rollDiceState);
rollDiceState.setCurrentState(firstRollState);
System.out.println(game.getBoard().getInfieldIndexOfPiece(pieceHost1));
//tests if the server is in firstRoll
assertEquals(serverGameLogic.getCurrentState(),gameState);
@@ -875,17 +886,19 @@ public void testOnlyEnterOwnHouse() {
//set the die in Game to 3
game.setDie(die3);
System.out.println(game.getBoard().getInfieldIndexOfPiece(pieceHost1));
//sends the request-Dice-message
serverGameLogic.received(new RequestDieMessage(),IDHost);
serverGameLogic.received(new AnimationEndMessage(),IDHost);
//sends the requestMove-message for pieceClient1
System.out.println(game.getBoard().getInfieldIndexOfPiece(pieceHost1));
serverGameLogic.received(new RequestMoveMessage(pieceHost1),IDHost);
System.out.println(game.getBoard().getInfieldIndexOfPiece(pieceHost1));
//tests if hostPiece1 is at idx 20 TODO false positive
assertTrue(game.getBoard().getInfield()[19].isOccupied());
assertEquals(game.getBoard().getInfield()[20].getOccupant(),pieceHost1);
assertTrue(game.getBoard().getInfield()[31].isOccupied());
assertEquals(game.getBoard().getInfield()[31].getOccupant(),pieceHost1);
}
/**
@@ -1174,24 +1187,36 @@ public void testUseSwap() {
*/
@Test
public void testUseShield() {
//set activePlayer to Host
PowerCard shield = new ShieldCard();
game.setActiveColor(hostColor);
playerHost.addHandCard(shield);
//send the server in choosePowerCard
serverGameLogic.setCurrentState(gameState);
serverGameLogic.getGameState().setCurrentState(turnState);
serverGameLogic.getGameState().getTurnState().setCurrentState(powerCardState);
powerCardState.setCurrentState(powerCardState.getChoosePowerCardState());
//tests if the server is in selectPieces
assertEquals(serverGameLogic.getCurrentState(),gameState);
assertEquals(gameState.getCurrentState(), turnState);
assertEquals(turnState.getCurrentState(),powerCardState);
assertEquals(powerCardState.getCurrentState(),powerCardState.getChoosePowerCardState());
//set activePlayer to Host
game.setActiveColor(hostColor);
//sends the requestPlayCard
serverGameLogic.received(new SelectCardMessage(shieldCard),IDHost);
serverGameLogic.received(new SelectCardMessage(shield),IDHost);
powerCardState.setSelectedCard(shield);
//tests if the server is in selectPieces
assertEquals(serverGameLogic.getCurrentState(),gameState);
assertEquals(gameState.getCurrentState(), turnState);
assertEquals(turnState.getCurrentState(),powerCardState);
assertEquals(powerCardState.getCurrentState(),powerCardState.getShieldCardState());
//sends the selectedPiece-message
serverGameLogic.received(RequestPlayCardMessage.requestPlayShield(pieceHost0.getUuid()),IDHost);
serverGameLogic.received(new SelectedPiecesMessage(new ArrayList<>(List.of(pieceHost0))),IDHost);
//tests if the piece at idx 28 has a shield
assertEquals(pieceHost0.getShield(),ShieldState.ACTIVE);
@@ -1213,6 +1238,7 @@ public void testLoseShield() {
//sets activePlayer to client
game.setActiveColor(clientColor);
game.removePlayer(IDCyber);
//set shieldState.ACTIVE im pieceHost1
pieceHost1.setShield(ShieldState.ACTIVE);
@@ -1222,24 +1248,28 @@ public void testLoseShield() {
//sends the server in selectPiece
serverGameLogic.setCurrentState(gameState);
serverGameLogic.getGameState().setCurrentState(turnState);
serverGameLogic.getGameState().getTurnState().setCurrentState(choosePieceState);
serverGameLogic.getGameState().getTurnState().getChoosePieceState().setCurrentState(selectPieceState);
serverGameLogic.getGameState().getTurnState().setCurrentState(rollDiceState);
rollDiceState.setCurrentState(firstRollState);
//tests if the server is in selectPieces
assertEquals(serverGameLogic.getCurrentState(),gameState);
assertEquals(gameState.getCurrentState(), turnState);
assertEquals(turnState.getCurrentState(),choosePieceState);
assertEquals(choosePieceState.getCurrentState(),selectPieceState);
assertEquals(turnState.getCurrentState(),rollDiceState);
assertEquals(rollDiceState.getCurrentState(),firstRollState);
//send requestDice-message
serverGameLogic.received(new RequestDieMessage(),IDClient);
serverGameLogic.received(new AnimationEndMessage(),IDClient);
//send requestMove-message for clientPiece0
serverGameLogic.received(new RequestMoveMessage(pieceClient0),IDClient);
serverGameLogic.received(new AnimationEndMessage(),IDClient);
serverGameLogic.received(new AnimationEndMessage(),IDHost);
serverGameLogic.received(new AnimationEndMessage(),IDCyber);
//tests if the server is in animation-state
//tests if the server is in turn-state
assertEquals(serverGameLogic.getCurrentState(),gameState);
assertEquals(gameState.getCurrentState(), gameState.getAnimationState());
assertEquals(gameState.getCurrentState(), gameState.getTurnState());
//send 3 animationEndMessage
serverGameLogic.received(new AnimationEndMessage(),IDClient);
@@ -1261,6 +1291,9 @@ public void testLoseShield() {
*/
@Test
public void testFinishedPiece() {
//sets the active color to client
game.setActiveColor(clientColor);
//sends the server in selectPiece
serverGameLogic.setCurrentState(gameState);
serverGameLogic.getGameState().setCurrentState(turnState);
@@ -1273,13 +1306,11 @@ public void testFinishedPiece() {
assertEquals(turnState.getCurrentState(),choosePieceState);
assertEquals(choosePieceState.getCurrentState(),selectPieceState);
//sets the active color to client
game.setActiveColor(clientColor);
//set the dice to 2
game.setDie(die2);
//sends the requestDie-Message
serverGameLogic.received(new RequestDieMessage(),IDClient);
serverGameLogic.received(new AnimationEndMessage(),IDClient);
//sends the requestMove-Message
serverGameLogic.received(new RequestMoveMessage(pieceClient1),IDClient);