-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.java
103 lines (84 loc) · 1.98 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
/*
Deck class (for TopCardPlacer class of project #1
*/
import java.util.*;
import java.io.*;
public class Deck
{
private int[] deck;
private final int MAX_DECK_SIZE = 30;
public Deck( int numCards )
{ if ( numCards%2 != 0 || numCards > MAX_DECK_SIZE )
{
System.out.format("\nINVALID DECK SIZE: (" + numCards + "). Must be an small even number <= %d\n", MAX_DECK_SIZE);
System.exit(0);
}
deck = new int[numCards];
for ( int i=0 ; i<numCards ; i++ ) deck[i] = i;
}
public String toString()
{
String deckStr = "";
for ( int i=0 ; i < deck.length ; ++i )
deckStr += deck[i] + " ";
return deckStr;
}
// ONLY WORKS ON DECK WITH EVEN NUMBER OF CARDS
// MODIFIES THE MEMBER ARRAY DECK
public void inShuffle()
{
int halfLength = deck.length / 2;
int[] topHalf = new int[halfLength];
int[] bottomHalf = new int[halfLength];
for (int i = 0; i < halfLength; i++) {
topHalf[i] = deck[i];
bottomHalf[i] = deck[i + halfLength];
}
int j = 0, k = 1;
for (int i = 0; i < halfLength; i++) {
deck[j] = bottomHalf[i];
deck[k] = topHalf[i];
j+=2;
k+=2;
}
}
// ONLY WORKS ON DECK WITH EVEN NUMBER OF CARDS
// MODIFIES THE MEMBER ARRAY DECK
public void outShuffle()
{
int halfLength = deck.length / 2;
int[] topHalf = new int[halfLength];
int[] bottomHalf = new int[halfLength];
for (int i = 0; i < halfLength; i++) {
topHalf[i] = deck[i];
bottomHalf[i] = deck[i + halfLength];
}
int j = 0, k = 1;
for (int i = 0; i < halfLength; i++) {
deck[j] = topHalf[i];
deck[k] = bottomHalf[i];
j+=2;
k+=2;
}
}
public String toBitString( int n )
{
if (n == 0)
return "";
int bitsNeeded = (int)(Math.log(n) / Math.log(2)) + 1;
char[] bits = new char[bitsNeeded];
int i = 0;
while (n != 0) {
bitsNeeded--;
double twoPow = Math.pow(2.0, (double)bitsNeeded);
if (twoPow > n)
bits[i] = '0';
else {
bits[i] = '1';
n -= twoPow;
}
i++;
}
return new String(bits);
}
} // END DECK CLASS