Advertisement
gdog2u

CardTrials

Jul 31st, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. package tests;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. public class CardTrial {
  7.    
  8.     public static class Card{
  9.        
  10.         static Suit suit;
  11.         static String name;
  12.         static int num;
  13.         boolean isFace;
  14.        
  15.         public Card(){
  16.             suit = Suit.HEARTS;
  17.             num = 1;
  18.             name = "1";
  19.             isFace = false;
  20.         }
  21.        
  22.         public Card(int s, int n){
  23.             setSuit(s);
  24.             setNum(n);
  25.         }
  26.        
  27.         public Card(Suit s, int n){
  28.             setSuit(s);
  29.             setNum(n);
  30.         }
  31.         //I tried my best at the enum thing, since we never covered it in class
  32.         public enum Suit{
  33.             HEARTS,DIAMONDS,CLUBS,SPADES;
  34.         }
  35.        
  36.         public Suit getSuit(){
  37.             return suit;
  38.         }
  39.        
  40.         public static int getNum(){
  41.             return num;
  42.         }
  43.        
  44.         public String getName(){
  45.             return name;
  46.         }
  47.  
  48.         public void setSuit(Suit s){
  49.             suit = s;
  50.         }
  51.        
  52.         public void setSuit(int s){
  53.             if(s == 1){
  54.                 suit = Suit.HEARTS;
  55.             }
  56.             if(s == 2){
  57.                 suit = Suit.DIAMONDS;
  58.             }
  59.             if(s == 3){
  60.                 suit = Suit.CLUBS;
  61.             }
  62.             if(s == 4){
  63.                 suit = Suit.SPADES;
  64.             }
  65.         }
  66.        
  67.         public void setNum(int in){ //decide if card is face card or not
  68.             if(in == 11){
  69.                 num = in;
  70.                 name = "Jack";
  71.                 isFace = true;
  72.             }
  73.             if(in == 12){
  74.                 num = in;
  75.                 name = "Queen";
  76.                 isFace = true;
  77.             }
  78.             if(in == 13){
  79.                 num = in;
  80.                 name = "King";
  81.                 isFace = true;
  82.             }
  83.             if(in == 14){
  84.                 num = in;
  85.                 name = "Ace";
  86.                 isFace = true;
  87.             }
  88.             else if(in <= 10 && in >= 2){
  89.                 num = in;
  90.                 name = Integer.toString(in);
  91.                 isFace = false;
  92.             }
  93.         }
  94.        
  95.         public String toString(){
  96.             return getName() + ":" + getSuit();
  97.         }
  98.        
  99.         public static ArrayList<Card> createDeck(){
  100.             ArrayList<Card> deck = new ArrayList<Card>();
  101.             /**
  102.              * my first attempt at mimicking what you sent
  103.              * Suit[] suit = new Suit[]{Suit.HEARTS,Suit.DIAMONDS,Suit.CLUBS,Suit.SPADES};
  104.             for(int i=0;i<14;i++){
  105.                 for(int j=0;j<suit.length;j++){
  106.                     deck.add(new Card(suit[j],i+2));
  107.                 }
  108.             }
  109.              */
  110.             //directly pasting what you sent, save for the foreach becuase it kept giving a ton of errors
  111.             Suit[] suit = new Suit[]{Suit.HEARTS,Suit.DIAMONDS, Suit.CLUBS, Suit.SPADES};
  112.             for(int i=0;i<14;i++){
  113.                 for(int j=0;j<suit.length;j++){
  114.                     Card card = new Card(suit[j],i+2);
  115.                     //if you do System.out.println(card.toString()); here, it prints normally as you would expect
  116.                     deck.add(card);
  117.                 }
  118.             }
  119.            
  120.             /**
  121.              *My original method
  122.              * int j = 0;
  123.             int k = 0;
  124.             int l = 0;
  125.             for(int i = 0;i < 14;i++){
  126.                 for(;j<(k+4) && j < 52;j++){
  127.                     deck.add(new Card((l+1),(int)(Math.floor(j/4)+2)));
  128.                     //deck.get(i).setSuit((l+1));
  129.                     //deck.get(i).setNum((int)(Math.floor(j/4)+2));
  130.                     //System.out.println((j+1) + ":" + deck.get(i).toString());
  131.                     l++;
  132.                 }
  133.                 l = 0;
  134.                 k = j;
  135.             }
  136.              */
  137.             //deck = shuffle(deck);
  138.             for(int m = 0; m <deck.size();m++){
  139.                 System.out.println((m+1) + ":" + deck.get(m).toString()); //when printing from here is where it returns the ace of spades a bazillion times
  140.             }
  141.             return deck;
  142.         }
  143.  
  144.         public static ArrayList<Card> shuffle(ArrayList<Card> in){ //method for shuffling the deck
  145.             ArrayList<Card> out = new ArrayList<Card>();
  146.             ArrayList<Integer> used = new ArrayList<Integer>();
  147.             int usedInt;
  148.             Random r = new Random();
  149.             for(int i = 0; i < in.size(); i++){
  150.                 usedInt = r.nextInt(in.size());
  151.                 while(used.contains(usedInt)){
  152.                     usedInt = r.nextInt(in.size());
  153.                 }
  154.                 out.add(in.get(usedInt));
  155.                 used.add(usedInt);
  156.             }
  157.             return out;
  158.         }
  159.     }
  160.    
  161.     public static void main(String[] args){
  162.         ArrayList<Card> deck = Card.createDeck(); //still don't know about this
  163.         for(int i = 0; i < 52;i++){
  164.             //System.out.println(deck.get(i).toString());// + ":" + deck.get(i).getSuit());
  165.             //System.out.println(Math.floor(i/4)+2);
  166.         }
  167.     }
  168.    
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement