Question
I do not know what is wrong with my code and how to correct it. the program I need to create: Write an application that
I do not know what is wrong with my code and how to correct it.
the program I need to create:
Write an application that stores the values for a deck of 52 cards. Use an integer array to hold the values. You are storing the values 0 to 12, 13 to 25, 26 to 38, and 39 to 51 which represent 13 spades (beginning with Ace and ending with a king) , 13 hearts, 13 diamonds, and 13 clubs respectively. This application is a form of the card game War or Battle. The application should load the deck. Make use of a loop to load the array. The application should allow the user or player to enter a value for their card. Be sure to validate their entry as a number between 0 and 51. Have the computer draw a random card from the deck. Compare the users card to the computers card and determine the winner. The winner has the card with the larger value. Aces are low. Remember card 0 is the ace of spades and has the same value as card 13 which is the ace of hearts. The winner of the round receives a point. Allow the user to continue playing until they are ready to stop. Make use of methods where appropriate. Display the winner of each round as well as the overall winner after the user quits the game. This application should have a method that displays the suit and the rank of the card. Meaning if the user enters 14 the method will display 2 of Hearts. This method should be called to display the suit and rank of the user and computers card each round. You should make use of the following equations to determine the suit and rank of your card: Suit: cardNum / 13 0 = Spades 1 = Hearts 2 = Diamonds 3 = Clubs Rank: cardNum % 13 0 = Ace 1 = 2 2 = 3 3 = 4 4 = 5 5 = 6 6 = 7 7 = 8 8 = 9 9 = 10 10 = Jack 11 = Queen 12 = King
My code:
import java.util.Random;
import java.util.Scanner;
public class cardGame {
public static void main(String[] args) {
// TODO Auto-generated method stub
cardDeck C = new cardDeck();
int theInput = 0;
Scanner card = new scanner (System.in);
while (theInput!=2) {
System.out.println("1. To play");
System.out.println("2. Exit");
System.out.println("Enter choice:");
theInput = card.nextInt();
if (theInput==1) {
System.out.println("Enter value of the card:");
int cardNum = card.nextInt();
if (cardNum >= 0 && cardNum<=51) {
int rightCard = C.systemReturn (cardNum);
System.out.println("Your card-" + C.insertCard(cardNum));
System.out.println("Winner is" + (rightCard > cardNum ? "system": "you") );
}
else {
System.out.println("Invalid Card!!");
}
}
}
}
}
class cardDeck{
String suits[]= {
"Spades",
"Hearts",
"Diamonds",
"Clubs",
};
Random set = new Random();
int systemWinner = 0;
int playerWins = 0;
private Object ran;
String insertCard(int card) {
int theSuit = card/13;
int theRank = card%13;
String output = "";
switch (theRank) {
case 0:
output = "Ace of" + suits [theSuit];
break;
case 11:
output = "Queen of" + suits [theSuit];
break;
case 12:
output = "King of" + suits [theSuit];
break;
default:
output = theRank + "of" + suits [theSuit];
break;
}
return output;
}
int systemReturn (int rankPlayer) {
int systemRank = this.ran.nextInt(52);
if (systemRank > rankPlayer) {
systemWinner++;
}
else {
playerWins++;
return systemRank;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started