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

Merge branch 'feat/GUI' into 'main'

Feat/gui

See merge request !4
parents f04db152 e3324138
No related branches found
No related tags found
1 merge request!4Feat/gui
Showing
with 174 additions and 31 deletions
package edu.ntnu.idatt2001;
public class Controller {
private final Main view;
public Controller(Main view) {
this.view = view;
}
public void doDrawHand(HandOfCards hand) {
DeckOfCards deckOfCards = new DeckOfCards();
deckOfCards.dealHand(hand, 5);
this.view.setHandInImageView(hand);
}
public void doCheckHand(HandOfCards hand) {
this.view.setHandData(hand);
}
}
...@@ -17,18 +17,28 @@ public class DeckOfCards { ...@@ -17,18 +17,28 @@ public class DeckOfCards {
} }
} }
<<<<<<< Updated upstream
public PlayingCard[] dealHand(int n) { public PlayingCard[] dealHand(int n) {
PlayingCard[] hand = new PlayingCard[n]; =======
public void dealHand(HandOfCards handOfCards, int n) {
>>>>>>> Stashed changes
List<PlayingCard> drawDeck = deckOfCards;
List<PlayingCard> hand = new ArrayList<>();
Random random = new Random(); Random random = new Random();
int i; int i;
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
i = random.nextInt(52-j); i = random.nextInt(52-j);
hand[j] = deckOfCards.get(i); hand.add(drawDeck.get(i));
deckOfCards.remove(i); drawDeck.remove(i);
} }
<<<<<<< Updated upstream
return hand; return hand;
=======
handOfCards.setHand(hand);
>>>>>>> Stashed changes
} }
public List<PlayingCard> getDeckOfCards() { 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();
}
}
package edu.ntnu.idatt2001; package edu.ntnu.idatt2001;
import javafx.application.Application; import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
...@@ -23,6 +23,11 @@ public class Main extends Application { ...@@ -23,6 +23,11 @@ public class Main extends Application {
private final Color DARKGRAY = Color.rgb(54, 69, 79).deriveColor( private final Color DARKGRAY = Color.rgb(54, 69, 79).deriveColor(
0.0, 1.0, 0.4, 1.0); 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) { public static void main(String[] args) {
launch(args); launch(args);
...@@ -30,7 +35,12 @@ public class Main extends Application { ...@@ -30,7 +35,12 @@ public class Main extends Application {
@Override @Override
public void start(Stage primaryStage) throws Exception { 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"); Text handText = new Text("Hand");
DeckOfCards deckOfCards = new DeckOfCards();
GridPane root = new GridPane(); GridPane root = new GridPane();
root.setPrefSize(1280, 800); root.setPrefSize(1280, 800);
...@@ -43,9 +53,11 @@ public class Main extends Application { ...@@ -43,9 +53,11 @@ public class Main extends Application {
BorderPane hand = new BorderPane(handText); BorderPane hand = new BorderPane(handText);
DialogPane handTitle = new DialogPane(); StackPane handTitle = new StackPane(handText);
handTitle.setBackground(new Background(new BackgroundFill(Color.GRAY,
new CornerRadii(0), new Insets(0))));
handTitle.setPrefHeight(20);
GridPane handImages = createHandImageViews(5); GridPane handImages = createHandImageViews(5);
handTitle.setHeader(handText);
hand.setTop(handTitle); hand.setTop(handTitle);
hand.setCenter(handImages); hand.setCenter(handImages);
root.add(hand, 0,0); root.add(hand, 0,0);
...@@ -68,9 +80,23 @@ public class Main extends Application { ...@@ -68,9 +80,23 @@ public class Main extends Application {
public VBox createButtons() { public VBox createButtons() {
Button dealHand = new Button("Deal hand"); Button dealHand = new Button("Deal hand");
DeckOfCards deckOfCards = new DeckOfCards();
dealHand.setPrefSize(100,40); dealHand.setPrefSize(100,40);
dealHand.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
controller.doDrawHand(handOfCards);
}
});
Button checkHand = new Button("Check hand"); Button checkHand = new Button("Check hand");
checkHand.setPrefSize(100,40); 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 vBox = new VBox(dealHand, checkHand);
vBox.setSpacing(20); vBox.setSpacing(20);
vBox.setAlignment(Pos.CENTER); vBox.setAlignment(Pos.CENTER);
...@@ -84,49 +110,63 @@ public class Main extends Application { ...@@ -84,49 +110,63 @@ public class Main extends Application {
gridPane.setVgap(5); gridPane.setVgap(5);
ArrayList<Text> texts = new ArrayList<>(); ArrayList<Text> texts = new ArrayList<>();
ArrayList<HBox> hBoxes = new ArrayList<>();
texts.add(new Text("Sum of the faces: ")); texts.add(new Text("Sum of the faces: "));
texts.add(new Text("Cards of hearts: ")); texts.add(new Text("Cards of hearts: "));
texts.add(new Text("Flush: ")); texts.add(new Text("Flush: "));
texts.add(new Text("Queen of spades: ")); texts.add(new Text("Queen of spades: "));
for (Text text : texts) { textFields.add(new TextField());
text.setFill(Color.WHITE);
for (int i = 0; i < 4; i++) {
texts.get(i).setFill(Color.WHITE);
textFields.add(new TextField());
textFields.get(i).setEditable(false);
hBoxes.add(new HBox(texts.get(i), textFields.get(i)));
hBoxes.get(i).setAlignment(Pos.CENTER);
if (i < 2) {
gridPane.add(hBoxes.get(i), i, 0);
} else {
gridPane.add(hBoxes.get(i), (i - 2), 1);
}
} }
TextField textField1 = new TextField();
TextField textField2 = new TextField();
TextField textField3 = new TextField();
TextField textField4 = new TextField();
HBox hBox1 = new HBox(texts.get(0), textField1);
HBox hBox2 = new HBox(texts.get(1), textField2);
HBox hBox3 = new HBox(texts.get(2), textField3);
HBox hBox4 = new HBox(texts.get(3), textField4);
hBox1.setAlignment(Pos.CENTER);
gridPane.add(hBox1, 0,0);
gridPane.add(hBox2, 1,0);
gridPane.add(hBox3, 0,1);
gridPane.add(hBox4, 1,1);
gridPane.setAlignment(Pos.CENTER); gridPane.setAlignment(Pos.CENTER);
return gridPane; return gridPane;
} }
public void setHandInImageView(HandOfCards hand) {
int i = 0;
Image image;
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) { public GridPane createHandImageViews(int n) {
GridPane gridPane = new GridPane(); GridPane gridPane = new GridPane();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
String path = "cardImages/0" + (i + 1) + "c.png"; ImageView imageView = new ImageView();
Image image = new Image(path);
ImageView imageView = new ImageView(image);
imageView.setFitHeight(100); imageView.setFitHeight(100);
imageView.setPreserveRatio(true); imageView.setPreserveRatio(true);
gridPane.add(imageView, i, 0); this.imageViews.add(imageView);
gridPane.add(imageViews.get(i), i, 0);
} }
gridPane.setAlignment(Pos.CENTER); gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(5); gridPane.setHgap(5);
gridPane.setVgap(5); gridPane.setVgap(5);
gridPane.setBackground(new Background(new BackgroundFill(DARKGRAY.darker(), gridPane.setBackground(new Background(new BackgroundFill(Color.GRAY,
new CornerRadii(0), new Insets(0)))); new CornerRadii(0), new Insets(0))));
return gridPane; return gridPane;
} }
......
...@@ -24,7 +24,12 @@ public class PlayingCard { ...@@ -24,7 +24,12 @@ public class PlayingCard {
public PlayingCard(char suit, int face) { public PlayingCard(char suit, int face) {
this.suit = suit; this.suit = suit;
this.face = face; this.face = face;
this.imagePath = face + suit + ".png"; if (face < 10) {
this.imagePath = "cardImages/0" + face + suit + ".png";
} else {
this.imagePath = "cardImages/" + face + suit + ".png";
}
} }
/** /**
......
File added
No preview for this file type
File added
File added
File added
No preview for this file type
No preview for this file type
edu\ntnu\idatt2001\PlayingCard.class 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\Main.class
edu\ntnu\idatt2001\Controller.class
edu\ntnu\idatt2001\DeckOfCards.class edu\ntnu\idatt2001\DeckOfCards.class
C:\Users\Torbjrn\OneDrive - NTNU\Documents\BIDATA\Vr 2023\IDATT2001\Arbeidskrav 4\CardGame\src\main\java\edu\ntnu\idatt2001\DeckOfCards.java C:\Users\Torbjrn\OneDrive - NTNU\Documents\BIDATA\Vr 2023\IDATT2001\Arbeidskrav 4\CardGame\src\main\java\edu\ntnu\idatt2001\DeckOfCards.java
C:\Users\Torbjrn\OneDrive - NTNU\Documents\BIDATA\Vr 2023\IDATT2001\Arbeidskrav 4\CardGame\src\main\java\edu\ntnu\idatt2001\Main.java C:\Users\Torbjrn\OneDrive - NTNU\Documents\BIDATA\Vr 2023\IDATT2001\Arbeidskrav 4\CardGame\src\main\java\edu\ntnu\idatt2001\Main.java
C:\Users\Torbjrn\OneDrive - NTNU\Documents\BIDATA\Vr 2023\IDATT2001\Arbeidskrav 4\CardGame\src\main\java\edu\ntnu\idatt2001\Controller.java
C:\Users\Torbjrn\OneDrive - NTNU\Documents\BIDATA\Vr 2023\IDATT2001\Arbeidskrav 4\CardGame\src\main\java\edu\ntnu\idatt2001\PlayingCard.java C:\Users\Torbjrn\OneDrive - NTNU\Documents\BIDATA\Vr 2023\IDATT2001\Arbeidskrav 4\CardGame\src\main\java\edu\ntnu\idatt2001\PlayingCard.java
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment