Answer in Java Please In Lab 4 you created a rudimentary war game. In this assignment we are going to use class construc

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Answer in Java Please In Lab 4 you created a rudimentary war game. In this assignment we are going to use class construc

Post by answerhappygod »

Answer in Java Please
In Lab 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 card's value,
suit and graphics.
Additionally, we will create an enumerated type to relate the
suit of the card.
For this first step you want to create a new project called
Lab05 and then copy the code from Lab04 into it - don't forget to
change your class name. This will be a multi-file project, so you
need to make sure you use a package. You need to copy the img
folder from Lab04 into Lab05's project folder as well. You
could just expand on the Lab04 project, 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 Lab 4.
Create and add a class called Card to your project. You will
need to make sure you have imported Image, ImageView, and
javafx.scene.control.*. In the field of the card class you will
need to create the following private variables:
Create a private void helper method called createCard that takes
as an argument a String that represents the full path to the Image.
This method should do the following:
Create a public void method called setCard that takes as an
argument a String that represents the file path of the image to
load. This method should just call the createCard() method.
Note - right now this seems silly. Think about why you would
do this to help develop the project in the future.
Create a public method called getCardLabel that simply returns
the Label that was created in the field.
At this point you should have a prototype Card class that can be
tested.
Step 2 - Test Card Class
Inside the Lab05.java 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 object.
We haven't created constructors yet so you will need two lines of
code to replace:
You will have to create a new Card and then call the setCard
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 getCardLabel() method that is a member of the Card
you just created.
Please note that you will have to do the other Labels you have
created.
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
Lab 4.
Step 3 Features
To complete the Card class we need to keep track of both the
Suit and value of the card. To begin, add a java file called
Suit.java to your package by right clicking your project package
-> New -> Java Enum... (you might have to look for it in
"Other" at the bottom of the list). Inside this file create an
enumerated type called Suit.
An enumerated type is a way to relate a numeric value to a name
(it can be thought of as a type of constant). Like the months of
the year are numbered 1 to 12 and correspond to the values January
- December. The enumerated type should simply look like the
following:
Inside the Card class create a private Suit type variable in the
field to hold the suit of the Card.
Step 4 - Calculating card value and suit
If you have not already looked at the card images, you should do
so now so you know the sequence of the cards in each suit and the
order of suits. I don't want you to change the file names,
work with the ones you were given.
Create a private helper method called calcCardValue.
The calcCardValue method's purpose is to
determine the point value of a card and store it in the field
variable you created earlier to hold the card point value. I want
you to extract the number of the card from the file path string
field and convert it to a numeric type, without changing the path
field. IE peel off the text before the number and after
it. Hint: you want to use the String replace or split
methods. We have not covered regular expressions yet, so do
not use them here.
Use the extracted numeric value of the card to calculate the
actual value the card has. Make the number cards have the value of
their numbers, and for the face cards: all Jacks to have a value of
11, Queens 12, Kings 13 and Aces 14 (we are not using the deck's
jokers). So the card values should range from 2 to 14.
I will leave it to you to design an algorithm to do this
but do not have a switch or if ladder
statement that contains 50+ cases/branches. You should work out an
algorithm that mathematically determines the values. You will need
to use some decision logic, but try to write the code as concise as
possible.
Once you have worked out the point value of the card set the
field variable to that value.
Create another private helper method called calcCardSuit to
determine the the suit of the card. You should set the suit created
in the field from this method. The suit would be set with code like
this:
Once you finish coding calcCardValue() and calcCardSuit() add
calls to the helper methods to the bottom of createCard().
Complete the Card class by creating three public accessor
methods as follows:
At this point you should have a working Card class that holds a
label, its graphics as well as the the suit and the value of a
card. The class should have 5 field variables, 3 private helper
methods, 1 mutator and 3 accessors.
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 determined by the number
in the file name of the card. We have just created an accessor
method that will return the Card's point value so we want to use
the values of the left and right cards to determine which side wins
(ties are still possible). You need to modify the decisions inside
the mouse handler method.
We also want to change how the score is updated. 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.
Here is my code from Lab 04
package lab4;

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 Lab4 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;

@Override
public void start(Stage primaryStage) {

btnReset.setOnAction(new EventHandler<ActionEvent() {
@Override
public void handle(ActionEvent event) {
rightVal = 0;
leftVal = 0;
score = 0;
tfRight.setText("0");
tfLeft.setText("0");
resetCardImages();
rightsTurn = true;
}
});
lblCardDeck.setOnMouseClicked(new
EventHandler<MouseEvent(){
@Override
public 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);
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply