-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoFish.java
More file actions
281 lines (227 loc) · 9.43 KB
/
GoFish.java
File metadata and controls
281 lines (227 loc) · 9.43 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//********************************************************************
// CardDriver.java Author: Lewis and Loftus
//
// Solution to Programming Project 7.7
//********************************************************************
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class GoFish
{
//-----------------------------------------------------------------
// Creates a deck, shuffles the deck and deals the cards.
//-----------------------------------------------------------------
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
DeckOfCards deck=new DeckOfCards();
deck.shuffle();
ArrayList<Card> Player1Deck= new ArrayList<Card>(7);
ArrayList<Integer> Player1DeckNumbers= new ArrayList<Integer>(7);
for (int x=0; x<7;x++)
{
Card t=deck.deal();
int a=t.getFace();
Player1DeckNumbers.add(a);
Player1Deck.add(t);
}
ArrayList<Card> Player2Deck= new ArrayList<Card>(7);
ArrayList<Integer> Player2DeckNumbers= new ArrayList<Integer>(7);
for (int x=0; x<7;x++)
{
Card t=deck.deal();
int a=t.getFace();
Player2DeckNumbers.add(a);
Player2Deck.add(t);
}
int Player1Points=0;
int Player2Points=0;
int PlayerTurn;
System.out.println("Original Lists: ");
System.out.println("");
System.out.println(Player1DeckNumbers);
System.out.println(Player2DeckNumbers);
System.out.println("");
System.out.println("Deck for Player 1: "+Player1Deck);
System.out.println("Deck for Player 2: "+Player2Deck);
String Player1Name=JOptionPane.showInputDialog(null,"Type the Name for Player 1",null,1);
String Player2Name=JOptionPane.showInputDialog(null,"Type the Name for Player 2",null,1);
// Main Game Code Begins
PlayerTurn=0;
Player1Points=MatchCheck(Player1DeckNumbers,PlayerTurn,Player1Points);
PlayerTurn=1;
Player2Points=MatchCheck(Player2DeckNumbers,PlayerTurn,Player2Points);
boolean deck_length_check=true;
while (deck_length_check==true)
{
for (int y=0;y<2;y++)
{
if (y==0&&deck_length_check==true)
{
String CardOptionsMenu="Type Which Card You Want to Ask "+ Player2Name +" For: "
+"\n" + "Type 1 for an Ace of any suit"
+"\n" + "Type 2 for 2 of any suit"
+"\n" + "Type 3 for 3 of any suit"
+"\n" + "Type 4 for 4 of any suit"
+"\n" + "Type 5 for 5 of any suit"
+"\n" + "Type 6 for 6 of any suit"
+"\n" + "Type 7 for 7 of any suit"
+"\n" + "Type 8 for 8 of any suit"
+"\n" + "Type 9 for 9 of any suit"
+"\n" + "Type 10 for 10 of any suit"
+"\n" + "Type 11 for a Joker of any suit"
+"\n" + "Type 12 for a Queen of any suit"
+"\n" + "Type 13 for a King of any suit"
+"\n";
PlayerTurn=0;
String pointstatement;
if (Player1Points==1)
pointstatement="You Have "+ Player1Points+ " Point";
else
pointstatement="You Have "+ Player1Points+ " Points";
JOptionPane.showMessageDialog(null, "Your deck is: "+"\n"+ Player1DeckNumbers+"\n"+"\n"+pointstatement, Player1Name+"'s Turn: ", JOptionPane.INFORMATION_MESSAGE);
String Player1ChoiceString=JOptionPane.showInputDialog(null,CardOptionsMenu,"Your Deck:"+Player1DeckNumbers,1);
int Player1Choice = Integer.parseInt(Player1ChoiceString);
Player1Points=MatchCheck(Player1DeckNumbers,PlayerTurn,Player1Points);
/// The Following Code Will See Whether Or Not Player 2 Has The Chosen Code
boolean CardPossession=false;
CardPossession=CardCheck(Player2DeckNumbers,Player1Choice,CardPossession);
System.out.println(CardPossession);
// m determines the number of cards lost(number of matches) after CardCheck
if (CardPossession==true)
{
Player1DeckNumbers.add(Player1DeckNumbers.size(),Player1Choice);
}
// if m=0 (no matches are found), Player1 draws a card from the deck
else
{
Card dcard=deck.deal();
int drawncard=dcard.getFace();
JOptionPane.showMessageDialog(null,"You drew a "+drawncard, Player1Name+"'s Turn: ", JOptionPane.INFORMATION_MESSAGE);
Player1DeckNumbers.add(drawncard);
}
Player1Points=MatchCheck(Player1DeckNumbers,PlayerTurn,Player1Points);
if (Player1Points==1)
JOptionPane.showMessageDialog(null, "You Have "+ Player1Points+ " Point", Player1Name+"'s Turn: ", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "You Have "+ Player1Points+ " Points", Player1Name+"'s Turn: ", JOptionPane.INFORMATION_MESSAGE);
}
int x=Player1DeckNumbers.size();
if (x==0)
{
deck_length_check=false;
}
else
{
if (y==1&&deck_length_check==true)// Else Player 2 plays
{
String CardOptionsMenu="Type Which Card You Want to Ask "+ Player1Name +" For: "
+"\n" + "Type 1 for an Ace of any suit"
+"\n" + "Type 2 for 2 of any suit"
+"\n" + "Type 3 for 3 of any suit"
+"\n" + "Type 4 for 4 of any suit"
+"\n" + "Type 5 for 5 of any suit"
+"\n" + "Type 6 for 6 of any suit"
+"\n" + "Type 7 for 7 of any suit"
+"\n" + "Type 8 for 8 of any suit"
+"\n" + "Type 9 for 9 of any suit"
+"\n" + "Type 10 for 10 of any suit"
+"\n" + "Type 11 for a Joker of any suit"
+"\n" + "Type 12 for a Queen of any suit"
+"\n" + "Type 13 for a King of any suit"
+"\n";
PlayerTurn=1;
String pointstatement;
if (Player2Points==1)
pointstatement="You Have "+ Player2Points+ " Point";
else
pointstatement="You Have "+ Player2Points+ " Points";
JOptionPane.showMessageDialog(null, "Your deck is: "+"\n"+ Player2DeckNumbers+"\n"+"\n"+pointstatement, Player2Name+"'s Turn: ", JOptionPane.INFORMATION_MESSAGE);
String Player2ChoiceString=JOptionPane.showInputDialog(null,CardOptionsMenu,"Your Deck:"+Player2DeckNumbers,1);
int Player2Choice = Integer.parseInt(Player2ChoiceString);
Player2Points=MatchCheck(Player2DeckNumbers,PlayerTurn,Player2Points);
/// The Following Code Will See Whether Or Not Player 2 Has The Chosen Code
boolean CardPossession=false;
CardPossession=CardCheck(Player1DeckNumbers,Player2Choice,CardPossession);
// m determines the number of cards lost(number of matches) after CardCheck
if (CardPossession==true)
{
Player2DeckNumbers.add(Player2DeckNumbers.size(),Player2Choice);
}
// if m=0 (no matches are found), Player1 draws a card from the deck
else
{
Card dcard=deck.deal();
int drawncard=dcard.getFace();
JOptionPane.showMessageDialog(null,"You drew a "+drawncard, Player2Name+"'s Turn: ", JOptionPane.INFORMATION_MESSAGE);
Player2DeckNumbers.add(drawncard);
}
Player2Points=MatchCheck(Player2DeckNumbers,PlayerTurn,Player2Points);
if (Player2Points==1)
JOptionPane.showMessageDialog(null, "You Have "+ Player2Points+ " Point", Player2Name+"'s Turn: ", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "You Have "+ Player2Points+ " Points", Player2Name+"'s Turn: ", JOptionPane.INFORMATION_MESSAGE);
}
int Player2DeckNumbers_length=Player2DeckNumbers.size();
if (Player2DeckNumbers_length==0)
{
deck_length_check=false;
}
}
}
}
if (Player2Points>Player1Points)
{
JOptionPane.showMessageDialog(null, "The winner of the game was "+Player2Name+"."
+"\n"
+"\n" +Player2Name+"--"+Player2Points+" pairs"
+"\n" + Player1Name+"--"+Player1Points+" pairs");
}
if (Player1Points>Player2Points)
{
JOptionPane.showMessageDialog(null, "The winner of the game was "+Player1Name+"."
+"\n"
+"\n" +Player1Name+"--"+Player1Points+" pairs"
+"\n" + Player2Name+"--"+Player2Points+" pairs");
}
if (Player1Points==Player2Points)
{
JOptionPane.showMessageDialog(null, "It was a tie. "
+"\n"
+"\n" +Player1Name+"--"+Player1Points+" pairs"
+"\n" + Player2Name+"--"+Player2Points+" pairs");
}
}
public static int MatchCheck(ArrayList<Integer> Player2DeckNumbers, int PlayerTurn,int PlayerPoints)
{
for (int i=0;i<Player2DeckNumbers.size();i++)
{
for (int j=i+1;j<Player2DeckNumbers.size();j++)
{
int number1=Player2DeckNumbers.get(i);
int number2=Player2DeckNumbers.get(j);
if (number1==number2)
{
Player2DeckNumbers.remove(j);
Player2DeckNumbers.remove(i);
i=i-1;
j=Player2DeckNumbers.size();
PlayerPoints=PlayerPoints+1;
}
}
}
return PlayerPoints;
}
public static boolean CardCheck(ArrayList<Integer> Player2DeckNumbers, int PlayerChoice, boolean CardPossession)
{
for (int i=0;i<Player2DeckNumbers.size();i++)
{
if (Player2DeckNumbers.get(i)==PlayerChoice)
{
Player2DeckNumbers.remove(i);
CardPossession=true;
}
}
return CardPossession;
}
}