Answered You can buy a ready-made answer or pick a professional tutor to order an original one.
CARD GAME
Introduction
In Assignment 4 you created a rudimentary war game. In this assignment we are going to use class construction to improve upon it a bit. You will create a Card class. The Card class will hold all of the information about an individual card. This means the cards value and suit.
Additionally, we will create an enumerated type to relate the suit of the card.
Step 1 – The Card Class
For this first step you might want to create a new project called Assignment5 and then copy the code from Assignment4 into it. You don’t have to do this step but it is always a good plan to be able to roll back to a previous version. Once you have done this test your code and make sure it works like assignment 4.
******CODE FOR ASSIGNMENT 4 IS PASTED AT BOTTOM OF THIS ASSIGNMENT*****
******CARD IMAGE DOWNLOAD LINK: For accessing the in-class card images needed for the assignment: Link
Create a class called Card. You will need to make sure you have imported Image, ImageView, and javafx.scene.control.*. In the field of the class you will need to create the following private variables:
- A Label to hold the image
- A reference to an Image to hold the card image and to place in the Label
- An int to hold the value of the card
- A String to hold the name of the path to the image
Create a private boolean method called loadCard that takes as an argument a String that represents the full path to the Image. This method should do the following
1. Create space for the Image reference you created in the field by calling the constructor that takes the path to the image
2. Set the graphic on the Label you created in the field. Don’t forget you will need an ImageView
3. For now simply have this method return true. In the future we will use this for potential error handling.
Create a public void method called setImage that takes as an argument a String that represents the path to the image to load. This method should do the following:
1. Set the String you created in the field that holds the path to the String passed as an argument
2. Call the loadCard method passing it the path to the Image
Create a public method called getCard that simply returns the Label that was created in the field.
At this point you should have a card that can be tested.
Step 2 – Test The Card
Inside the Assignment5.java (or whatever you called the main file) you should be able to create Cards and use them for the game. This involves the following:
Replace the code that creates the image with a new Card. We haven’t created constructors yet so you will need two lines of code to replace:
Image imgCardLeft = new Image(“file:img\\155.gif”);
You will have to create a new Card and then call the setImage method to initialize the Card. You will then call the setGraphic method on the label this time you are going to supply it an image by calling the getCard method that is a member of the Card you just created.
Please note that you will have to do this for all of the Labels you have created. Also note that this is not quite ideal but we have abstracted some of the details. We will get to the rest as we go.
Once you have replaced all of the Labels with Cards you should test your game to make sure it still works the same as it did in assignment 4
Step 3 Features
To make the card complete we need to keep track of both the Suit and that Cards value. To begin with create a java file called Suit.java. Inside this file create an enumerated type called Suit. An enumerated type is a way to relate a numeric value to a name. Like the days of the week are numbered 1 to 7 and correspond to the values Monday – Sunday. The enumerated type should simply look like the following:
public enum Suit{Diamonds, Hearts, Spades, Clubs}
Inside the Card class create a private Suit variable
to hold the suit of the Card. You might want to consider calling this variable suit.
In addition create a private int variable to hold the value of the card. You might want to consider calling this variable value.
Create a private method called getCardValue that takes as an argument a String that holds the path to the Card.
Step 4 – getCardValue
The getCardValue method’s purpose is to determine the suit of the card, and it’s value. You are going to have to extract the number of the card from the path and convert it to a numeric type. Consider King, Queen, and Jack to be worth 10 and the Ace worth 11.
Using the numeric value of the card determine the actual value the card has. I will leave it to you to design an algorithm to do this but please do not have a switch statement that contains 52 cases. You should try to work out an algorithm that mathematically determines the value. You will undoubtedly need to use decisions but try to write the code as concise as possible.
In addition to this you need to determine the the suit of the card. You should set the value and suit created in the field from this method. The suit would be something like this:
this.suit = Suit.Spades
Once you have this method finished create a public accessor method called getValue that simply returns the value of the Card. Also, create an accessor method called getSuit that will return the suit of the card. In order for this to work when a Card is created you are also going to want to call this method from the loadCard method.
At this point you should have a working Card class that will keep track of the suit and the value. It is not ideal at this point but is getting better. The last thing to do is to update the game so that it uses the value of the card instead of the name of the card.
Step 5 – Modifying The Game
Currently the game keeps score by adding one to the side that has the higher value card. This value is simply determined by the name of the card. We have just created a method that will return the Card’s value so we want to use this method to determine which side wins. You are going to have to modify the decisions inside the handle method of the mouse clicked event.
We also want to change the score. Instead of adding one to the side with the higher value we now want to add the value of the card to the score. This means if the Card value is 10 the score will go up by 10.
At this point your game should work with the Card Class. It should also have updated scoring.
ASSIGNMENT 4 CODE:
package lab4solution;
import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.event.EventType;import javafx.geometry.HPos;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.image.Image;import javafx.scene.image.ImageView;import javafx.scene.input.MouseEvent;import javafx.scene.layout.*;import javafx.stage.Stage;import java.util.Random;import javafx.scene.paint.Color;import javafx.scene.text.Font;import javafx.scene.text.FontWeight;public class Lab4Solution extends Application {private boolean rightsTurn = true;private Label lblCardLeft = new Label();private Label lblCardDeck = new Label();private Label lblCardRight = new Label();private TextField tfLeft = new TextField();private TextField tfRight = new TextField();private Button btnReset = new Button(“Reset”);Random rnd = new Random();
private int rightVal = 0;private int leftVal = 0;private int score = 0;
@Overridepublic void start(Stage primaryStage) {
btnReset.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {rightVal = 0;leftVal = 0;score = 0;tfRight.setText(“0”);tfLeft.setText(“0”);resetCardImages();rightsTurn = true;}});lblCardDeck.setOnMouseClicked(new EventHandler<MouseEvent>(){@Overridepublic void handle(MouseEvent arg0) {
int max = 152;int min = 101;int val = (rnd.nextInt((max – min) + 1) + min);String cardName = “file:img\\” + val + “.gif”;if(rightsTurn == true){rightVal = val;Image imgCardRight = new Image(cardName);lblCardRight.setGraphic(new ImageView(imgCardRight));}else{leftVal = val;Image imgCardLeft = new Image(cardName);lblCardLeft.setGraphic(new ImageView(imgCardLeft));if(rightVal > leftVal){score = Integer.parseInt(tfRight.getText());score++;tfRight.setText(“” + score);}else if(leftVal > rightVal){score = Integer.parseInt(tfLeft.getText());score++;tfLeft.setText(“” + score);}}rightsTurn = !rightsTurn;
}
});
tfLeft.setPrefWidth(50);tfRight.setPrefWidth(50);tfLeft.setDisable(true);tfRight.setDisable(true);tfLeft.setText(“0”);tfRight.setText(“0”);
this.resetCardImages();
BorderPane root = new BorderPane();GridPane cardPane = new GridPane();cardPane.setHgap(20.0);cardPane.add(lblCardLeft, 0, 0);cardPane.add(lblCardDeck, 1, 0);cardPane.add(lblCardRight, 2, 0);cardPane.setAlignment(Pos.CENTER);
GridPane topPane = new GridPane();topPane.setHgap(20.0);topPane.setVgap(10.0);Label lblScore = new Label(“Score:”);Font fntScore = Font.font(“Verdana”, FontWeight.BOLD, 14.0);lblScore.setFont(fntScore);lblScore.setTextFill(Color.RED);topPane.add(lblScore, 0, 0);topPane.add(new Label(“Left: “), 0, 1);topPane.add(tfLeft, 1, 1);
topPane.add(new Label(“Right: “), 2, 1);topPane.add(tfRight, 3, 1);
root.setTop(topPane);root.setCenter(cardPane);root.setBottom(btnReset);
Scene scene = new Scene(root, 400, 300);
primaryStage.setTitle(“Assignment 4 – Simple Game of War”);primaryStage.setScene(scene);primaryStage.show();}private void resetCardImages(){Image imgCardLeft = new Image(“file:img\\155.gif”);Image imgCardRight = new Image(“file:img\\155.gif”);Image imgCardDeck = new Image(“file:img\\155.gif”);
lblCardLeft.setGraphic(new ImageView(imgCardLeft));lblCardDeck.setGraphic(new ImageView(imgCardDeck));lblCardRight.setGraphic(new ImageView(imgCardRight));}public static void main(String[] args) {launch(args);}}
- @
- 331 orders completed
- ANSWER
-
Tutor has posted answer for $70.00. See answer's preview
************** ********** 4 *** ******* * *********** *** **** ** **** ********** ** are going ** use class construction ** improve **** it * *** *** **** ****** a Card class *** **** ***** will **** *** ** the information about ** individual **** **** means *** ***** ***** and **************** ** **** ****** an ********** type ** ****** the **** ** *** ********** * *** *** Card ClassFor **** ***** **** *** ***** want ** ****** * *** ******* called Assignment5 and then **** the code **** *********** **** ** *** ******* have to ** **** **** but it ** ****** a **** **** ** ** **** ** **** back ** * previous ******* **** *** **** **** this **** your **** *** **** **** it ***** like ********** *********** *** ASSIGNMENT * ** ****** ** BOTTOM ** **** ************************* IMAGE ******** ***** *** accessing *** ******** **** ****** ****** *** *** assignment:LinkCreate a ***** ****** **** *** **** need to make **** *** **** imported Image ImageView and javafxscenecontrol* In *** ***** of *** class *** will **** ** ****** the ********* ******* variables:A ***** ** **** the imageA ********* ** ** Image ** **** *** card ***** and ** place ** the ******* *** ** hold *** ***** ** *** ***** String to **** *** name ** *** **** ** *** *********** a ******* ******* ****** called loadCard that ***** ** ** argument a String **** represents *** full path ** *** Image **** ****** should ** *** following1 ****** ***** *** *** ***** ********* *** ******* ** the ***** ** calling the constructor **** ***** *** **** to the ****** *** *** ******* ** the ***** *** ******* in *** field ******* forget *** **** **** an ImageView3 *** *** ****** **** **** method ****** **** ** the ****** ** **** *** **** *** ********* ***** ************** * public **** ****** called setImage **** ***** ** ** ******** * ****** that represents the **** ** *** ***** to **** **** ****** ****** ** *** *********** Set *** String *** ******* ** *** ***** **** ***** the **** ** *** ****** ****** ** ** ********* **** *** loadCard ****** ******* ** *** path ** *** *********** * ****** ****** ****** ******* that simply ******* *** ***** that *** created in the ******* **** ***** you ****** **** a card **** can be ********** 2 *** Test The ********** *** Assignment5java (or whatever *** ****** the main ***** *** should ** **** to create ***** *** use **** *** the **** This ******** the ***************** *** code **** creates *** ***** with * *** **** ** ********* created ************ *** ** *** will **** *** ***** ** **** ** ************* *********** * new ********************************* **** have ** ****** * *** **** and **** **** *** setImage ****** ** ********** the **** You **** **** **** the setGraphic ****** ** the label **** time you are ***** ** ****** ** ** ***** ** ******* the getCard ****** **** is * ****** of the **** *** **** ************* note **** *** **** **** ** ** **** *** *** ** *** Labels *** **** created Also **** **** **** ** not quite ideal but ** **** ********** **** of *** ******* We **** *** ** *** rest ** we goOnce you have ******** *** ** the ****** **** Cards you should test **** **** to **** sure ** ***** ***** the same as it *** ** ********** 4Step 3 ********** **** *** **** complete we **** to **** ***** ** **** *** **** and that ***** ***** To ***** with ****** a **** **** ****** ******** ****** **** **** create an ********** type ****** **** ** ********** **** ** * *** ** relate a ******* ***** to * **** **** *** **** ** the week *** ******** * ** 7 *** ********** ** the ****** ****** *** Sunday *** ********** **** ****** simply look **** *** **************** enum Suit{Diamonds ****** ****** ************ *** **** ***** ****** a private Suit ********** **** *** **** of *** **** *** ***** **** ** ******** ******* this variable ****** ******** ****** a private *** ******** ** **** the ***** of *** card You ***** **** ** ******** ******* **** ******** valueCreate * ******* ****** ****** ************ **** ***** as an ******** * ****** **** ***** *** **** ** *** CardStep * *** getCardValueThe ************ ********** purpose is to determine *** suit ** the **** *** ****** ***** *** *** ***** ** **** ** ******* *** ****** of the **** from *** path and ******* ** ** a ******* **** ******** **** ***** and **** ** ** worth ** *** the *** ***** ******* *** ******* ***** ** the card ********* *** actual ***** the **** *** * **** ***** it ** you to ****** ** ********* to ** **** *** ****** ** not have * ****** ********* **** contains 52 ***** *** ****** *** to **** out an ********* that ************** ********** *** ***** *** **** *********** need to *** ********* *** *** to ***** *** code ** concise ** ********** addition ** **** *** **** ** ********* *** *** suit of *** **** *** ****** set *** value *** **** ******* ** the ***** from **** ****** The **** ***** ** something **** this:thissuit = ************** *** **** this ****** ******** ****** * ****** accessor method called ******** **** ****** returns *** value ** the **** **** ****** ** ******** ****** ****** ******* **** **** ****** *** **** ** *** card ** ***** *** this to **** **** * **** ** ******* *** *** **** going ** **** ** call **** ****** **** *** ******** ******** **** ***** *** ****** **** * working Card ***** **** will **** ***** ** *** suit and *** value ** ** not ***** ** **** ***** *** is ******* ****** *** last ***** ** ** is to ****** *** **** ** **** ** **** the ***** ** the **** ******* ** *** **** ** *** ******** 5 *** ********* *** ************* *** game keeps ***** ** ****** one to the **** that has *** ****** value **** This ***** is ****** ********** by *** **** of *** **** ** have **** ******* a ****** **** **** return the ******** ***** ** ** **** ** *** this ****** ** ********* which side **** *** *** going to **** ** ****** *** ********* ****** *** ****** method ** the ***** ******* ******* also want ** ****** *** ***** ******* ** ****** *** ** *** **** **** the ****** ***** we *** want to *** *** value ** the **** ** *** ***** **** ***** if *** **** ***** ** ** the ***** **** ** ** ** **** **** ***** your **** ****** work with *** **** Class It ****** **** **** ******* ******************* 4 ************ ******************* javafxapplicationApplication;import ***************************** javafxeventEventHandler;import *************************** javafxgeometryHPos;import javafxgeometryPos;import javafxsceneScene;import ************************** **************************** ******************************** ********************************* javafxscenelayout*;import *********************** javautilRandom;import javafxscenepaintColor;import ************************** javafxscenetextFontWeight;public class Lab4Solution ******* Application {private ******* ********** * true;private Label *********** * *** *************** ***** *********** * *** Label();private ***** ************ * new *************** TextField tfLeft * *** TextField();private ********* ******* = *** ******************* Button ******** = new ************************** *** * *** **************** *** rightVal * ********* int ******* * 0;private *** score * ***************** **** *********** ************* ************************ ********************************* **************** void ****************** ****** ********* = 0;leftVal * ******* * ***************************************************************************** = ***************************************** EventHandler<MouseEvent>(){@Overridepublic **** handle(MouseEvent ***** {int max * ******* *** * 101;int val * **************** *** min) + ** * *********** cardName * **************** * *** + *********************** ** ************** * val;Image ************ * new ****************************************** ************************************** = ********* *********** * *** Image(cardName);lblCardLeftsetGraphic(new *********************************** > ************** * *************************************************************** * ************ ********** > rightVal){score * ************************************************************* * score);}}rightsTurn = ********************************************************************************************************************************************************************************************* **** * *** ********************* ******** * new GridPane();cardPanesetHgap(200);cardPaneadd(lblCardLeft ************************** * *************************** * ******************************************* ******* = *** ******************************************************** ******** = *** ************************ ******** * ********************** ************** ******************************************************************************** ***************** ************** **** ******************** 1 1);topPaneadd(new *************** **** * ********************* 3 **************************************************************************** ***** = new ********** 400 *************************************** * – ****** **** ** War”);primaryStagesetScene(scene);primaryStageshow();}private **** *********************** *********** * *** Image(“file:img\\155gif”);Image ************ * new *********************************** *********** * *** ******************************************************* ImageView(imgCardLeft));lblCardDecksetGraphic(new ************************************************** ******************************** ****** **** main(String[] ***** ******************