diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..245af407185d22779be42e9047c1f3693f187a00 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +.idea/workspace.xml +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..9ca3b0b01bba40b4d745864f67d491479c1de581 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,17 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml + +checkstyle-idea.xml + +/compiler.xml +/encodings.xml +/jarRepositories.xml +/misc.xml +/vcs.xml +./shelf \ No newline at end of file diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 0000000000000000000000000000000000000000..3bab646322fd4a4653a993ecbc5d3fd14935eb71 --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="CheckStyle-IDEA" serialisationVersion="2"> + <checkstyleVersion>10.13.0</checkstyleVersion> + <scanScope>JavaOnly</scanScope> + <option name="thirdPartyClasspath" /> + <option name="activeLocationIds" /> + <option name="locations"> + <list> + <ConfigurationLocation id="bundled-sun-checks" type="BUNDLED" scope="All" description="Sun Checks">(bundled)</ConfigurationLocation> + <ConfigurationLocation id="bundled-google-checks" type="BUNDLED" scope="All" description="Google Checks">(bundled)</ConfigurationLocation> + </list> + </option> + </component> +</project> \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000000000000000000000000000000000000..aa00ffab7828f4818589659c804ec2cfd99baed3 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Encoding"> + <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> + <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..fdc35ea8167a5fae3f335ef1d46269eaebfa6b03 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ExternalStorageConfigurationManager" enabled="true" /> + <component name="MavenProjectsManager"> + <option name="originalFiles"> + <list> + <option value="$PROJECT_DIR$/pom.xml" /> + </list> + </option> + </component> + <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK"> + <output url="file://$PROJECT_DIR$/out" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec b/.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/.idea/sonarlint/issuestore/d/5/d54772303db45029ffa547ae0cc6732dbac23e86 b/.idea/sonarlint/issuestore/d/5/d54772303db45029ffa547ae0cc6732dbac23e86 new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb new file mode 100644 index 0000000000000000000000000000000000000000..166bc851fc1dad5e18e4721e07592d555795977c --- /dev/null +++ b/.idea/sonarlint/issuestore/index.pb @@ -0,0 +1,30 @@ + +Z +*src/main/java/edu/ntnu/idatt2003/Main.java,d/5/d54772303db45029ffa547ae0cc6732dbac23e86 +7 +pom.xml,4/4/442292b8a7efeabbe4cc176709b833b1792140ec +: + +.gitignore,a/5/a5cc2925ca8258af241be7e5b0381edf30266302 +a +1src/main/java/edu/ntnu/idatt2003/DeckOfCards.java,9/a/9a7f7241d6cb1223232b94ce728408a537908733 +S +#.idea/sonarlint/issuestore/index.pb,9/f/9fe84ebb15faf917b7def6236dba604453cc61e0 +� +Q.idea/sonarlint/securityhotspotstore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec,2/c/2cabb939da04d3de3a200bf2b575690e1a16316d +w +G.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec,8/6/863e0292fd7e9b3e74dafd6cc01f73ba76346482 +� +Q.idea/sonarlint/securityhotspotstore/d/5/d54772303db45029ffa547ae0cc6732dbac23e86,3/5/35af4e01311927943f75d38eaa44707aaadb08f6 +] +-.idea/sonarlint/securityhotspotstore/index.pb,8/8/88a9255124c95bdc913197c120a8d560edc59c8e +Z +*src/main/java/edu/ntnu/idatt2003/Hand.java,c/5/c5f6d93eae6b1eaef762a247993789b87818888f +a +1src/main/java/edu/ntnu/idatt2003/CardDeckGUI.java,e/0/e01492a75939a913d48c4e47c802fa6ed084f814 +a +1src/main/java/edu/ntnu/idatt2003/PlayingCard.java,0/9/090ec46a502392ab528efbf8c1049720f16c7813 +^ +.src/test/java/edu/ntnu/idatt2003/HandTest.java,f/0/f06fd316e1ba465eb8b2a738856478bdfd77abda +e +5src/test/java/edu/ntnu/idatt2003/DeckOfCardsTest.java,b/a/ba58807c8e5623ee01656d3bcfb79abd925c0617 \ No newline at end of file diff --git a/.idea/sonarlint/securityhotspotstore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec b/.idea/sonarlint/securityhotspotstore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/.idea/sonarlint/securityhotspotstore/d/5/d54772303db45029ffa547ae0cc6732dbac23e86 b/.idea/sonarlint/securityhotspotstore/d/5/d54772303db45029ffa547ae0cc6732dbac23e86 new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/.idea/sonarlint/securityhotspotstore/index.pb b/.idea/sonarlint/securityhotspotstore/index.pb new file mode 100644 index 0000000000000000000000000000000000000000..166bc851fc1dad5e18e4721e07592d555795977c --- /dev/null +++ b/.idea/sonarlint/securityhotspotstore/index.pb @@ -0,0 +1,30 @@ + +Z +*src/main/java/edu/ntnu/idatt2003/Main.java,d/5/d54772303db45029ffa547ae0cc6732dbac23e86 +7 +pom.xml,4/4/442292b8a7efeabbe4cc176709b833b1792140ec +: + +.gitignore,a/5/a5cc2925ca8258af241be7e5b0381edf30266302 +a +1src/main/java/edu/ntnu/idatt2003/DeckOfCards.java,9/a/9a7f7241d6cb1223232b94ce728408a537908733 +S +#.idea/sonarlint/issuestore/index.pb,9/f/9fe84ebb15faf917b7def6236dba604453cc61e0 +� +Q.idea/sonarlint/securityhotspotstore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec,2/c/2cabb939da04d3de3a200bf2b575690e1a16316d +w +G.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec,8/6/863e0292fd7e9b3e74dafd6cc01f73ba76346482 +� +Q.idea/sonarlint/securityhotspotstore/d/5/d54772303db45029ffa547ae0cc6732dbac23e86,3/5/35af4e01311927943f75d38eaa44707aaadb08f6 +] +-.idea/sonarlint/securityhotspotstore/index.pb,8/8/88a9255124c95bdc913197c120a8d560edc59c8e +Z +*src/main/java/edu/ntnu/idatt2003/Hand.java,c/5/c5f6d93eae6b1eaef762a247993789b87818888f +a +1src/main/java/edu/ntnu/idatt2003/CardDeckGUI.java,e/0/e01492a75939a913d48c4e47c802fa6ed084f814 +a +1src/main/java/edu/ntnu/idatt2003/PlayingCard.java,0/9/090ec46a502392ab528efbf8c1049720f16c7813 +^ +.src/test/java/edu/ntnu/idatt2003/HandTest.java,f/0/f06fd316e1ba465eb8b2a738856478bdfd77abda +e +5src/test/java/edu/ntnu/idatt2003/DeckOfCardsTest.java,b/a/ba58807c8e5623ee01656d3bcfb79abd925c0617 \ No newline at end of file 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/pom.xml b/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..02274e42490e913d25b4b63f52500f5eb8c7450f --- /dev/null +++ b/pom.xml @@ -0,0 +1,57 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>edu.ntnu.idatt2003</groupId> + <artifactId>oving4</artifactId> + <version>1.0-SNAPSHOT</version> + + <properties> + <maven.compiler.source>21</maven.compiler.source> + <maven.compiler.target>21</maven.compiler.target> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>org.openjfx</groupId> + <artifactId>javafx-controls</artifactId> + <version>21.0.1</version> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <version>5.10.1</version> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.12.1</version> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>3.2.3</version> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>3.4.1</version> + </plugin> + <plugin> + <groupId>org.openjfx</groupId> + <artifactId>javafx-maven-plugin</artifactId> + <version>0.0.8</version> + <configuration> + <mainClass>edu.ntnu.idatt2003.Main</mainClass> + </configuration> + </plugin> + </plugins> + </build> + +</project> \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/CardDeckGUI.java b/src/main/java/edu/ntnu/idatt2003/CardDeckGUI.java new file mode 100644 index 0000000000000000000000000000000000000000..468e65a0f741b29ac6f093390f6fe6a38eb087bd --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/CardDeckGUI.java @@ -0,0 +1,126 @@ +package edu.ntnu.idatt2003; + +import javafx.application.Application; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; +import javafx.stage.Stage; +import java.util.ArrayList; + +/** + * The CardDeckGUI class provides a graphical user interface for interacting with a deck of playing cards. + * It allows users to deal hands of cards, check the properties of the hand, and manipulate the deck. + */ +public class CardDeckGUI extends Application { + + /** The deck of cards used in the GUI. */ + private DeckOfCards deckOfCards = new DeckOfCards(); + + /** The hand of cards dealt in the GUI. */ + Hand handOfcards = new Hand(DeckOfCards.dealHand(5)); + + /** Button to deal a hand of cards. */ + Button dealHandButton = new Button("Deal Hand"); + + /** Button to check the properties of the hand. */ + Button checkHandButton = new Button("Check Hand"); + + /** Button to fill up the deck with a standard set of cards. */ + Button fillUpDeckButton = new Button("Fill up deck"); + + /** Text displaying the sum of face values of the cards in the hand. */ + Text sumofCards = new Text(); + + /** Text displaying whether the hand has a flush. */ + Text flush = new Text(); + + /** Text displaying the hearts cards in the hand. */ + Text hearts = new Text(); + + /** Text displaying whether the hand contains the Queen of Spades. */ + Text queenOfSpades = new Text(); + + /** Container for displaying cards in the hand. */ + HBox cardBox = new HBox(); + + /** + * Starts the JavaFX application by initializing the GUI components. + * @param primaryStage The primary stage for the application. + */ + @Override + public void start(Stage primaryStage) { + + Label label = new Label(); + + VBox checkHand = new VBox(); + + dealHandButton.setOnAction(e -> { + cardBox.getChildren().clear(); + handOfcards.playersHand(5); + ArrayList<PlayingCard> hand = handOfcards.getHandCards(); + HBox cards = new HBox(); + for (PlayingCard card : hand) { + cards.getChildren().add(new Label(card.getAsString())); + } + label.setText("Hand dealt: " ); + cardBox.getChildren().addAll(label, cards); + }); + + checkHandButton.setOnAction(e -> checkHand.getChildren().add(getResult())); + + fillUpDeckButton.setOnAction(e -> deckOfCards.fillUpTheDeck()); + + HBox buttons = new HBox(50); + buttons.setSpacing(20); + buttons.getChildren().addAll(dealHandButton, checkHandButton, fillUpDeckButton); + + VBox root = new VBox(10); + root.setPadding((new Insets(10))); + root.getChildren().addAll(buttons, cardBox, checkHand); + + Scene scene = new Scene(root, 1000,1200); + + primaryStage.setTitle("CardGame"); + primaryStage.setScene(scene); + primaryStage.show(); + } + + /** + * Constructs a VBox containing the results of checking the properties of the hand. + * @return A VBox containing the results. + */ + private VBox getResult() { + VBox result = new VBox(); + + sumofCards.setText("Sum of Cards: " + handOfcards.getSumOfHand()); + + flush.setText("Flush: " + handOfcards.hasFlush()); + + if (handOfcards.checkHandForHearts().isEmpty()) { + hearts.setText("No hearts"); + } else { + hearts.setText("Check Hearts: " + handOfcards.checkHandForHearts()); + } + + queenOfSpades.setText("Check Queen of Spades: " + handOfcards.checkHandForQueenOfSpades()); + + result.getChildren().addAll(sumofCards,flush,hearts,queenOfSpades); + + result.setAlignment(Pos.CENTER); + + return result; + } + + /** + * Launches the JavaFX application. + * @param args The command-line arguments. + */ + public static void main(String[] args) { + launch(args); + } +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/DeckOfCards.java b/src/main/java/edu/ntnu/idatt2003/DeckOfCards.java new file mode 100644 index 0000000000000000000000000000000000000000..49c6f428170f942bd7ff2845968dacbc117c6f71 --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/DeckOfCards.java @@ -0,0 +1,66 @@ +package edu.ntnu.idatt2003; + +import java.util.ArrayList; +import java.util.Random; + + +/** + * The DeckOfCards class represents a standard deck of playing cards. + * It provides methods to initialize the deck, retrieve the deck, and deal hands of cards. + */ +public class DeckOfCards { + + /** The list of playing cards in the deck. */ + private static ArrayList<PlayingCard> deck; + + /** + * Constructs a DeckOfCards object and initializes the deck with a standard set of playing cards. + */ + public DeckOfCards() { + deck = new ArrayList<>(); + fillUpTheDeck(); + } + + /** + * Fills up the deck with a standard set of playing cards. + * If the deck is not empty, it clears the existing cards before refilling. + */ + public void fillUpTheDeck() { + if (!deck.isEmpty()) { + deck.clear(); + } + for (int i = 1; i < 14; i++){ + deck.add(new PlayingCard('S', i)); + deck.add(new PlayingCard('H', i)); + deck.add(new PlayingCard('D', i)); + deck.add(new PlayingCard('C', i)); + } + } + + public ArrayList<PlayingCard> getDeck() { + return deck; + } + + + /** + * Deals a hand of cards from the deck. + * @param n The number of cards to deal for the hand. + * @return An ArrayList containing the cards dealt for the hand. + * @throws IllegalArgumentException if the specified number of cards is invalid (less than 1 or greater than 52). + */ + public static ArrayList<PlayingCard> dealHand(int n) throws IllegalArgumentException { + if (n < 1 || n > 52) { + throw new IllegalArgumentException("Invalid number of cards requested"); + } + + ArrayList<PlayingCard> hand = new ArrayList<>(); + Random random = new Random(); + + for (int i = 0; i < n; i++) { + int randomIndex = random.nextInt(deck.size()); + hand.add(deck.get(randomIndex)); + deck.remove(randomIndex); + } + return hand; + } +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/Hand.java b/src/main/java/edu/ntnu/idatt2003/Hand.java new file mode 100644 index 0000000000000000000000000000000000000000..93ae3d0d89a48fc0e76dc17ad9a18ba948ae4c6f --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/Hand.java @@ -0,0 +1,84 @@ +package edu.ntnu.idatt2003; + +import java.util.ArrayList; +import java.util.stream.Collectors; + +/** + * The Hand class represents a player's hand in a card game. + * It contains methods to manage and analyze the hand of playing cards. + */ +public class Hand { + + /** The list of playing cards in the hand. */ + private ArrayList<PlayingCard> hand; + + /** + * Constructs a Hand object with the given list of playing cards. + * @param dealHand The list of playing cards to initialize the hand with. + */ + public Hand(ArrayList<PlayingCard> dealHand) { + this.hand = dealHand; + } + + /** + * Deals a hand of cards of the specified size. + * @param sizeHandCards The number of cards to deal for the hand. + */ + public void playersHand(int sizeHandCards) { + hand = DeckOfCards.dealHand(sizeHandCards); + } + + /** + * Retrieves the list of playing cards in the hand. + * @return The list of playing cards in the hand. + */ + public ArrayList<PlayingCard> getHandCards() { + return hand; + } + + /** + * Calculates the sum of face values of the cards in the hand. + * @return The sum of face values of the cards in the hand. + */ + public int getSumOfHand() { + return hand.stream().mapToInt(PlayingCard::getFace).sum(); + } + + /** + * Checks if the hand has a flush: all cards have the same suit. + * @return True if the hand has a flush, otherwise false. + */ + public boolean hasFlush() { + return hand.stream() + .allMatch(card -> card.getSuit() == hand.get(0).getSuit()); + } + + /** + * Checks the hand for cards of hearts suit and returns their string representations. + * @return A list of string representations of hearts cards in the hand. + */ + public ArrayList<String> checkHandForHearts() { + return hand.stream() + .filter(card -> card.getSuit() == 'H') + .map(PlayingCard::getAsString) + .collect(Collectors.toCollection(ArrayList::new)); + } + + /** + * Checks if the hand contains the Queen of Spades. + * @return True if the hand contains the Queen of Spades, otherwise false. + */ + public boolean checkHandForQueenOfSpades() { + return hand.stream() + .anyMatch(card -> card.getAsString().equals("S12")); + } + + /** + * Returns a string representation of the hand. + * @return A string representation of the hand. + */ + @Override + public String toString() { + return "Hand{ hand = " + hand + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/Main.java b/src/main/java/edu/ntnu/idatt2003/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..35e2359f8720328db2ca0d2cef75af35a6d730ce --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/Main.java @@ -0,0 +1,14 @@ +package edu.ntnu.idatt2003; + +public class Main { + + /** + * The main method to start the GUI. + * + * @param args the args + */ + public static void main(String[] args) { + + CardDeckGUI.main(args); + } +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/PlayingCard.java b/src/main/java/edu/ntnu/idatt2003/PlayingCard.java new file mode 100644 index 0000000000000000000000000000000000000000..9e48b7b9820c239ca80b582a8275c2e5010bff8b --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/PlayingCard.java @@ -0,0 +1,90 @@ +package edu.ntnu.idatt2003; + +/** + * 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; + } +} diff --git a/src/test/java/edu/ntnu/idatt2003/DeckOfCardsTest.java b/src/test/java/edu/ntnu/idatt2003/DeckOfCardsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1e513fdb62bd9634d0730966a028b286373a1454 --- /dev/null +++ b/src/test/java/edu/ntnu/idatt2003/DeckOfCardsTest.java @@ -0,0 +1,38 @@ +package edu.ntnu.idatt2003; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.*; + +class DeckOfCardsTest { + private DeckOfCards deckOfCards; + + @BeforeEach + void setUp() { + deckOfCards = new DeckOfCards(); + } + + @Test + void fillUpTheDeck() { + deckOfCards.fillUpTheDeck(); + assertEquals(52, deckOfCards.getDeck().size()); + } + + @Test + void dealHand() { + ArrayList<PlayingCard> hand = deckOfCards.dealHand(5); + assertEquals(5, hand.size()); + assertEquals(47, deckOfCards.getDeck().size()); + } + + @Test + void dealHandInvalidNumberOfCards() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + deckOfCards.dealHand(0); + }); + assertEquals("Invalid number of cards requested", exception.getMessage()); + } +} \ No newline at end of file diff --git a/src/test/java/edu/ntnu/idatt2003/HandTest.java b/src/test/java/edu/ntnu/idatt2003/HandTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0b0d27deb909b2fd6017a90ac241cc1365bdef08 --- /dev/null +++ b/src/test/java/edu/ntnu/idatt2003/HandTest.java @@ -0,0 +1,49 @@ +package edu.ntnu.idatt2003; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.*; + +class HandTest { + + ArrayList<PlayingCard> cards; + Hand hand; + @BeforeEach + void setUp() { + cards = new ArrayList<>(); + cards.add(new PlayingCard('S',1)); + cards.add(new PlayingCard('H',5)); + cards.add(new PlayingCard('D',10)); + hand = new Hand(cards); + } + + @Test + void getHandCards() { + assertEquals(cards, hand.getHandCards()); + } + + @Test + void getSumOfHand() { + assertEquals(1+5+10, hand.getSumOfHand()); + } + + @Test + void hasFlush() { + assertFalse(hand.hasFlush()); + } + + @Test + void checkHandForHearts() { + ArrayList<String> hearts = hand.checkHandForHearts(); + assertEquals(1, hearts.size()); + assertTrue(hearts.contains("H5")); + } + + @Test + void checkHandForQueenOfSpades() { + assertFalse(hand.checkHandForQueenOfSpades()); + } +} \ No newline at end of file