Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
2102-Group-Project-Spring-2017/src/model/CardStack.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
72 lines (59 sloc)
1.23 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.Random; | |
import java.util.Stack; | |
/** | |
* This class extends Java's linked list but had added methods to make interacting with the card stack much easier | |
* @author Joe | |
*/ | |
public class CardStack extends LinkedList<Card> | |
{ | |
private static final long serialVersionUID = 1L; | |
private int _size; | |
public CardStack() | |
{ | |
super(); | |
_size = 0; | |
} | |
@Override | |
public void push(Card c) | |
{ | |
_size++; | |
super.addFirst(c); | |
} | |
@Override | |
public Card pop() | |
{ | |
Card c = super.removeFirst(); | |
_size--; | |
super.addLast(c); | |
return c; | |
} | |
public Card pickCard() | |
{ | |
return this.pop(); | |
} | |
public int getSize() | |
{ | |
return _size; | |
} | |
// Randomizes the stack with the cards in the array argument | |
public void randomizeCards(Card[] cardArray) | |
{ | |
// CardStack cardStack = new CardStack(); | |
ArrayList<Card> cardList = new ArrayList<Card>(); | |
Random r = new Random(); | |
// Add all array contents to list | |
for(Card c : cardArray) | |
{ | |
cardList.add(c); | |
} | |
// Pushes a card at a random index in the card list | |
int initialLength = cardList.size(); | |
for(int i=0; i < initialLength; i++) | |
{ | |
push(cardList.remove(r.nextInt(cardList.size()))); | |
} | |
} | |
} |