SharkBait Posted June 27, 2007 Share Posted June 27, 2007 I am wanting to create a little blackjack game (text for now) for fun but I can't seem to figure out how I should store the cards. Total of 52 cards (right? can't remember lol) Values: 2 through 10, Jack, Queen, King and Ace 4 Suites: Club, Diamond, Hearts and Spades I will also want to do 7 decks but for now I think I'll stick to working out with one. I am also not sure how I would handle the values of the face cards (which of course are 10) and the ace being either 1 or 11. Would a multi-dimension array work? $cards = array("S" => array(2, 3, 4, 5....), "C" => array(2, 3, 4, 5)); Quote Link to comment https://forums.phpfreaks.com/topic/57344-blackjack/ Share on other sites More sharing options...
448191 Posted June 27, 2007 Share Posted June 27, 2007 I don't fully recall how the game is played, but here's how I would go about this: Have a Deck object, 2 Hand objects, 2 Player objects, Card objects (have the Deck create those on demand--you don't want 52 objects when you might only need 15 or so) a GamePlay object and a Table object. GamePlay Player object calculates the maximum score of a Player based on it's Hand and the Table. For an ace, it assumes 11, if the total score's over 21, it recalculates using 1. As for generating Card objects, an array is a good source. The Deck can hold an array of Card-id's. array('S_J' = >array('suite' => 'Spades', 'value' = > 'J'), 'C_10' => array('suite' => 'Club', 'value' = > '10'), etc... That way it can easily shuffle a deck, and keep track of Cards dealt. Remember, the actual scores they represent is encapsulated by the GamePlay object. That way, if you would want to change the scoring, you only change the GamePlay object. Quote Link to comment https://forums.phpfreaks.com/topic/57344-blackjack/#findComment-283798 Share on other sites More sharing options...
SharkBait Posted June 27, 2007 Author Share Posted June 27, 2007 This seems more difficult than I thought it might be hehe. I'll try your approach and see what I can come up with. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/57344-blackjack/#findComment-284180 Share on other sites More sharing options...
448191 Posted June 28, 2007 Share Posted June 28, 2007 It shouldn't be that difficult. The hard part will be the AI part. I've included a basic UML diagram to illustrate the proposed structure. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/57344-blackjack/#findComment-284648 Share on other sites More sharing options...
Buyocat Posted June 28, 2007 Share Posted June 28, 2007 It's not clear why the hand class is needed? What's the benefit delegating some of the player's responsibility to a hand class? Likewise the table may not be necessary, but I could see that being potentially useful as a database of objects "in the game" which could be interpreted by the AI. Quote Link to comment https://forums.phpfreaks.com/topic/57344-blackjack/#findComment-284660 Share on other sites More sharing options...
ReDucTor Posted June 28, 2007 Share Posted June 28, 2007 I would just make an associative array, key's being card name, value being points. $deck = array( 'S2' => 2, 'S3' => 3, ... 'DK' => 10); // King of Diamond Then make use of functions array_shuffle() and array_pop/array_shift Quote Link to comment https://forums.phpfreaks.com/topic/57344-blackjack/#findComment-284793 Share on other sites More sharing options...
448191 Posted June 28, 2007 Share Posted June 28, 2007 When dealing with business logic I tend to respect the general semantics of the domain. It ain't foolproof, but it can save you a lot of trouble at very little cost. IMO: design for the domain first--other considerations (like performance) come later. Quote Link to comment https://forums.phpfreaks.com/topic/57344-blackjack/#findComment-285127 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.