-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardGameV2.java
More file actions
219 lines (189 loc) · 6.03 KB
/
Copy pathCardGameV2.java
File metadata and controls
219 lines (189 loc) · 6.03 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import java.util.Arrays;
import java.util.Random;
public class CardGameV2 {
public static void main(String[] args) {
Random generator = new Random();
int countH = 0, countD = 0, countC = 0, countS = 0, countDraw = 0, myDraw = 0, yourDraw = 0;
String mySuit = null;
String yourSuit = null;
int[] myHand = new int[5];
int[] yourHand = new int[5];
int[] deck = new int[52];
boolean winner = false;
int mySuitHigh=0;
int mySuitLow=0;
int yourSuitHigh=0;
int yourSuitLow=0;
int yourGoodCount=0;
int myGoodCount=0;
int card =0;
boolean goodCard=false;
//take all cards out of play to begin game
for (int c=0;c<52;c++)
{deck[c]=0;}
//assigns card values to MYHAND and determines suit
for (int i = 0; i < myHand.length; i++) {
//draw a card until a card if drawn that is not in play, insert into hand, mark as out of play
while (goodCard==false)
{
card = generator.nextInt(52)+1;
if (deck[card-1]==0)
{myHand[i] = card; deck[card-1]=1;break;}
}
//end of while loop
if (myHand[i] >= 1 && myHand[i] <= 13) {
countH++;
}
else if (myHand[i] >= 14 && myHand[i] <= 26) {
countD++;
}
else if (myHand[i] >= 27 && myHand[i] <= 39) {
countC++;
}
else if (myHand[i] >= 40 && myHand[i] <= 52) {
countS++;
}
if (countH >= countD && countH >= countC && countH >= countS) {
mySuit = "Hearts";
}
else if (countD >= countH && countD >= countC && countD >= countS) {
mySuit = "Diamonds";
}
else if (countC >= countH && countC >= countD && countC >= countS) {
mySuit = "Clubs";
}
else if (countS >= countH && countS >= countC && countS >= countD) {
mySuit = "Spades";
}
}
//assigns card values to YOURHAND and determines suit
for (int i = 0; i < yourHand.length; i++) {
//draw a card until a card if drawn that is not in play, insert into hand, mark as out of play
while (goodCard==false)
{
card = generator.nextInt(52)+1;
if (deck[card-1]==0)
{yourHand[i] = card; deck[card-1]=1;break;}
}
//end of while loop
if (yourHand[i] >= 1 && yourHand[i] <= 13) {
countH++;
}
else if (yourHand[i] >= 14 && yourHand[i] <= 26) {
countD++;
}
else if (yourHand[i] >= 27 && yourHand[i] <= 39) {
countC++;
}
else if (yourHand[i] >= 40 && yourHand[i] <= 52) {
countS++;
}
if (countH >= countD && countH >= countC && countH >= countS) {
yourSuit = "Hearts";
}
else if (countD >= countH && countD >= countC && countD >= countS) {
yourSuit = "Diamonds";
}
else if (countC >= countH && countC >= countD && countC >= countS) {
yourSuit = "Clubs";
}
else if (countS >= countH && countS >= countC && countS >= countD) {
yourSuit = "Spades";
}
}
System.out.println("My hand has five cards: " + Arrays.toString(myHand));
System.out.println("Your hand has five cards: " + Arrays.toString(yourHand));
System.out.println("My suit is " + mySuit + ".");
System.out.println("Your suit is " + yourSuit + ".");
//PRINT THE DECK ARRAY 1 MEANS CARD IS IN A HAND 0 MEANS IT IS AVAILABLE
System.out.println("The deck: " + Arrays.toString(deck));
System.out.println();
if (mySuit.equals("Hearts")){mySuitLow=1;mySuitHigh = 13;}
if (mySuit.equals("Diamonds")){mySuitLow=14;mySuitHigh = 26;}
if (mySuit.equals("Clubs")){mySuitLow=27;mySuitHigh = 39;}
if (mySuit.equals("Spades")){mySuitLow=40;mySuitHigh = 52;}
if (yourSuit.equals("Hearts")){yourSuitLow=0;yourSuitHigh = 13;}
if (yourSuit.equals("Diamonds")){yourSuitLow=14;yourSuitHigh = 26;}
if (yourSuit.equals("Clubs")){yourSuitLow=27;yourSuitHigh = 39;}
if (yourSuit.equals("Spades")){yourSuitLow=40;yourSuitHigh = 52;}
//THE GAMELOOP repeats card draw until winner
while (!winner)
{
countDraw++;
//draw a card for MYHAND until a card if drawn that is not in play
while (goodCard==false)
{
card = generator.nextInt(52)+1;
if (deck[card-1]==0)
{myDraw=card;break;}
}
//end of while loop
System.out.println("My draw " + countDraw + ": " + myDraw);
//draw a card for YOURHAND until a card if drawn that is not in play
while (goodCard==false)
{
card = generator.nextInt(52)+1;
if (deck[card-1]==0)
{yourDraw=card;break;}
}
//end of while loop
System.out.println("Your draw " + countDraw + ": " + yourDraw);
//Insert a card into MYHAND if it is in mysuit
if (myDraw>=mySuitLow && myDraw<=mySuitHigh)
{
for (int i = 0; i < 5; i++)
{
if (myHand[i] < mySuitLow || myHand[i]> mySuitHigh)
{
deck[myHand[i]-1]=0;
myHand[i] = myDraw;
deck[myDraw-1]=1;
break;}
}
}
myGoodCount = 0;
//check if MYHAND is a winner
for (int j=0; j<5;j++)
{
if (myHand[j]>=mySuitLow && myHand[j]<=mySuitHigh)
{
myGoodCount = myGoodCount+1;
}
}
System.out.println("\tMy new hand is " + Arrays.toString(myHand));
if (myGoodCount == 5 )
{winner=true;System.out.println("I am the winner.");break;}
//Insert a card into YOURHAND if it is in yoursuit
if (yourDraw>=yourSuitLow && yourDraw<=yourSuitHigh)
{
for (int i = 0; i < 5; i++)
{
if (yourHand[i] < yourSuitLow || yourHand[i]> yourSuitHigh)
{
deck[yourHand[i]-1]=0;
yourHand[i] = yourDraw;
deck[yourDraw-1]=0;
break;}
}
}
yourGoodCount = 0;
//check if YOURHAND a winner
for (int j=0; j<5;j++)
{
if (yourHand[j]>=yourSuitLow && yourHand[j]<=yourSuitHigh)
{
yourGoodCount = yourGoodCount+1;
}
}
System.out.println("\tYour new hand is " + Arrays.toString(yourHand));
if (yourGoodCount == 5 )
{winner=true;System.out.println("You are the winner.");break;}
//PRINT THE DECK ARRAY 1 MEANS CARD IS IN A HAND 0 MEANS IT IS AVAILABLE
System.out.println("The deck: " + Arrays.toString(deck));
System.out.println();
}
System.out.println("The game is over.");
//PRINT THE DECK ARRAY 1 MEANS CARD IS IN A HAND 0 MEANS IT IS AVAILABLE
System.out.println("The deck: " + Arrays.toString(deck));
}
}