Jump to content

[java] blackjack problem


Recommended Posts

public class Game
{
/** Suggested constants for use throughout the game. To use one of these in another class, specify
* the Game class first, e.g. Game.ACE_LOW_VALUE.
*/
public static final int ACE_LOW_VALUE = 1; // bottom value for ace
public static final int ACE_HIGH_VALUE = 11; // top value for ace
public static final int BLACKJACK = 21; // instant win - to go over is to lose
public static final int DEALER_STOP = 17; // dealer cannot stand until at least this value

/* Some suggested fields */
private Deck deck; // a deck of cards
private InputReader reader; // keyboard reader
private ArrayList<Card> hand; // the cards the player is holding

/**
* Constructor for objects of class Game
*/
public Game()
{
// initialize fields
deck = new Deck();
reader = new InputReader();
hand = new ArrayList<Card>();
}


/**
* This method holds the game logic. The user is repeatedly
* prompted to play another round. When the user has chosen to stop
* playing, the final tally of wins and losses is reported.
*
*/
public void play()
{
dealCard();
dealCard();
dealCard();
showHand();
}


/*
* Deal one card from the deck and put it into the player's hand.
*/
private void dealCard()
{
// deal card to player
hand.add (deck.takeCard());
}

/*
* Play out the player's turn.
* The player may choose to hit or stand, unless the player's
* cards total more than BLACKJACK, in which case the player
* is "busted".
*/
public void playerTurn()
{
// player's turn
// an example of how to use the reader object:
System.out.print("Your choice: hit or stand? ");
String response = reader.readInputLine().trim().toLowerCase();
if (response.equals("hit")) {// process response
}
}

/*
* Calculates and returns the value of the hand. An ace is worth ACE_HIGH_VALUE
* unless it would cause a "bust", in which case it is worth ACE_LOW_VALUE.
*/
public int getHandValue()
{
// calculate hand value
int sum = 0;
Iterator<Card> it = hand.iterator();
while (it.hasNext())
{
for (Card card: hand)
{
int handValue = card.getValue();
sum += handValue;
handValue++;
return sum;
}
}
return 0;
}

/*
* Shows the cards in the player's hand.
*/
private void showHand()
{
System.out.println("your cards:");
// show the cards
for(Card card : hand)
{
System.out.println(""+card.toString());
}
System.out.println(); // blank line
}

}

Card is a class.

hand is an arraylist of Card.

the problem is that return 10. why?

when i invoked play(), it should return 30.

but it return as 10.

i think hardest.

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...