In this assignment we are going to use class construction to

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

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

Create space for the Image reference you created in the field by calling the constructor that takes the path to the image

Set the graphic on the Label you created in the field. Don\'t forget you will need an ImageView

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:

Set the String you created in the field that holds the path to the String passed as an argument

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.

Link to download card images:

https://mega.nz/#!tBJD3YLZ!YjGYuP7d4-6Etq1J_i4o3XTNVqjqjyaIhIxkGgaSLGw

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.

Solution

import java.util.Random; import java.util.Scanner; public class ScissorPaperStone { public static void main(String[] args) { Random random = new Random(); // Create a random number generator boolean gameOver = false; HandSign playerMove = HandSign.SCISSOR; HandSign computerMove; int numTrials = 0; int numComputerWon = 0; int numPlayerWon = 0; int numTie = 0; Scanner in = new Scanner(System.in); System.out.println(\"Let us begin...\"); while (!gameOver) { System.out.printf(\"%nScissor-Paper-Stone\"); // Player move // Use a do-while loop to handle invalid input boolean validInput; do { System.out.print(\" Your turn (Enter s for scissor, p for paper, t for stone, q to quit): \"); char inChar = in.next().toLowerCase().charAt(0); // Convert to lowercase and extract first char validInput = true; if (inChar == \'q\') { gameOver = true; } else if (inChar == \'s\') { playerMove = HandSign.SCISSOR; } else if (inChar == \'p\') { playerMove = HandSign.PAPER; } else if (inChar == \'t\') { playerMove = HandSign.STONE; } else { System.out.println(\" Invalid input, try again...\"); validInput = false; } } while (!validInput); if (!gameOver) { // Computer Move int aRandomNumber = random.nextInt(3); // random int between 0 (inclusive) and 3 (exclusive) if (aRandomNumber == 0) { computerMove = HandSign.SCISSOR; System.out.println(\" My turn: SCISSOR\"); } else if (aRandomNumber == 0) { computerMove = HandSign.PAPER; System.out.println(\" My turn: PAPER\"); } else { computerMove = HandSign.STONE; System.out.println(\" My turn: STONE\"); } // Check result if (computerMove == playerMove) { System.out.println(\" Tie!\"); ++numTie; } else if (computerMove == HandSign.SCISSOR && playerMove == HandSign.PAPER) { System.out.println(\" Scissor cuts paper, I won!\"); ++numComputerWon; } else if (computerMove == HandSign.PAPER && playerMove == HandSign.STONE) { System.out.println(\" Paper wraps stone, I won!\"); ++numComputerWon; } else if (computerMove == HandSign.STONE && playerMove == HandSign.SCISSOR) { System.out.println(\" Stone breaks scissor, I won!\"); ++numComputerWon; } else { System.out.println(\" You won!\"); ++numPlayerWon; } ++numTrials; } } // Print statistics System.out.printf(\"%nNumber of trials: \" + numTrials); System.out.printf(\"I won %d(%.2f%%). You won %d(%.2f%%).%n\", numComputerWon, 100.0*numComputerWon/numTrials, numPlayerWon, 100.0*numPlayerWon/numTrials); System.out.println(\"Bye! \"); } }
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 informatio
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 informatio
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 informatio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site