Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tests;
- import java.util.ArrayList;
- import java.util.Random;
- public class CardTrial {
- public static class Card{
- static Suit suit;
- static String name;
- static int num;
- boolean isFace;
- public Card(){
- suit = Suit.HEARTS;
- num = 1;
- name = "1";
- isFace = false;
- }
- public Card(int s, int n){
- setSuit(s);
- setNum(n);
- }
- public Card(Suit s, int n){
- setSuit(s);
- setNum(n);
- }
- //I tried my best at the enum thing, since we never covered it in class
- public enum Suit{
- HEARTS,DIAMONDS,CLUBS,SPADES;
- }
- public Suit getSuit(){
- return suit;
- }
- public static int getNum(){
- return num;
- }
- public String getName(){
- return name;
- }
- public void setSuit(Suit s){
- suit = s;
- }
- public void setSuit(int s){
- if(s == 1){
- suit = Suit.HEARTS;
- }
- if(s == 2){
- suit = Suit.DIAMONDS;
- }
- if(s == 3){
- suit = Suit.CLUBS;
- }
- if(s == 4){
- suit = Suit.SPADES;
- }
- }
- public void setNum(int in){ //decide if card is face card or not
- if(in == 11){
- num = in;
- name = "Jack";
- isFace = true;
- }
- if(in == 12){
- num = in;
- name = "Queen";
- isFace = true;
- }
- if(in == 13){
- num = in;
- name = "King";
- isFace = true;
- }
- if(in == 14){
- num = in;
- name = "Ace";
- isFace = true;
- }
- else if(in <= 10 && in >= 2){
- num = in;
- name = Integer.toString(in);
- isFace = false;
- }
- }
- public String toString(){
- return getName() + ":" + getSuit();
- }
- public static ArrayList<Card> createDeck(){
- ArrayList<Card> deck = new ArrayList<Card>();
- /**
- * my first attempt at mimicking what you sent
- * Suit[] suit = new Suit[]{Suit.HEARTS,Suit.DIAMONDS,Suit.CLUBS,Suit.SPADES};
- for(int i=0;i<14;i++){
- for(int j=0;j<suit.length;j++){
- deck.add(new Card(suit[j],i+2));
- }
- }
- */
- //directly pasting what you sent, save for the foreach becuase it kept giving a ton of errors
- Suit[] suit = new Suit[]{Suit.HEARTS,Suit.DIAMONDS, Suit.CLUBS, Suit.SPADES};
- for(int i=0;i<14;i++){
- for(int j=0;j<suit.length;j++){
- Card card = new Card(suit[j],i+2);
- //if you do System.out.println(card.toString()); here, it prints normally as you would expect
- deck.add(card);
- }
- }
- /**
- *My original method
- * int j = 0;
- int k = 0;
- int l = 0;
- for(int i = 0;i < 14;i++){
- for(;j<(k+4) && j < 52;j++){
- deck.add(new Card((l+1),(int)(Math.floor(j/4)+2)));
- //deck.get(i).setSuit((l+1));
- //deck.get(i).setNum((int)(Math.floor(j/4)+2));
- //System.out.println((j+1) + ":" + deck.get(i).toString());
- l++;
- }
- l = 0;
- k = j;
- }
- */
- //deck = shuffle(deck);
- for(int m = 0; m <deck.size();m++){
- System.out.println((m+1) + ":" + deck.get(m).toString()); //when printing from here is where it returns the ace of spades a bazillion times
- }
- return deck;
- }
- public static ArrayList<Card> shuffle(ArrayList<Card> in){ //method for shuffling the deck
- ArrayList<Card> out = new ArrayList<Card>();
- ArrayList<Integer> used = new ArrayList<Integer>();
- int usedInt;
- Random r = new Random();
- for(int i = 0; i < in.size(); i++){
- usedInt = r.nextInt(in.size());
- while(used.contains(usedInt)){
- usedInt = r.nextInt(in.size());
- }
- out.add(in.get(usedInt));
- used.add(usedInt);
- }
- return out;
- }
- }
- public static void main(String[] args){
- ArrayList<Card> deck = Card.createDeck(); //still don't know about this
- for(int i = 0; i < 52;i++){
- //System.out.println(deck.get(i).toString());// + ":" + deck.get(i).getSuit());
- //System.out.println(Math.floor(i/4)+2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement