Skip to content
Snippets Groups Projects
Commit a96959d0 authored by Mathilde Hertaas's avatar Mathilde Hertaas
Browse files

Test classes

parent a4639ad0
Branches master
No related tags found
No related merge requests found
......@@ -4,7 +4,7 @@
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>no.ntnu.idatx2003.oblig3</groupId>
<groupId>no.ntnu.idatx2003.oblig3.cardgame</groupId>
<artifactId>CardGame</artifactId>
<version>1.0-SNAPSHOT</version>
......@@ -46,8 +46,16 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>20</source>
<target>20</target>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>Main</mainClass>
</configuration>
</plugin>
</plugins>
......
module com.example.cardgame {
module no.ntnu.idatx2003.oblig3.cardgame {
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
......
......@@ -47,6 +47,10 @@ public class DeckOfCards {
}
return string.toString();
}
public List<PlayingCard> getDeck() {
return deck;
}
}
......
......@@ -2,6 +2,7 @@ package no.ntnu.idatx2003.oblig3.cardgame;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public class HandOfCards {
private final List<PlayingCard> hand;
......@@ -10,23 +11,15 @@ public class HandOfCards {
hand = new ArrayList<>();
}
public boolean checkIfFlush(){
boolean isFlush = false;
for (int i = 0; i < hand.size()-1; i++) {
if (getHand().get(i).getSuit() == getHand().get(i+1).getSuit()) {
if (getHand().get(i).getSuit() == getHand().get(i+2).getSuit()) {
if (getHand().get(i).getSuit() == getHand().get(i+3).getSuit()) {
if (getHand().get(i).getSuit() == getHand().get(i+4).getSuit()) {
isFlush = true;
}
}
}
}
}
return isFlush;
public boolean checkIfFlush() {
return IntStream.range(0, hand.size() - 4)
.anyMatch(i -> hand.subList(i, i + 5)
.stream()
.allMatch(card -> card.getSuit() == hand.get(i).getSuit()));
}
/*
public int checkNumberOfHeart(){
int numberOfHearts = 0;
for (int i = 0; i < hand.size(); i++) {
......@@ -37,6 +30,16 @@ public class HandOfCards {
return numberOfHearts;
}
*/
public int checkNumberOfHeart() {
return (int) hand.stream()
.filter(card -> card.getSuit() == 'H')
.count();
}
/*
public boolean checkIfContainsQueenSpades(){
boolean containsQueenOfSpades = false;
for (int i = 0; i < hand.size(); i++) {
......@@ -47,6 +50,14 @@ public class HandOfCards {
return containsQueenOfSpades;
}
*/
public boolean checkIfContainsQueenSpades() {
return hand.stream()
.anyMatch(card -> card.getFace() == 12 && card.getSuit() == 'S');
}
/*
public int checkSum(){
int sum = 0;
for (int i = 0; i < hand.size(); i++) {
......@@ -54,6 +65,14 @@ public class HandOfCards {
}
return sum;
}
*/
public int checkSum() {
return hand.stream()
.mapToInt(PlayingCard::getFace)
.sum();
}
public List<PlayingCard> getHand() {
return hand;
}
......
......@@ -91,7 +91,7 @@ public class Main extends Application {
card.setFill(Color.WHITE);
card.setStrokeWidth(1);
root.getChildren().add(card);
card.setTranslateX(-140 + 73 * i);
card.setTranslateX(-138 + 73 * i);
}
addTextCards();
}
......
package no.ntnu.idatx2003.oblig3.cardgame;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class DeckOfCardsTest {
@Test
void testDeckOfCards() {
DeckOfCards deck = new DeckOfCards();
assertEquals(52, deck.getDeck().size());
}
@Test
void testDealHand() {
DeckOfCards deck = new DeckOfCards();
HandOfCards hand = deck.dealHand(5);
assertEquals(5, hand.getHand().size());
}
@Test
void testDealHandInvalid() {
DeckOfCards deck = new DeckOfCards();
assertThrows(IllegalArgumentException.class, () -> deck.dealHand(53));
}
}
\ No newline at end of file
package no.ntnu.idatx2003.oblig3.cardgame;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class HandOfCardsTest {
@Test
void checkIfFlush() {
HandOfCards hand = new HandOfCards();
hand.getHand().add(new PlayingCard('H', 1));
hand.getHand().add(new PlayingCard('H', 2));
hand.getHand().add(new PlayingCard('H', 3));
hand.getHand().add(new PlayingCard('H', 4));
hand.getHand().add(new PlayingCard('H', 5));
assertTrue(hand.checkIfFlush());
}
@Test
void checkIfNotFlush() {
HandOfCards hand = new HandOfCards();
hand.getHand().add(new PlayingCard('S', 1));
hand.getHand().add(new PlayingCard('H', 2));
hand.getHand().add(new PlayingCard('H', 3));
hand.getHand().add(new PlayingCard('H', 4));
hand.getHand().add(new PlayingCard('H', 5));
assertFalse(hand.checkIfFlush());
}
@Test
void checkNumberOfHeart() {
HandOfCards hand = new HandOfCards();
hand.getHand().add(new PlayingCard('H', 1));
hand.getHand().add(new PlayingCard('S', 2));
hand.getHand().add(new PlayingCard('H', 3));
hand.getHand().add(new PlayingCard('C', 4));
hand.getHand().add(new PlayingCard('H', 5));
assertEquals(3, hand.checkNumberOfHeart());
}
@Test
void checkIfContainsQueenSpades() {
HandOfCards hand = new HandOfCards();
hand.getHand().add(new PlayingCard('H', 1));
hand.getHand().add(new PlayingCard('S', 2));
hand.getHand().add(new PlayingCard('H', 3));
hand.getHand().add(new PlayingCard('C', 4));
hand.getHand().add(new PlayingCard('S', 12));
assertTrue(hand.checkIfContainsQueenSpades());
}
@Test
void checkIfNotContainsQueenSpades() {
HandOfCards hand = new HandOfCards();
hand.getHand().add(new PlayingCard('H', 1));
hand.getHand().add(new PlayingCard('S', 2));
hand.getHand().add(new PlayingCard('H', 3));
hand.getHand().add(new PlayingCard('C', 4));
hand.getHand().add(new PlayingCard('S', 11));
assertFalse(hand.checkIfContainsQueenSpades());
}
@Test
void checkSum() {
HandOfCards hand = new HandOfCards();
hand.getHand().add(new PlayingCard('H', 1));
hand.getHand().add(new PlayingCard('S', 2));
hand.getHand().add(new PlayingCard('H', 3));
hand.getHand().add(new PlayingCard('C', 4));
hand.getHand().add(new PlayingCard('S', 5));
assertEquals(15, hand.checkSum());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment