Commit 52fcc376 authored by Awet Ghebrekidan's avatar Awet Ghebrekidan
Browse files

oppgave 3 done

parent 4d782802
......@@ -33,11 +33,41 @@
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>no.prg2.ntnu.Deck</mainClass>
<mainClass>view.DeckGam</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>view.DeckGame</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package Model;public class Deck {
package Model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* The deck class is a collection of the 52 cards found in a standard deck.
*
* @author Awet
*/
public class Deck {
private ArrayList<Card> cards;
private char[] suit = {'S','H', 'D', 'C'};
/**
* Initializes a new Deck with 52 cards in order where face is sorted before
* suit.
*/
public Deck() {
this.cards = new ArrayList<Card>(52);
for (int face = 1; face <= 13; face++) {
this.cards.add(new Card(suit[0], face));
this.cards.add(new Card(suit[1], face));
this.cards.add(new Card(suit[2], face));
this.cards.add(new Card(suit[3], face));
}
}
/** Gets n random cards from the deck.
* @param n is the amount of random cards you want
* @return a list containing n random cards
* @throws IllegalArgumentException if n is not between 1 and 52
*/
public List<Card> dealHand(int n) {
if (n < 1 || n > 52) {
throw new IllegalArgumentException("n has to be between 1 and 52");
}
ArrayList<Card> returnList = new ArrayList<Card>(cards);
Collections.shuffle(returnList);
return returnList.stream().filter(card -> returnList.indexOf(card) < n).collect(Collectors.toList());
}
}
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;
public class DeckGame extends Application{
private static TextField txt;
@Override
public void start(Stage primaryStage) {
try {
VBox rootNode = new VBox();
this.txt = new TextField();
txt.setPromptText("Card");
ListView listView = new ListView();
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));
for (Card card : cards) {
System.out.println(card.getAsString());
}
}
});
HBox box = new HBox();
HBox buttonBox = new HBox(btn, txt);
rootNode.setPadding(new Insets(200, 100, 200, 300));
rootNode.getChildren().addAll(box, buttonBox);
Scene scene = new Scene(rootNode);
primaryStage.setScene(scene);
primaryStage.setTitle("Card Game");
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e){
System.out.println(e.getMessage());
}
}
@Override
public void stop(){
System.exit(0);
}
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");
}
}
*/
private static int getIntFromTextField(TextField textField) {
String text = textField.getText();
return Integer.parseInt(text);
}
}
package Model;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CardTest {
@org.junit.jupiter.api.BeforeEach
void setUp() {
}
Card card = new Card('S', 2);
void getAsString(){
assertEquals("S2", card.getAsString());
}
void getSuit(){
assertEquals('S', card.getSuit());
}
@Test
void getFace() {
assertNotEquals("H", card.getFace());
}
@org.junit.jupiter.api.AfterEach
void tearDown() {
}
}
\ No newline at end of file
package Model;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class DeckTest {
@BeforeEach
void setUp() {
}
Deck deck = new Deck();
@AfterEach
void tearDown() {
}
@Test
void dealHand() {
List<Card> actual = deck.dealHand(5);
List<Card> expected = actual;
assertEquals(expected, actual);
}
}
\ No newline at end of file
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