Browse Source

Merge branch 'refactoringAndMore' into 'main'

Refactoring and more

See merge request fdai7736/onses!21
main
fdai7906 11 months ago
parent
commit
6ad80bd3ff
  1. 18
      src/main/java/de/hsfulda/onses/App.java
  2. 3
      src/main/java/de/hsfulda/onses/Main.java
  3. 29
      src/main/java/de/hsfulda/onses/controllers/AppController.java
  4. 9
      src/main/java/de/hsfulda/onses/controllers/Controller.java
  5. 22
      src/main/java/de/hsfulda/onses/controllers/MainController.java
  6. 0
      src/main/java/de/hsfulda/onses/models/.gitkeep
  7. 37
      src/main/java/de/hsfulda/onses/models/Card.java
  8. 25
      src/main/java/de/hsfulda/onses/models/Game.java
  9. 23
      src/main/java/de/hsfulda/onses/models/Player.java
  10. 22
      src/main/resources/de/hsfulda/onses/views/app.fxml
  11. 14
      src/main/resources/de/hsfulda/onses/views/main.fxml
  12. 36
      src/test/java/de/hsfulda/onses/CardTest.java

18
src/main/java/de/hsfulda/onses/App.java

@ -0,0 +1,18 @@
package de.hsfulda.onses;
import de.hsfulda.onses.controllers.AppController;
import de.hsfulda.onses.models.Game;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) throws Exception {
final AppController appController = new AppController(new Game());
stage.setTitle("Onses - Uno Game");
stage.setScene(new Scene(appController.render()));
stage.show();
}
}

3
src/main/java/de/hsfulda/onses/Main.java

@ -1,10 +1,9 @@
package de.hsfulda.onses;
import de.hsfulda.onses.controllers.MainController;
import javafx.application.Application;
public class Main {
public static void main(String[] args) {
Application.launch(MainController.class);
Application.launch(App.class);
}
}

29
src/main/java/de/hsfulda/onses/controllers/AppController.java

@ -0,0 +1,29 @@
package de.hsfulda.onses.controllers;
import de.hsfulda.onses.Main;
import de.hsfulda.onses.models.Game;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import java.io.IOException;
import java.util.Objects;
public class AppController implements Controller {
private final Game game;
public AppController(Game game) {
this.game = game;
}
@Override
public Parent render() throws IOException {
final Parent parent = FXMLLoader.load(Main.class.getResource("views/app.fxml"));
Button button = (Button) parent.lookup("#startGameBtn");
button.setOnAction(e -> {
System.out.println("Pressed");
});
return parent;
}
}

9
src/main/java/de/hsfulda/onses/controllers/Controller.java

@ -0,0 +1,9 @@
package de.hsfulda.onses.controllers;
import javafx.scene.Parent;
import java.io.IOException;
public interface Controller {
Parent render() throws IOException;
}

22
src/main/java/de/hsfulda/onses/controllers/MainController.java

@ -1,22 +0,0 @@
package de.hsfulda.onses.controllers;
import de.hsfulda.onses.Main;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.Objects;
public class MainController extends Application {
@Override
public void start(Stage stage) throws IOException {
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/main.fxml")));
stage.setTitle("Onses");
stage.setScene(new Scene(parent));
stage.show();
}
}

0
src/main/java/de/hsfulda/onses/models/.gitkeep

37
src/main/java/de/hsfulda/onses/models/Card.java

