From 6da606939fa252702f03b5fe62693b4b3ead7036 Mon Sep 17 00:00:00 2001 From: fdai7920 Date: Wed, 24 Jan 2024 12:25:58 +0100 Subject: [PATCH 1/6] add GameService --- .../de/hsfulda/onses/services/GameService.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/main/java/de/hsfulda/onses/services/GameService.java diff --git a/src/main/java/de/hsfulda/onses/services/GameService.java b/src/main/java/de/hsfulda/onses/services/GameService.java new file mode 100644 index 0000000..9e0c6cf --- /dev/null +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -0,0 +1,14 @@ +package de.hsfulda.onses.services; + +import de.hsfulda.onses.models.Game; + +public class GameService { + private final Game game; + public GameService(Game game) { + this.game = game; + } + public GameService() { + this(new Game()); + } + +} From 7f6bdc90220d94742f6c996f3ce588d5f5714c8e Mon Sep 17 00:00:00 2001 From: fdai7920 Date: Wed, 24 Jan 2024 12:31:14 +0100 Subject: [PATCH 2/6] add playCard --- src/main/java/de/hsfulda/onses/services/GameService.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/de/hsfulda/onses/services/GameService.java b/src/main/java/de/hsfulda/onses/services/GameService.java index 9e0c6cf..26aa2c2 100644 --- a/src/main/java/de/hsfulda/onses/services/GameService.java +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -1,6 +1,8 @@ package de.hsfulda.onses.services; +import de.hsfulda.onses.models.Card; import de.hsfulda.onses.models.Game; +import de.hsfulda.onses.models.Player; public class GameService { private final Game game; @@ -11,4 +13,11 @@ public class GameService { this(new Game()); } + public void playCard(Player player, Card card) + { + // add lastPlayedCard back to drawCardDeck + game.setLastPlayedCard(card); + // check for special rules (draw, colorchoose, skip,...) + + } } From 4a80d5d3e9b28f08080ee9dddb84517f6a9e6b01 Mon Sep 17 00:00:00 2001 From: fdai7920 Date: Wed, 24 Jan 2024 13:52:29 +0100 Subject: [PATCH 3/6] Test.playCard --- .../hsfulda/onses/services/GameService.java | 5 +++ .../de/hsfulda/onses/GameServiceTest.java | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/test/java/de/hsfulda/onses/GameServiceTest.java diff --git a/src/main/java/de/hsfulda/onses/services/GameService.java b/src/main/java/de/hsfulda/onses/services/GameService.java index 26aa2c2..b50422a 100644 --- a/src/main/java/de/hsfulda/onses/services/GameService.java +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -13,6 +13,10 @@ public class GameService { this(new Game()); } + public Game getGame() { + return game; + } + public void playCard(Player player, Card card) { // add lastPlayedCard back to drawCardDeck @@ -20,4 +24,5 @@ public class GameService { // check for special rules (draw, colorchoose, skip,...) } + } diff --git a/src/test/java/de/hsfulda/onses/GameServiceTest.java b/src/test/java/de/hsfulda/onses/GameServiceTest.java new file mode 100644 index 0000000..2d7f904 --- /dev/null +++ b/src/test/java/de/hsfulda/onses/GameServiceTest.java @@ -0,0 +1,38 @@ + +package de.hsfulda.onses; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import de.hsfulda.onses.models.Card; +import de.hsfulda.onses.models.Player; +import de.hsfulda.onses.models.Game; +import de.hsfulda.onses.services.GameService; + +public class GameServiceTest { + @Test + @DisplayName("playCardRedEight") + public void playCardRedEight() { + // arrange + Card input = new Card().setColor(Card.Color.RED).setValue(Card.Value.EIGHT); + // act + GameService gameService = new GameService(); + gameService.playCard(new Player(), input); + Card answer = gameService.getGame().getLastPlayedCard(); + // assert + assertEquals(input, answer); + } + @Test + @DisplayName("playCardBlackChoose") + public void playCardBlackChoose() { + // arrange + Card input = new Card().setColor(Card.Color.BLACK).setValue(Card.Value.CHOOSE); + // act + GameService gameService = new GameService(); + gameService.playCard(new Player(), input); + Card answer = gameService.getGame().getLastPlayedCard(); + // assert + assertEquals(input, answer); + } +} From d7dfa4a2e39261458a711a1e6f4e06c24933b8b6 Mon Sep 17 00:00:00 2001 From: fdai7920 Date: Wed, 24 Jan 2024 14:36:16 +0100 Subject: [PATCH 4/6] add allowedMove --- .../java/de/hsfulda/onses/services/GameService.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/hsfulda/onses/services/GameService.java b/src/main/java/de/hsfulda/onses/services/GameService.java index b50422a..3edca44 100644 --- a/src/main/java/de/hsfulda/onses/services/GameService.java +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -20,9 +20,18 @@ public class GameService { public void playCard(Player player, Card card) { // add lastPlayedCard back to drawCardDeck - game.setLastPlayedCard(card); + game.setLastPlayedCard(card); // check for special rules (draw, colorchoose, skip,...) + } + + public boolean allowedMove(Player player, Card card) + { + boolean isAllowed = false; + Card lastCard = game.getLastPlayedCard(); + // rules: + if (card.getColor() == lastCard.getColor()) isAllowed = true; // same color + return isAllowed; } } From daea448c5ce7226bf27163c8c02c9291a1e154f5 Mon Sep 17 00:00:00 2001 From: fdai7920 Date: Wed, 24 Jan 2024 14:50:04 +0100 Subject: [PATCH 5/6] Test.allowedMove --- .../de/hsfulda/onses/GameServiceTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/java/de/hsfulda/onses/GameServiceTest.java b/src/test/java/de/hsfulda/onses/GameServiceTest.java index 2d7f904..97cba48 100644 --- a/src/test/java/de/hsfulda/onses/GameServiceTest.java +++ b/src/test/java/de/hsfulda/onses/GameServiceTest.java @@ -35,4 +35,34 @@ public class GameServiceTest { // assert assertEquals(input, answer); } + + @Test + @DisplayName("allowedMoveSameColorRed") + public void allowedMoveSameColorRed() { + // arrange + Card input = new Card().setColor(Card.Color.RED).setValue(Card.Value.TWO); + boolean expected = true; + // act + GameService gameService = new GameService(); + gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.RED).setValue(Card.Value.FIVE)); + + boolean answer = gameService.allowedMove(new Player(), input); + // assert + assertEquals(expected, answer); + } + @Test + @DisplayName("allowedMoveSameColorBlue") + public void allowedMoveSameColorBlue() { + // arrange + Card input = new Card().setColor(Card.Color.BLUE).setValue(Card.Value.THREE); + boolean expected = true; + // act + GameService gameService = new GameService(); + gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.BLUE).setValue(Card.Value.ONE)); + + boolean answer = gameService.allowedMove(new Player(), input); + // assert + assertEquals(expected, answer); + } + } From bac78b56ffd7a2d53ee9cf722108b74716f453f5 Mon Sep 17 00:00:00 2001 From: fdai7920 Date: Thu, 25 Jan 2024 19:39:48 +0100 Subject: [PATCH 6/6] Refactoring: change Name of allowedMove --- .../java/de/hsfulda/onses/services/GameService.java | 8 ++++---- src/test/java/de/hsfulda/onses/GameServiceTest.java | 13 ++++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/hsfulda/onses/services/GameService.java b/src/main/java/de/hsfulda/onses/services/GameService.java index 3edca44..bdee4d5 100644 --- a/src/main/java/de/hsfulda/onses/services/GameService.java +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -24,14 +24,14 @@ public class GameService { // check for special rules (draw, colorchoose, skip,...) } - public boolean allowedMove(Player player, Card card) + public boolean legalMove(Player player, Card card) { - boolean isAllowed = false; + boolean legalMoveFound = false; Card lastCard = game.getLastPlayedCard(); // rules: - if (card.getColor() == lastCard.getColor()) isAllowed = true; // same color + if (card.getColor() == lastCard.getColor()) legalMoveFound = true; // same color - return isAllowed; + return legalMoveFound; } } diff --git a/src/test/java/de/hsfulda/onses/GameServiceTest.java b/src/test/java/de/hsfulda/onses/GameServiceTest.java index 97cba48..8a22108 100644 --- a/src/test/java/de/hsfulda/onses/GameServiceTest.java +++ b/src/test/java/de/hsfulda/onses/GameServiceTest.java @@ -7,7 +7,6 @@ import static org.junit.jupiter.api.Assertions.*; import de.hsfulda.onses.models.Card; import de.hsfulda.onses.models.Player; -import de.hsfulda.onses.models.Game; import de.hsfulda.onses.services.GameService; public class GameServiceTest { @@ -37,8 +36,8 @@ public class GameServiceTest { } @Test - @DisplayName("allowedMoveSameColorRed") - public void allowedMoveSameColorRed() { + @DisplayName("legalMoveSameColorRed") + public void legalMoveSameColorRed() { // arrange Card input = new Card().setColor(Card.Color.RED).setValue(Card.Value.TWO); boolean expected = true; @@ -46,13 +45,13 @@ public class GameServiceTest { GameService gameService = new GameService(); gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.RED).setValue(Card.Value.FIVE)); - boolean answer = gameService.allowedMove(new Player(), input); + boolean answer = gameService.legalMove(new Player(), input); // assert assertEquals(expected, answer); } @Test - @DisplayName("allowedMoveSameColorBlue") - public void allowedMoveSameColorBlue() { + @DisplayName("legalMoveSameColorBlue") + public void legalMoveSameColorBlue() { // arrange Card input = new Card().setColor(Card.Color.BLUE).setValue(Card.Value.THREE); boolean expected = true; @@ -60,7 +59,7 @@ public class GameServiceTest { GameService gameService = new GameService(); gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.BLUE).setValue(Card.Value.ONE)); - boolean answer = gameService.allowedMove(new Player(), input); + boolean answer = gameService.legalMove(new Player(), input); // assert assertEquals(expected, answer); }