10 Replies - 11310 Views - Last Post: 17 February 2011 - 04:05 PM Rate Topic: -----

#1 Guest_UAB2011*


Reputation:

Blackjack Program Help

Posted 09 February 2011 - 11:10 AM

Ok, so I have been working on this code trying to create a simple blackjack game. There will be no double down, spliting, or anything else complicated. I am having trouble with my Card.java class and my Blackjack.java class. I started with the Card class, but after many changes it is no longer working with the rest. In the blackjack class I am having trouble with the finishDealerHand and the wrapUpScoring. In the Card class I am having problems with the getValue method. I know I am missing code, but what I had was not working. Here is the code:

Card.java
import java.util.*;

public class Card implements Comparable
{
	private int rank;
	private int face;
	
	private final int ACE = 1;
	private final int KING = 13;
	private final int QUEEN = 12;
	private final int JACK = 11;
	private final int TEN = 10;
	private final int NINE = 9;
	private final int EIGHT = 8;
	private final int SEVEN = 7;
	private final int SIX = 6;
	private final int FIVE = 5;
	private final int FOUR = 4;
	private final int THREE = 3;
	private final int TWO = 2;
	private final int HEARTS;
	private final int DIAMONDS;
	private final int CLUBS;
	private final int SPADES;
	
	
	
	public GetValue()
	{
		int TEN =10 | 
	}

	public String toString() {

		String[] r = {"", "SPADES", "CLUBES", "DIAMONDS", "HEARTS"};
		String[] c = {"", "","ACE", "TWO", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
	   return str[c] + " of " + color[r];
}
}



Blackjack.java
//Blackjack.java
import java.util.Scanner;

public class Blackjack
{
	static Deck deck = new Deck();
	static Hand player = new Hand();
	static Hand dealer = new Hand();
	
	public static void main(String[] args)
	{
		Blackjack game = new Blackjack();
		deal();
		if (finishPlayersHand()) return;
		if (finishDealersHand()) return;
		wrapUpScoring();
	}
	
	static void deal()
	{
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		System.out.println("Dealer's up card = " + dealer.lastCard() + '\n');
		System.out.println("Player's hand is: ");
		System.out.println(player);
	}
	
	static boolean finishPlayerHand()
	{	//returns true if player busts; otherwise
		// player is still in game
		Scanner input = new Scanner(System.in);
		System.out.println("Do you want a hit (y/n): ");
		String reply = input.next();
		while (reply.equalsIsIgnoreCase("y"));
		{
		player.hit(deck.draw());
		System.out.println(player.lastCard());
		if (player.getScore() > 21)
		{
		System.out.println("You busted!");
		}
		return true;
		
	}


	static boolean finishDealerHand()
	{
		while 
		{
		dealer.hit(deck.draw());
		System.out.println(dealer.lastCard());
		if (dealer.getScore() > 21)
		{
		System.out.println("Dealer busted!");
		}
		return true;
		}
	}
	
	static void wrapUpScoring()
	{
	
	}
}


Hand.java
//Hand.java

import java.util.*; //List interface & ArrayList class

class Hand
{	//use the List interface and the ArrayList implementation to hold cards;
	private List<Card> cards = new ArrayList<Card>;
	private int Score = 0;
	private int num11ptAces = 0; //num of aces valued at 11pts. in the hand
	
	void hit(Card c)
	{	//mutator; modifies the hand and the score
		cards.add(c);
		if (c.getValue() == Card.ACE && (score +11) <=21)
		{
			score += 11;
			++ num11ptAces;
		} else score += c.getValue();
	}
	
	Card lastCard()
	{	//returns most recently dealt card
		if(cards.size() > 0) return cards.get(cards.size() - 1);
		return null;
	}
	
	int getScore()
	{	//returns score in this hand
		while (score > 21 && num11ptAce > 0)
		{
			score -= 10;
			-- num11ptAce;
		}
		return score;
	}
	
	public String toString()
	{	//returns a 'print string' for the hand
		String r = "";
		for (Card c : cards)
			r += c + "\n";
		return r;
	}
}



Deck.java
//Deck.java

   import java.util.*;

   class Deck
   {
      private int numCards = 52;
      private Card[] deck = new Card[numCards];
   
      Deck()
      {	
         fill();
         shuffle();
      }
   
      private void fill()
      {
         for (int i = 0; i < deck.length; ++i)
            deck[i] = new Card((i / 13) + 1, (i % 13) + 1);
      }
   
      private void shuffle()
      {
         Collections.shuffle(Arrays.asList(deck));
      }
   
      Card draw()
      {
         if (numCards < 0) 
            return null;
         return deck[--numCards];
      }
   }



Thank you in advance for your time and help.

Is This A Good Question/Topic? 0

Replies To: Blackjack Program Help

#2 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Blackjack Program Help

Posted 09 February 2011 - 11:25 AM

First things first...

 int TEN =10 |



"Dude! Where's my semicolon!"

Second,

That is constructor syntax and it must match the name of the class. No idea what you are trying to accomplish there. If it is a function it must declare a return type and what to return. Even if that was to be a constructor the variable TEN would not matter because it remains in scope.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_UAB2011*


Reputation:

Re: Blackjack Program Help

Posted 09 February 2011 - 11:30 AM

Updated code:

Blackjack.java
//Blackjack.java
import java.util.Scanner;

public class Blackjack
{
	static Deck deck = new Deck();
	static Hand player = new Hand();
	static Hand dealer = new Hand();
	
	public static void main(String[] args)
	{
		Blackjack game = new Blackjack();
		deal();
		if (finishPlayersHand()) return;
		if (finishDealersHand()) return;
		wrapUpScoring();
	}
	
	static void deal()
	{
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		System.out.println("Dealer's up card = " + dealer.lastCard() + '\n');
		System.out.println("Player's hand is: ");
		System.out.println(player);
	}
	
	static boolean finishPlayerHand()
	{	//returns true if player busts; otherwise
		// player is still in game
		System.out.println("Do you want a hit (y/n): ");
		String reply = input.next();
		while (reply.equalsIsIgnoreCase("y"));
		{
		player.hit(deck.draw());
		System.out.println(player.lastCard());
		if (player.getScore() > 21)
		{
		System.out.println("You busted!");
		}
		return true;
		
	}


	static boolean finishDealerHand()
	{
		System.out.prinln("\n" + "Dealer's hand is: \n" + dealer);
		while(dealer.getScore() < 17)
		{
		System.out.print("Dealer draws: ");
		dealer.hit(deck.draw());
		System.out.println(dealer.lastCard());
		if (dealer.getScore() > 21)
		{
		System.out.println("Dealer busted!");
		++playerwins;
		}
		return false;
		}
		return true;
	}
	
	static void wrapUpScoring()
	{
		if (player.getScore() > dealer.getScore())
		{	System.out.println("You Win!");
		}
		else if (player.getScore() < dealer.getScore())
		{	System.out.println("Dealer Wins!");
		}
		else if (player.getScore() == dealer.getScore())
		{	System.out.println("The Player and the Dealer have tied.");
		}		
	}
}



Card.java
import java.util.*;

public class Card implements Comparable
{
	private int rank;
	private int face;
	
	private final int ACE = 1;
	private final int KING = 13;
	private final int QUEEN = 12;
	private final int JACK = 11;
	private final int TEN = 10;
	private final int NINE = 9;
	private final int EIGHT = 8;
	private final int SEVEN = 7;
	private final int SIX = 6;
	private final int FIVE = 5;
	private final int FOUR = 4;
	private final int THREE = 3;
	private final int TWO = 2;
	private final int HEARTS;
	private final int DIAMONDS;
	private final int CLUBS;
	private final int SPADES;
	
	
	
	public getValue()
	{
		int TEN =10; int JACK = 10; int Queen = 10; int KING = 10;
		return c; 
	}

	public String toString() {

		String[] r = {"", "SPADES", "CLUBES", "DIAMONDS", "HEARTS"};
		String[] c = {"", "","ACE", "TWO", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
	   return str[c] + " of " + color[r];
}
}



These two aren't compiling. The other two are.
Was This Post Helpful? 0

#4 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Blackjack Program Help

Posted 09 February 2011 - 11:34 AM

You know reading the error console and maybe even providing us with the output would probably identify the issues. Still

