-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
332 lines (278 loc) · 10.5 KB
/
Player.java
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.Math;
import java.time.LocalDate;
public class Player extends Thread {
// I/O variables
Socket socket;
BufferedReader in;
PrintWriter out;
// Game variables
private String name;
private String channel;
private boolean isScribe;
private boolean ready;
File story;
File story_org;
int numPlayers;
int turn;
// Shared variables
public HashMap<String, File> dictionaries;
public HashMap<String, File> stories;
public ArrayList<Player> players;
public Player(Socket s, String n, ArrayList<Player> p, HashMap<String, File> d,
HashMap<String, File> st, String c, int num, int count){
this.socket = s;
this.name = n;
this.players = p;
this.dictionaries = d;
this.stories = st;
this.channel = c;
this.numPlayers = num;
this.turn = count;
}
public void run(){
try{
printToAll(name + " has joined the game.");
// Initialize variables
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.out = new PrintWriter(socket.getOutputStream(), true);
this.ready = true;
while(!allReady()){
// *whistling while twiddling thumbs*
}
// ************* SET UP GAME *************
printToAll("\nLet the game begin!");
boolean gameOver = false;
// Initialize game with scribe
Player scribe = players.get(0);
scribe.setScribeStatus(true);
// Load chosen story and file reader/writer for scribe
BufferedReader fr = scribe.chooseStory();
String filename = scribe.channel + "_" + LocalDate.now() + ".txt";
File newStory = new File(filename);
// Successfully created new file
if(newStory.createNewFile()){
System.out.println("Successfully created new story file.");
} else {
System.out.println("File " + filename + " already exists in this directory.");
}
this.story = newStory;
PrintWriter fw = new PrintWriter(this.story);
// Assign players and roles
Player p1 = players.get(1);
Player p2 = null;
Player p3 = null;
Player p4 = null;
if(numPlayers == 3)
p2 = players.get(2);
if(numPlayers == 4)
p3 = players.get(3);
ArrayList<String> toScribe = new ArrayList<>();
turn = 1;
int max = numPlayers-1;
// ************* PLAY GAME *************
String currSentence;
while(!gameOver && (currSentence = fr.readLine()) != null){
if(currSentence.length() == 0){ // Add white space to new document
fw.println("");
currSentence = fr.readLine();
} else if(currSentence == null) // Reached end of story
gameOver = true;
if(turn == max)
turn = 1;
else{
turn++;
}
toScribe = players.get(turn).takeTurn(currSentence);
scribe.writeToStory(currSentence, toScribe, fw);
}
fw.close();
// Scribe reveals the nonsense to all
BufferedReader storyTime = new BufferedReader(new FileReader(story));
String x = "";
while((x = storyTime.readLine()) != null){
printToAll(x);
}
printToAll("\nThanks for playing!");
} catch (IOException e) { }
}
// ***************** HELPER METHODS *****************
// ********** Set methods **********
public void setPlayerName(String pname){ this.name = pname; }
public void setScribeStatus(boolean isScribe){ this.isScribe = isScribe; }
public void setPlayers(ArrayList<Player> players){ this.players = players; }
public void setReadyState(boolean ready){ this.ready = ready; }
public void setStory(String storyName){ this.story_org = stories.get(storyName); }
public void setChannelName(String channel) { this.channel = channel; }
public void setNumPlayers(int num) { this.numPlayers = num; }
// ********** Get methods **********
public boolean isScribe(){ return this.isScribe; }
public BufferedReader getInput(){ return this.in; }
public PrintWriter getOutput(){ return this.out; }
public ArrayList<Player> getPlayers(){ return this.players; }
public boolean isReady(){ return this.ready; }
public int getTurn(){ return this.turn; }
public void printToAll(String message){
try{
for(int i = 0; i < players.size(); i++){
Socket curSocket = players.get(i).socket;
PrintWriter otherOut = new PrintWriter(curSocket.getOutputStream(), true);
otherOut.println(message);
}
} catch(IOException ie){}
}
public boolean allReady(){
if(players.size() < numPlayers)
return false;
else {
for(int i = 0; i < players.size(); i++){
if(!players.get(i).isReady())
return false;
}
}
return true;
}
private BufferedReader chooseStory(){
BufferedReader fr = null;
boolean valid = false;
try {
String title = "";
// Input check
while(!valid){
out.println("Which story would you like to use?");
title = in.readLine();
if(!stories.containsKey(title)){
out.println("Sorry, that story doesn't exist.");
} else {
valid = true;
}
}
File story = stories.get(title);
this.story_org = story;
fr = new BufferedReader(new FileReader(story));
return fr;
} catch(IOException ie){
out.println("File not found.");
} finally {
this.story_org = story;
return fr;
}
}
// Write new sentence into story
private void writeToStory(String sentence, ArrayList<String> words, PrintWriter fw){
String[] breakdown = sentence.split("@");
int count = 0; // Counter for current position in words
// Replace all blanks with new words, in order
for(int i = 0; i < breakdown.length; i++){
String current = breakdown[i];
// If prompt is found, replace with next substitute word
if(current.contains("[")){
breakdown[i] = words.get(count);
count++;
}
}
// Add sentence to story file
String newSentence = String.join("", breakdown);
fw.println(newSentence);
}
private ArrayList<String> takeTurn(String sentence){
printToAll("\nIt's " + name + "'s turn.\n");
/* Look for markers (identified by @[type]@) and extract them from sentence:
@[vp]@ = verb present tense
@[vpp]@ = verb past tense
@[vf]@ = verb future tense
@[n]@ = noun
@[np]@ = noun plural
@[p]@ = place
@[a]@ = adjective
@[av]@ = adverb
*/
String[] markers = sentence.split("@");
ArrayList<String> prompts = new ArrayList<>();
for(int i = 0; i < markers.length; i++){
String s = markers[i];
// Check if current marker is actually a prompt
if(s.contains("[")){
String currPrompt = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
prompts.add(currPrompt);
}
}
// Choose words
ArrayList<String> words = new ArrayList<>();
for(int i = 0; i < prompts.size(); i++){
String word = "";
word = chooseWord(prompts.get(i));
words.add(word);
}
return words;
}
public String chooseWord(String prompt){
String word = "";
String wordType = "word";
boolean valid = false;
// Get appropriate dictionary
File currDict = null;
switch(prompt){
case "vp":
currDict = dictionaries.get("Verbs (Present)");
wordType = "verb (present tense)";
break;
case "vpp":
currDict = dictionaries.get("Verbs (Past)");
wordType = "verb (past tense)";
break;
case "vf":
currDict = dictionaries.get("Verbs (Future)");
wordType = "verb (future tense)";
break;
case "n":
currDict = dictionaries.get("Nouns");
wordType = "noun";
break;
case "np":
currDict = dictionaries.get("Nouns (Plural)");
wordType = "noun (plural)";
break;
case "p":
currDict = dictionaries.get("Places");
wordType = "place";
break;
case "a":
currDict = dictionaries.get("Adjectives");
wordType = "adjective";
break;
case "av":
currDict = dictionaries.get("Adverbs");
wordType = "adverb";
break;
}
// Prompt for word
while(!valid){
try {
out.println("Type in a(n) " + wordType + ": ");
// Check validity of word
if(isValidWord((word = in.readLine()), currDict))
valid = true;
else
out.println("That word is not in the dictionary. Try again.");
} catch (IOException ie){}
}
return word;
}
// Check if given word is in the appropriate dictionary
private static boolean isValidWord(String word, File dictionary){ // WORKS
try{
BufferedReader temp = new BufferedReader(new FileReader(dictionary));
String currWord;
while((currWord = temp.readLine()) != null){
if(currWord.equalsIgnoreCase(word)) {
return true;
}
}
temp.close();
} catch(IOException ie){}
return false;
}
}