-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
223 lines (195 loc) · 8.12 KB
/
Model.java
File metadata and controls
223 lines (195 loc) · 8.12 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
// CLASS: Model
//
// Author: Huayi Chen
//
// REMARKS: initialize players and process game
//
//----------------------------------------
import java.util.ArrayList;
import java.util.Collections;
public class Model {
private IPlayer iPlayer;
private ArrayList<IPlayer> players;
private ArrayList<Card> people;
private ArrayList<Card> places;
private ArrayList<Card> weapons;
private ArrayList<Card> allCards;
private ArrayList<Integer> answerList;
public Model(ArrayList<IPlayer> players, ArrayList<Card> people, ArrayList<Card> places, ArrayList<Card> weapons){
this.players=players;
this.people=people;
this.places=places;
this.weapons=weapons;
allCards=new ArrayList<>();
answerList=new ArrayList<>();
}
//------------------------------------------------------
// Method: startPlay
// PURPOSE: run the game
// PARAMETERS: non
// Returns: void
//------------------------------------------------------
public void startPlay(){
setUpGame();
setAnswerAndDeal();
runProcess();
}
//------------------------------------------------------
// Method: showList
// PURPOSE: show you all cards the game has
// PARAMETERS: kinds of cards list
// Returns: void
//------------------------------------------------------
public void showList(ArrayList<Card> arrayList){
for (int i = 0; i < arrayList.size(); i++) {
System.out.print(arrayList.get(i).getName());
if (i!=arrayList.size()-1){
System.out.print(", ");
}
}
}
//------------------------------------------------------
// Method: setUpGame
// PURPOSE: initialize the game and set up players
// PARAMETERS: non
// Returns: void
//------------------------------------------------------
public void setUpGame(){
System.out.println("Setting up players...\nHere are the name of suspects:");
showList(people);
System.out.println("\nHere are all the locations:");
showList(places);
System.out.println("\nHere are all the weapons:");
showList(weapons);
for (int i = 0; i < players.size(); i++) {
iPlayer=players.get(i);
iPlayer.setUp(players.size(), i, people, places, weapons);
}
}
//------------------------------------------------------
// Method: setAnswerAndDeal
// PURPOSE: pick the answer, and then shuffle remaining cards to deal them
// PARAMETERS: non
// Returns: void
//------------------------------------------------------
public void setAnswerAndDeal(){
int suspectChoice=(int)(Math.random()*(people.size()));
int locationChoice=(int)(Math.random()*(places.size()));
int weaponChoice=(int)(Math.random()*(weapons.size()));
ArrayList<Card> newPeople = new ArrayList<>(people);
ArrayList<Card> newPlaces = new ArrayList<>(places);
ArrayList<Card> newWeapons = new ArrayList<>(weapons);
answerList.add(suspectChoice);
answerList.add(locationChoice);
answerList.add(weaponChoice);
newPeople.remove(suspectChoice);
newPlaces.remove(locationChoice);
newWeapons.remove(weaponChoice);
System.out.println("\nDealing cards...");
allCards.addAll(newPeople);
allCards.addAll(newPlaces);
allCards.addAll(newWeapons);
Collections.shuffle(allCards);
for (int i = 0, playerIndex=0; i < allCards.size(); i++, playerIndex++) {
if (playerIndex==players.size()){
playerIndex=0;
}
iPlayer=players.get(playerIndex);
iPlayer.setCard(allCards.get(i));
}
}
//------------------------------------------------------
// Method: runProcess
// PURPOSE: process the logic of game
// PARAMETERS: non
// Returns: void
//------------------------------------------------------
public void runProcess(){
System.out.println("Playing...");
int currTurn=0;
int numActivePlayer=players.size();
boolean isGameOver=false;
boolean isGetNextPlayer=false;
while (!isGameOver) {
iPlayer=players.get(currTurn);
if (numActivePlayer == 1) { // if there is only one active player, this player win the game
if (iPlayer instanceof HumanPlayer) {
((HumanPlayer) iPlayer).toBeWinner();
} else {
((ComputerPlayer) iPlayer).toBeWinner();
}
isGameOver = true;
} else {
// ask player for their guess
Guess playerGuess = iPlayer.getGuess();
System.out.println(playerGuess.printResult());
if (playerGuess.isAccusation()) { // Accusation
// check answer, correct to win, wrong to be removed
if (iPlayer instanceof HumanPlayer) {
HumanPlayer hPlayer = (HumanPlayer) iPlayer;
if (hPlayer.isCorrect(answerList)) {
hPlayer.toBeWinner();
isGameOver = true;
} else {
hPlayer.removed();
numActivePlayer--;
}
} else {
ComputerPlayer cPlayer = (ComputerPlayer) iPlayer;
if (cPlayer.isCorrect(answerList)) {
cPlayer.toBeWinner();
isGameOver = true;
} else {
cPlayer.removed();
numActivePlayer--;
}
}
} else { // Suggestion
boolean isAnswered=false;
int i=currTurn;
int askingTimes=0;
IPlayer currPlayer=iPlayer;
Card theCard;
// ask next player for their helpful tips
while (!isAnswered && askingTimes<players.size()-1){
i=(i+1)%players.size();
iPlayer = players.get(i);
System.out.println("Asking player "+iPlayer.getIndex());
theCard = iPlayer.canAnswer(playerGuess, currPlayer);
if (theCard != null){
currPlayer.receiveInfo(iPlayer, theCard);
isAnswered=true;
System.out.println("Player "+iPlayer.getIndex()+" answered.");
}
askingTimes++;
}
// no one answered
if (!isAnswered){
if (currPlayer instanceof HumanPlayer){
System.out.println("No one could refute your suggestion.");
}
System.out.println("No one could answer");
}
}
// more to next player's turn if game is not over
if (!isGameOver) {
while (!isGetNextPlayer) {
currTurn=(currTurn+1)%players.size();
iPlayer=players.get(currTurn);
if (iPlayer instanceof HumanPlayer) {
if (((HumanPlayer) iPlayer).isActive()) {
isGetNextPlayer = true;
}
} else {
if (((ComputerPlayer) iPlayer).isActive()) {
isGetNextPlayer = true;
}
}
}
isGetNextPlayer = false;
}
}
} // end while
// Game is Over here
}
}