- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.java
More file actions
Latest commit
131 lines (81 loc) · 3.25 KB
/
Copy pathPlayer.java
File metadata and controls
131 lines (81 loc) · 3.25 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
importjava.util.ArrayList;
importjava.util.Scanner;
//Defining the Player class.
publicclassPlayer {
// A player has a hand, and it is defined as an ArrayList
// of Card objects.
ArrayList<Card> hand;
// Options of things the human player can ask for.
// Don't want them asking for cards they don't have
// in their hand.
ArrayList<String> options = newArrayList<String>();
// Number of books the Player has.
intbooks;
// Player constructor giving the Player a hand.
Player(){
hand = newArrayList<Card>();
}
// Method for when the Player asks the Computer for a card.
publicStringask() {
// Displays the Player's hand.
System.out.println("---------------YOUR HAND------------------------");
for (intp = 0; p < hand.size(); p++) {
System.out.println(hand.get(p).rank + " of " + hand.get(p).suit);
}
options.clear();
for (intp = 0; p < hand.size(); p++) {
options.add(hand.get(p).rank);
}
// Have the Player enter the card they want to ask.
Scannerscanner = newScanner(System.in);
System.out.println("\n");
System.out.println("Ask P2 if they have any ... ? (q to quit)");
Stringrank = scanner.nextLine();
// Quit the game if the Player enters q or Q at any time during the game.
if (rank.equals("q") || rank.equals("Q")){
System.out.println("Bye now");
System.exit(0);
}
// If the rank asked for is in the player's hand then proceed.
if (options.contains(rank)) {
returnrank;
}
// If the card asked for is not in the Player's hand, then don't let them ask for this card.
else {
while (!options.contains(rank)) {
if (rank.equals("q") || rank.equals("Q")){
System.out.println("Bye now");
System.exit(0);
}
System.out.println("Invalid Rank Entered / You do not have that rank...\n");
System.out.println("Ask P2 if they have any ... ? (q to quit)");
rank = scanner.nextLine();
}
returnrank;
}
}
// check either requested rank (if P1 recieved cards) or the card that was draw from GO FISH.
publicvoidcheckBooks(ComputerPlayerP2, StringrankOfInterest){
if (hasFour(rankOfInterest)) {
hand.removeIf(card -> card.rank.equals(rankOfInterest));
hand.trimToSize();
books = books + 1;
P2.memory.removeIf(s -> s.equals(rankOfInterest));
P2.memory.trimToSize();
}
}
//Chekck to see if the Player's hand has four of a rank entered and returns a boolean.
publicbooleanhasFour(StringrankSeek) {
intsum = 0;
for (intcard = 0; card < hand.size(); card++) {
if (hand.get(card).rank.equals(rankSeek)) {
sum = sum + 1;
}
}
if (sum == 4) {
returntrue;
} else {
returnfalse;
}
}
}