Card.Java and BlackJack

I need some help with my coding

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 5462 Views - Last Post: 28 January 2009 - 09:26 AM Rate Topic: -----

#1 Cuzenu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-January 09

Card.Java and BlackJack

Post icon  Posted 14 January 2009 - 06:46 PM

/**
 * Cards.java stores the values of 52 playing cards.
 * 
 * @author Melissa Gendron
 * @version 1/14/09
 */

import java.util.Random;
public class Cards
{
	//************************************************************************
	// Create the enumerated type to determine the each card face
	//************************************************************************
	enum Card {ace, two, three, four, five, six, seven, eight, nine, ten, 
			   jack, queen, king, error}
	
	public static void main(String[] args)
	{
			//*********************************************************************
			//Initial variable declarations
			//*********************************************************************
			int card = 1;  //used to determine which card, 1-13, to draw
			int suit = 1; //used to determine which suit the card it
			Card c = Card.ace;  //creates an enum d of type Card and gives it the
								//initial value of "ace"
								
				//*****************************************************************
				//This switch sets the value of the enumerated type c showing
				//the face value of the card.
				//*****************************************************************
				switch(card)
				{
					case 1: c = Card.ace; break;  
					case 2: c = Card.two; break;
					case 3: c = Card.three; break;
					case 4: c = Card.four; break;
					case 5: c = Card.five; break;
					case 6: c = Card.six; break;
					case 7: c = Card.seven; break;
					case 8: c = Card.eight; break;
					case 9: c = Card.nine; break;
					case 10: c = Card.ten; break;
					case 11: c = Card.jack; break;
					case 12: c = Card.queen; break;
					case 13: c = Card.king; break;
					default: c = Card.error; break;
				}
			
				
				 //*****************************************************************
				//This switch sets the value of the enumerated type for printing
				//the chosen suit of the card.
				//*****************************************************************
				switch(suit)
				{   
					case 1: System.out.println(" of spades");
					case 2: System.out.println(" of clubs");
					case 3: System.out.println(" of hearts");
					case 4: System.out.println(" of diamonds");
					default: System.out.println(); break;
				}
	}
}




Hello I have question regarding a program I am supposed to create.
We are supposed to: Design and implement a class called Card that represents a standard playing card. Each card has a suit and a face value.
And I am supposed to create a driver program that plays blackjack with the user.

What I'm wondering is, how can I make the switch statments of the card's face and suit value, print as a random generated number without doubling up? (like ace of heart x2)

I have learned about enums as well as basic switch statements. I have not learned about arrays.

I have not started the blackjack runner because I'm not sure how to create a correct shuffle and deal method, or at least a dealing method that ensure no glitches (repeated cards).

I already forsee a problem with my ace value because in blackjack it can count as 1 or 11, so I'm not sure if a user accepted loop can boolean the value into one or the other according to what the user wants, or if the circumstances force it, such as having an Ace, Jack, and 3, so ace must be a 1 to continue.

Can anybody help me with a skeleton of example or what I need for my program?

I use BlueJ, and I have only used Java for 4 months.

This post has been edited by Cuzenu: 14 January 2009 - 06:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Card.Java and BlackJack

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Card.Java and BlackJack

Posted 14 January 2009 - 08:00 PM

You need a class Card not an enum card or you will switch() for ever
Something like:

class Card {
   int suit;
   int value;

   Card(int suit, int value) {
	  this.suit = suit;
	  this.value = value;
   }
}



Then you can have a class Deck

class Deck {
	Card[] card;
	
	Deck() {
	   card = new Card[52];
	   int k = 0;
	   for(int i = 0; i < 4; i++) {
		   for(int j = 1; j <= 13; j++) {
			   card[k++] = new Card(i, j);
		   }
	   }
	 }
}


This post has been edited by pbl: 14 January 2009 - 08:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 Cuzenu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-January 09

Re: Card.Java and BlackJack

Posted 14 January 2009 - 08:30 PM

I'm not sure what the "this." is, I have never seen that in my book.

I've decided a new approach, of using just the jack, queen, king, and ace as switch statements as well as the suits, but the numbers (2-10) will remain integers. I talked this over with a friend and she says I need to set my default (of the J, Q, K, A) to generate a random int, I'm guessing and checking but I am not sure how to write that specifically within my code.

