Skip to content

Commit

Permalink
WIP: Card and Deck ADTs
Browse files Browse the repository at this point in the history
  • Loading branch information
yichen0104 committed Aug 5, 2020
1 parent a76bbe4 commit 24f1d66
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/csci4963u20/project/doudizhu/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@ public class Application {

public static void main(String[] args){
System.out.println("Welcome to Doudizhu!");
Deck d = new Deck(10);
d.add(new Card(14, "A"));
d.add(new Card(15, "2"));
d.add(new Card(16, "Joker"));
d.add(new Card(4, "4"));
d.add(new Card(5, "5"));
d.add(new Card(11, "J"));
d.add(new Card(12, "Q"));
d.add(new Card(13, "K"));
d.add(new Card(13, "K"));
d.add(new Card(11, "J"));

d.sortDeck();

d.printDeck();
}
}
42 changes: 42 additions & 0 deletions src/csci4963u20/project/doudizhu/Deck.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,55 @@ public class Deck {
private ArrayList<Card> cards;
private int card_count;

/**
* Default empty constructor.
*/
public Deck(){
cards = new ArrayList<>();
card_count = 0;
}

/**
* Constructor from deck size.
* @param size deck size
*/
public Deck(int size){
card_count = size;
cards = new ArrayList<>(card_count);
}

/**
* Add a Card to this Deck.
* @param c Card to be added
* @return boolean if Card is added successfully
*/
public boolean add(Card c){
boolean ret = cards.add(c);
if(ret){ card_count++; }
return ret;
}


/**
* Shuffle the deck.
*/
public void shuffle(){
Collections.shuffle(cards);
}

/**
* Sort the deck.
*/
public void sortDeck(){
Collections.sort(cards);
}

/**
* Print the deck.
*/
public void printDeck(){
for(Card c:cards){
System.out.println(c.getName());
}
}
}

0 comments on commit 24f1d66

Please sign in to comment.