diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index be140a6..0000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,35 +0,0 @@ -include: - - template: SAST.gitlab-ci.yml - -default: - tags: ['docker-exec'] - - -image: maven:3.9.6-eclipse-temurin-21-jammy -variables: - MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode" - MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dmaven.artifact.threads=50" -cache: - paths: - - .m2/repository/ -junit tests: - stage: test - script: - - mvn test - allow_failure: false - artifacts: - expire_in: 1 week - reports: - junit: - - target/surefire-reports/TEST-*.xml - -script tests: - stage: test - script: - - ./build-project.sh - allow_failure: false - artifacts: - expire_in: 1 week - reports: - junit: - - target/surefire-reports/TEST-*.xml \ No newline at end of file diff --git a/build-project.sh b/build-project.sh index 1c3edee..c58d0f4 100755 --- a/build-project.sh +++ b/build-project.sh @@ -1,3 +1,7 @@ #!/bin/bash +echo "Init" +mvn dependency:resolve +echo "Build" +mvn clean compile echo "RUN JUnit Tests" -mvn clean test \ No newline at end of file +mvn clean test diff --git a/pom.xml b/pom.xml index 7775e6f..6c76b08 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,14 @@ ${junit.version} test + + org.testfx + testfx-junit5 + 4.0.17 + test + + + diff --git a/src/main/java/de/hsfulda/onses/App.java b/src/main/java/de/hsfulda/onses/App.java index 8712287..bda0c20 100644 --- a/src/main/java/de/hsfulda/onses/App.java +++ b/src/main/java/de/hsfulda/onses/App.java @@ -1,7 +1,7 @@ package de.hsfulda.onses; import de.hsfulda.onses.controllers.AppController; -import de.hsfulda.onses.models.Game; +import de.hsfulda.onses.services.GameService; import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; @@ -9,9 +9,9 @@ import javafx.stage.Stage; public class App extends Application { @Override public void start(Stage stage) throws Exception { - final AppController appController = new AppController(new Game()); + final AppController appController = new AppController(new GameService(), stage); - stage.setTitle("Onses - Uno Game"); + stage.setTitle("Onses - Uno"); stage.setScene(new Scene(appController.render())); stage.show(); } diff --git a/src/main/java/de/hsfulda/onses/controllers/AppController.java b/src/main/java/de/hsfulda/onses/controllers/AppController.java index 5f8a4cc..d2e0b80 100644 --- a/src/main/java/de/hsfulda/onses/controllers/AppController.java +++ b/src/main/java/de/hsfulda/onses/controllers/AppController.java @@ -2,27 +2,38 @@ package de.hsfulda.onses.controllers; import de.hsfulda.onses.Main; import de.hsfulda.onses.models.Game; +import de.hsfulda.onses.services.GameService; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; +import javafx.scene.Scene; import javafx.scene.control.Button; +import javafx.stage.Stage; import java.io.IOException; import java.util.Objects; public class AppController implements Controller { - private final Game game; + private final GameService gameService; + private final Stage stage; - public AppController(Game game) { - this.game = game; + public AppController(GameService gameService, Stage stage) { + this.gameService = gameService; + this.stage = stage; } @Override public Parent render() throws IOException { - final Parent parent = FXMLLoader.load(Main.class.getResource("views/app.fxml")); + GameController gameController = new GameController(gameService); + final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/app.fxml"))); Button button = (Button) parent.lookup("#startGameBtn"); button.setOnAction(e -> { - System.out.println("Pressed"); + try { + stage.setScene(new Scene(gameController.render())); + stage.setTitle("Onses - Uno Game"); + } catch (IOException ex) { + throw new RuntimeException(ex); + } }); return parent; } diff --git a/src/main/java/de/hsfulda/onses/controllers/CardController.java b/src/main/java/de/hsfulda/onses/controllers/CardController.java new file mode 100644 index 0000000..0f9ca86 --- /dev/null +++ b/src/main/java/de/hsfulda/onses/controllers/CardController.java @@ -0,0 +1,57 @@ +package de.hsfulda.onses.controllers; + +import de.hsfulda.onses.Main; +import de.hsfulda.onses.models.Card; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.control.Label; +import javafx.scene.layout.Pane; + +import java.io.IOException; +import java.util.Objects; + +public class CardController implements Controller { + + private Card card; + + public CardController(Card card) { + this.card = card; + } + @Override + public Parent render() throws IOException { + final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/card.fxml"))); + final Pane mainPane = (Pane) parent.lookup("#cardPane"); + final Label cardName = (Label) parent.lookup("#labelName"); + + switch(card.getColor()) { + case RED -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: red")); + case BLUE -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: blue")); + case GREEN -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: green")); + case YELLOW -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: yellow")); + default -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: black")); + } + + switch(card.getValue()) { + case ONE -> cardName.setText("1"); + case TWO -> cardName.setText("2"); + case THREE -> cardName.setText("3"); + case FOUR -> cardName.setText("4"); + case FIVE -> cardName.setText("5"); + case SIX -> cardName.setText("6"); + case SEVEN -> cardName.setText("7"); + case EIGHT -> cardName.setText("8"); + case NINE -> cardName.setText("9"); + case SKIP -> cardName.setText("skip player"); + case CHOOSE -> cardName.setText("wish card"); + case DRAWTWO -> cardName.setText("+2"); + case REVERSE -> cardName.setText("reverse"); + } + + + return parent; + } + + private String addStyle(String oldStyle, String newStyle) { + return oldStyle + "; " + newStyle; + } +} diff --git a/src/main/java/de/hsfulda/onses/controllers/GameController.java b/src/main/java/de/hsfulda/onses/controllers/GameController.java new file mode 100644 index 0000000..11ae0ae --- /dev/null +++ b/src/main/java/de/hsfulda/onses/controllers/GameController.java @@ -0,0 +1,54 @@ +package de.hsfulda.onses.controllers; + +import de.hsfulda.onses.Main; +import de.hsfulda.onses.models.Card; +import de.hsfulda.onses.models.Game; +import de.hsfulda.onses.services.GameService; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.control.Button; +import javafx.scene.layout.Pane; + +import java.io.IOException; +import java.util.Objects; + +public class GameController implements Controller { + private final GameService gameService; + private final Game game; + public GameController(GameService gameService) { + this.gameService = gameService; + this.game = gameService.getGame(); + } + @Override + public Parent render() throws IOException { + final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/game.fxml"))); + final Pane lastPlayedCardPane = (Pane) parent.lookup("#lastPlayedCardPane"); + final Pane enemyPane = (Pane) parent.lookup("#enemyPane"); + final Pane playerPane = (Pane) parent.lookup("#playerPane"); + final Button playButton = (Button) parent.lookup("#playCardBtn"); + + CardController lastPlayedCardController = new CardController(game.getLastPlayedCard()); + PlayerController playerController = new PlayerController(gameService.getGame().getPlayerService().getPlayerList().getFirst()); + PlayerController enemyController = new PlayerController(gameService.getGame().getPlayerService().getPlayerList().getLast()); + + game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, e -> { + lastPlayedCardPane.getChildren().removeAll(); + try { + lastPlayedCardPane.getChildren().add(new CardController((Card) e.getNewValue()).render()); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + }); + + playButton.setOnAction(e -> { + gameService.playCard(new Card().setColor(Card.Color.GREEN).setValue(Card.Value.ONE)); + }); + + + lastPlayedCardPane.getChildren().add(lastPlayedCardController.render()); + playerPane.getChildren().add(playerController.render()); + enemyPane.getChildren().add(enemyController.render()); + + return parent; + } +} diff --git a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java new file mode 100644 index 0000000..1757520 --- /dev/null +++ b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java @@ -0,0 +1,47 @@ +package de.hsfulda.onses.controllers; + +import de.hsfulda.onses.Main; +import de.hsfulda.onses.models.Card; +import de.hsfulda.onses.models.Player; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.control.Label; +import javafx.scene.layout.HBox; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Objects; + +public class PlayerController implements Controller { + + private final Player player; + + public PlayerController(Player player) { + this.player = player; + } + @Override + public Parent render() throws IOException { + final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/player.fxml"))); + final Label playerNameLabel = (Label) parent.lookup("#playerNameLabel"); + final HBox cards = (HBox) parent.lookup("#cardsHBox"); + + for(Card card : player.getPlayerDeck()) { + cards.getChildren().add(new CardController(card).render()); + } + + player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, e -> { + cards.getChildren().clear(); + for(Card card : player.getPlayerDeck()) { + try { + cards.getChildren().add(new CardController(card).render()); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + }); + + playerNameLabel.setText("Test"); + + return parent; + } +} diff --git a/src/main/java/de/hsfulda/onses/models/Player.java b/src/main/java/de/hsfulda/onses/models/Player.java index 09696bd..9dbfa14 100644 --- a/src/main/java/de/hsfulda/onses/models/Player.java +++ b/src/main/java/de/hsfulda/onses/models/Player.java @@ -12,6 +12,9 @@ public class Player { private PlayerService playerService; private Game game; + private String playerName; + private boolean enemy; + private Card currentCard; private final ArrayList playerDeck = new ArrayList<>(); public ArrayList getPlayerDeck() { @@ -24,6 +27,12 @@ public class Player { this.firePropertyChange(PROPERTY_PLAYER_DECK, oldplayerDeck, playerDeck); } + public void removeCardFromPlayerDeck(Card card) { + final ArrayList oldPlayerDeck = new ArrayList<>(this.playerDeck); + this.playerDeck.remove(card); + this.firePropertyChange(PROPERTY_PLAYER_DECK, oldPlayerDeck, playerDeck); + } + public PlayerService getPlayerService() { return playerService; } @@ -42,6 +51,33 @@ public class Player { return this; } + public String getPlayerName() { + return playerName; + } + + public Player setPlayerName(String playerName) { + this.playerName = playerName; + return this; + } + + public boolean isEnemy() { + return enemy; + } + + public Player setEnemy(boolean enemy) { + this.enemy = enemy; + return this; + } + + public Card getCurrentCard() { + return currentCard; + } + + public Player setCurrentCard(Card currentCard) { + this.currentCard = currentCard; + return this; + } + public PropertyChangeSupport listeners() { if(this.listeners == null) { this.listeners = new PropertyChangeSupport(this); diff --git a/src/main/java/de/hsfulda/onses/services/GameService.java b/src/main/java/de/hsfulda/onses/services/GameService.java index a996b0b..fc4edf3 100644 --- a/src/main/java/de/hsfulda/onses/services/GameService.java +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -4,6 +4,7 @@ import de.hsfulda.onses.models.Card; import de.hsfulda.onses.models.Game; import de.hsfulda.onses.models.Player; +import java.util.ArrayList; import java.util.Collections; import java.util.Random; @@ -23,6 +24,7 @@ public class GameService { public GameService(Game game) { this.game = game; this.game.setGameService(this); + this.game.getPlayerService().getPlayerList().getLast().setEnemy(true); fillDrawDeck(); shuffleDeck(); setFirstCard(); @@ -109,6 +111,21 @@ public class GameService { } } + public void playSeven() { + ArrayList übergangBot = new ArrayList<>(game.getPlayerService().getPlayerList().getLast().getPlayerDeck()); + ArrayList übergangSpieler = new ArrayList<>(game.getPlayerService().getPlayerList().getFirst().getPlayerDeck()); + + game.getPlayerService().getPlayerList().getFirst().getPlayerDeck().clear(); + for (int i = 0; i < übergangBot.size(); i++) { + game.getPlayerService().getPlayerList().getFirst().getPlayerDeck().add(übergangBot.get(i)); + } + + game.getPlayerService().getPlayerList().getLast().getPlayerDeck().clear(); + for (int i = 0; i < übergangSpieler.size(); i++) { + game.getPlayerService().getPlayerList().getLast().getPlayerDeck().add(übergangSpieler.get(i)); + } + } + public void checkForWin() { if (this.game.getPlayerService().getPlayerList().getFirst().getPlayerDeck().isEmpty() || this.game.getPlayerService().getPlayerList().getLast().getPlayerDeck().isEmpty()) { this.game.setGameOver(true); diff --git a/src/main/resources/de/hsfulda/onses/views/app.fxml b/src/main/resources/de/hsfulda/onses/views/app.fxml index 7facd52..7205ca5 100644 --- a/src/main/resources/de/hsfulda/onses/views/app.fxml +++ b/src/main/resources/de/hsfulda/onses/views/app.fxml @@ -5,15 +5,14 @@ - - + - + diff --git a/src/main/resources/de/hsfulda/onses/views/player.fxml b/src/main/resources/de/hsfulda/onses/views/player.fxml new file mode 100644 index 0000000..ae57f4a --- /dev/null +++ b/src/main/resources/de/hsfulda/onses/views/player.fxml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + diff --git a/src/test/java/de/hsfulda/onses/ExampleTest.java b/src/test/java/de/hsfulda/onses/ExampleTest.java deleted file mode 100644 index d4e9302..0000000 --- a/src/test/java/de/hsfulda/onses/ExampleTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.hsfulda.onses; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -public class ExampleTest { - @Test - @DisplayName("Example Test") - public void exampleTest() { - int test = 1 + 1; - assertEquals(2, test); - } -} \ No newline at end of file diff --git a/src/test/java/de/hsfulda/onses/GameServiceTest.java b/src/test/java/de/hsfulda/onses/GameServiceTest.java index 161d8dc..0ffff9f 100644 --- a/src/test/java/de/hsfulda/onses/GameServiceTest.java +++ b/src/test/java/de/hsfulda/onses/GameServiceTest.java @@ -1,6 +1,7 @@ package de.hsfulda.onses; +import com.sun.jdi.ArrayReference; import de.hsfulda.onses.models.Game; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -11,6 +12,8 @@ import de.hsfulda.onses.models.Card; import de.hsfulda.onses.models.Player; import de.hsfulda.onses.services.GameService; +import java.util.ArrayList; + public class GameServiceTest { @Test @DisplayName("playCardRedEight") @@ -534,4 +537,32 @@ public class GameServiceTest { assertTrue(gameService.getGame().getDrawCardDeck().getLast().isFacedown()); } + + @DisplayName("PlaySevenPlayerDeckIsNowBotDeck") + public void PlaySevenPlayerDeckIsNowBotDeck() { + GameService gameService = new GameService(); + ArrayList botDeck = new ArrayList<>(gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck()); + gameService.playSeven(); + ArrayList answer = new ArrayList<>(gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck()); + assertEquals(botDeck, answer); + } + + @Test + @DisplayName("PlaySevenBotDeckIsNowPlayerDeck") + public void PlaySevenBotDeckIsNowPlayerDeck() { + GameService gameService = new GameService(); + ArrayList playerDeck = new ArrayList<>(gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck()); + gameService.playSeven(); + ArrayList answer = new ArrayList<>(gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck()); + assertEquals(playerDeck, answer); + } + + @Test + @DisplayName("BotisEnemy") + public void botIsEnemy() { + GameService gameService = new GameService(); + boolean answer = gameService.getGame().getPlayerService().getPlayerList().getLast().isEnemy(); + boolean expected = true; + assertEquals(expected, answer); + } } diff --git a/src/test/java/de/hsfulda/onses/GuiTest.java b/src/test/java/de/hsfulda/onses/GuiTest.java new file mode 100644 index 0000000..845c004 --- /dev/null +++ b/src/test/java/de/hsfulda/onses/GuiTest.java @@ -0,0 +1,32 @@ +package de.hsfulda.onses; + +import javafx.stage.Stage; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.testfx.api.FxAssert; +import org.testfx.framework.junit5.ApplicationTest; +import static org.junit.jupiter.api.Assertions.*; +import static org.testfx.matcher.control.LabeledMatchers.*; + +public class GuiTest extends ApplicationTest { + private Stage stage; + + @Override + public void start(Stage stage) throws Exception { + this.stage = stage; + new App().start(stage); + } + + @Test + @DisplayName("Check Window Title") + void checkWindowsTitle() { + assertEquals("Onses - Uno", stage.getTitle()); + } + + @Test + @DisplayName("Check if window Switch is working") + void checkSceneSwitch() { + clickOn("#startGameBtn"); + assertEquals("Onses - Uno Game", stage.getTitle()); + } +} diff --git a/src/test/java/de/hsfulda/onses/PlayerTest.java b/src/test/java/de/hsfulda/onses/PlayerTest.java index fcdeeac..9ce04fd 100644 --- a/src/test/java/de/hsfulda/onses/PlayerTest.java +++ b/src/test/java/de/hsfulda/onses/PlayerTest.java @@ -58,4 +58,60 @@ public class PlayerTest { assertEquals(expected, answer1); assertEquals(expected, answer2); } + + @Test + @DisplayName("remove card from player card deck") + void removeCardFromPlayerCardDeck() { + Player player = new Player(); + Card card1 = new Card().setColor(Card.Color.RED).setValue(Card.Value.FIVE); + Card card2 = new Card().setColor(Card.Color.GREEN).setValue(Card.Value.ONE); + + player.addCardToPlayerDeck(card1); + player.addCardToPlayerDeck(card2); + + assertEquals(2, player.getPlayerDeck().size()); + + player.removeCardFromPlayerDeck(card1); + assertEquals(1, player.getPlayerDeck().size()); + assertEquals(card2, player.getPlayerDeck().getFirst()); + } + + @Test + @DisplayName("GivePlayerName") + public void GivePlayerName() { + Player player = new Player(); + String expected = "Spieler"; + player.setPlayerName(expected); + String answer = player.getPlayerName(); + assertEquals(expected, answer); + } + + @Test + @DisplayName("ChangePlayerName") + public void ChangePlayerName() { + PlayerService playerService = new PlayerService(); + String name1 = "Name1"; + String name2 = "Name2"; + playerService.getPlayerList().getFirst().setPlayerName(name1); + playerService.getPlayerList().getFirst().setPlayerName(name2); + String answer = playerService.getPlayerList().getFirst().getPlayerName(); + assertEquals(name2, answer); + } + + @Test + @DisplayName("BotIsEnemy") + public void BotIsEnemy() { + Player player = new Player(); + player.setEnemy(true); + assertTrue(player.isEnemy()); + } + + @Test + @DisplayName("CurrentCard") + public void CurrentCard() { + Player player = new Player(); + Card card = new Card().setValue(Card.Value.ONE).setColor(Card.Color.GREEN); + player.setCurrentCard(card); + assertEquals(card, player.getCurrentCard()); + } }