Commit a808201a authored by Awet Ghebrekidan's avatar Awet Ghebrekidan
Browse files

pushing local

parent 52fcc376
Pipeline #9860 failed with stages
in 0 seconds
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -44,8 +44,13 @@ public class Deck {
ArrayList<Card> returnList = new ArrayList<Card>(cards);
Collections.shuffle(returnList);
return returnList.stream().filter(card -> returnList.indexOf(card) < n).collect(Collectors.toList());
List<Card> list = returnList.stream().filter(card -> returnList.indexOf(card) < n).collect(Collectors.toList());
return list;
}
}
package view;
import Model.*;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.layout.*;
import javafx.scene.shape.Box;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.Scene;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Gui class for Card game
* launches to buttons to deal hand of card and show these cards, calculate the sum of the cards
* check if the hand is a flush and prints yes, and no if it is not flush
*/
public class DeckGame extends Application{
private static TextField txt;
private static Deck deck;
private ArrayList<Card> cards ;
@Override
public void start(Stage primaryStage) {
try {
VBox rootNode = new VBox();
// The main node of the scene
VBox rootNode = new VBox();
this.txt = new TextField();
txt.setPromptText("Card");
ListView listView = new ListView();
// Box to show the output of deal hand method
HBox output = new HBox() ;
output.setPrefWidth(200);
output.setPrefHeight(200);
output.setSpacing(20);
output.setAlignment(Pos.CENTER);
output.setStyle("-fx-background-color:WHITE");
// Initialize Deck class
this.deck = new Deck();
// Box to show the sum of cards dealt
HBox sum = new HBox();
sum.setPrefWidth(20);
sum.setPrefHeight(20);
sum.setStyle("-fx-background-color:WHITE");
// sum text to be added to a grid
Text sumText = new Text("Sum");
// Hearts to be put on the side of the box that will show the number of hearts delt
Text heartsText = new Text("Hearts");
// The box that eill show the number of hearts delt
HBox hearts = new HBox();
hearts.setPrefHeight(20);
hearts.setPrefWidth(20);
hearts.setStyle("-fx-background-color:WHITE");
// Flush text
Text flshText = new Text("Flush yes/No");
// Box to show yes/no according to wether the deal is a flush or not
HBox flsh = new HBox();
flsh.setPrefHeight(20);
flsh.setPrefWidth(20);
flsh.setStyle("-fx-background-color:WHITE");
// Deal hand button
Button btn = new Button("Deal Hand");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
Deck deck = new Deck();
ArrayList<Card> cards = (ArrayList<Card>) deck.dealHand(getIntFromTextField(txt));
cards = (ArrayList<Card>) deck.dealHand(getIntFromTextField(txt));
output.getChildren().clear();
for (Card card : cards) {
output.getChildren().add(new Text(card.getAsString()));
}
}
});
System.out.println(card.getAsString());
// Check hand button
Button button = new Button("Check hand");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
// Returns the sum of dealt hand
int result = cards.stream().map(card -> card.getFace())
.reduce(0, Integer::sum);
try {
String s = tostring(result);
// sum is hbox
sum.getChildren().clear();
sum.getChildren().add(new Text(s));
} catch (Exception e) {
System.out.println(e.getMessage());
}
// Filters cards that are hearts and prints them out
cards.stream()
.filter(card -> card.getSuit() == 'H')
.collect(Collectors.toList())
.forEach(card -> {
hearts.getChildren().clear();
hearts.getChildren().add(new Text(card.getAsString()));
});
// Checks wether dealt hand is a flush or not
flsh.getChildren().clear();
boolean flush;
flush = cards.stream()
.collect(Collectors.groupingBy(Card::getSuit, Collectors.counting()))
.values().stream().anyMatch(count -> count >= 5);
if(flush = true) {
flsh.getChildren().clear();
flsh.getChildren().add(new Text("Yes"));
}
flsh.getChildren().clear();
flsh.getChildren().add(new Text("No"));
}
});
// Box to hold button and textfield to take input
VBox box = new VBox(btn, txt, button);
box.setSpacing(10);
// Holds all boxes to show utton results and the text that goes with them
GridPane grid = new GridPane();
grid.setAlignment(Pos.BASELINE_LEFT);
grid.add(hearts, 0,1);
grid.add(heartsText, 1,1);
grid.add(flsh, 0, 2);
grid.add(flshText, 1, 2);
HBox box = new HBox();
HBox buttonBox = new HBox(btn, txt);
grid.add(sum, 0, 3);
grid.add(sumText, 1, 3);
rootNode.setPadding(new Insets(200, 100, 200, 300));
rootNode.getChildren().addAll(box, buttonBox);
grid.setHgap(10);
grid.setVgap(10);
// Root node
rootNode.setPadding(new Insets(100, 100, 100, 100));
rootNode.getChildren().addAll(box, output, grid);
Scene scene = new Scene(rootNode);
primaryStage.setScene(scene);
primaryStage.setTitle("Card Game");
......@@ -78,42 +186,39 @@ public class DeckGame extends Application{
}
public static void main(String[] args) {
launch(args);
}
/**
private void print(Node node) {
Deck deck = new Deck();
PrinterJob job = PrinterJob.createPrinterJob();
ArrayList<Card> cards = (ArrayList<Card>) deck.dealHand(getIntFromTextField(txt));
if(job != null) {
for (Card card : cards) {
System.out.println(card.getAsString());
boolean printed = job.printPage(node);
if (printed) {
job.endJob();
}
}
}
else{
System.out.println("Printing Failed");
}
}
*/
/**
* Method to convert input of text field from string to int
* @param textField
* @return
*/
private static int getIntFromTextField(TextField textField) {
String text = textField.getText();
return Integer.parseInt(text);
}
/**
* Converts from int to value
* @param i
* @return
*/
private static String tostring(int i){
try {
String string = String.valueOf(i);
return string;
}
catch (Exception e){
System.out.println(e.getMessage());
}
return null;
}
}
C:\Users\AwetT\Documents\prog2projects\CardGame\src\main\java\Model\Card.java
C:\Users\AwetT\Documents\prog2projects\CardGame\src\main\java\view\DeckGame.java
C:\Users\AwetT\Documents\prog2projects\CardGame\src\main\java\Model\Deck.java
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment