Jump to content

Sum Problems

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Sheld

Sheld

    Newbie

  • Members
  • Pip
  • 2 posts
I am making a BlackJack game using multiple classes and have come across a problem with my sum() method in my Player class, which uses a getNum() method from my Card class. Code:
class Card{
    private int num, suit;
    private static String[] suitArray= {"s", "h", "c", "d", "trees"};
    private static int[] numArray=new int[13];
    
    
    public Card(int suit, int num){ //create cards
            this.num=num;          //this.num means the num variable in the card class not in the card constructor method parameters
            this.suit=suit;
            for (int i=0; i<13; i++){ //populate numArray with number 1-13
                numArray[i]=i+1;
            }
    }
    public int getNum(){ //get the integer value of a card
        return num;
    }
class Player{
    private Scanner input= new Scanner(System.in);
    private Deck deckObj= new Deck();
    private Card[] hand;
    private int sums, draw, count;
    private String player, choice;
    
    public Player(){
        hand=new Card[5];
        draw=1;
        choice="hit";
        count=0;
        deckObj.displayDeck();
        deckObj.shuffle();
        deckObj.displayDeck();
    }
    public void intro(){ //simple introductory sentence to welcome player
        System.out.println("Who is brave enough to face the dealer?");
        player=input.nextLine();
        System.out.println("Good Luck "+player);
    }
    public int sum(){ //finds sum of the cards in the hand
        sums=0;
        boolean ace=false;
        for (int i=0; i<draw; i++){
            if (hand[i].getNum()==1){
                ace=true;
                sums+=hand[i].getNum();
            }
            if (ace==true && sums<=11) {
                sums+=10;
            }
        }
        return sums;
    }
    public int playerHand(){ //compile a hand for the player and returns the sum
        while (choice.equals("hit")&&sums<21&&draw<=5){ //adds cards until draw is over 5 and sum is greater 21
            draw++; //keeps track of number of cards drawn
            for (int i=count; i<=draw; i++){
                hand[i]=deckObj.draw(); //outofbounds exception?
                count++;
            }
            System.out.println(this.displayHand());
            System.out.println("Your sum is "+this.sum()+". Type hit for another card or stay to keep your current sum:");
            choice=input.nextLine();
            clear();
        }
        input.close();    
        return count;
    }
Any help would be greatly appreciated.

Edited by ZekeDragon, 08 April 2010 - 12:45 AM.
Please use [code] tags (the # button) when posting code.


#2
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
Here are the few things i would like you to know .
First of all , please use CODE TAG!!!

I have arranged the code nicely without alter any code.
class Card
{
    private int num, suit;
    private static String[] suitArray= {"s", "h", "c", "d", "trees"};
    private static int[] numArray=new int[13];


    public Card(int suit, int num )//create cards\
    {
        this.num=num; //this.num means the num variable in the card class not in the card constructor method parameters
        this.suit=suit;
        for (int i=0; i<13; i++) //populate numArray with number 1-13
        {
            numArray[i]=i+1;
        }
    }
    
    public int getNum() //get the integer value of a card
    {
        return num;
    }
    
class Player
{
    private Scanner input= new Scanner(System.in);
    private Deck deckObj= new Deck();
    private Card[] hand;
    private int sums, draw, count;
    private String player, choice;

    public Player()
    {
        hand=new Card[5];
        draw=1;
        choice="hit";
        count=0;
        deckObj.displayDeck();
        deckObj.shuffle();
        deckObj.displayDeck();
    }
    
    public void intro() //simple introductory sentence to welcome player
    {
        System.out.println("Who is brave enough to face the dealer?");
        player=input.nextLine();
        System.out.println("Good Luck "+player);
    }
    
    public int sum() //finds sum of the cards in the hand
    {
        sums=0;
        boolean ace=false;
        for (int i=0; i<draw; i++)
        {
            if (hand[i].getNum()==1)
            {
                ace=true;
                sums+=hand[i].getNum();
            }
            if (ace==true && sums<=11) 
            {
                sums+=10;
            }
        }
        return sums;
    }
    
    public int playerHand() //compile a hand for the player and returns the sum
    {
        while (choice.equals("hit")&&sums<21&&draw<=5) //adds cards until draw is over 5 and sum is greater 21
        {
            draw++; //keeps track of number of cards drawn
            for (int i=count; i<=draw; i++)
            {
                hand[i]=deckObj.draw(); //outofbounds exception?
                count++;
            }
            
            System.out.println(this.displayHand());
            System.out.println("Your sum is "+this.sum()+". Type hit for another card or stay to keep your current sum:");
            choice=input.nextLine();
            clear();
        }
        input.close();
        return count;
    }
As you can see , you have some missing curly bracket due to your laziness in arranging your code
And please please please , remember to put an access modifier for your class
Eg , public class Card ...
There also some missing class as well and remember to import your scanner class .
Missing class : Deck class

Can i know where is the clear method came from ?
What problems do you encounter ? Can you specify it?

Please provide a full code next time .