Advertisement
StefanTobler

T2 Assignment 5 Deck

Mar 16th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. /*
  2.  * AP CS MOOC
  3.  * Term 2 - Assignment 5: Shuffle
  4.  * A class which represents a Deck of cards. For this assignment, you will need to implement the method shuffleDeck, which appears at the bottom of this class.
  5.  */
  6.  
  7. import java.util.ArrayList;
  8.  
  9. public class Deck
  10. {
  11.      private ArrayList<Card> deck;
  12.  
  13.      
  14.      public Deck ()
  15.      {
  16.           deck = new ArrayList <Card>();
  17.           deck = initDeck();
  18.           deck = shuffleDeck();
  19.           System.out.println(deck);
  20.  
  21.      }
  22.      
  23.      public String toString ()
  24.      {
  25.           String temp = "";
  26.          
  27.           for (Card c: deck)
  28.           {
  29.                temp += c.toString() + "\n";
  30.           }
  31.          
  32.           return temp;
  33.      }
  34.  
  35.      public Card getTopCard ()
  36.      {
  37.           Card c = deck.get(0);
  38.           deck.remove(0);
  39.  
  40.      
  41.           return c;
  42.      }
  43.      
  44.      public static ArrayList <Card> initDeck ()
  45. {
  46.      ArrayList <String> ranks = new ArrayList <String> ();
  47.      
  48.      ranks.add ("ace");
  49.      
  50.      ranks.add ("two");
  51.      ranks.add ("three");
  52.      ranks.add ("four");
  53.      ranks.add ("five");
  54.      ranks.add ("six");
  55.      ranks.add ("seven");
  56.      ranks.add ("eight");
  57.      ranks.add ("nine");
  58.      ranks.add ("ten");
  59.      ranks.add ("jack");
  60.      ranks.add ("queen");
  61.      ranks.add ("king");
  62.      
  63.      ArrayList <String> suites = new ArrayList <String> ();
  64.      suites.add("clubs");
  65.      suites.add("diamonds");
  66.      suites.add("hearts");
  67.      suites.add("spades");
  68.      
  69.      ArrayList <Card> deck = new ArrayList <Card> ();
  70.      for (String s : suites)
  71.      {
  72.           int p = 1;
  73.           for (String r: ranks)
  74.           {
  75.                Card c = new Card (r, s, p);
  76.                p++;
  77.                deck.add(c);
  78.           }
  79.      }
  80.      return deck;
  81. }
  82.  
  83.      //SHUFFLE ****************************
  84.      
  85.      
  86.      public  ArrayList <Card> shuffleDeck ()
  87.      {
  88.           int random;
  89.           int size = deck.size();
  90.          
  91.           ArrayList <Card> t = new ArrayList <Card> ();
  92.           for (int i=0; i<size; i++) {
  93.                random=(int)(Math.random()*deck.size());
  94.                t.add(deck.get(random));
  95.                deck.remove(random);
  96.           }
  97.           return t;
  98.          
  99.          
  100.          
  101.          
  102.      }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement