diff --git a/src/main/java/de/hsfulda/onses/models/Game.java b/src/main/java/de/hsfulda/onses/models/Game.java index e5066bc..0f9a56c 100644 --- a/src/main/java/de/hsfulda/onses/models/Game.java +++ b/src/main/java/de/hsfulda/onses/models/Game.java @@ -1,8 +1,15 @@ package de.hsfulda.onses.models; +import de.hsfulda.onses.services.GameService; +import de.hsfulda.onses.services.PlayerService; + import java.util.ArrayList; public class Game { + + private GameService gameService; + private PlayerService playerService; + private final ArrayList drawCardDeck = new ArrayList<>(); private Card lastPlayedCard = null; @@ -24,4 +31,26 @@ public class Game { drawCardDeck.add(card); } + public GameService getGameService() { + return gameService; + } + + public Game setGameService(GameService gameService) { + this.gameService = gameService; + return this; + } + + public PlayerService getPlayerService() { + return playerService; + } + + public Game setPlayerService(PlayerService playerService) { + this.playerService = playerService; + return this; + } + + public Game() + { + this.playerService = new PlayerService().setGame(this); + } } diff --git a/src/main/java/de/hsfulda/onses/models/Player.java b/src/main/java/de/hsfulda/onses/models/Player.java index 77eb2b1..88ce707 100644 --- a/src/main/java/de/hsfulda/onses/models/Player.java +++ b/src/main/java/de/hsfulda/onses/models/Player.java @@ -1,9 +1,13 @@ package de.hsfulda.onses.models; +import de.hsfulda.onses.services.PlayerService; + import java.util.ArrayList; public class Player { + private PlayerService playerService; + private Game game; private final ArrayList playerDeck = new ArrayList<>(); public ArrayList getPlayerDeck() { @@ -13,4 +17,24 @@ public class Player { public void addCardToPlayerDeck(Card card) { playerDeck.add(card); } + + + + public PlayerService getPlayerService() { + return playerService; + } + + public Player setPlayerService(PlayerService playerService) { + this.playerService = playerService; + return this; + } + + public Game getGame() { + return game; + } + + public Player setGame(Game game) { + this.game = game; + return this; + } } diff --git a/src/main/java/de/hsfulda/onses/services/GameService.java b/src/main/java/de/hsfulda/onses/services/GameService.java index df7cf87..c9acd67 100644 --- a/src/main/java/de/hsfulda/onses/services/GameService.java +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -10,6 +10,7 @@ public class GameService { private final Game game; public GameService(Game game) { this.game = game; + this.game.setGameService(this); } public GameService() { this(new Game()); diff --git a/src/main/java/de/hsfulda/onses/services/PlayerService.java b/src/main/java/de/hsfulda/onses/services/PlayerService.java index d726af8..b3b4cc4 100644 --- a/src/main/java/de/hsfulda/onses/services/PlayerService.java +++ b/src/main/java/de/hsfulda/onses/services/PlayerService.java @@ -1,11 +1,13 @@ package de.hsfulda.onses.services; +import de.hsfulda.onses.models.Game; import de.hsfulda.onses.models.Player; import java.util.ArrayList; public class PlayerService { + private Game game; private boolean currentTurn = true; // true --> real player, false --> Bot private final ArrayList playerList = new ArrayList<>(); @@ -31,4 +33,20 @@ public class PlayerService { return this; } + public Game getGame() { + return game; + } + + public PlayerService setGame(Game game) { + this.game = game; + playerList.getFirst().setGame(this.game); + playerList.getLast().setGame(this.game); + return this; + } + + public PlayerService() + { + addPlayerToList(new Player().setPlayerService(this)); + addPlayerToList(new Player().setPlayerService(this)); + } } diff --git a/src/test/java/de/hsfulda/onses/GameServiceTest.java b/src/test/java/de/hsfulda/onses/GameServiceTest.java index 7be6f27..c76e1d4 100644 --- a/src/test/java/de/hsfulda/onses/GameServiceTest.java +++ b/src/test/java/de/hsfulda/onses/GameServiceTest.java @@ -157,4 +157,32 @@ public class GameServiceTest { } } } + + @Test + @DisplayName("RelationshipGameService_Game") + public void RelationshipGameService_Game() + { + // arrange + GameService expected = new GameService(); + //act + GameService answer = expected.getGame().getGameService(); + // assert + assertEquals(expected, answer); + } + @Test + @DisplayName("allRelationsTest") + public void allRelationsTest () + { + // arrange + GameService expected = new GameService(); + //act + Player input1 = expected.getGame().getPlayerService().getPlayerList().getFirst(); + Player input2 = expected.getGame().getPlayerService().getPlayerList().getLast(); + GameService answer1 = input1.getPlayerService().getGame().getGameService(); + GameService answer2 = input2.getGame().getGameService(); + // assert + assertEquals(expected, answer1); + assertEquals(expected, answer2); + } + } diff --git a/src/test/java/de/hsfulda/onses/GameTest.java b/src/test/java/de/hsfulda/onses/GameTest.java index 13bb0ff..10b0dc8 100644 --- a/src/test/java/de/hsfulda/onses/GameTest.java +++ b/src/test/java/de/hsfulda/onses/GameTest.java @@ -46,4 +46,14 @@ public class GameTest { assertEquals(game.getDrawCardDeck().getFirst(), answer1); assertEquals(game.getDrawCardDeck().get(1), answer2); } + @Test + @DisplayName("RelationshipGame_PlayerService") + public void RelationshipGame_PlayerService() { + // arrange + Game expected = new Game(); + // act + Game answer = expected.getPlayerService().getGame(); + // assert + assertEquals(expected, answer); + } } diff --git a/src/test/java/de/hsfulda/onses/PlayerServiceTest.java b/src/test/java/de/hsfulda/onses/PlayerServiceTest.java index 08630c6..5dc2fd2 100644 --- a/src/test/java/de/hsfulda/onses/PlayerServiceTest.java +++ b/src/test/java/de/hsfulda/onses/PlayerServiceTest.java @@ -15,11 +15,15 @@ public class PlayerServiceTest { void addPlayerToPlayerList() { //arrange Player input = new Player(); - Player expected = input; + boolean expected = true; + boolean answer = false; // act PlayerService playerservice = new PlayerService(); playerservice.addPlayerToList(input); - Player answer = playerservice.getPlayerList().getFirst(); + for (Player i : playerservice.getPlayerList()) + { + if (i == input) {answer = true; break;} + } // assert assertEquals(expected, answer); } @@ -28,20 +32,24 @@ public class PlayerServiceTest { @DisplayName("addMultiplePlayerToPlayerList") void addMultiplePlayerToPlayerList() { //arrange + boolean expected = true; Player input1 = new Player(); - Player expected1 = input1; + boolean answer1 = false; Player input2 = new Player(); - Player expected2 = input2; + boolean answer2 = false; // act PlayerService playerservice = new PlayerService(); playerservice.addPlayerToList(input1); playerservice.addPlayerToList(input2); - Player answer1 = playerservice.getPlayerList().getFirst(); - Player answer2 = playerservice.getPlayerList().get(1); + for (Player i : playerservice.getPlayerList()) + { + if (i == input1) {answer1 = true;} + if (i == input2) {answer2 = true;} + } // assert - assertEquals(expected1, answer1); - assertEquals(expected2, answer2); + assertEquals(expected, answer1); + assertEquals(expected, answer2); } @Test @DisplayName("checkCurrentTurnAfterNextTurn") @@ -70,4 +78,16 @@ public class PlayerServiceTest { // assert assertEquals(expected, answer); } + @Test + @DisplayName("RelationShip_Player_PlayerService") + void Relationship_Player_PlayerService() { + // arrange + PlayerService expected = new PlayerService(); + // act + PlayerService answer1 = expected.getPlayerList().getFirst().getPlayerService(); + PlayerService answer2 = expected.getPlayerList().getLast().getPlayerService(); + // assert + assertEquals(expected, answer1); + assertEquals(expected, answer2); + } } diff --git a/src/test/java/de/hsfulda/onses/PlayerTest.java b/src/test/java/de/hsfulda/onses/PlayerTest.java index 80c68d5..fcdeeac 100644 --- a/src/test/java/de/hsfulda/onses/PlayerTest.java +++ b/src/test/java/de/hsfulda/onses/PlayerTest.java @@ -1,6 +1,8 @@ package de.hsfulda.onses; +import de.hsfulda.onses.models.Game; +import de.hsfulda.onses.services.PlayerService; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -43,4 +45,17 @@ public class PlayerTest { assertEquals(testCard1, firstCardPlayerDeck); assertEquals(testCard2, secondCardPlayerDeck); } + @Test + @DisplayName("Relationship_Player_Game") + void Relationship_Player_Game() { + // arrange + PlayerService input = new PlayerService(); + Game expected = input.getGame(); + // act + Game answer1 = input.getPlayerList().getFirst().getPlayerService().getGame(); + Game answer2 = input.getPlayerList().getLast().getPlayerService().getGame(); + // assert + assertEquals(expected, answer1); + assertEquals(expected, answer2); + } }