Well I'm trying to change my code but I keep on getting a:

";" expected error message in regards to attempt to randomly generate the default

 /**
 * Cards.java stores the values of 52 playing cards.
 * 
 * @author Melissa Gendron
 * @version 1/14/09
 */

import java.util.Random;
public class Card02
{
	//************************************************************************
	// Create the enumerated type to determine the each card face
	//************************************************************************
	enum Face {jack, queen, king, ace, error}

	{
			//*********************************************************************
			//Initial variable declarations
			//*********************************************************************
			Random generator = new Random();
			int face = 1;  //used to determine which card, 1-13, to draw
			int suit = 1; //used to determine which suit the card it
			Face f = Face.ace;  //creates an enum c of type Card and gives it the
								//initial value of "ace"
			int faceValue;

								
				//*****************************************************************
				//This switch sets the value of the enumerated type c showing
				//the face value of the card.
				//*****************************************************************
				switch(faceValue)
				{
					case 11: f = Face.jack; break;
					case 12: f = Face.queen; break;
					case 13: f = Face.king; break;
					case 14: f =Face.ace; break;
					default: f = Random generator.nextInt(int 10) + 2; break;
				}
			
				
				 //*****************************************************************
				//This switch sets the value of the enumerated type for printing
				//the chosen suit of the card.
				//*****************************************************************
				switch(suit)
				{   
					case 1: System.out.println(" of spades");
					case 2: System.out.println(" of clubs");
					case 3: System.out.println(" of hearts");
					case 4: System.out.println(" of diamonds");
					default: System.out.println(); break;
				}
	}
}



Was This Post Helpful? 0
  • +
  • -

#4 xCraftyx  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 258
  • Joined: 13-September 08

Re: Card.Java and BlackJack

Posted 14 January 2009 - 08:32 PM

If you would like some different strategies for blackjack contact me over msn or aim (both can be found in my profile), i finished writing a blackjack program a couple of days ago and would be happy to help with the logic. I'm not too good at proofreading others' code yet :D

This post has been edited by xCraftyx: 14 January 2009 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Card.Java and BlackJack

Posted 14 January 2009 - 08:38 PM

Sorry I had a k++ missing
Here is an enhanced copy

class Card {
	private static final String[] suitName = {"Spade", "Diamond", "Heart", "Spade"};
	private static final String[] cardName = {"", "As", "Two", "Three", "Four", "Five", "Six", 
		"Seven", "Height", "Nine", "Ten", "Jack", "Queen", "King"};

	int suit;
	int value;

	Card(int suit, int value) {
		this.suit = suit;
		this.value = value;
	}

	public String toString() {
		return cardName[value] + " of " + suitName[suit];
	}

	public static void main(String[] arg) {
		Deck deck = new Deck();
		for(int j = 0; j < 10; j++) {
			deck.shuffle();
			System.out.println("Pick number #" + j);
			for(int i = 0; i < 10; i++)
				System.out.println("Card picked: " + deck.getCard());
		}
	}
}


import java.util.*;

class Deck {
	Card[] card;
	Random ran;
	ArrayList<Card> arrayList;
	
	Deck() {
	   ran = new Random();
	   arrayList = new ArrayList<Card>();
	   card = new Card[52];
	   int k = 0;
	   for(int i = 0; i < 4; i++) {
		   for(int j = 1; j <= 13; j++) {
			   card[k++] = new Card(i, j);
		   }
	   }
	}
	
	// shuffle reset the ArrayList
	void shuffle() {
		arrayList.clear();
		for(int i = 0; i < 52; i++)
			arrayList.add(card[i]);
	}
	
	// return randomly a card
	Card getCard() {
		return arrayList.remove(ran.nextInt(arrayList.size()));
	}
}



Which generates

