Browse Source

Merge branch 'Relationships' into 'main'

Relationships

See merge request fdai7736/onses!16
main
fdai7920 11 months ago
parent
commit
e0092c93a1
  1. 29
      src/main/java/de/hsfulda/onses/models/Game.java
  2. 24
      src/main/java/de/hsfulda/onses/models/Player.java
  3. 1
      src/main/java/de/hsfulda/onses/services/GameService.java
  4. 18
      src/main/java/de/hsfulda/onses/services/PlayerService.java
  5. 28
      src/test/java/de/hsfulda/onses/GameServiceTest.java
  6. 10
      src/test/java/de/hsfulda/onses/GameTest.java
  7. 36
      src/test/java/de/hsfulda/onses/PlayerServiceTest.java
  8. 15
      src/test/java/de/hsfulda/onses/PlayerTest.java

29
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<Card> 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);
}
}

24
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<Card> playerDeck = new ArrayList<>();
public ArrayList<Card> 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;
}
}

1
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());

18
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<Player> 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));
}
}

28
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);
}
}

10
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);
}
}

36
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);
}
}

15
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);
}
}
Loading…
Cancel
Save