/
Deck.java
131 lines (115 loc) · 3.67 KB
/
Deck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.awt.Graphics;
import java.util.ArrayList;
public class Deck
{
protected ArrayList<Card> deckOfCards; //initialize arraylist of cards type Card
public Deck() //empty constructor
{
deckOfCards = new ArrayList<Card>();
}
public Deck(boolean initialize) //adds 52 cards if true
{
this();
if (initialize)
{
for (int i = 0; i < 52; i++)
deckOfCards.add(new Card(i));
}
else
deckOfCards.clear();
}
public void add(Card newCard) //adds a new card to the end of list
{
deckOfCards.add(newCard);
}
public int search (Card getThisCard, int startPos) //searches for card in deck and returns first position
{
int posNum = -1;
int i = startPos; //initialize counting variable
while (posNum == -1 && i < deckOfCards.size())
{
if (getThisCard.equals(deckOfCards.get(i)))
posNum = i;
i++;
}
return posNum;
}
public void shuffle () //removes a card randomly from deck and add it to the end
{
for (int i =0; i < deckOfCards.size()*4; i++)
{
int rand = (int)(Math.random()*(deckOfCards.size()-1));
Card temp = deckOfCards.get(rand);
deckOfCards.remove(rand);
deckOfCards.add(temp);
}
}
public Card get(int pos) //get position
{
return deckOfCards.get(pos);
}
public Card deal() //deals the first card
{
Card dealtCard; //temp variable of card dealt
try{
dealtCard = deckOfCards.get(0);
deckOfCards.remove(0);
return dealtCard;
}
catch (Exception e)
{}
return null;
}
public Card deal(int pos) //gets position of card to be removed from
{ //first card is 0
Card dealtCard;
try{ //if no card, nothing happens
dealtCard = deckOfCards.get(pos);
deckOfCards.remove(pos); //arraylist starts at 0
return dealtCard;
}
catch(Exception e){
}
return null;
}
public void showDeck (Graphics g, int x, int y) //draws a deck
{
for (int i = 0; i < deckOfCards.size(); i++)
deckOfCards.get(i).showCard(g, x + i*12, y);
}
public void turnFaceUp(boolean face) //turns entire deck face up or face down
{
for (int i = 0; i < deckOfCards.size(); i++)
deckOfCards.get(i).turnFaceUp(face);
}
//sort
public void swap(int index1, int index2)
{
Card tempCard1 = deckOfCards.get(index1); //create two temporary cards
Card tempCard2 = deckOfCards.get(index2);
deckOfCards.set(index1, tempCard2); //swaps the two cards
deckOfCards.set(index2, tempCard1);
}
public void combSort () //sorts the deck using combsort
{
int gap = deckOfCards.size();
int swaps;// = 0;
do
{
gap = (int)(gap / 1.247330950103979); //set the gap
if (gap < 1) //set gap to 1 if it is less than 1
gap = 1;
int i = 0;
swaps = 0;
do
{
if ((deckOfCards.get(i)).compareHeartsRankTo(deckOfCards.get(i + gap)) > 0) //compare pos 0 to pos gap, if greater than 0
{
swap(i, i + gap); //swap i and gap
swaps = 1; //adds to swap count
}
i++; //i increments
} while (!(i + gap >= deckOfCards.size())); //repeat if i+gap < deck size
} while (!(gap == 1 && swaps == 0)); //repeat if gap != 1 or swaps != 0
}
}//end of deck class