Skip to content
Snippets Groups Projects
Commit ab4160d6 authored by Kjetil Karstensen Indrehus's avatar Kjetil Karstensen Indrehus
Browse files

ADD: method for getting the url for each card

parent 140d7a95
No related branches found
No related tags found
No related merge requests found
......@@ -52,4 +52,56 @@ public class PlayingCard {
public int getFace() {
return face;
}
/**
* Method that gets the png url for the card.
* @return return the sting url for the card.
*/
public String getUrlSting(){
String result = "";
if(this.face <= 10 && this.face > 1){
result += String.valueOf(this.face);
}else{
switch (this.face){
case 11:
result += "jack";
break;
case 12:
result += "queen";
break;
case 13:
result += "king";
break;
case 1:
result += "ace";
break;
}
}
result +="_of_";
switch (this.suit){
case 'S':
result+="spades.png";
break;
case 'D':
result +="diamonds.png";
break;
case 'H':
result += "hearths.png";
break;
case 'C':
result += "clubs.png";
}
return result;
}
}
\ No newline at end of file
package no.ntnu.cardgame;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import no.ntnu.cardgame.Backend.*;
public class Controller {
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private ImageView deckImage;
/* Backend fields */
private DeckOfCards deck;
private Hand hand;
/* FXML fields */
@FXML
private ImageView card1;
private TextField txtHand;
@FXML
private ImageView card2;
private Button buttonDealHand;
@FXML
private ImageView card3;
/* Constructor for the controller*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
deck = new DeckOfCards();
}
/* Methods */
@FXML
private ImageView card4;
@FXML
private ImageView card5;
private void onDealHand(ActionEvent actionEvent){
hand = new Hand(deck.dealHand(5));
String result = "";
for (PlayingCard card :hand.getHand()){
result += card.getAsString() + ", ";
}
/* Show hands*/
txtHand.setText(result);
buttonDealHand.setDisable(true);
}
@FXML
private Label txtCurrentHand;
private void onExit(ActionEvent actionEvent){
System.exit(0);
}
@FXML
public void setTxtCurrentHand(String hand){
txtCurrentHand.setText(hand);
private void onNewDeck(){
/* Reset everything */
deck = new DeckOfCards();
buttonDealHand.setDisable(false);
txtHand.setText(" ");
}
}
......@@ -42,20 +42,22 @@
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="727.0" maxWidth="1055.0" minHeight="727.0" minWidth="1055.0" prefHeight="727.0" prefWidth="1055.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.cardgame.Controller">
<children>
<MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="1055.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu mnemonicParsing="false" text="Deck">
<items>
<MenuItem mnemonicParsing="false" text="New deck..." />
<MenuItem mnemonicParsing="false" onAction="#onNewDeck" text="New deck..." />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Exit" />
<Menu mnemonicParsing="false" text="Exit">
<items>
<MenuItem mnemonicParsing="false" onAction="#onExit" text="Exit application..." />
</items></Menu>
</menus>
</MenuBar>
<Button layoutX="806.0" layoutY="451.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="101.0" text="Deal Hand" AnchorPane.bottomAnchor="251.0" AnchorPane.rightAnchor="148.0" />
<Button fx:id="buttonDealHand" layoutX="806.0" layoutY="451.0" mnemonicParsing="false" onAction="#onDealHand" prefHeight="25.0" prefWidth="101.0" text="Deal Hand" AnchorPane.bottomAnchor="251.0" AnchorPane.rightAnchor="148.0" />
<ImageView fitHeight="150.0" fitWidth="309.0" layoutX="388.0" layoutY="31.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="546.0" AnchorPane.leftAnchor="388.0" AnchorPane.rightAnchor="441.8592834472656" AnchorPane.topAnchor="31.0">
<image>
<Image url="@img/fixedImg/card-set.jpg" />
......@@ -90,7 +92,7 @@
<Text layoutX="53.0" layoutY="476.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Current hand:" wrappingWidth="80.0" />
<Text layoutX="53.0" layoutY="572.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Has flush:" wrappingWidth="80.0" />
<Text layoutX="37.0" layoutY="522.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Amount of hearts: " wrappingWidth="137.5078125" />
<TextField layoutX="133.0" layoutY="459.0" />
<TextField fx:id="txtHand" layoutX="133.0" layoutY="459.0" />
<TextField layoutX="141.0" layoutY="505.0" />
<TextField layoutX="126.0" layoutY="555.0" />
</children>
......
......@@ -27,4 +27,17 @@ class PlayingCardTest {
void getFace() {
assertEquals(3,card.getFace());
}
@Test
@DisplayName("Test get card URL")
void testGetCardURl(){
PlayingCard jackClubs = new PlayingCard('C',11);
assertEquals("jack_of_clubs.png",jackClubs.getUrlSting());
PlayingCard oneHeart = new PlayingCard('H',1);
assertEquals("ace_of_hearths.png",oneHeart.getUrlSting());
PlayingCard fourSpades = new PlayingCard('S',4);
assertEquals("4_of_spades.png",fourSpades.getUrlSting());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment