d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Blackjack / Card Program > Need Help With 1 Part Pls
123Next
Add Reply New Topic New Poll
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jan 31 2013 02:41pm
So I've written code for some of a blackjack game - this is an assignment and we're not required to do everything in blackjack.

Anyways, I'm having trouble finding out how to take a card and put it into a hand.

Card is in the class Dealer, and it is a string that ends up saying - rank + " of " + suit.
playerHand is in the Player class, and dealerHand is in the Dealer class.

So the big question is: how do i send a card from the Dealer class into a playerHand (which is a list of all cards in the hand).

Thanks so much for help, fg is donated for the effort :)
Member
Posts: 4,980
Joined: Jan 16 2010
Gold: 0.00
Warn: 20%
Jan 31 2013 02:53pm
Assuming playerHand is a datastructure of some sort, you could add a dealCard method to your Dealer class, which - given a Player object - adds a card to the Player object's playerHand.

Personally, I would probably want Card to be a class of its own too.
Member
Posts: 27,514
Joined: Feb 12 2006
Gold: 8,035.01
Jan 31 2013 03:01pm
check my PM

here is also a BlackJack program in java:
http://introcs.cs.princeton.edu/java/36inheritance/BlackJack.java.html
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Jan 31 2013 03:04pm
http://code.google.com/p/cisc370-finalproject/source/browse/game_server/src/blackjack/server/Dealer.java

Line 21 of my program.

Obviously you won't be able to just copy/paste, but you can at learn from it.

This post was edited by DirtyRasa on Jan 31 2013 03:04pm
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jan 31 2013 03:45pm
I think I need help reorganizing the code atm now heh.

i have a class for card - has getter and setter for rank and suit
inside dealer class (is way to much that needs to be put somewhere) - initializing the deck, shuffling deck, using setter & getter for card picked, then handing 2 cards to player & 2 cards to dealer.

although I haven't put anything in yet to hand 2 cards to player or dealer, and I would rather do one card at a time in case cards need to be added to the hand.

class for player, which nothing is there atm, but i need a way to hold the 'hand' information..

empty main BlackJack class that has the public static void main(String[] args) in it.

would really like some advice on how to clean it up, etc.....please be gentle - novice here.
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jan 31 2013 03:57pm
Grandebedte: ur inbox is full -

am i able to msg you on skype or anything so you can see the code and talk real-time instead of posting? it would be much appreciated.
Member
Posts: 4,980
Joined: Jan 16 2010
Gold: 0.00
Warn: 20%
Jan 31 2013 04:03pm
Quote (shinigamiapple777 @ 31 Jan 2013 23:45)
I think I need help reorganizing the code atm now heh.

i have a class for card - has getter and setter for rank and suit
inside dealer class (is way to much that needs to be put somewhere) - initializing the deck, shuffling deck, using setter & getter for card picked, then handing 2 cards to player & 2 cards to dealer.

although I haven't put anything in yet to hand 2 cards to player or dealer, and I would rather do one card at a time in case cards need to be added to the hand.

class for player, which nothing is there atm, but i need a way to hold the 'hand' information..

empty main BlackJack class that has the  public static void main(String[] args) in it.

would really like some advice on how to clean it up, etc.....please be gentle - novice here.


Okay. Here's my input in regards to distributing roles, and the classes I'd make:

- A Card class, which handles the way cards are initialized separately.
- A Deck class, containing the cards currently in the game.
- A Player class. This class has some data structure holding the cards in the player's hand, and will also be able to either hit or stay.
- A Dealer class. This class will be dealing cards to players from a Deck, exhausting the cards as he goes. I see no reason to use a polymorphic approach here, which I'd consider poor Java architecture compared to a compositional design anyways.
- A Game class. This is your skeleton class, passing turns, checking who won, setting up new rounds, all by calling appropriate methods in the respective classes.

The main idea with classes, is to delegate responsibility and roles. You need to have specific information about both individual cards, as well as an entire deck, but the two are different roles held by different actors; two classes make sure you are able to work on both things separately. The dealer should not have the responsibility to initialize the deck; he should merely deal it.

I don't know what the focus of your course and assignment is, though, so the answers may differ a little depending on that.
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Feb 1 2013 07:32am
You should really take a look at the source code I posted earlier and analyze how I structured it.
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Feb 1 2013 11:35am
need some help with the skeleton class of the code...I -believe- everything else should work (for what i was assigned). If I can get some more help, that'd be great :). also want to say thanks to Grandebedte for the help so far.

package object.blackjack;

public class BlackJack {


public static void main(String[] args) {
Dealer test1 = new Dealer();
test1.deal();
}
}

^Dealer test1 = new Dealer(); comes up as an error : cannot find symbol - Dealer

Member
Posts: 2,579
Joined: Jun 1 2012
Gold: 1,524.00
Feb 1 2013 02:12pm
Unless the Dealer and BlackJack classes are in the same directory/package, you must import the Dealer class after the package declaration in the BlackJack class.
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll