diff --git a/.gitignore b/.gitignore index 5ff6309b7199129c1afe4f4ec1906e640bec48c6..29d90fccaaabe703b7ed79e580ac066b9a7321a3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ target/ .idea/jarRepositories.xml .idea/compiler.xml .idea/libraries/ +target *.iws *.iml *.ipr diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..94a25f7f4cb416c083d265558da75d457237d671 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/src/main/java/edu/ntnu/stud/cardgame/Main.java b/src/main/java/edu/ntnu/stud/cardgame/Main.java index 261f75369d28f9f7399817a7d3ca5fd893f9672d..a4657916088b3a4ada0a1ecfe758af53db07150e 100644 --- a/src/main/java/edu/ntnu/stud/cardgame/Main.java +++ b/src/main/java/edu/ntnu/stud/cardgame/Main.java @@ -1,12 +1,23 @@ package edu.ntnu.stud.cardgame; import javafx.application.Application; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Main extends Application { + Button button; @Override public void start(Stage stage) throws Exception { + stage.setTitle("Card game"); + button = new Button("Knapp"); + + StackPane layout = new StackPane(); + layout.getChildren().add(button); + stage.setScene(new Scene(layout)); + stage.show(); } public static void main(String[] args) { diff --git a/src/main/java/edu/ntnu/stud/cardgame/PlayingCard.java b/src/main/java/edu/ntnu/stud/cardgame/PlayingCard.java new file mode 100644 index 0000000000000000000000000000000000000000..8233aa184d2fe724cbc21d991f62b041d5c8d3c2 --- /dev/null +++ b/src/main/java/edu/ntnu/stud/cardgame/PlayingCard.java @@ -0,0 +1,90 @@ +package edu.ntnu.stud.cardgame; + +/** + * Represents a playing card. A playing card has a number (face) between + * 1 and 13, where 1 is called an Ace, 11 = Knight, 12 = Queen and 13 = King. + * The card can also be one of 4 suits: Spade, Heart, Diamonds and Clubs. + * + * @author ntnu + * @version 2021-03-13 + */ +public class PlayingCard { + + private final char suit; // 'S'=spade, 'H'=heart, 'D'=diamonds, 'C'=clubs + private final int face; // a number between 1 and 13 + + /** + * Creates an instance of a PlayingCard with a given suit and face. + * The face value is an integer between 1 and 13, where 11 represents the jack, + * 12 represents the queen and 13 represents the king. The Ace is represented by the + * number 1. + * + * <p>If the suit or face are invalid, an {@code IllegalArgumentException} is thrown.</p> + * + * @param suit The suit of the card, as a single character. 'S' for Spades, + * 'H' for Heart, 'D' for Diamonds and 'C' for clubs + * @param face The face value of the card, an integer between 1 and 13 + * @throws IllegalArgumentException if suit or face have invalid values. + */ + public PlayingCard(char suit, int face) { + if (suit != 'H' && suit != 'D' && suit != 'C' && suit != 'S') { + throw new IllegalArgumentException("Parameter suit must be one of H, D, C or S"); + } + + if (face < 1 || face > 13) { + throw new IllegalArgumentException("Parameter face must be a number between 1 to 13"); + } + + this.suit = suit; + this.face = face; + } + + /** + * Returns the suit and face of the card as a string. + * A 4 of hearts is returned as the string "H4". + * + * @return the suit and face of the card as a string + */ + public String getAsString() { + return String.format("%s%s", suit, face); + } + + /** + * Returns the suit of the card, 'S' for Spades, 'H' for Heart, + * 'D' for Diamonds and 'C' for clubs. + * + * @return the suit of the card + */ + public char getSuit() { + return suit; + } + + /** + * Returns the face of the card (value between 1 and 13). + * + * @return the face of the card + */ + public int getFace() { + return face; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PlayingCard otherCard = (PlayingCard) o; + return getSuit() == otherCard.getSuit() && getFace() == otherCard.getFace(); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 31 * hash + getSuit(); + hash = 31 * hash + getFace(); + return hash; + } +}