@ -1,7 +1,14 @@
package de.hsfulda.onses.models;
import java.beans.PropertyChangeSupport;
public class Card {
private int id;
public final static String PROPERTY_COLOR = "color";
public final static String PROPERTY_VALUE = "value";
protected PropertyChangeSupport listeners;
private Color color;
private Value value;
@ -12,25 +19,20 @@ public class Card {
ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, DRAWTWO, SKIP, REVERSE, CHOOSE, CHOOSEDRAW;
};
public Card setId(int id) {
this.id = id;
return this;
}
public Card setColor(Color color) {
final Card.Color oldColor = this.color;
this.color = color;
this.firePropertyChange(PROPERTY_COLOR, oldColor, color);
return this;
}
public Card setValue(Value value) {
final Card.Value oldValue = this.value;
this.value = value;
this.firePropertyChange(PROPERTY_VALUE, oldValue, value);
return this;
}
public int getId() {
return this.id;
}
public Color getColor() {
return this.color;
}
@ -38,4 +40,19 @@ public class Card {
public Value getValue() {
return this.value;
}
public PropertyChangeSupport listeners() {
if(this.listeners == null) {
this.listeners = new PropertyChangeSupport(this);
}
return this.listeners;
}
public boolean firePropertyChange(String propertyName, Object oldValue, Object newValue) {
if (this.listeners != null) {
this.listeners.firePropertyChange(propertyName, oldValue, newValue);
return true;
}
return false;
}
}

25
src/main/java/de/hsfulda/onses/models/Game.java

@ -3,10 +3,16 @@ package de.hsfulda.onses.models;
import de.hsfulda.onses.services.GameService;
import de.hsfulda.onses.services.PlayerService;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
public class Game {
public final static String PROPERTY_LAST_PLAYED_CARD = "lastPlayedCard";
public final static String PROPERTY_DRAW_CARD_DECK = "drawCardDeck";
protected PropertyChangeSupport listeners;
private GameService gameService;
private PlayerService playerService;
@ -22,7 +28,9 @@ public class Game {
lastPlayedCard.setColor(color);
}
public Game setLastPlayedCard(Card lastPlayedCard) {
final Card oldLastPlayedCard = this.lastPlayedCard;
this.lastPlayedCard = lastPlayedCard;
this.firePropertyChange(PROPERTY_LAST_PLAYED_CARD, oldLastPlayedCard, lastPlayedCard);
return this;
}
@ -31,7 +39,9 @@ public class Game {
}
public void addCardToDrawCardDeck(Card card) {
final ArrayList<Card> oldCards = new ArrayList<>(this.drawCardDeck);
drawCardDeck.add(card);
this.firePropertyChange(PROPERTY_DRAW_CARD_DECK, oldCards, drawCardDeck);
}
public GameService getGameService() {
@ -56,4 +66,19 @@ public class Game {
{
this.playerService = new PlayerService().setGame(this);
}
public PropertyChangeSupport listeners() {
if(this.listeners == null) {
this.listeners = new PropertyChangeSupport(this);
}
return this.listeners;
}
public boolean firePropertyChange(String propertyName, Object oldValue, Object newValue) {
if(this.listeners != null) {
this.listeners.firePropertyChange(propertyName, oldValue, newValue);
return true;
}
return false;
}
}

23
src/main/java/de/hsfulda/onses/models/Player.java

@ -2,9 +2,13 @@ package de.hsfulda.onses.models;
import de.hsfulda.onses.services.PlayerService;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
public class Player {
public final static String PROPERTY_PLAYER_DECK = "playerDeck";
protected PropertyChangeSupport listeners;
private PlayerService playerService;
private Game game;
@ -15,11 +19,11 @@ public class Player {
}
public void addCardToPlayerDeck(Card card) {
final ArrayList<Card> oldplayerDeck = new ArrayList<>(this.playerDeck);
playerDeck.add(card);
this.firePropertyChange(PROPERTY_PLAYER_DECK, oldplayerDeck, playerDeck);
}
public PlayerService getPlayerService() {
return playerService;
}
@ -37,4 +41,19 @@ public class Player {
this.game = game;
return this;
}
public PropertyChangeSupport listeners() {
if(this.listeners == null) {
this.listeners = new PropertyChangeSupport(this);
}
return this.listeners;
}
public boolean firePropertyChange(String propertyName, Object oldValue, Object newValue) {
if (this.listeners != null) {
this.listeners.firePropertyChange(propertyName, oldValue, newValue);
return true;
}
return false;
}
}

22
src/main/resources/de/hsfulda/onses/views/app.fxml

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="585.0" prefWidth="1125.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="473.0" layoutY="121.0" text="Uno Game" AnchorPane.leftAnchor="473.0" AnchorPane.topAnchor="121.0">
<font>
<Font size="34.0" />
</font>
</Label>
<Button fx:id="startGameBtn" layoutX="495.0" layoutY="275.0" mnemonicParsing="false" text="Start Game" AnchorPane.leftAnchor="495.0" AnchorPane.topAnchor="275.0">
<font>
<Font size="19.0" />
</font>
</Button>
</children>
</AnchorPane>

14
src/main/resources/de/hsfulda/onses/views/main.fxml

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="de.hsfulda.onses.controllers.MainController"
prefHeight="400.0" prefWidth="600.0">
</AnchorPane>

36
src/test/java/de/hsfulda/onses/CardTest.java

@ -9,34 +9,6 @@ import de.hsfulda.onses.models.Card;
public class CardTest {
@Test
@DisplayName("CardGetID5")
public void CardTestGetID5() {
// arrange
int expected = 5;
int ID = 5;
// act
Card testcard = new Card().setId(ID).setColor(Card.Color.BLACK).setValue(Card.Value.FIVE);
int answer = testcard.getId();
// assert
assertEquals(expected, answer);
}
@Test
@DisplayName("CardGetID112")
public void CardTestGetID112() {
// arrange
int expected = 112;
int ID = 112;
// act
Card testcard = new Card().setId(ID).setColor(Card.Color.BLUE).setValue(Card.Value.CHOOSEDRAW);
int answer = testcard.getId();
// assert
assertEquals(expected, answer);
}
@Test
@DisplayName("CardGetColorBlue")
@ -46,7 +18,7 @@ public class CardTest {
Card.Color expected = Card.Color.BLUE;
Card.Color color = Card.Color.BLUE;
// act
Card testcard = new Card().setId(9).setColor(color).setValue(Card.Value.FIVE);
Card testcard = new Card().setColor(color).setValue(Card.Value.FIVE);
Card.Color answer = testcard.getColor();
// assert
assertEquals(expected, answer);
@ -59,7 +31,7 @@ public class CardTest {
Card.Color expected = Card.Color.BLACK;
Card.Color color = Card.Color.BLACK;
// act
Card testcard = new Card().setId(15).setColor(color).setValue(Card.Value.CHOOSE);
Card testcard = new Card().setColor(color).setValue(Card.Value.CHOOSE);
Card.Color answer = testcard.getColor();
// assert
assertEquals(expected, answer);
@ -74,7 +46,7 @@ public class CardTest {
Card.Value expected = Card.Value.THREE;
Card.Value value = Card.Value.THREE;
// act
Card testcard = new Card().setId(2).setColor(Card.Color.BLUE).setValue(value);
Card testcard = new Card().setColor(Card.Color.BLUE).setValue(value);
Card.Value answer = testcard.getValue();
// assert
assertEquals(expected, answer);
@ -87,7 +59,7 @@ public class CardTest {
Card.Value expected = Card.Value.SKIP;
Card.Value value = Card.Value.SKIP;
// act
Card testcard = new Card().setId(19).setColor(Card.Color.BLACK).setValue(value);
Card testcard = new Card().setColor(Card.Color.BLACK).setValue(value);
Card.Value answer = testcard.getValue();
// assert
assertEquals(expected, answer);

Loading…
Cancel
Save