Skip to content
Snippets Groups Projects
Commit cbda6351 authored by Sander August Heggland Schrader's avatar Sander August Heggland Schrader
Browse files

Added javadoc to the classes

parent 6ce7946a
No related branches found
No related tags found
No related merge requests found
package no.ntnu.idatt2001.cardgame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* Represents a player hand. A player hand stores PlayingCards that the player got.
* The class also managed method to check the check the hand for specific combinations like a flush
*/
public class CardHand {
private ArrayList<PlayingCard> cards;
......@@ -13,30 +15,68 @@ public class CardHand {
cards = new ArrayList<>();
}
/**
* Adds a single card to the players deck.
* @param card PlayingCard to be added
*/
public void addCard(PlayingCard card) {
cards.add(card);
}
/**
* Adds all cards in an arraylist to the deck
* @param cardList Arraylist of PlayingCards
*/
public void addCards(ArrayList<PlayingCard> cardList) {
cardList.forEach(c -> cards.add(c));
}
/**
* Method to get all cards that the player got.
* @return ArraList of PlayingCards
*/
public ArrayList<PlayingCard> getCards() {
return cards;
}
/**
* Returns all cards of a specific suit.
* @param type suit
* @return Arraylist of PlayingCards
*/
public ArrayList<PlayingCard> getAllWithSuit(String type) {
return new ArrayList<PlayingCard>(cards.stream().filter(c -> Character.toString(c.getSuit()).equalsIgnoreCase(type)).collect(Collectors.toList()));
}
/**
* Gets a sum of all the cards in the deck
* <b>Type - Value</b>
* Ace - 1
* Two - 2
* ...
* Jack - 11
* Queen - 12
* King 13
* @return
*/
public int getCardSum() {
return cards.stream().map(c -> c.getFace()).reduce(0, Integer::sum);
}
/**
* Checks if the deck contains a single card
* @param type card in the format "SuitFace" e.g "D12"
* @return true or false dependent on if the deck contains the card.
*/
public boolean containsCard(String type) {
return cards.stream().anyMatch(c -> (c.getSuit()+""+c.getFace()).equals(type));
}
/**
* Checks if the deck contains a flush.
* A flush is a hand that contains at least 5 cards of the same suit.
* @return true or false dependent on if the deck got a flush
*/
public boolean flush() {
Map<String, Long> count = cards.stream().map(c -> Character.toString(c.getSuit())).collect(Collectors.groupingBy(e -> e, Collectors.counting()));
return count.values().stream().anyMatch(l -> l >= 5);
......
......@@ -5,10 +5,18 @@ import java.util.Collection;
import java.util.Random;
import java.util.stream.IntStream;
/**
* Represents a deck of cards. A deck contains 52 cards. 4 types and 13 faces.
* Types: S, H, D, C
* Faces: Ace, 1, 2, ... , 10, Jack, Queen, King
*/
public class DeckOfCards {
private final char[] suit = {'S', 'H', 'D', 'C'};
private ArrayList<PlayingCard> deck = new ArrayList<>();
/**
* Creates a deck of 52 cards
*/
public DeckOfCards () {
for(char face: suit) {
for(int i = 1; i <= 13; i++) {
......@@ -17,10 +25,23 @@ public class DeckOfCards {
}
}
/**
* Get all the cards remaining inside the deck
* @return Arraylist of PlayingCards
*/
public ArrayList<PlayingCard> getDeck() {
return deck;
}
/**
* Takes n random cards from the deck and returns them in a list. These cards are then removed from the deck.
* n needs to be between 1 and 52. Else it will throw and IllegalArgumentException.
* If n is greater than the amount of cards left in the deck you will draw the remaining cards. If there is no cards
* left in deck it will throw an ArrayIndexOutOfBoundsException
*
* @param n amount of cards that will be taken
* @return ArrayList of PlayingCards taken from the deck
*/
public ArrayList<PlayingCard> dealHand(int n) {
if(n <= 0) throw new IllegalArgumentException("Invalid input: The input needs to be between 1 and 52");
if(deck.size() == 0) throw new ArrayIndexOutOfBoundsException("No cards left in deck. All cards have been picked");
......
......@@ -6,9 +6,9 @@ import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import java.util.Map;
/**
* Class that manges card.fxml.
*/
public class CardController {
private final String suit = "CDHS";
private final int IMAGE_WIDTH = 79;
......@@ -17,11 +17,23 @@ public class CardController {
@FXML Text cardText;
@FXML ImageView cardImg;
/**
* Sets a string to the text in card.fxml
* @param text string to be added in the Text field.
*/
public void setText(String text){
cardText.setText(text);
}
/**
* Method that creates and sets a card image ImageView.
* @param face face of the card (1-13)
* @param suit suit of the card (C,D,H or S)
*/
public void setImage(int face, char suit) {
if(face < 1 || face > 13) throw new IllegalArgumentException("Face needs to be between 1 (Ace) and 13 (King)");
if(!this.suit.contains(suit + "")) throw new IllegalArgumentException("The suit can only be C, D, H or S");
Image wholeImg = new Image("./cardsSheet2.png");
cardImg.setImage(wholeImg);
Rectangle2D imagePart = new Rectangle2D((face - 1) * IMAGE_WIDTH, (this.suit.indexOf(suit)) * IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_HEIGHT);
......
......@@ -34,16 +34,27 @@ public class MainController {
DeckOfCards deck = new DeckOfCards();
CardHand hand = new CardHand();
/**
* Initialises the app and fills it with data
*/
public void initialize() {
cardsVbox.getChildren().clear();
loadCards(hand.getCards());
this.addCard(hand.getCards());
}
/**
* Method that draws 4 cards and adds it into the deck
* @param event
* @throws IOException
*/
public void buttonDrawCards(ActionEvent event) throws IOException {
hand.addCards(deck.dealHand(4));
this.initialize();
}
/**
* Method that managed checking if the deck got a flush, a spesific card (S12) and that finds all hearts and the sum. The method showing this data in the application
*/
public void buttonCheckHand() {
boolean isFlush = hand.flush();
boolean containsCard = hand.containsCard("S12");
......@@ -56,10 +67,11 @@ public class MainController {
sumOfFaces.setText(sum + "");
}
public void loadCards(ArrayList<PlayingCard> cards) {
this.addCard(cards);
}
/**
* Adds a card to the vbox.
* @param cardObject
* @throws IOException
*/
public void addCard(PlayingCard cardObject) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/card.fxml"));
Pane card = loader.load();
......@@ -70,6 +82,10 @@ public class MainController {
cardsVbox.getChildren().add( cardsVbox.getChildren().size(),card);
}
/**
* Takes a list of cards and display them to the vbox
* @param cardList list of cards
*/
public void addCard(ArrayList<PlayingCard> cardList) {
if(cardList != null) {
cardList.forEach(c -> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment