Finished task 3

This commit is contained in:
Michael Chen 2022-07-15 12:01:37 +02:00
parent d8470790b2
commit f973e4db11
No known key found for this signature in database
GPG Key ID: 1CBC7AA5671437BB
3 changed files with 82 additions and 6 deletions

View File

@ -4,7 +4,7 @@ name = MichaelChen
solutionname = Solution_Phase$(phase)_$(name) solutionname = Solution_Phase$(phase)_$(name)
target = $(solutionname)_V$(version).zip target = $(solutionname)_V$(version).zip
package = $(solutionname).pdf package = $(solutionname).pdf PlayerTest.java
latexmkflags = latexmkflags =
.PHONY : all dev .PHONY : all dev

View File

@ -0,0 +1,79 @@
package nl.tudelft.jpacman.level;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.google.common.collect.Lists;
import nl.tudelft.jpacman.board.Board;
import nl.tudelft.jpacman.board.BoardFactory;
import nl.tudelft.jpacman.board.Direction;
import nl.tudelft.jpacman.board.Square;
import nl.tudelft.jpacman.game.Game;
import nl.tudelft.jpacman.game.GameFactory;
import nl.tudelft.jpacman.npc.ghost.GhostFactory;
import nl.tudelft.jpacman.sprite.PacManSprites;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test suite to confirm the correct behaviour of pellet consumtion as
* described in Scenario S2.1.
*
* @author Michael Chen
*/
class PlayerTest {
private MapParser parser;
private GameFactory gameFact;
/**
* Prepare the game factory
*/
@BeforeEach
void setUpTest() {
PacManSprites sprites = new PacManSprites();
parser = new MapParser(new LevelFactory(sprites,
new GhostFactory(sprites)), new BoardFactory(sprites));
gameFact = new GameFactory(new PlayerFactory(sprites));
}
/**
* Scenario S2.1: The player consumes
*/
@Test
void playerConsumeTest() {
Level level = parser.parseMap(
Lists.newArrayList("####", "#P.#", "####"));
Board board = level.getBoard();
Game game = gameFact.createSinglePlayerGame(level);
Player player = game.getPlayers().get(0);
Square playerStart = board.squareAt(1, 1);
Square pelletPos = board.squareAt(2, 1);
// Given the game has started,
game.start();
assertThat(game.isInProgress()).isTrue();
assertThat(player.getScore()).isZero();
// and my Pacman is next to a square containing a pellet;
assertThat(pelletPos.getOccupants())
.hasAtLeastOneElementOfType(Pellet.class);
assertThat(pelletPos.getOccupants()).hasSize(1);
Pellet pellet = (Pellet) pelletPos.getOccupants().get(0);
assertThat(playerStart.getOccupants()).contains(player);
// When I press an arrow key towards that square;
game.move(player, Direction.EAST);
// Then my Pacman can move to that square,
assertThat(pelletPos.getOccupants()).contains(player);
// and I earn the points for the pellet,
assertThat(player.getScore()).isEqualTo(pellet.getValue());
// and the pellet disappears from that square.
assertThat(pelletPos.getOccupants()).doesNotContain(pellet);
game.stop();
}
}

View File

@ -87,12 +87,9 @@
\begin{question}{Write your own JUnit Test}%[0] \begin{question}{Write your own JUnit Test}%[0]
Inside the \texttt{src/test/java} folder, create a new test file "CustomTest.java", and write your own JUnit test for one scenario picked from "scenarios.md" (located in the \texttt{doc/} folder). The test is supposed to call the sequence of functions corresponding to the steps described in the chosen scenario, and should include reasonable \textit{assertions} to check if the intermediate program states meet the scenario requirements. Inside the \texttt{src/test/java} folder, create a new test file "CustomTest.java", and write your own JUnit test for one scenario picked from "scenarios.md" (located in the \texttt{doc/} folder). The test is supposed to call the sequence of functions corresponding to the steps described in the chosen scenario, and should include reasonable \textit{assertions} to check if the intermediate program states meet the scenario requirements.
\begin{answer} \begin{answer}
[TODO: Add answer here] I picked the scenarion \texttt{S2.1} to verify that the pellets are successfully consumed by the player once the player moves towards them.
\begin{lstlisting}[language=Java,belowskip=-0.8\baselineskip] \lstinputlisting[firstline=43,lastline=78,language=Java,belowskip=-0.8\baselineskip]{PlayerTest.java}
/* Add code here */
\end{lstlisting}
% or: \lstinputlisting[language=Java,belowskip=-0.8\baselineskip]{file_name.java}
\end{answer} \end{answer}
\end{question} \end{question}