    public getValue()
    {
        int TEN =10; int JACK = 10; int Queen = 10; int KING = 10;
        return c;
    }




is invalid syntax. There is no return type but you are returning 'c'. 'c' is undeclared so that doesn't even matter. You are declaring values in the scope of getValue() which means they won't exist outside of it. Not to mention they already exist which is likely to cause errors.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_UAB2011*


Reputation:

Re: Blackjack Program Help

Posted 09 February 2011 - 12:34 PM

View PostSpeedisaVirus, on 09 February 2011 - 11:34 AM, said:

You know reading the error console and maybe even providing us with the output would probably identify the issues. Still

    public getValue()
    {
        int TEN =10; int JACK = 10; int Queen = 10; int KING = 10;
        return c;
    }




is invalid syntax. There is no return type but you are returning 'c'. 'c' is undeclared so that doesn't even matter. You are declaring values in the scope of getValue() which means they won't exist outside of it. Not to mention they already exist which is likely to cause errors.


I see. What should the return and the ints look like for getValue?
Was This Post Helpful? 0

#6 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Blackjack Program Help

Posted 09 February 2011 - 06:46 PM

Its starting to seem like you did not write a good portion of this code or are piecing it together from snippets here and there. Visit the tutorials on this site or the oracle java site and figure it out.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Blackjack Program Help

Posted 09 February 2011 - 08:46 PM

final variables must be initialized at declaration
HART, DIAMOND, ... are not initialized

And what is exactly your question ?

finishPlayerHand() always return true.
What is the use of a boolean method that always return the same value ? Better to make it void.
Was This Post Helpful? 1
  • +
  • -

#8 Badgers  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 29
  • Joined: 16-December 10

Re: Blackjack Program Help

Posted 16 February 2011 - 11:42 AM

I am also working on a BJ program. With your code I added void to those booleans.

I am still getting this error:

Blackjack.java:30: <identifier> expected
static void boolean finishPlayerHand()
^
Blackjack.java:30: '(' expected
static void boolean finishPlayerHand()
^



//Blackjack.java
import java.util.Scanner;

public class Blackjack
{
	static Deck deck = new Deck();
	static Hand player = new Hand();
	static Hand dealer = new Hand();
	
	public static void main(String[] args)
	{
		Blackjack game = new Blackjack();
		deal();
		if (finishPlayersHand()) return;
		if (finishDealersHand()) return;
		wrapUpScoring();
	}
	
	static void deal()
	{
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		System.out.println("Dealer's up card = " + dealer.lastCard() + '\n');
		System.out.println("Player's hand is: ");
		System.out.println(player);
	}
	
	static void boolean finishPlayerHand()
	{	//returns true if player busts; otherwise
		// player is still in game
		System.out.println("Do you want a hit (y/n): ");
		String reply = input.next();
		while (reply.equalsIsIgnoreCase("y"));
		{
		player.hit(deck.draw());
		System.out.println(player.lastCard());
		if (player.getScore() > 21)
		{
		System.out.println("You busted!");
		}
		return true;
		
	}


	static void boolean finishDealerHand()
	{
		System.out.prinln("\n" + "Dealer's hand is: \n" + dealer);
		while(dealer.getScore() < 17)
		{
		System.out.print("Dealer draws: ");
		dealer.hit(deck.draw());
		System.out.println(dealer.lastCard());
		if (dealer.getScore() > 21)
		{
		System.out.println("Dealer busted!");
		++playerwins;
		}
		
		}
		return true;
	}
	
	static void wrapUpScoring()
	{
		if (player.getScore() > dealer.getScore())
		{	System.out.println("You Win!");
		}
		else if (player.getScore() < dealer.getScore())
		{	System.out.println("Dealer Wins!");
		}
		else if (player.getScore() == dealer.getScore())
		{	System.out.println("The Player and the Dealer have tied.");
		}		
	}
}

This post has been edited by Badgers: 16 February 2011 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Blackjack Program Help

Posted 16 February 2011 - 08:32 PM

make your method void or boolean can't be both

so

static void finishPlayerHand()
static void finishDealerHand()

and removed the

return true; // statement at th end of them

I just said that these method do not need to be boolean because they always return true
Was This Post Helpful? 1
  • +
  • -

#10 Badgers  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 29
  • Joined: 16-December 10

Re: Blackjack Program Help

Posted 16 February 2011 - 10:33 PM

I made the changes. The finishDealerHand() is still not compiling. Here is the code:

//Blackjack.java
import java.util.Scanner;

public class Blackjack
{
	static Deck deck = new Deck();
	static Hand player = new Hand();
	static Hand dealer = new Hand();
	
