-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.java
More file actions
131 lines (81 loc) · 3.25 KB
/
Player.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
import java.util.ArrayList;
import java.util.Scanner;
//Defining the Player class.
public class Player {
// 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 = new ArrayList<String>();
// Number of books the Player has.
int books;
// Player constructor giving the Player a hand.
Player(){
hand = new ArrayList<Card>();
}
// Method for when the Player asks the Computer for a card.
public String ask() {
// Displays the Player's hand.
System.out.println("---------------YOUR HAND------------------------");
for (int p = 0; p < hand.size(); p++) {
System.out.println(hand.get(p).rank + " of " + hand.get(p).suit);
}
options.clear();
for (int p = 0; p < hand.size(); p++) {
options.add(hand.get(p).rank);
}
// Have the Player enter the card they want to ask.
Scanner scanner = new Scanner(System.in);
System.out.println("\n");
System.out.println("Ask P2 if they have any ... ? (q to quit)");
String rank = 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)) {
return rank;
}
// 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();
}
return rank;
}
}
// check either requested rank (if P1 recieved cards) or the card that was draw from GO FISH.
public void checkBooks(ComputerPlayer P2, String rankOfInterest){
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.
public boolean hasFour(String rankSeek) {
int sum = 0;
for (int card = 0; card < hand.size(); card++) {
if (hand.get(card).rank.equals(rankSeek)) {
sum = sum + 1;
}
}
if (sum == 4) {
return true;
} else {
return false;
}
}
}