-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanPlayer.java
More file actions
229 lines (200 loc) · 7.82 KB
/
HumanPlayer.java
File metadata and controls
229 lines (200 loc) · 7.82 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
// CLASS: HumanPlayer
//
// Author: Huayi Chen
//
// REMARKS: initialize human player and what will be happen on player
//
//----------------------------------------
import java.util.ArrayList;
import java.util.Scanner;
public class HumanPlayer implements IPlayer{
private int index;
private ArrayList<Card> people;
private ArrayList<Card> places;
private ArrayList<Card> weapons;
private ArrayList<Card> hand;
private Guess guess;
private boolean isActive;
public HumanPlayer(){
isActive=true;
hand=new ArrayList<>();
}
//------------------------------------------------------
// Method: removed
// PURPOSE: remove player from game for its turn
// PARAMETERS: non
// Returns: void
//------------------------------------------------------
public void removed() {
System.out.println("Player "+index+" (You) made a bad accusation and was removed from the game.");
isActive = false;
}
//------------------------------------------------------
// Method: isActive
// PURPOSE: return whether player is active
// PARAMETERS: non
// Returns: boolean
//------------------------------------------------------
public boolean isActive(){
return isActive;
}
//------------------------------------------------------
// Method: toBeWinner
// PURPOSE: print the winner information
// PARAMETERS: non
// Returns: void
//------------------------------------------------------
public void toBeWinner(){
System.out.println("Player "+index+" (You) won the game.\nGAME OVER");
}
//------------------------------------------------------
// Method: selectOption
// PURPOSE: show options details for human player, and get their selection
// PARAMETERS: list of information
// Returns: int
//------------------------------------------------------
public int selectOption(ArrayList<Card> arrayList){
int choice=-1;
boolean isValid=false;
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(i+": "+arrayList.get(i).getName());
}
while (!isValid){
Scanner keyBoard=new Scanner(System.in);
if (keyBoard.hasNextInt()){
choice=keyBoard.nextInt();
if (choice>=0 && choice<arrayList.size()){
isValid=true;
}else {
System.out.println("Invalid choice index. Try again.");
}
} else {
System.out.println("Please enter a number. Try again.");
}
}
return choice;
}
//------------------------------------------------------
// Method: isCorrect
// PURPOSE: check the player's guess is equal to answer
// PARAMETERS: answer list of game
// Returns: boolean
//------------------------------------------------------
public boolean isCorrect(ArrayList<Integer> answerList){
boolean result=false;
if (answerList.equals(guess.getChoice())){
result=true;
}
return result;
}
//------------------------------------------------------
// Method: compareName
// PURPOSE: compare two cards' name, return true if they are same
// PARAMETERS: two cards
// Returns: boolean
//------------------------------------------------------
public boolean compareName(Card card1, Card card2){
boolean isSame=false;
if (card1.getName().equals(card2.getName())){
isSame=true;
}
return isSame;
}
//------------------------------------------------------
// Method: setUp
// PURPOSE: set up player with some information
// PARAMETERS:
// Returns: void
//------------------------------------------------------
public void setUp(int numPlayers, int index, ArrayList<Card> people, ArrayList<Card> places, ArrayList<Card> weapons){
this.index=index;
this.people=people;
this.places=places;
this.weapons=weapons;
}
//------------------------------------------------------
// Method: setCard
// PURPOSE: deal a card for player
// PARAMETERS: Card
// Returns: void
//------------------------------------------------------
public void setCard (Card c){
System.out.println("You received the card '"+c.getName()+"'");
hand.add(c);
}
//------------------------------------------------------
// Method: getIndex
// PURPOSE: return index
// PARAMETERS: non
// Returns: int
//------------------------------------------------------
public int getIndex(){
return index;
}
//------------------------------------------------------
// Method: canAnswer
// PURPOSE: answer a Card or null for different each follow game rules
// PARAMETERS: a player's guess, the player who asked
// Returns: Card
//------------------------------------------------------
public Card canAnswer(Guess g, IPlayer ip) {
Card theCard = null;
ArrayList<Integer> list = g.getChoice();
ArrayList<Card> respondAnswer = new ArrayList<>();
for (int i = 0; i < hand.size(); i++) {
if (compareName(people.get(list.get(0)), hand.get(i))) {
respondAnswer.add(hand.get(i));
}
if (compareName(places.get(list.get(1)), hand.get(i))) {
respondAnswer.add(hand.get(i));
}
if (compareName(weapons.get(list.get(2)), hand.get(i))){
respondAnswer.add(hand.get(i));
}
}
System.out.print("Player " + g.getIndex() + " asked you about" + g.printResult());
if (respondAnswer.size()==0){
System.out.print(", but you couldn't answer.\n");
}else if (respondAnswer.size()==1) {
System.out.print(", you only have one card, " + respondAnswer.get(0).getName() + ", showed it to them.\n");
theCard=respondAnswer.get(0);
} else {
System.out.print(". Which do you show?\n");
int choice=selectOption(respondAnswer);
theCard=respondAnswer.get(choice);
}
return theCard;
}
//------------------------------------------------------
// Method: getGuess
// PURPOSE: make a guess and return it
// PARAMETERS: non
// Returns: Guess
//------------------------------------------------------
public Guess getGuess(){
System.out.println("It is your turn:\nWhich person do you want to suggest?");
int choice1=selectOption(people);
System.out.println("Which location do you want to suggest?");
int choice2=selectOption(places);
System.out.println("Which weapon do you want to suggest?");
int choice3=selectOption(weapons);
System.out.println("Is this an accusation (Y/[N])?");
boolean isAccusation = false;
Scanner input4=new Scanner(System.in);
String decision=input4.next();
if (decision.equals("Y") || decision.equals("y")){
isAccusation=true;
}
guess = new Guess(index, choice1, choice2, choice3, people, places, weapons, isAccusation);
return guess;
}
//------------------------------------------------------
// Method: receiveInfo
// PURPOSE: receive card from other player
// PARAMETERS: the player who answer for you, a card
// Returns: void
//------------------------------------------------------
public void receiveInfo(IPlayer ip, Card c){
System.out.println("You receive the card "+c.getName()+"from Player "+ip.getIndex());
}
}