Pick number #0
Card picked: Ten of Heart
Card picked: King of Diamond
Card picked: King of Heart
Card picked: Nine of Spade
Card picked: Jack of Spade
Card picked: Jack of Diamond
Card picked: Five of Diamond
Card picked: As of Spade
Card picked: Height of Diamond
Card picked: Six of Spade
Pick number #1
Card picked: Height of Diamond
Card picked: As of Diamond
Card picked: Jack of Spade
Card picked: Queen of Heart
Card picked: As of Spade
Card picked: Four of Heart
Card picked: Five of Diamond
Card picked: Height of Spade
Card picked: Jack of Diamond
Card picked: Nine of Spade
Pick number #2
Card picked: King of Diamond
Card picked: Four of Spade
Card picked: King of Heart
Card picked: As of Heart
Card picked: Five of Heart
Card picked: Nine of Diamond
Card picked: King of Spade
Card picked: Height of Heart
Card picked: Nine of Spade
Card picked: Seven of Spade
Pick number #3
Card picked: Queen of Spade
Card picked: Seven of Heart
Card picked: Ten of Spade
Card picked: Two of Diamond
Card picked: Six of Heart
Card picked: As of Spade
Card picked: Four of Spade
Card picked: Seven of Spade
Card picked: Ten of Heart
Card picked: Seven of Diamond
Pick number #4
Card picked: Queen of Spade
Card picked: Seven of Heart
Card picked: Queen of Heart
Card picked: Height of Diamond
Card picked: Height of Spade
Card picked: Two of Spade
Card picked: Six of Heart
Card picked: Six of Spade
Card picked: Seven of Spade
Card picked: King of Heart
Pick number #5
Card picked: Seven of Spade
Card picked: Queen of Diamond
Card picked: Five of Spade
Card picked: Six of Heart
Card picked: Queen of Spade
Card picked: Nine of Diamond
Card picked: Five of Heart
Card picked: Six of Spade
Card picked: King of Spade
Card picked: King of Diamond
Pick number #6
Card picked: Three of Diamond
Card picked: As of Spade
Card picked: King of Spade
Card picked: Four of Spade
Card picked: Nine of Spade
Card picked: Jack of Spade
Card picked: Height of Spade
Card picked: Ten of Spade
Card picked: Four of Spade
Card picked: Ten of Heart
Pick number #7
Card picked: Two of Heart
Card picked: Three of Heart
Card picked: Nine of Diamond
Card picked: Four of Heart
Card picked: Four of Spade
Card picked: Four of Spade
Card picked: Nine of Heart
Card picked: Six of Heart
Card picked: Three of Spade
Card picked: Queen of Spade
Pick number #8
Card picked: Five of Spade
Card picked: Four of Spade
Card picked: Ten of Spade
Card picked: Queen of Heart
Card picked: Jack of Diamond
Card picked: Queen of Spade
Card picked: Two of Spade
Card picked: Five of Spade
Card picked: King of Spade
Card picked: Six of Diamond
Pick number #9
Card picked: Ten of Diamond
Card picked: Queen of Diamond
Card picked: Six of Spade
Card picked: Four of Diamond
Card picked: Ten of Spade
Card picked: Three of Spade
Card picked: Ten of Heart
Card picked: Height of Spade
Card picked: Height of Diamond
Card picked: Six of Heart


Was This Post Helpful? 0
  • +
  • -

#6 Cuzenu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-January 09

Re: Card.Java and BlackJack

Posted 14 January 2009 - 09:11 PM

Well I've attempted a few other changed but my major problem is that when I added the "deal" method the header says "public" and my compiler says it is an illegal start of expression.

I'm sorry if some of my errors make you cringe, but my current teacher's motto about teaching (any class, AP CS included): "If I can do it, you can do it." Which I find completely wrong because I have not attended college and worked as a debugger for IBM for xxx#? of years.

My basic learning method is readings from the book and then writing specific programs and working on labs.

So basically my teacher does not offer any assistance to all of her (4) students, so I'm struggling with CS and losing interest in Java/CS at a very steep decline, not to say I'm still trying to figure it all out, but I have no one within my family who can help me, my best advise giver is a girl in my class who would do a better job at teaching than the current teachers.

Anyways, that explains my very "noob" coding, any hints are appreciated. I don't mind constructive criticism as long as there is a "way to improve" that could help make it better, sort of comment.

Back to my actual point and topic, here is the updated code (with a few minor errors).

/**
 * Cards.java stores the values of 52 playing cards.
 * 
 * @author Melissa Gendron
 * @version 1/14/09
 */

import java.util.Random;
public class Card02
{
	//************************************************************************
	// Create the enumerated type to determine the each card face
	//************************************************************************
	enum Face {jack, queen, king, ace, error}

	{
			//*********************************************************************
			//Initial variable declarations
			//*********************************************************************
			Random generator = new Random();
			Face f = Face.ace;  //creates an enum c of type Card and gives it the
								//initial value of "ace"
			int faceValue;
			int faceSuit;

								
				//*****************************************************************
				//This switch sets the value of the enumerated type c showing
				//the face value of the card.
				//*****************************************************************
				switch(faceValue)
				{
					case 11: f = Face.jack; break;
					case 12: f = Face.queen; break;
					case 13: f = Face.king; break;
					case 14: f =Face.ace; break;
					default: f = "What?"; break;
				}
			   
				
				
				 //*****************************************************************
				//This switch sets the value of the enumerated type for printing
				//the chosen suit of the card.
				//*****************************************************************
				switch(faceSuit)
				{   
					case 1: System.out.println(" of spades");
					case 2: System.out.println(" of clubs");
					case 3: System.out.println(" of hearts");
					case 4: System.out.println(" of diamonds");
					default: System.out.println(); break;
				}
				

				// Determine the card's face value with
				// randomly generated int parameters
				
				public int deal()
				{
					faceValue = generator.nextInt(faceValue) + 1;
					faceSuit = generator.nextInt(faceSuit) + 1;
					
					result = System.out.println (+faceValue + faceSuite);
					return result;
	}							
  }
}




Thanks for helping me so far, but I still have not learned the "this" object declaration thing, so it will not be acceptable in my program.

Here is a link to my actual book PDF (only up to Chapter 4, but I am on Chapter 4) the files are offered as a demo of the full book, so it's free and legal. It might help point out to my current limitations.
Link: Book PDF Demo Link
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:03 PM

This a method in the middle of nowhere
You need a } to close an balance your previous {}
	}
	// Determine the card's face value with
	// randomly generated int parameters

	public int deal()
	{
		faceValue = generator.nextInt(faceValue) + 1;
		faceSuit = generator.nextInt(faceSuit) + 1;

		result = System.out.println (+faceValue + faceSuite);
		return result;
	}							
}



result is not define
System.out.println() returns void you cannot assign its returned value to something. It is:
- System.out.pritnln()
not
- abc = System.out.println();

You have a ; missing after your enum

{
			//*********************************************************************
			//Initial variable declarations
			//*********************************************************************
			Random generator = new Random();


This is the beginning of a method... it needs a type an a name like
public static void main(String[] arg)
{
			//*********************************************************************
			//Initial variable declarations
			//*********************************************************************
			Random generator = new Random();


Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:51 PM

I had to do a blackjack program for last semester. I have released the code for anyone to look as a learning tool. See here

Let me know if you have any questions or you want to tell me how bad my code is :)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:54 PM

View PostKYA, on 14 Jan, 2009 - 09:51 PM, said:

I had to do a blackjack program for last semester. I have released the code for anyone to look as a learning tool. See here

Let me know if you have any questions or you want to tell me how bad my code is :)

KYA my good friend,

don't challenge me :D
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:55 PM

Uh oh :o

Be merciful pbl! I beseech thee!
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:56 PM

View PostKYA, on 14 Jan, 2009 - 09:51 PM, said:

I had to do a blackjack program for last semester. I have released the code for anyone to look as a learning tool. See here

Let me know if you have any questions or you want to tell me how bad my code is :)

Actually your "signature" is quite impressive
How did you manage that ?
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:57 PM

It's a widget from a site that just reflects your IP address information back at you. People can only see their own info.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:59 PM

View PostKYA, on 14 Jan, 2009 - 09:51 PM, said:

I had to do a blackjack program for last semester. I have released the code for anyone to look as a learning tool. See here

Let me know if you have any questions or you want to tell me how bad my code is :)

Hey !!
Your site requirres a username and a password... if not NO NO
I have a site with a lot of free Gigabytes if you want to post stuff
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Card.Java and BlackJack

Posted 14 January 2009 - 11:01 PM

Whoa what? I'll just upload it here.


edit file uploaded

Attached File(s)


This post has been edited by KYA: 14 January 2009 - 11:01 PM

Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Card.Java and BlackJack

Posted 14 January 2009 - 11:10 PM

View PostKYA, on 14 Jan, 2009 - 10:01 PM, said:

Whoa what? I'll just upload it here.


edit file uploaded


My WinZip says "not a valid archived file"
are you trying to send me a virus ? :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2