I'm relatively new to Java and I'm writing a code that implements a simple "Hi-Lo" game using a standard deck of 52 playing cards. I could give you the entire description of the program's requirements, but I won't let you all suffer the same headache I do. :P Anyhow, here's my two chunks of code:
import java.util.Random;
public class PlayingCard
{
private int face;
private int suit;
//-------------------------------------------------------------------
// Sets up the playing card by drawing it initially.
//-------------------------------------------------------------------
public PlayingCard ()
{
drawCard();
}
//-------------------------------------------------------------------
// Draws the playing card by randomly choosing face and suit values.
//-------------------------------------------------------------------
public void drawCard ()
{
face = (int) (Math.random() * 13) + 2;
suit = (int) (Math.random() * 4);
}
//-------------------------------------------------------------------
// Returns true if the current playing card is exactly the same as
// the card passed in as a parameter.
//-------------------------------------------------------------------
public boolean isEquals (PlayingCard c)
{
return (face == c.face && suit == c.suit);
}
//-------------------------------------------------------------------
// Returns the face value of the current playing card.
//-------------------------------------------------------------------
public int getFace()
{
return face;
}
//-------------------------------------------------------------------
// Returns the suit value of the current playing card.
//-------------------------------------------------------------------
public int getSuit()
{
return suit;
}
//-------------------------------------------------------------------
// Returns the current playing card as a string.
//-------------------------------------------------------------------
public String toString()
{
String cardName = null;
switch (face)
{
case 2: cardName = "Two";
break;
case 3: cardName = "Three";
break;
case 4: cardName = "Four";
break;
case 5: cardName = "Five";
break;
case 6: cardName = "Six";
break;
case 7: cardName = "Seven";
break;
case 8: cardName = "Eight";
break;
case 9: cardName = "Nine";
break;
case 10: cardName = "Ten";
break;
case 11: cardName = "Jack";
break;
case 12: cardName = "Queen";
break;
case 13: cardName = "King";
break;
case 14: cardName = "Ace";
}
switch (suit)
{
case 0: cardName += " of Clubs";
break;
case 1: cardName += " of Spades";
break;
case 2: cardName += " of Hearts";
break;
case 3: cardName += " of Diamonds";
break;
}
return cardName;
}
}
import java.util.Scanner;
import java.util.Random;
public class HiLoGame
{
public static void main (String[] args)
{
double bankroll, bet;
int lostGames, tiedGames, totalGames, wonGames;
Scanner scan = new Scanner (System.in);
System.out.println("\n\nThis program implements a Hi-Lo game using");
System.out.println("a standard deck of 52 playing cards. You are");
System.out.println("required to enter your bankroll as well as the");
System.out.println("amount of money you wish to bet.");
System.out.print("\n\nHow much is in your bank? ");
bankroll = scan.nextDouble();
System.out.print("\n\nHow much would you like to bet? ");
bet = scan.nextDouble();
System.out.println("First card: " + getFace + " of " + getSuit);
System.out.print("\n\nDo you think the second card will be higher or lower?");
String choiceString = scan.nextLine();
choiceString = choiceString.toLowerCase();
PlayingCard = cardOne = new PlayingCard();
PlayingCard = cardTwo = new PlayingCard();
if (cardOne.getface() < cardTwo.getTwo.getface() && guess.equals("higher"))
do
{
cardTwo.drawCard();
}
while (cardOne.isEquals(cardTwo));
if (choiceString > 0)
if (choiceString == cardTwo)
{
System.out.println("Good job! You're right!");
bankroll = bankroll + bet;
wonGames++;
totalGames++;
System.out.println("The amount in your bankroll is " + bankroll + ".");
System.out.println("Would you like to play again?");
}
if (choiceString < cardTwo)
{
System.out.println("Sorry, your guess was too low.");
bankroll = bankroll - bet;
lostGames++;
totalGames++;
System.out.println("The amount in your bankroll is " + bankroll + ".");
System.out.println("Would you like to play again?");
}
else
{
System.out.println("Sorry, your guess was too high.");
bankroll = bankroll - bet;
lostGames++;
totalGames++;
System.out.println("The amount in your bankroll is " + bankroll + ".");
System.out.println("Would you like to play again?");
}
System.out.println("You played "+ totalGames +" games total.");
System.out.println("You won " + wonGames + " times, lost " + lostGames + " times, and tied " + tiedGames + " times.");
System.out.println("The amount left in your bankroll is " + bankroll + ".");
}
}
I do realize there's a number of errors in this, but what I want to start with is how can I configure the HiLo.java so that it reads off of the PlayingCard.java?