diff --git a/README.md b/README.md index b7219f9..b5308df 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # Onses - Uno Game +## Disclaimer for Tests +**!!Do not move the mouse while tests are running!!** + ## git setup after clone ```shell git config user.name "" diff --git a/src/main/java/de/hsfulda/onses/App.java b/src/main/java/de/hsfulda/onses/App.java index bda0c20..d57e6e6 100644 --- a/src/main/java/de/hsfulda/onses/App.java +++ b/src/main/java/de/hsfulda/onses/App.java @@ -1,18 +1,54 @@ package de.hsfulda.onses; import de.hsfulda.onses.controllers.AppController; +import de.hsfulda.onses.controllers.Controller; import de.hsfulda.onses.services.GameService; import javafx.application.Application; +import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.control.Label; import javafx.stage.Stage; +import java.io.IOException; + public class App extends Application { + + private Stage stage; + private Controller controller; + + private final GameService gameService; + + public App() { + this(new GameService()); + } + + public App(GameService gameService) { + this.gameService = gameService; + } @Override - public void start(Stage stage) throws Exception { - final AppController appController = new AppController(new GameService(), stage); + public void start(Stage stage) { + this.stage = stage; + final AppController appController = new AppController(this, this.gameService); + + stage.setScene(new Scene(new Label("Loading..."))); + stage.setOnCloseRequest(e -> controller.destroy()); - stage.setTitle("Onses - Uno"); - stage.setScene(new Scene(appController.render())); + show(appController); stage.show(); } + + public void show(Controller controller) { + try { + final Parent parent = controller.render(); + stage.getScene().setRoot(parent); + } catch (IOException e) { + throw new RuntimeException(e); + } + + if(this.controller != null) { + this.controller.destroy(); + } + this.controller = controller; + stage.setTitle(controller.getTitle()); + } } diff --git a/src/main/java/de/hsfulda/onses/controllers/AppController.java b/src/main/java/de/hsfulda/onses/controllers/AppController.java index d2e0b80..4a59ee0 100644 --- a/src/main/java/de/hsfulda/onses/controllers/AppController.java +++ b/src/main/java/de/hsfulda/onses/controllers/AppController.java @@ -1,13 +1,11 @@ package de.hsfulda.onses.controllers; +import de.hsfulda.onses.App; 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; @@ -15,26 +13,32 @@ import java.util.Objects; public class AppController implements Controller { private final GameService gameService; - private final Stage stage; + private final App app; - public AppController(GameService gameService, Stage stage) { - this.gameService = gameService; - this.stage = stage; + public AppController(App app, GameService gameService) { + this.app = app; + this.gameService = gameService; } + @Override public Parent render() throws IOException { - GameController gameController = new GameController(gameService); + GameController gameController = new GameController(app, gameService); final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/app.fxml"))); Button button = (Button) parent.lookup("#startGameBtn"); button.setOnAction(e -> { - try { - stage.setScene(new Scene(gameController.render())); - stage.setTitle("Onses - Uno Game"); - } catch (IOException ex) { - throw new RuntimeException(ex); - } + app.show(gameController); }); return parent; } + + @Override + public String getTitle() { + return "Onses - Uno"; + } + + @Override + public void destroy() { + + } } diff --git a/src/main/java/de/hsfulda/onses/controllers/CardController.java b/src/main/java/de/hsfulda/onses/controllers/CardController.java index 0f9ca86..f8ed28a 100644 --- a/src/main/java/de/hsfulda/onses/controllers/CardController.java +++ b/src/main/java/de/hsfulda/onses/controllers/CardController.java @@ -2,51 +2,98 @@ 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.Pane; +import java.beans.PropertyChangeListener; import java.io.IOException; import java.util.Objects; public class CardController implements Controller { - private Card card; + private final Card card; + private final Player player; - public CardController(Card card) { + private PropertyChangeListener cardSelectedChangeListener; + private PropertyChangeListener cardColorChangeListener; + + public CardController(Card card, Player player) { this.card = card; + this.player = player; } + @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")); + if(!card.isFacedown()) { + 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")); + } + } + + if(card.isFacedown()) { + cardName.setText("Card"); + } else { + 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"); + case CHOOSEDRAW -> cardName.setText("+4"); + } } - 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"); + if(player != null && !player.isEnemy()) { + mainPane.setOnMousePressed(e -> { + Card oldCard = player.getCurrentCard(); + if(oldCard != null) { + oldCard.setSelected(false); + } + player.setCurrentCard(card); + card.setSelected(true); + }); } + cardSelectedChangeListener = e -> { + boolean oldValue = (boolean) e.getOldValue(); + if(oldValue) { + mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: black")); + } else { + mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: pink")); + } + + }; + card.listeners().addPropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener); + + cardColorChangeListener = e -> { + switch((Card.Color)e.getNewValue()) { + 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")); + } + }; + card.listeners().addPropertyChangeListener(Card.PROPERTY_COLOR, cardColorChangeListener); return parent; } @@ -54,4 +101,15 @@ public class CardController implements Controller { private String addStyle(String oldStyle, String newStyle) { return oldStyle + "; " + newStyle; } + + @Override + public String getTitle() { + return "Onses - Uno Game"; + } + + @Override + public void destroy() { + card.listeners().removePropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener); + card.listeners().removePropertyChangeListener(Card.PROPERTY_COLOR, cardColorChangeListener); + } } diff --git a/src/main/java/de/hsfulda/onses/controllers/Controller.java b/src/main/java/de/hsfulda/onses/controllers/Controller.java index 3a92766..4ce5b11 100644 --- a/src/main/java/de/hsfulda/onses/controllers/Controller.java +++ b/src/main/java/de/hsfulda/onses/controllers/Controller.java @@ -6,4 +6,8 @@ import java.io.IOException; public interface Controller { Parent render() throws IOException; + + void destroy(); + + String getTitle(); } diff --git a/src/main/java/de/hsfulda/onses/controllers/GameController.java b/src/main/java/de/hsfulda/onses/controllers/GameController.java index 11ae0ae..45d0e47 100644 --- a/src/main/java/de/hsfulda/onses/controllers/GameController.java +++ b/src/main/java/de/hsfulda/onses/controllers/GameController.java @@ -1,5 +1,6 @@ package de.hsfulda.onses.controllers; +import de.hsfulda.onses.App; import de.hsfulda.onses.Main; import de.hsfulda.onses.models.Card; import de.hsfulda.onses.models.Game; @@ -9,16 +10,26 @@ import javafx.scene.Parent; import javafx.scene.control.Button; import javafx.scene.layout.Pane; +import java.beans.PropertyChangeListener; import java.io.IOException; +import java.util.ArrayList; import java.util.Objects; public class GameController implements Controller { private final GameService gameService; private final Game game; - public GameController(GameService gameService) { + private final App app; + + private PropertyChangeListener lastPlayedCardPropertyChangeListener; + private PropertyChangeListener gameOverListener; + + private final ArrayList controllers = new ArrayList<>(); + public GameController(App app, GameService gameService) { + this.app = app; 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"))); @@ -26,22 +37,79 @@ public class GameController implements Controller { final Pane enemyPane = (Pane) parent.lookup("#enemyPane"); final Pane playerPane = (Pane) parent.lookup("#playerPane"); final Button playButton = (Button) parent.lookup("#playCardBtn"); + final Button drawCardButton = (Button) parent.lookup("#drawCardBtn"); + final Button exitGameButton = (Button) parent.lookup("#exitBtn"); + final Button wishRedButton = (Button) parent.lookup("#btnWishRed"); + final Button wishBlueButton = (Button) parent.lookup("#btnWishBlue"); + final Button wishGreenButton = (Button) parent.lookup("#btnWishGreen"); + final Button wishYellowButton = (Button) parent.lookup("#btnWishYellow"); - CardController lastPlayedCardController = new CardController(game.getLastPlayedCard()); + CardController lastPlayedCardController = new CardController(game.getLastPlayedCard(), null); 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 -> { + controllers.add(lastPlayedCardController); + controllers.add(playerController); + controllers.add(enemyController); + + wishRedButton.setOnAction(e -> { + Card currentSelectedCard = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); + currentSelectedCard.setColor(Card.Color.RED); + }); + + wishBlueButton.setOnAction(e -> { + Card currentSelectedCard = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); + currentSelectedCard.setColor(Card.Color.BLUE); + }); + + wishGreenButton.setOnAction(e -> { + Card currentSelectedCard = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); + currentSelectedCard.setColor(Card.Color.GREEN); + }); + + wishYellowButton.setOnAction(e -> { + Card currentSelectedCard = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); + currentSelectedCard.setColor(Card.Color.YELLOW); + }); + + lastPlayedCardPropertyChangeListener = e -> { lastPlayedCardPane.getChildren().removeAll(); try { - lastPlayedCardPane.getChildren().add(new CardController((Card) e.getNewValue()).render()); + CardController tmp = new CardController((Card) e.getNewValue(), null); + controllers.add(tmp); + lastPlayedCardPane.getChildren().add(tmp.render()); } catch (IOException ex) { throw new RuntimeException(ex); } + }; + game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, lastPlayedCardPropertyChangeListener); + + gameOverListener = e -> { + app.show(new GameOverController(app)); + }; + game.listeners().addPropertyChangeListener(Game.PROPERTY_GAME_OVER, gameOverListener); + + exitGameButton.setOnAction(e -> { + app.show(new AppController(app, new GameService())); }); playButton.setOnAction(e -> { - gameService.playCard(new Card().setColor(Card.Color.GREEN).setValue(Card.Value.ONE)); + Card cardToPlay = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); + if(cardToPlay.getValue().equals(Card.Value.CHOOSE) || cardToPlay.getValue().equals(Card.Value.CHOOSEDRAW)) { + if(!cardToPlay.getColor().equals(Card.Color.BLACK)) { + if(cardToPlay.getPlayer() != null) { + gameService.playCard(cardToPlay); + } + } + } else if(gameService.legalMove(cardToPlay)){ + if(cardToPlay.getPlayer() != null) { + gameService.playCard(cardToPlay); + } + } + }); + + drawCardButton.setOnAction(e -> { + gameService.drawCard(1); }); @@ -51,4 +119,18 @@ public class GameController implements Controller { return parent; } + + @Override + public String getTitle() { + return "Onses - Uno Game"; + } + + @Override + public void destroy() { + for (Controller controller : controllers) { + controller.destroy(); + } + game.listeners().removePropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, lastPlayedCardPropertyChangeListener); + game.listeners().removePropertyChangeListener(Game.PROPERTY_GAME_OVER, gameOverListener); + } } diff --git a/src/main/java/de/hsfulda/onses/controllers/GameOverController.java b/src/main/java/de/hsfulda/onses/controllers/GameOverController.java new file mode 100644 index 0000000..5aaf6f9 --- /dev/null +++ b/src/main/java/de/hsfulda/onses/controllers/GameOverController.java @@ -0,0 +1,40 @@ +package de.hsfulda.onses.controllers; + +import de.hsfulda.onses.App; +import de.hsfulda.onses.Main; +import de.hsfulda.onses.services.GameService; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.control.Button; + +import java.io.IOException; +import java.util.Objects; + +public class GameOverController implements Controller { + + private final App app; + + public GameOverController(App app) { + this.app = app; + } + @Override + public Parent render() throws IOException { + final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/gameOver.fxml"))); + final Button newGameButton = (Button) parent.lookup("#newGameBtn"); + + newGameButton.setOnAction(e -> { + app.show(new GameController(app, new GameService())); + }); + return parent; + } + + @Override + public void destroy() { + + } + + @Override + public String getTitle() { + return null; + } +} diff --git a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java index 1757520..67286ea 100644 --- a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java +++ b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java @@ -8,6 +8,7 @@ import javafx.scene.Parent; import javafx.scene.control.Label; import javafx.scene.layout.HBox; +import java.beans.PropertyChangeListener; import java.io.IOException; import java.util.ArrayList; import java.util.Objects; @@ -15,10 +16,13 @@ import java.util.Objects; public class PlayerController implements Controller { private final Player player; + private final ArrayList controllers = new ArrayList<>(); + private PropertyChangeListener playerDeckChangeListener; 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"))); @@ -26,22 +30,38 @@ public class PlayerController implements Controller { final HBox cards = (HBox) parent.lookup("#cardsHBox"); for(Card card : player.getPlayerDeck()) { - cards.getChildren().add(new CardController(card).render()); + CardController newCardController = new CardController(card, player); + controllers.add(newCardController); + cards.getChildren().add(newCardController.render()); } - player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, e -> { + playerDeckChangeListener = e -> { cards.getChildren().clear(); for(Card card : player.getPlayerDeck()) { try { - cards.getChildren().add(new CardController(card).render()); + cards.getChildren().add(new CardController(card, player).render()); } catch (IOException ex) { throw new RuntimeException(ex); } } - }); + }; + player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, playerDeckChangeListener); - playerNameLabel.setText("Test"); + playerNameLabel.setText(player.getPlayerName()); return parent; } + + @Override + public String getTitle() { + return "Onses - Uno Game"; + } + + @Override + public void destroy() { + for (Controller controller : controllers) { + controller.destroy(); + } + player.listeners().removePropertyChangeListener(Player.PROPERTY_PLAYER_DECK, playerDeckChangeListener); + } } diff --git a/src/main/java/de/hsfulda/onses/models/Card.java b/src/main/java/de/hsfulda/onses/models/Card.java index 2dbc3d5..ab2a1a9 100644 --- a/src/main/java/de/hsfulda/onses/models/Card.java +++ b/src/main/java/de/hsfulda/onses/models/Card.java @@ -7,11 +7,16 @@ public class Card { public final static String PROPERTY_COLOR = "color"; public final static String PROPERTY_VALUE = "value"; + public final static String PROPERTY_SELECTED = "selected"; + protected PropertyChangeSupport listeners; private Color color; private Value value; private boolean facedown = false; + private boolean selected; + + private Player player; public enum Color { RED, BLUE, GREEN, YELLOW, BLACK; @@ -51,6 +56,26 @@ public class Card { return this; } + public boolean isSelected() { + return selected; + } + + public Card setSelected(boolean selected) { + final boolean oldValue = this.selected; + this.selected = selected; + this.firePropertyChange(PROPERTY_SELECTED, oldValue, selected); + return this; + } + + public Player getPlayer() { + return player; + } + + public Card setPlayer(Player player) { + this.player = player; + return this; + } + public PropertyChangeSupport listeners() { if(this.listeners == null) { this.listeners = new PropertyChangeSupport(this); diff --git a/src/main/java/de/hsfulda/onses/models/Game.java b/src/main/java/de/hsfulda/onses/models/Game.java index 6a9c67a..343de38 100644 --- a/src/main/java/de/hsfulda/onses/models/Game.java +++ b/src/main/java/de/hsfulda/onses/models/Game.java @@ -10,6 +10,7 @@ public class Game { public final static String PROPERTY_LAST_PLAYED_CARD = "lastPlayedCard"; public final static String PROPERTY_DRAW_CARD_DECK = "drawCardDeck"; + public final static String PROPERTY_GAME_OVER = "gameOver"; protected PropertyChangeSupport listeners; @@ -70,7 +71,9 @@ public class Game { } public void setGameOver(boolean gameOver) { + final boolean oldValue = this.gameOver; this.gameOver = gameOver; + this.firePropertyChange(PROPERTY_GAME_OVER, oldValue, gameOver); } public boolean getGameOver() { diff --git a/src/main/java/de/hsfulda/onses/models/Player.java b/src/main/java/de/hsfulda/onses/models/Player.java index 9dbfa14..e20dd1d 100644 --- a/src/main/java/de/hsfulda/onses/models/Player.java +++ b/src/main/java/de/hsfulda/onses/models/Player.java @@ -7,6 +7,7 @@ import java.util.ArrayList; public class Player { public final static String PROPERTY_PLAYER_DECK = "playerDeck"; + public final static String PROPERTY_CURRENT_CARD = "currentCard"; protected PropertyChangeSupport listeners; @@ -22,6 +23,7 @@ public class Player { } public void addCardToPlayerDeck(Card card) { + card.setPlayer(this); final ArrayList oldplayerDeck = new ArrayList<>(this.playerDeck); playerDeck.add(card); this.firePropertyChange(PROPERTY_PLAYER_DECK, oldplayerDeck, playerDeck); @@ -74,7 +76,9 @@ public class Player { } public Player setCurrentCard(Card currentCard) { + final Card oldCard = this.currentCard; this.currentCard = currentCard; + this.firePropertyChange(PROPERTY_CURRENT_CARD, oldCard, currentCard); 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 fc4edf3..34da7cb 100644 --- a/src/main/java/de/hsfulda/onses/services/GameService.java +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -54,20 +54,16 @@ public class GameService { player = game.getPlayerService().getPlayerList().getLast(); } for (int i = 0; i < amount; i++) { - player.getPlayerDeck().add(game.getDrawCardDeck().getFirst().setFacedown(isFaceDown)); + Card newCard = game.getDrawCardDeck().getFirst().setFacedown(isFaceDown).setPlayer(player); + player.addCardToPlayerDeck(newCard); game.getDrawCardDeck().removeFirst(); } if (amount == 1 && !player.getPlayerDeck().isEmpty()) { - Card drawnCard = player.getPlayerDeck().getLast(); - - if (legalMove(drawnCard)) { - player.getPlayerService().removeCardFromPlayerDeck(drawnCard); - playCard(drawnCard); - } - else nextPlayer(); + nextPlayer(); + if (!game.getPlayerService().getCurrentTurn()) game.getPlayerService().botMove(); } } - if (amount != 1) { + if (amount == 7) { nextPlayer(); } } @@ -82,32 +78,9 @@ public class GameService { public void chooseColor() { // Abfrage Farbe - Card.Color color = Card.Color.BLUE; //Vorübergehend Blau - game.changeLastPlayedCardColor(color); - - if (!game.getPlayerService().getCurrentTurn()){ - int min = 0; - int max = 3; - Random rand = new Random(); - int randomNum = rand.nextInt((max - min) + 1) + min; - switch (randomNum){ - case 0: - color = Card.Color.BLUE; - game.changeLastPlayedCardColor(color); - break; - case 1: - color = Card.Color.RED; - game.changeLastPlayedCardColor(color); - break; - case 2: - color = Card.Color.YELLOW; - game.changeLastPlayedCardColor(color); - break; - case 3: - color = Card.Color.GREEN; - game.changeLastPlayedCardColor(color); - break; - } + if (game.getPlayerService().getCurrentTurn()) { + Card.Color color = Card.Color.BLUE; //Vorübergehend Blau + game.changeLastPlayedCardColor(color); } } @@ -133,6 +106,9 @@ public class GameService { } public void playCard(Card card) { + if(card.getPlayer() != null) { + card.getPlayer().removeCardFromPlayerDeck(card); + } // add lastPlayedCard back to drawCardDeck this.addLastPlayedCardToDrawCardDeck(); game.setLastPlayedCard(card); @@ -145,11 +121,11 @@ public class GameService { nextPlayer(); break; case CHOOSE: - chooseColor(); + //chooseColor(); nextPlayer(); break; case CHOOSEDRAW: - chooseColor(); + //chooseColor(); nextPlayer(); drawCard(4); break; @@ -177,8 +153,6 @@ public class GameService { if (card.getValue() == lastCard.getValue()) legalMoveFound = true; // same value - if (card.getColor() == Card.Color.BLACK) legalMoveFound = true; // Color Black - return legalMoveFound; } @@ -218,4 +192,4 @@ public class GameService { this.drawCard(7); } -} +} \ No newline at end of file diff --git a/src/main/java/de/hsfulda/onses/services/PlayerService.java b/src/main/java/de/hsfulda/onses/services/PlayerService.java index 14382d3..70e816d 100644 --- a/src/main/java/de/hsfulda/onses/services/PlayerService.java +++ b/src/main/java/de/hsfulda/onses/services/PlayerService.java @@ -6,6 +6,7 @@ import de.hsfulda.onses.models.Card; import de.hsfulda.onses.services.GameService; import java.util.ArrayList; +import java.util.Random; public class PlayerService { @@ -46,8 +47,8 @@ public class PlayerService { } public PlayerService() { - addPlayerToList(new Player().setPlayerService(this)); - addPlayerToList(new Player().setPlayerService(this)); + addPlayerToList(new Player().setPlayerService(this).setPlayerName("Player")); + addPlayerToList(new Player().setPlayerService(this).setPlayerName("Bot")); } public void removeCardFromPlayerDeck(Card card) { @@ -57,7 +58,7 @@ public class PlayerService { } else { player = playerList.getLast(); } - player.getPlayerDeck().remove(card); + player.removeCardFromPlayerDeck(card); } public void botMove() { @@ -66,12 +67,33 @@ public class PlayerService { if (getGame().getGameService().legalMove(this.playerList.getLast().getPlayerDeck().get(i))) { Card playCard = this.playerList.getLast().getPlayerDeck().get(i); this.removeCardFromPlayerDeck(playCard); + if (playCard.getValue() == Card.Value.CHOOSE || playCard.getValue() == Card.Value.CHOOSEDRAW) + { + int min = 0; + int max = 3; + Random rand = new Random(); + int randomNum = rand.nextInt((max - min) + 1) + min; + switch (randomNum){ + case 0: + playCard.setColor(Card.Color.BLUE); + break; + case 1: + playCard.setColor(Card.Color.RED); + break; + case 2: + playCard.setColor(Card.Color.YELLOW); + break; + case 3: + playCard.setColor(Card.Color.GREEN); + break; + } + } getGame().getGameService().playCard(playCard); break; } } - if (lastPlayedCard == game.getLastPlayedCard()) { + if (lastPlayedCard == game.getLastPlayedCard() && !playerList.getLast().getPlayerDeck().isEmpty()) { game.getGameService().drawCard(1); } } -} +} \ No newline at end of file diff --git a/src/main/resources/de/hsfulda/onses/views/app.fxml b/src/main/resources/de/hsfulda/onses/views/app.fxml index 7205ca5..42e2610 100644 --- a/src/main/resources/de/hsfulda/onses/views/app.fxml +++ b/src/main/resources/de/hsfulda/onses/views/app.fxml @@ -5,14 +5,14 @@ - + - diff --git a/src/main/resources/de/hsfulda/onses/views/gameOver.fxml b/src/main/resources/de/hsfulda/onses/views/gameOver.fxml new file mode 100644 index 0000000..9c3b6d7 --- /dev/null +++ b/src/main/resources/de/hsfulda/onses/views/gameOver.fxml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + diff --git a/src/test/java/de/hsfulda/onses/CardTest.java b/src/test/java/de/hsfulda/onses/CardTest.java index d7ea092..57d5060 100644 --- a/src/test/java/de/hsfulda/onses/CardTest.java +++ b/src/test/java/de/hsfulda/onses/CardTest.java @@ -87,4 +87,14 @@ public class CardTest { } + @Test + @DisplayName("test if card is selected") + void testIfCardIsSelected() { + Card card = new Card().setSelected(true); + + assertTrue(card.isSelected()); + card.setSelected(false); + assertFalse(card.isSelected()); + } + } \ 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 0ffff9f..d46cde5 100644 --- a/src/test/java/de/hsfulda/onses/GameServiceTest.java +++ b/src/test/java/de/hsfulda/onses/GameServiceTest.java @@ -1,4 +1,3 @@ - package de.hsfulda.onses; import com.sun.jdi.ArrayReference; @@ -120,37 +119,6 @@ public class GameServiceTest { assertEquals(expected, answer); } - @Test - @DisplayName("legalMoveColorBlack") - public void legalMoveColorBlack() { - // arrange - Card input = new Card().setColor(Card.Color.BLACK).setValue(Card.Value.CHOOSE); - boolean expected = true; - // act - GameService gameService = new GameService(); - gameService.setTest(true); - gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.YELLOW).setValue(Card.Value.THREE)); - - boolean answer = gameService.legalMove(input); - // assert - assertEquals(expected, answer); - } - - @Test - @DisplayName("legalMoveColorBlackCHOOSEDRAW") - public void legalMoveColorBlackCHOOSEDRAW() { - // arrange - Card input = new Card().setColor(Card.Color.BLACK).setValue(Card.Value.CHOOSEDRAW); - boolean expected = true; - // act - GameService gameService = new GameService(); - gameService.setTest(true); - gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.GREEN).setValue(Card.Value.FIVE)); - - boolean answer = gameService.legalMove(input); - // assert - assertEquals(expected, answer); - } @Test @DisplayName("ShuffelDeck") @@ -225,41 +193,6 @@ public class GameServiceTest { assertEquals(expected, gameService.getGame().getPlayerService().getCurrentTurn()); } - @Test - @DisplayName("ChooseTest") - public void ChooseTest() { - GameService gameService = new GameService(); - gameService.setTest(true); - boolean expected = true; - boolean answer = false; - - gameService.playCard(new Card().setValue(Card.Value.CHOOSE).setColor(Card.Color.BLACK)); - - if (gameService.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK) { - answer = true; - } - - assertEquals(expected, answer); - - } - - @Test - @DisplayName("ChooseDrawTest") - public void ChooseDrawTest() { - GameService gameService = new GameService(); - gameService.setTest(true); - boolean expected = true; - boolean answer = false; - - gameService.playCard(new Card().setValue(Card.Value.CHOOSEDRAW).setColor(Card.Color.BLACK)); - - if (gameService.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK) { - answer = true; - } - - assertEquals(expected, answer); - - } @Test @DisplayName("DrawByChoiceForceKeep") public void DrawByChoiceForceKeep() { @@ -323,26 +256,6 @@ public class GameServiceTest { assertEquals(expected, answer); } - @Test - @DisplayName("DrawByChoiceForcePlay") - public void DrawByChoiceForcePlay() { - - GameService gameService = new GameService(); - gameService.setTest(true); - Card input = new Card().setColor(Card.Color.RED).setValue(Card.Value.ONE); - - gameService.getGame().getDrawCardDeck().clear(); - gameService.getGame().getPlayerService().setCurrentTurn(true); - gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.RED).setValue(Card.Value.SEVEN)); - gameService.getGame().addCardToDrawCardDeck(input); - gameService.getGame().getPlayerService().getPlayerList().getFirst().addCardToPlayerDeck(new Card().setColor(Card.Color.BLUE).setValue(Card.Value.TWO)); - gameService.drawCard(1); - - Card answer = gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck().getLast(); - - assertNotEquals(input, answer); - } - @Test @DisplayName("CheckThatBotDeckHasSevenCardsAfterGameStart") public void CheckThatBotDeckHasSevenCardsAfterGameStart() { @@ -436,7 +349,7 @@ public class GameServiceTest { gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().add(card); gameService.getGame().getPlayerService().setCurrentTurn(false); - gameService.playCard(card); + gameService.getGame().getPlayerService().botMove(); if (gameService.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK){ answer = true; @@ -565,4 +478,4 @@ public class GameServiceTest { boolean expected = true; assertEquals(expected, answer); } -} +} \ No newline at end of file diff --git a/src/test/java/de/hsfulda/onses/GuiTest.java b/src/test/java/de/hsfulda/onses/GuiTest.java index 845c004..eec7715 100644 --- a/src/test/java/de/hsfulda/onses/GuiTest.java +++ b/src/test/java/de/hsfulda/onses/GuiTest.java @@ -10,11 +10,13 @@ import static org.testfx.matcher.control.LabeledMatchers.*; public class GuiTest extends ApplicationTest { private Stage stage; + private App app; @Override public void start(Stage stage) throws Exception { this.stage = stage; - new App().start(stage); + app = new App(); + app.start(stage); } @Test diff --git a/src/test/java/de/hsfulda/onses/PlayerTest.java b/src/test/java/de/hsfulda/onses/PlayerTest.java index 9ce04fd..7dab45d 100644 --- a/src/test/java/de/hsfulda/onses/PlayerTest.java +++ b/src/test/java/de/hsfulda/onses/PlayerTest.java @@ -114,4 +114,15 @@ public class PlayerTest { player.setCurrentCard(card); assertEquals(card, player.getCurrentCard()); } + + @Test + @DisplayName("Set Player by Card") + void testPlayerSetByCard() { + Player player = new Player(); + Card card = new Card(); + + assertNull(card.getPlayer()); + player.addCardToPlayerDeck(card); + assertEquals(player, card.getPlayer()); + } }