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

Added function to deal hand button

parent bae079e8
Branches
No related tags found
1 merge request!4Feat/gui
Showing
with 76 additions and 30 deletions
package edu.ntnu.idatt2001;
public class Controller {
private final Main view;
public Controller(Main view) {
this.view = view;
}
public void doDrawHand() {
DeckOfCards deckOfCards = new DeckOfCards();
this.view.setHandInImageView(deckOfCards.dealHand(5));
}
}
......@@ -18,6 +18,7 @@ public class DeckOfCards {
}
public PlayingCard[] dealHand(int n) {
List<PlayingCard> drawDeck = deckOfCards;
PlayingCard[] hand = new PlayingCard[n];
Random random = new Random();
......@@ -25,8 +26,8 @@ public class DeckOfCards {
for (int j = 0; j < n; j++) {
i = random.nextInt(52-j);
hand[j] = deckOfCards.get(i);
deckOfCards.remove(i);
hand[j] = drawDeck.get(i);
drawDeck.remove(i);
}
return hand;
}
......
package edu.ntnu.idatt2001;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
......@@ -23,6 +23,9 @@ public class Main extends Application {
private final Color DARKGRAY = Color.rgb(54, 69, 79).deriveColor(
0.0, 1.0, 0.4, 1.0);
private Controller controller;
private ArrayList<ImageView> imageViews;
public static void main(String[] args) {
launch(args);
......@@ -30,7 +33,10 @@ public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
this.imageViews = new ArrayList<>();
this.controller = new Controller(this);
Text handText = new Text("Hand");
DeckOfCards deckOfCards = new DeckOfCards();
GridPane root = new GridPane();
root.setPrefSize(1280, 800);
......@@ -43,9 +49,11 @@ public class Main extends Application {
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);
handTitle.setHeader(handText);
hand.setTop(handTitle);
hand.setCenter(handImages);
root.add(hand, 0,0);
......@@ -68,7 +76,14 @@ public class Main extends Application {
public VBox createButtons() {
Button dealHand = new Button("Deal hand");
DeckOfCards deckOfCards = new DeckOfCards();
dealHand.setPrefSize(100,40);
dealHand.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
controller.doDrawHand();
}
});
Button checkHand = new Button("Check hand");
checkHand.setPrefSize(100,40);
VBox vBox = new VBox(dealHand, checkHand);
......@@ -84,49 +99,57 @@ 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: "));
texts.add(new Text("Cards of hearts: "));
texts.add(new Text("Flush: "));
texts.add(new Text("Queen of spades: "));
for (Text text : texts) {
text.setFill(Color.WHITE);
textFields.add(new TextField());
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);
return gridPane;
}
public void setHandInImageView(PlayingCard[] hand) {
int i = 0;
Image image;
for (PlayingCard playingCard : hand) {
image = new Image(playingCard.getImagePath());
this.imageViews.get(i).setImage(image);
i++;
}
}
public GridPane createHandImageViews(int n) {
GridPane gridPane = new GridPane();
for (int i = 0; i < n; i++) {
String path = "cardImages/0" + (i + 1) + "c.png";
Image image = new Image(path);
ImageView imageView = new ImageView(image);
ImageView imageView = new ImageView();
imageView.setFitHeight(100);
imageView.setPreserveRatio(true);
gridPane.add(imageView, i, 0);
this.imageViews.add(imageView);
gridPane.add(imageViews.get(i), i, 0);
}
gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(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))));
return gridPane;
}
......
......@@ -24,7 +24,12 @@ public class PlayingCard {
public PlayingCard(char suit, int face) {
this.suit = suit;
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
No preview for this file type
No preview for this file type
edu\ntnu\idatt2001\PlayingCard.class
edu\ntnu\idatt2001\Main$1.class
edu\ntnu\idatt2001\Main.class
edu\ntnu\idatt2001\Controller.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\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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment