Results 1 to 4 of 4
  1. #1
    mandrake446 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default Help On Coding problem

    I'm having a problem coding a simple blackjack project. Here is what I have my questions are posted at the bottom of the code.

    Note that in Blackjack the cards are valued as follows:
    An Ace can count as either 1 or 11, depending on the hand;The cards from 2 through 9 are valued as indicated by rank;The 10, Jack, Queen, and King are all valued at 10.

    1. Add a new class, called DealerHand, to the project. This class simulates a dealer’s hand in the game of Blackjack. Implement class DealerHand according to the following declaration:
    public class DealerHand {
    protected int numberOfPoints;
    //points in the hand
    public DealerHand(){ ... }
    public void dealCards(CardDeck deck){ ... }
    public int handTotal() { ... }
    }

    In your implementation, you must follow these guidelines:

    – Constructor of the class must initialize field numberOfPoints with 0;
    – Method dealCards should call the method topCard() of class CardDeck
    in a loop, adding the value of each resulting card (see above rules) to the current
    value of numberOfPoints. Use the object returned by method topCard() to
    call method displayCard of class PlayingCard. The loop terminates when
    value of the field numberOfPoints reaches 17 or higher;
    – For simplicity, always count Ace as 11 points for the dealer's hand;
    – Method handTotal() returns the value of field numberOfPoints

    2. Add a new class, called PlayerHand, to the project. Implement class
    PlayerHand according to the following declaration:

    public class PlayerHand extends DealerHand {
    public void dealCards(CardDeck deck){ ... }
    }

    In your implementation, you must follow these guidelines:

    – Method dealCards first calls the method topCard() of class CardDeck twice, to deal the first two “cards”, and adds the value of each resulting card to the
    value of numberOfPoints. Use the object returned by method topCard()
    to call method displayCard of class PlayingCard. Then, in a loop, the
    following steps are repeated:

    a. Show a confirm dialog box containing the question: “Another Card?”,
    and wait for user response.

    b. The user responds by clicking either “yes” or “no” button; this input is
    accepted by the program.

    c. If the user answers “yes”, call method topCard() of class CardDeck
    again, and add the value of the card to numberOfPoints; display a
    picture of each card on canvas. Note that rank of an ace is counted as
    either 1 or 11 depending on the hand.

    d. If the user answers “no”, terminate the loop. The loop is also terminated as
    soon as field numberOfPoints reaches 21 or higher.

    3. Add another class, called GameApp, to the project cards. This class should contain only the method public static void main(String[] args), implemented according to the following guidelines (note: for ease of implementation, a player's hand is dealt first, before the dealer's hand):

    – First, create an object deck of type CardDeck, and invoke method shuffle
    on this object;

    – Next, create an object of type PlayerHand, and call method dealCards on
    this object, passing to it the above object deck as parameter value; when method dealCards returns, call the method handTotal to display the value of
    player's hand;

    – Next, create an object of type DealerHand, and call method dealCards on
    this object, again passing to it the above object deck as parameter value; when method dealCards returns, call the method handTotal to display the value of dealer's hand;

    – Display a message box stating who won: the dealer or the player.


    I have done part of it and i'm having some serious problems


    My CardDeck code:

    import java.util.Random;
    import java.util.Scanner;
    public class CardDeck
    {
    public PlayingCard[] deck;
    public static final int DECK_SIZE = 52;
    private int numCardsLeft;

    public CardDeck(){
    deck = new PlayingCard[DECK_SIZE];
    numCardsLeft = DECK_SIZE;
    int i;
    for (i = 0; i < 13; i++)
    deck[i] = new PlayingCard(PlayingCard.Spades, i+1, true);
    for(i = 13; i < 26; i++)
    deck[i] = new PlayingCard(PlayingCard.Clubs, i-12, true);
    for(i = 26; i < 39; i++)
    deck[i] = new PlayingCard(PlayingCard.Hearts, i-25, true);
    for(i = 39; i < 52; i++)
    deck[i] = new PlayingCard(PlayingCard.Diamonds, i-38, true);
    }

    public void shuffle(){
    PlayingCard temp;
    Random randomGenerator = new Random();
    for(int k = 0; k < 100; k++){
    int i = randomGenerator.nextInt(DECK_SIZE);
    int j = randomGenerator.nextInt(DECK_SIZE);
    temp = deck[i];
    deck[i] = deck[j];
    deck[j] = temp;
    }
    }

    public void wholeDeck(){
    for(PlayingCard card: deck){
    if(card != null)
    card.describeCard();
    }
    }

    public PlayingCard topCard(){
    PlayingCard temp = deck[0];
    this.removeCard(0);
    return temp;
    }

    public PlayingCard anyCard() {
    PlayingCard temp;
    Random randomGenerator = new Random();
    int number = randomGenerator.nextInt(numCardsLeft);
    temp = deck[number];
    removeCard(number);
    return temp;
    }

    private void removeCard(int index) {
    if(index >= 0 && index < numCardsLeft){
    for(int i = index; i < numCardsLeft-1; i++)
    deck [i] = deck[i+1];
    deck[numCardsLeft-1] = null;
    numCardsLeft--;
    }
    }
    }

    My DealerHand code :

    public class DealerHand
    {
    protected int numberOfPoints; //Points in the hand
    public DealerHand()
    {
    numberOfPoints = 0;
    }
    public void dealCards(CardDeck deck)
    {

    while(numberOfPoints < 17){

    switch(CardDeck.topCard()){
    case Ace:
    numberOfPoints = 11;
    break;
    case King:
    numberOfPoints = 10;
    break;
    case Queen:
    numberOfPoints = 10;
    break;
    case Jack:
    numberOfPoints = 10;
    break;
    case 10:
    numberOfPoints = 10;
    break;
    case 9:
    numberOfPoints = 9;
    break;
    case 8:
    numberOfPoints = 8;
    break;
    case 7:
    numberOfPoints = 7;
    break;
    case 6:
    numberOfPoints = 6;
    break;
    case 5:
    numberOfPoints = 5;
    break;
    case 4:
    numberOfPoints = 4;
    break;
    case 3:
    numberOfPoints = 3;
    break;
    case 2:
    numberOfPoints = 2;
    break;
    default:
    numberOfPoints = 0;
    break;
    }
    }
    }
    public int handTotal()
    {
    return numberOfPoints;
    }
    }

    My GameApp code:

    public class GameApp
    {
    public static void main (String[] args)
    {
    CardDeck deck = new CardDeck();
    deck.shuffle();

    PlayerHand bobsHand = new PlayerHand();
    bobsHand.dealCards(deck);

    DealerHand shady = new DealerHand();
    shady.dealCards(deck);

    if(dealersHand >= playersHand){
    System.out.println("Dealter wins");}
    else
    System.out.println("Player wins");
    }
    }

    I do not currently have anything written in class PlayerHand because it relies heavily on Dealerhand.

    What I cant figure out is....

    1. If i'm going about doing deal cards right by my instructions. <-- step 1
    2. how to write a dialog box (i only know how to do regular lines of code) <---step 2 and 3
    3. what it means when it says "when method dealCards returns, call the method handTotal to display the value of the players hand" <--- step 3

    If you can post a explanation of any code you actually post i would apprciate it so that i can finish the project, and actually understand how to do it.

  2. #2
    mandrake446 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    Nvm on number 2 i figured it out :D

  3. #3
    mandrake446 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default my updated DealerHand (easier to read)

    mport java.util.Random;
    import java.util.Scanner;
    public class DealerHand
    {
    protected int numberOfPoints; //Points in the hand
    public DealerHand()
    {
    numberOfPoints = 0;
    }
    public void dealCards(CardDeck deck)
    {

    while(numberOfPoints < 17){
    PlayingCard pc = deck.topCard();
    int rank = pc.getRank();
    switch(rank){
    case 1: //Ace
    numberOfPoints += 11;
    break;
    case 13: //King
    case 12: //Queen
    case 11: //Jack
    numberOfPoints += 10;
    break;
    default: //the rest of cards
    numberOfPoints += rank;
    break;
    }
    }
    }
    public int handTotal()
    {
    return numberOfPoints;
    }
    }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    If i'm going about doing deal cards right by my instructions
    As far as I can tell with casual inspection, yes it looks okay.
    If I had the PlayingCard class I could run it and maybe learn more.
    what it means when it says "when method dealCards returns, call the method handTotal to display the value of the players hand"
    Like this:
    Java Code:
    PlayerHand bobsHand = new PlayerHand();
    bobsHand.dealCards(deck);
    int bobsPoints = bobsHand.handTotal();
    System.out.println("bobsPoints = " + bobsPoints););
    
    DealerHand shady = new DealerHand();
    shady.dealCards(deck);
    int dealerPoints = shady.handTotal();
    System.out.println("dealerPoints = " + dealerPoints);
    
    if(dealerPoints >= bobsPoints){
        System.out.println("Dealter wins");}
    else
        System.out.println("Player wins");

Similar Threads

  1. Coding an FTP server in java
    By Zucheto in forum Networking
    Replies: 3
    Last Post: 06-22-2008, 04:24 AM
  2. Error in my coding
    By one198 in forum New To Java
    Replies: 2
    Last Post: 10-13-2007, 05:07 AM
  3. Cannot solve the coding problem of my assignment
    By elimmom in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 11:33 AM
  4. Problem in my coding
    By one198 in forum New To Java
    Replies: 9
    Last Post: 08-09-2007, 10:07 AM
  5. Help with program coding
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 07:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •