Skip to content
Snippets Groups Projects
Commit 6c07da14 authored by André Wikheim Merkesdal's avatar André Wikheim Merkesdal
Browse files

Cleaned the application and refactored it

parent 66445d3d
No related branches found
No related tags found
No related merge requests found
......@@ -4,11 +4,7 @@ target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/*
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea
*.iws
*.iml
*.ipr
......
......@@ -22,11 +22,15 @@ public class CardGame extends Application {
HomeFrame homeFrame = new HomeFrame();
ButtonController btnController = new ButtonController(homeFrame);
homeFrame.getButtons().actionOnDealButton(e -> btnController.actionOnDealButton());
homeFrame.getButtons().actionOnCheckButton(e -> btnController.actionOnCheckButton());
homeFrame.getButtons().actionOnQuitButton(e -> btnController.actionOnQuitButton());
setupButtonActions(homeFrame, btnController);
primaryStage.setScene(new Scene(homeFrame, 1000, 800));
primaryStage.show();
}
private void setupButtonActions(HomeFrame homeFrame, ButtonController btnController) {
homeFrame.getButtons().actionOnDealButton(e -> btnController.actionOnDealButton());
homeFrame.getButtons().actionOnCheckButton(e -> btnController.actionOnCheckButton());
homeFrame.getButtons().actionOnQuitButton(e -> btnController.actionOnQuitButton());
}
}
......@@ -6,20 +6,22 @@ import edu.ntnu.stud.cardgame.model.PlayingCard;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.stream.Collectors;
public class DeckOfCards {
private static final char[] SUITS = {'H', 'D', 'S', 'C'};
private PlayingCard[] cards;
private Random random;
public DeckOfCards(ImageImporter imageImporter) {
this.cards = new PlayingCard[52];
this.random = new Random();
char[] suits = {'H', 'D', 'S', 'C'};
for (int i = 0; i < 4; i++) {
for (int i = 0; i < SUITS.length; i++) {
for (int j = 1; j <= 13; j++) {
String key = String.format("%s%d", suits[i], j);
cards[i * 13 + j - 1] = new PlayingCard(suits[i], j, imageImporter.getImage(key));
String key = String.format("%s%d", SUITS[i], j);
cards[i * 13 + j - 1] = new PlayingCard(SUITS[i], j, imageImporter.getImage(key));
}
}
}
......@@ -29,15 +31,10 @@ public class DeckOfCards {
throw new IllegalArgumentException("Invalid number of cards: " + n);
}
Hand hand = new Hand(new ArrayList<>());
while (hand.getCards().size() < n) {
int i = random.nextInt(this.cards.length);
if (!hand.getCards().contains(this.cards[i])) {
hand.getCards().add(this.cards[i]);
}
}
return hand.getCards();
return random.ints(0, cards.length)
.distinct()
.limit(n)
.mapToObj(i -> cards[i])
.toList();
}
}
package edu.ntnu.stud.cardgame.view.components;
import edu.ntnu.stud.cardgame.model.Hand;
import edu.ntnu.stud.cardgame.model.PlayingCard;
import javafx.geometry.Pos;
import javafx.scene.layout.TilePane;
......@@ -18,7 +19,9 @@ public class CardFrame extends TilePane {
}
public void addCards(Hand hand) {
hand.getCards().forEach(card -> this.getChildren().add(card.getImage()));
hand.getCards().stream()
.map(PlayingCard::getImage)
.forEach(this.getChildren()::add);
}
public void clear() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment