Wednesday 15 August 2012

arrays - Having an issue in determining which class to carry out method in java code for Blackjack game. Very basic, new to programming -



arrays - Having an issue in determining which class to carry out method in java code for Blackjack game. Very basic, new to programming -

i'm attempting create simple blackjack game written in java , consisting of blackjack (tester), card, deck, dealer, player, , game class.

my code still much incomplete, i'm having difficulty determining, 1 time i've created deck of cards, class should dealing cards in , how should storing card objects in both player , dealer's hand (arraylists).

for example, thought solve issue using hand.add(deck.draw()); player class, thereby adding card drawn array cards arraylist hand. however, in order have create deck object in player class, different deck object create in game class. however, if draw card in game class using deck.draw(), i'm unsure of how take value , store in arraylist hand within player class.

hopefully making sense!

essentially, question if utilize deck.draw method in game class returns card, how take card , store in private arraylist hand in player class? or wrong approach entirely?

thank guidance can provide!!!

first, seeing code before edit, wanted point out think you're doing pretty new developer,keep @ it!

i think should maintain next path you're on...you've used domain language name you're model (deck, card, dealer, etc) , you've got start in naming behavior of classes based on model (draw). stick this...

anyway, hope helps below...it's pseudo-code (in case, way of saying didn't write on ide , didn't validate code...but should quite close) =)

dealer should manage deck, there should aggregate relationship there. since dealer has instance of deck (the deck backed array , position recorded int, no need collection type) length of 52). on construction, 52 card instances added array. deck indeed had draw, increments card position , returns card.

//this interface create sense little later public interface handmanager { card draw(); } public class dealer implements handmanager { private card[] deck; private int position; @override public card draw () { seek { homecoming deck[position]; } grab (arrayoutofboundsexception ex) { //handle when run out of cards in deck...do add together 52? } { position++; } } }

the dealer has draw method, takes vararg on players (and deals initial hand players).

//also in dealer public void deal (player... players) { (player p : players) { hand hand = initializehand(); //we'll in bit p.addhand(hand); } }

meanwhile, player aggregates hand....they own , decide want it. hand shouldn't simple array because should able grow, list or set should (a set more appropriate in theory...but because don't need enforce uniqueness here, there isn't need overhead really). but...to future proof it, can alter set later if want, should programme collection interface (you should seek programme against highest super class if can, improve code maintenance).

public class player { private hand hand; //can named "sethand" if want back upwards 1 hand per player, named //like because might fun exercise enable n hands per player later public void addhand (hand hand) { this.hand = hand; } }

public interface hand {

/** * returns cards in hand. * * changes returned array <b>not</b> reflected in hand. */ public card[] getcards(); public void hit(); public void split(); public void fold();

}

anyway, player acts on hand, decides hit, split, fold, etc. neat thing here that, in theory, player can mange n hands way ;) anyway...here magic comes in maintain code cleaner: 'communication' between player , dealer happen through hand instance. allows player phone call shots, more importantly, allows applications orchestration focus not on passing commands player dealer , card dealer player...but aspect needs to: managing turn based play , knowing when end game.

public class managedhand implements hand { private collection<card> cards; private handmanager manager; public hand (handmanager manager, card[] cards) { this.manager = manager; cards = arrays.aslist(cards); //to create set, pass hashset constructor =) } /** * returns cards in hand. * * changes returned array <b>not</b> reflected in hand. */ public card[] getcards() { //bonus lesson: "defensive copy" homecoming cards.toarray(new card[cards.size()]); } @override public void hit() { if (isbust()) { throw new bustexception("need wait next hand!"); } //***right here, dealer (the handmanager) gives card on draw request**** cards.add(manager.draw()); } @override public void split() { if (isbust()) { throw new bustexception("need wait next hand!"); } //do split...beyond demo code scope =) } @override public void fold() { //clean up, take money/car/happiness } @override public string tostring() { //pretty-print current hand } } //the initializehand method dealer promised public hand initializehand() { card[] initialhand = new card[init_hand_size]; (int =0; i<init_hand_size; i++) { initialhand[i] = draw(); } //****dealer registers handmanager hand********** homecoming new managedhand(this, initialhand); }

since dealer instantiates (creates) hand, had chance register listener (itself, in case) player actions on hand. since player given instance of hand, take action on hand...the hand validation (ie checks if isn't bust before requesting more cards) in implementation (just because more complex illustration have dealer it) , manager "notified" when more cards needed.

side note: have been built out little more, in general, kinda-sorta follows called observer pattern. might not perfect match pattern...but should check out...and read design patterns. learning these in career, you'll become rockstar.

java arrays class methods arraylist

No comments:

Post a Comment