Commit d7094c63 authored by Eirin Svinsås's avatar Eirin Svinsås
Browse files

final touches

parent a7221648
......@@ -26,6 +26,18 @@
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -22,7 +22,7 @@ public class App extends Application {
@Override
public void start(Stage stage) throws IOException {
GridPane root = (GridPane) FXMLLoader.load((getClass().getResource("primary.fxml")));
BorderPane root = FXMLLoader.load((getClass().getResource("primary.fxml")));
stage.setScene( new Scene(root, 640, 470));
stage.setTitle("card-game");
stage.show();
......
......@@ -15,17 +15,17 @@ public class DeckOfCards {
}
}
public OneHand dealHand(int n) throws IllegalArgumentException{
if(n>0){
if(n>0 && n<53){
ArrayList<PlayingCard> clone = (ArrayList<PlayingCard>) deckOfCards.clone();
OneHand hand = new OneHand();
for(int i = 0; i<=n; i++){
for(int i = 0; i<n; i++){
PlayingCard randomPlayingCard = clone.get(random.nextInt(52));
hand.addPlayingCard(randomPlayingCard);
clone.remove(randomPlayingCard);
}
return hand;
} else{
throw new IllegalArgumentException("You must deal at least one card.");
throw new IllegalArgumentException("You must deal at least one card, and not more than 52.");
}
}
}
......@@ -5,14 +5,23 @@ import java.util.List;
import java.util.stream.Collectors;
public class OneHand {
private ArrayList<PlayingCard> oneHand;
private ArrayList<PlayingCard> oneHand = new ArrayList<>();
public OneHand(){
oneHand = new ArrayList<>();
public OneHand(){ }
@Override
public String toString() {
String oneHandText = "";
for(PlayingCard p : oneHand) {
oneHandText += p.getAsString() + " ";
}
return oneHandText;
}
public void addPlayingCard(PlayingCard playingcard){
oneHand.add(playingcard);
if(!oneHand.contains(playingcard)) {
oneHand.add(playingcard);
}
}
public ArrayList<PlayingCard> getOneHand(){
......@@ -39,7 +48,11 @@ public class OneHand {
return oneHand.stream().anyMatch(p -> p.getAsString().equals("S12"));
}
public boolean flush(){
return oneHand.stream().map(PlayingCard::getSuit).distinct().count() <=1;
public boolean flush() throws IllegalArgumentException{
if(oneHand.size()>4) {
return oneHand.stream().map(PlayingCard::getSuit).distinct().count() <= 1;
} else {
throw new IllegalArgumentException("The hand must contain at least 5 cards.");
}
}
}
package no.ntnu.eirinsvi;
import java.io.File;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
......@@ -9,11 +11,6 @@ public class PrimaryController{
DeckOfCards deck = new DeckOfCards();
OneHand hand = deck.dealHand(5);
@FXML
private Button dealHandButton;
@FXML
private Button checkHandButton;
@FXML
private Label oneHand;
......@@ -30,6 +27,7 @@ public class PrimaryController{
@FXML
private Label hasQueenOfSpades;
@FXML
private void clickedDealHandButton(){
oneHand.setText(String.valueOf(hand));
......
......@@ -6,18 +6,23 @@
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.eirinsvi.PrimaryController">
<right>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="73.0" prefWidth="121.0" spacing="20.0" BorderPane.alignment="CENTER">
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="107.0" prefWidth="121.0" spacing="20.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="dealHandButton" minWidth="-Infinity" mnemonicParsing="false" onAction="#clickedDealHandButton" text="Deal hand" />
<Button fx:id="checkHandButton" mnemonicParsing="false" onAction="#clickedCheckHandButton" text="Check hand" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<padding>
<Insets left="10.0" />
</padding>
</VBox>
</right>
<bottom>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="135.0" prefWidth="463.0" spacing="5.0" BorderPane.alignment="BOTTOM_LEFT">
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="135.0" prefWidth="600.0" spacing="5.0" BorderPane.alignment="BOTTOM_LEFT">
<children>
<Label fx:id="sumOfFaces">
<graphic>
......@@ -41,14 +46,14 @@
</Label>
</children>
<padding>
<Insets left="10.0" />
<Insets left="10.0" top="20.0" />
</padding>
</VBox>
</bottom>
<center>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: darkgreen;" BorderPane.alignment="CENTER">
<children>
<Label fx:id="oneHand" alignment="CENTER" contentDisplay="CENTER" />
<Label fx:id="oneHand" alignment="CENTER" contentDisplay="CENTER" style="-fx-background-color: darkgreen;" textFill="WHITE" />
</children>
</VBox>
</center>
......
package PACKAGE_NAME;public class DeckOfCardsTest {
}
package PACKAGE_NAME;public class OneHandTest {
}
package no.ntnu.eirinsvi;
import no.ntnu.eirinsvi.DeckOfCards;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class DeckOfCardsTest {
DeckOfCards deckOfCards = new DeckOfCards();
@Nested
class dealHandMethodWorksWithLegalAndIllegalValues {
@Test
public void dealHandBetweenOneAndFiftytwoCards() {
assertEquals(3, deckOfCards.dealHand(3).getOneHand().size());
}
@Test
public void dealHandWithNegativAmountOfCards() {
assertThrows(IllegalArgumentException.class, () -> deckOfCards.dealHand(-1));
}
}
}
package no.ntnu.eirinsvi;
import no.ntnu.eirinsvi.OneHand;
import no.ntnu.eirinsvi.PlayingCard;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class OneHandTest {
OneHand oneHand = new OneHand();
@Nested
class OneHandFunctionalities {
@Test
public void sumOfFaces() {
oneHand.getOneHand().add(new PlayingCard('S', 13));
assertEquals(13, oneHand.sumOfHand());
}
@Test
public void getHeartsPrintStringWithCorrectCards() {
oneHand.getOneHand().add(new PlayingCard('S', 13));
oneHand.getOneHand().add(new PlayingCard('H', 7));
oneHand.getOneHand().add(new PlayingCard('H', 11));
assertEquals("H7 H11", oneHand.getHearts());
}
@Test
public void oneHandContainQueenOfSpade(){
oneHand.getOneHand().add(new PlayingCard('S', 12));
assertTrue(oneHand.searchForSpadeQueen());
}
@Test
public void oneHandDontContainQueenOfSpade(){
oneHand.getOneHand().add(new PlayingCard('S', 13));
assertFalse(oneHand.searchForSpadeQueen());
}
@Test
public void oneHandHasFlush(){
oneHand.getOneHand().add(new PlayingCard('S', 1));
oneHand.getOneHand().add(new PlayingCard('S', 2));
oneHand.getOneHand().add(new PlayingCard('S', 3));
oneHand.getOneHand().add(new PlayingCard('S', 4));
oneHand.getOneHand().add(new PlayingCard('S', 5));
assertTrue(oneHand.flush());
}
@Test
public void oneHandDoesntHaveFlush(){
oneHand.getOneHand().add(new PlayingCard('S', 1));
oneHand.getOneHand().add(new PlayingCard('S', 2));
oneHand.getOneHand().add(new PlayingCard('S', 3));
oneHand.getOneHand().add(new PlayingCard('S', 4));
oneHand.getOneHand().add(new PlayingCard('C', 5));
assertFalse(oneHand.flush());
}
@Test
public void oneHandHasTooFewCardsForFlush(){
oneHand.getOneHand().add(new PlayingCard('S', 1));
assertThrows(IllegalArgumentException.class, () -> oneHand.flush());
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment