Difference between revisions of "CISC181 S2017 DeckClass"
From class_wiki
(Created page with " <nowiki> package cisc181.mylab_4; // Christopher Rasmussen // CISC181, University of Delaware // March, 2015 import java.util.ArrayList; import java.util.Collections;...") |
|||
Line 1: | Line 1: | ||
<nowiki> | <nowiki> | ||
− | package cisc181. | + | package cisc181.mylab_0; |
// Christopher Rasmussen | // Christopher Rasmussen |
Latest revision as of 00:27, 16 March 2017
package cisc181.mylab_0; // Christopher Rasmussen // CISC181, University of Delaware // March, 2015 import java.util.ArrayList; import java.util.Collections; import java.util.Random; public class Deck { ArrayList<Card> myDeck; ArrayList<ArrayList<Card>> myHands; Random rand; // default constructor Deck() { myDeck = new ArrayList<Card>(); // initialize empty hands as ArrayList of empty ArrayLists of Cards. // at this point the number of hands has not been determined myHands = new ArrayList<ArrayList<Card>>(); // fill deck in order for (Suit s : Suit.values()) { for (Rank r : Rank.values()) { myDeck.add(new Card(r, s)); } } // prepare for random operations rand = new Random(); //Collections.shuffle(myDeck); } // print every card in the deck void print() { for (Card c : myDeck) { System.out.println(c); } System.out.println(""); } // creates empty hands and deals deck to them void deal(int numHands) { print(); for (int i = 0; i < numHands; i++) { myHands.add(new ArrayList<Card>()); } // deal randomly from deck to hands until deck is gone while (!myDeck.isEmpty()) { for (int i = 0; i < myHands.size(); i++) { int indexOfCardToDeal = rand.nextInt(myDeck.size()); Card c = myDeck.remove(indexOfCardToDeal); myHands.get(i).add(c); } } // print hands...the clunky way /* for (int i = 0; i < myHands.size(); i++) { System.out.println("Hand " + i); for (int j = 0; j < myHands.get(i).size(); j++) { System.out.println(myHands.get(i).get(j)); } System.out.println(""); } */ // print hands...like a boss for (ArrayList<Card> hand : myHands) { System.out.println("Hand " + myHands.indexOf(hand)); for (Card c : hand) { System.out.println(c); } System.out.println(); } } }