Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Mike Amling Newsgroups: comp.lang.java.programmer Subject: Re: Help with Deck of cards assignment and getting it to deal... Date: Sun, 03 Aug 2014 16:05:20 -0500 Lines: 41 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net Sx1YGk2G8Axd9GzE++miWQslmSose+WbWfG2PPTUr5LgqUb2+u Cancel-Lock: sha1:SuO9kWbwhYLKkocE9c/MRE3Sd/k= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:29540 On 8/3/14 9:23 AM, Eric Sosman wrote: > public class DeckOfCards { > private final Card[] cards; > private int cardCount; > > public DeckOfCards() { > cards = new Card[52]; > // ... fill the array with Cards ... > cardCount = 52; > } > > public Card dealOneCard() { > // Sanity check: > if (cardCount <= 0) { > throw new IllegalStateException("no cards left!"); > } > > // Choose the card to deal: > int which = (int) (cardCount * Math.random()); > Card dealt = cards[which]; > > // Remove the dealt card from the deck by moving > // the last card into the vacated place (this works > // even when dealing the last card): > cards[which] = cards[cardCount - 1]; > > // There's one less card than there used to be: > --cardCount; > > return dealt; > } > > // ... other methods ... > } So, the shuffle method is superfluous. Rather than myDeck.shuffle(), one would code myDeck=new DeckOfCards()? Not bad. --Mike Amling R2VuZXJhbGl6YWJsZSB0byBkcmF3aW5nIGZyb20gdXJucy4=