	public static void main(String[] args)
	{
		Blackjack game = new Blackjack();
		deal();
		if (finishPlayersHand()) return;
		if (finishDealersHand()) return;
		wrapUpScoring();
	}
	
	static void deal()
	{
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		System.out.println("Dealer's up card = " + dealer.lastCard() + '\n');
		System.out.println("Player's hand is: ");
		System.out.println(player);
	}
	
	static void finishPlayerHand()
	{	//returns true if player busts; otherwise
		// player is still in game
		System.out.println("Do you want a hit (y/n): ");
		String reply = input.next();
		while (reply.equalsIsIgnoreCase("y"));
		{
		player.hit(deck.draw());
		System.out.println(player.lastCard());
		if (player.getScore() > 21)
		{
		System.out.println("You busted!");
		}
	}


	static void finishDealerHand()
	{
		System.out.prinln("\n" + "Dealer's hand is: \n" + dealer);
		while(dealer.getScore() < 17)
		{
		System.out.print("Dealer draws: ");
		dealer.hit(deck.draw());
		System.out.println(dealer.lastCard());
		if (dealer.getScore() > 21)
		{
		System.out.println("Dealer busted!");
		++playerwins;
		}
		}
		
	}
	
	static void wrapUpScoring()
	{
		if (player.getScore() > dealer.getScore())
		{	System.out.println("You Win!");
		}
		else if (player.getScore() < dealer.getScore())
		{	System.out.println("Dealer Wins!");
		}
		else if (player.getScore() == dealer.getScore())
		{	System.out.println("The Player and the Dealer have tied.");
		}		
	}
}


Here is the error:

Blackjack.java:46: illegal start of expression
static void finishDealerHand()

I don't understand why this is not working.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Blackjack Program Help

Posted 17 February 2011 - 04:05 PM

Indent your code correctly, how many times will we have to repeat that ? :(
Then it becomes obvious :)

//Blackjack.java
import java.util.Scanner;

public class Blackjack
{
	static Deck deck = new Deck();
	static Hand player = new Hand();
	static Hand dealer = new Hand();

	public static void main(String[] args)
	{
		Blackjack game = new Blackjack();
		deal();
		if (finishPlayersHand()) return;
		if (finishDealersHand()) return;
		wrapUpScoring();
	}

	static void deal()
	{
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		player.hit(deck.draw());
		dealer.hit(deck.draw());
		System.out.println("Dealer's up card = " + dealer.lastCard() + '\n');
		System.out.println("Player's hand is: ");
		System.out.println(player);
	}

	static void finishPlayerHand()
	{	//returns true if player busts; otherwise
		// player is still in game
		System.out.println("Do you want a hit (y/n): ");
		String reply = input.next();
		while (reply.equalsIsIgnoreCase("y"));
		{
			player.hit(deck.draw());
			System.out.println(player.lastCard());
			if (player.getScore() > 21)
			{
				System.out.println("You busted!");
			}
		}


// I guess there is a } missing here to finish finishPlayerHand


		static void finishDealerHand()
		{
			System.out.prinln("\n" + "Dealer's hand is: \n" + dealer);
			while(dealer.getScore() < 17)
			{
				System.out.print("Dealer draws: ");
				dealer.hit(deck.draw());
				System.out.println(dealer.lastCard());
				if (dealer.getScore() > 21)
				{
					System.out.println("Dealer busted!");
					++playerwins;
				}
			}

		}

		static void wrapUpScoring()
		{
			if (player.getScore() > dealer.getScore())
			{	System.out.println("You Win!");
			}
			else if (player.getScore() < dealer.getScore())
			{	System.out.println("Dealer Wins!");
			}
			else if (player.getScore() == dealer.getScore())
			{	System.out.println("The Player and the Dealer have tied.");
			}		
		}
	}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1