1 Replies - 5823 Views - Last Post: 25 March 2010 - 01:49 AM Rate Topic: -----

#1 velop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-April 09

Hi-Lo Java

Posted 25 March 2010 - 01:17 AM

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. It's supposed to ask the user to enter their bankroll and must verify that the user enters a postive real number. The program should repeat until the bankroll's empty. When the game ends, it displays the total number of games played, wins, losses, ties and the amount in the user's bankroll.

For each play of the game, the user is supposed to enter their bet. He or she must have sufficient funds remaining in their bankroll to cover their bet and so it's under $100.00. The program then draws a card and displays it to the user. Then it asks the user if he or she thinks the next card will be higher or lower and draws a second card. The second card has to be different in some way, be it face, suit or both. If the user guessed correctly, the bet is added to their bankroll. If not, the bet is subtracted from their bankroll. Display the amount of the user's bankroll after each game before asking whether the user wishes to play again.

Now here's what I have:

Playing Card

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


The Hi-Lo Game

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 + ".");
	 
   }
}



How can I configure my code so that it reads off of PlayingCard.java? Also what can I do to make it understand 'Higher' or 'Lower' for the second card?

Is This A Good Question/Topic? 0
  • +

Replies To: Hi-Lo Java

#2 Get.carter()  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 31
  • Joined: 17-June 09

Re: Hi-Lo Java

Posted 25 March 2010 - 01:49 AM

Hi, could you try editing your post so that the code is easier to read.
As it stands it's very hard to read.
Bottom line is, keep it all to the left so as to minimize the workload on the horizontal scroll bars.
Eg:
 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 + ".");
 }}




The easier it is to read the greater your chances of a reply :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1