Skip to content
Snippets Groups Projects
Commit e3324138 authored by Torbjørn Antonsen's avatar Torbjørn Antonsen
Browse files

implemented checkHand functions to check hand button

parent 988571d9
No related branches found
No related tags found
1 merge request!4Feat/gui
Showing with 105 additions and 8 deletions
......@@ -7,8 +7,13 @@ public class Controller {
this.view = view;
}
public void doDrawHand() {
public void doDrawHand(HandOfCards hand) {
DeckOfCards deckOfCards = new DeckOfCards();
this.view.setHandInImageView(deckOfCards.dealHand(5));
deckOfCards.dealHand(hand, 5);
this.view.setHandInImageView(hand);
}
public void doCheckHand(HandOfCards hand) {
this.view.setHandData(hand);
}
}
......@@ -17,19 +17,28 @@ public class DeckOfCards {
}
}
<<<<<<< Updated upstream
public PlayingCard[] dealHand(int n) {
=======
public void dealHand(HandOfCards handOfCards, int n) {
>>>>>>> Stashed changes
List<PlayingCard> drawDeck = deckOfCards;
PlayingCard[] hand = new PlayingCard[n];
List<PlayingCard> hand = new ArrayList<>();
Random random = new Random();
int i;
for (int j = 0; j < n; j++) {
i = random.nextInt(52-j);
hand[j] = drawDeck.get(i);
hand.add(drawDeck.get(i));
drawDeck.remove(i);
}
<<<<<<< Updated upstream
return hand;
=======
handOfCards.setHand(hand);
>>>>>>> Stashed changes
}
public List<PlayingCard> getDeckOfCards() {
......
package edu.ntnu.idatt2001;
import java.util.ArrayList;
import java.util.List;
public class HandOfCards {
private List<PlayingCard> hand;
public HandOfCards() {
this.hand = new ArrayList<>();
}
public int getSize() {
return this.hand.size();
}
public List<PlayingCard> getHand() {
return this.hand;
}
public void setHand(List<PlayingCard> hand) {
this.hand = hand;
}
public int getSumOfFaces() {
return this.hand.stream()
.map(PlayingCard::getFace)
.reduce(Integer::sum)
.orElse(-1);
}
public List<PlayingCard> getCardsOfHearts() {
return this.hand.stream()
.filter(p -> 'H' == p.getSuit())
.toList();
}
public boolean anyQueenOfSpades() {
return hand.stream()
.anyMatch(p -> 'S' == p.getSuit() && 12 == p.getFace());
}
public boolean isFlush() {
return hand.stream()
.allMatch(p -> 'S' == p.getSuit())
|| hand.stream()
.allMatch(p -> 'H' == p.getSuit())
|| hand.stream()
.allMatch(p -> 'D' == p.getSuit())
|| hand.stream()
.allMatch(p -> 'C' == p.getSuit());
}
public String cardsToString(List<PlayingCard> cards) {
StringBuilder str = new StringBuilder();
if (cards.size() != 0) {
for (PlayingCard card : cards) {
str.append(card.getAsString()).append(" ");
}
} else {
str.append("No hearts in hand");
}
return str.toString();
}
}
......@@ -24,7 +24,9 @@ public class Main extends Application {
0.0, 1.0, 0.4, 1.0);
private Controller controller;
private HandOfCards handOfCards;
private ArrayList<ImageView> imageViews;
private ArrayList<TextField> textFields;
public static void main(String[] args) {
......@@ -34,7 +36,9 @@ public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
this.imageViews = new ArrayList<>();
this.textFields = new ArrayList<>();
this.controller = new Controller(this);
this.handOfCards = new HandOfCards();
Text handText = new Text("Hand");
DeckOfCards deckOfCards = new DeckOfCards();
......@@ -81,11 +85,18 @@ public class Main extends Application {
dealHand.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
controller.doDrawHand();
controller.doDrawHand(handOfCards);
}
});
Button checkHand = new Button("Check hand");
checkHand.setPrefSize(100,40);
checkHand.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
controller.doCheckHand(handOfCards);
}
});
VBox vBox = new VBox(dealHand, checkHand);
vBox.setSpacing(20);
vBox.setAlignment(Pos.CENTER);
......@@ -99,7 +110,6 @@ public class Main extends Application {
gridPane.setVgap(5);
ArrayList<Text> texts = new ArrayList<>();
ArrayList<TextField> textFields = new ArrayList<>();
ArrayList<HBox> hBoxes = new ArrayList<>();
texts.add(new Text("Sum of the faces: "));
......@@ -126,15 +136,22 @@ public class Main extends Application {
gridPane.setAlignment(Pos.CENTER);
return gridPane;
}
public void setHandInImageView(PlayingCard[] hand) {
public void setHandInImageView(HandOfCards hand) {
int i = 0;
Image image;
for (PlayingCard playingCard : hand) {
for (PlayingCard playingCard : hand.getHand()) {
image = new Image(playingCard.getImagePath());
this.imageViews.get(i).setImage(image);
i++;
}
}
public void setHandData(HandOfCards hand) {
this.textFields.get(0).setText(Integer.toString(hand.getSumOfFaces()));
this.textFields.get(1).setText(hand.cardsToString(hand.getCardsOfHearts()));
this.textFields.get(2).setText(Boolean.toString(hand.isFlush()));
this.textFields.get(3).setText(Boolean.toString(hand.anyQueenOfSpades()));
}
public GridPane createHandImageViews(int n) {
GridPane gridPane = new GridPane();
for (int i = 0; i < n; i++) {
......
No preview for this file type
No preview for this file type
File added
No preview for this file type
File added
No preview for this file type
edu\ntnu\idatt2001\PlayingCard.class
edu\ntnu\idatt2001\Main$1.class
edu\ntnu\idatt2001\HandOfCards.class
edu\ntnu\idatt2001\Main$2.class
edu\ntnu\idatt2001\Main.class
edu\ntnu\idatt2001\Controller.class
edu\ntnu\idatt2001\DeckOfCards.class
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment