-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameFragment.java
More file actions
574 lines (523 loc) · 23 KB
/
Copy pathGameFragment.java
File metadata and controls
574 lines (523 loc) · 23 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package edu.neu.madcourse.michellelee.numad18s_michellelee;
import android.app.Fragment;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class GameFragment extends Fragment {
// Data structures go here...
static private int mLargeIds[] = {R.id.large1, R.id.large2, R.id.large3,
R.id.large4, R.id.large5, R.id.large6, R.id.large7, R.id.large8,
R.id.large9,};
static private int mSmallIds[] = {R.id.small1, R.id.small2, R.id.small3,
R.id.small4, R.id.small5, R.id.small6, R.id.small7, R.id.small8,
R.id.small9,};
private Tile mEntireBoard = new Tile(this);
private Tile mLargeTiles[] = new Tile[9];
private Tile mSmallTiles[][] = new Tile[9][9];
private Tile.Owner mPlayer = Tile.Owner.X;
private Set<Tile> mAvailable = new HashSet<Tile>();
private int mLastLarge;
private int mLastSmall;
// DICTIONARY VARIABLES
private HashSet<String> foundWords = new HashSet<String>();
// SELECTED SMALL TILES
private int tile1Int[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
private int tile2Int[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
private int tile3Int[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
private int tile4Int[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
private int tile5Int[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
private int tile6Int[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
private int tile7Int[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
private int tile8Int[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
private int tile9Int[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
private int allTilesInt[][] = {tile1Int, tile2Int, tile3Int, tile4Int, tile5Int, tile6Int, tile7Int, tile8Int, tile9Int};
// PLAYER WORD GUESSES
private StringBuilder tile1 = new StringBuilder();
private StringBuilder tile2 = new StringBuilder();
private StringBuilder tile3 = new StringBuilder();
private StringBuilder tile4 = new StringBuilder();
private StringBuilder tile5 = new StringBuilder();
private StringBuilder tile6 = new StringBuilder();
private StringBuilder tile7 = new StringBuilder();
private StringBuilder tile8 = new StringBuilder();
private StringBuilder tile9 = new StringBuilder();
private StringBuilder allTiles[] = {tile1, tile2, tile3, tile4, tile5, tile6, tile7, tile8, tile9};
// BUTTON LIST
public Button buttonList[][] = new Button[9][9];
// LIST VIEW SET UP
private ListView wordListView; // listview for words
private ArrayList<String> itemList; // word list
private ArrayAdapter<String> adapter; // string adapter
// SOUND MANAGEMENT
private int mSoundClick, mSoundCorrect, mSoundIncorrect;
private SoundPool mSoundPool;
private float mVolume = 1f;
// SCOREBOARD
private TextView scoreBoard; // score board
private int pointsScore; // points
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true); // retain this fragment across configuration changes.
initGame();
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
mSoundClick = mSoundPool.load(getActivity(), R.raw.click, 1);
mSoundCorrect = mSoundPool.load(getActivity(), R.raw.correct, 1);
mSoundIncorrect = mSoundPool.load(getActivity(), R.raw.wrong, 1);
dictionary = new HashSet<String>();
AssetManager assetM = getActivity().getAssets(); // get asset manager to access dictionary file
try {
InputStream is = assetM.open("dictionary");
BufferedReader r = new BufferedReader(new InputStreamReader(is)); // read from input stream
String line;
while ((line = r.readLine()) != null) { // while line is not null
if (line.length() >= 3) { // if the word is 3 letters long
dictionary.add(line); // take only the 3 letter words
}
}
} catch (IOException e) {
}
}
private void clearAvailable() {
mAvailable.clear();
}
private void addAvailable(Tile tile) {
mAvailable.add(tile);
}
public boolean isAvailable(Tile tile) {
return mAvailable.contains(tile);
}
HashSet<String> dictionary;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.large_board, container, false);
initViews(rootView);
updateAllTiles();
return rootView;
}
/**
* Initialize the board with words
* @param rootView the board layout
*/
private void initViews(View rootView) {
mEntireBoard.setView(rootView); // set view outer board
// CREATE WORD BANK
final ArrayList<String> wordBank = createWordBank(); // 9 letter word bank
// INITIALIZE THE BOARDS
for (int large = 0; large < 9; large++) { // intitialize each 1/9 board
View outer = rootView.findViewById(mLargeIds[large]); // get view for inner board
mLargeTiles[large].setView(outer); // set view for 1/9 board
int r = (int) (Math.random() * wordBank.size()); // get random number of index in wordBank
String word = wordBank.get(r); // get word from wordBank
// Log.e("word = ", word);
char[] shuffled = shuffle(word).toCharArray(); // shuffle the letters of the word
for (int small = 0; small < 9; small++) { // small boards within the large board
final Button inner = (Button) outer.findViewById(mSmallIds[small]); // initialize the button
char letter = Character.toUpperCase(shuffled[small]); // get current letter in word
inner.setText(""+letter); // set text of the button to be the current letter
final Tile smallTile = mSmallTiles[large][small]; // small tile within large tile
buttonList[large][small] = inner; // put this button into the button array
smallTile.setView(inner); // set view of the inner tile to be the button view
smallTile.setLargeTileNo(large); //
smallTile.setSmallTileNo(small); //
inner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// make a sound when clicked
mSoundPool.play(mSoundClick, mVolume, mVolume, 1, 0, 1f);
// get tile coordinates
int largeI = smallTile.getLargeTileNo();
// Log.e("large tile no", String.valueOf(largeI));
int smallI = smallTile.getSmallTileNo();
// Log.e("small tile no", String.valueOf(smallI));
// change background color
inner.setSelected(!inner.isSelected());
if (inner.isSelected()) {
// selected state change
inner.setBackgroundResource(R.drawable.tile_blue); // selected background turns blue
String letter = (inner.getText()).toString();
// Log.e("letter", String.valueOf(letter));
allTiles[largeI].append(letter); // add new letter selected to current string
// Log.e("character appended", String.valueOf(letter));
allTilesInt[largeI][smallI] = 1; // set background highlighted
} else {
// de-selected state change
inner.setBackgroundResource(R.drawable.tile_gray); // deselected background is grey
String letter = (inner.getText()).toString(); // get letter from inner tile clicked
// Log.e("letter", String.valueOf(letter));
allTiles[largeI].deleteCharAt(allTiles[largeI].length()-1); // remove most recently selected
allTilesInt[largeI][smallI] = 0; // deselect background highlighted
}
}
}
);}
}
// INITIALIZE SUBMISSION BUTTONS FOR CHECKING
Button b1 = (Button) rootView.findViewById(R.id.B1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkWord(1);
}
});
Button b2 = (Button) rootView.findViewById(R.id.B2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkWord(2);
}
});
Button b3 = (Button) rootView.findViewById(R.id.B3);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkWord(3);
}
});
Button b4 = (Button) rootView.findViewById(R.id.B4);
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkWord(4);
}
});
Button b5 = (Button) rootView.findViewById(R.id.B5);
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkWord(5);
}
});
Button b6 = (Button) rootView.findViewById(R.id.B6);
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkWord(6);
}
});
Button b7 = (Button) rootView.findViewById(R.id.B7);
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkWord(7);
}
});
Button b8 = (Button) rootView.findViewById(R.id.B8);
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkWord(8);
}
});
Button b9 = (Button) rootView.findViewById(R.id.B9);
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkWord(9);
}
});
// SETTING UP LIST VIEW
wordListView = (ListView) rootView.findViewById(R.id.accepted_words);
itemList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.word_list_row, R.id.listRowTextView, itemList);
wordListView.setAdapter(adapter);
// SETTING UP SCOREBOARD
scoreBoard = (TextView) rootView.findViewById(R.id.score);
}
/**
* Create word bank for 9 letter words
* @return array list with all the 9 letter words
*/
public ArrayList<String> createWordBank() {
ArrayList<String> wordBank = new ArrayList<String>();
AssetManager assetM = getActivity().getAssets(); // get asset manager to access dictionary file
try {
InputStream is = assetM.open("dictionary"); // open input stream to read dictionary
BufferedReader r = new BufferedReader(new InputStreamReader(is)); // read from input stream
String line;
while ((line = r.readLine()) != null) { // while line is not null
if (line.length() == 9) { // if the word is 9 letters long
wordBank.add(line); // take only the 9 letter words
}
}
} catch (IOException e) {
}
return wordBank;
}
/**
* Create look up dictionary for all words > 3 letters
*/
public HashSet<String> createDictionary() {
HashSet<String> dictionary = new HashSet<String>();
AssetManager assetM = getActivity().getAssets(); // get asset manager to access dictionary file
try {
InputStream is = assetM.open("dictionary");
BufferedReader r = new BufferedReader(new InputStreamReader(is)); // read from input stream
String line;
while ((line = r.readLine()) != null) { // while line is not null
if (line.length() >= 3) { // if the word is 3 letters long
dictionary.add(line); // take only the 3 letter words
}
}
} catch (IOException e) {
}
return dictionary;
}
/**
* If a guess is greater than or equal to three words, the dictionary is checked
* to see if the word is in there. If the word is not in the found words set, the
* word is added.
*
* @param largeTile the large tile for the word to check
*/
public void checkWord(int largeTile) {
String guess = "";
int length = 0;
int score = 0;
boolean correct = false; // flag for whether the word is valid
// get word string based on which button was pressed
switch(largeTile) {
case 1:
guess = tile1.toString();
break;
case 2:
guess = tile2.toString();
break;
case 3:
guess = tile3.toString();
break;
case 4:
guess = tile4.toString();
break;
case 5:
guess = tile5.toString();
break;
case 6:
guess = tile6.toString();
break;
case 7:
guess = tile7.toString();
break;
case 8:
guess = tile8.toString();
break;
case 9:
guess = tile9.toString();
break;
default:
guess = "";
}
// Log.e("guess= ", guess);
length = guess.length();
// Log.e("dictionary length = ", Integer.toString(dictionary.size()));
// check if valid word
if (length >= 3) { // check if the length of the word is >= 3
// Log.e("length= ", Integer.toString(guess.length()));
if (dictionary.contains(guess.toLowerCase())) { // check if the dictionary contains the word
foundWords.add(guess); // add to set
correct = true; // true if found
// Log.e("correct", "= true");
pointsScore += length;
scoreBoard.setText(""+pointsScore);
}
} else {
correct = false; // false if not found
// Log.e("correct", "= false");
}
// if word is in the dictionary
if (correct) {
mSoundPool.play(mSoundCorrect, mVolume, mVolume, 1, 0, 1f); // chime when correct
adapter.add(guess); // add to list
// Log.e("word", String.valueOf(tile1));
// set unused tiles to be blank
for (int small = 0; small < 9; small++) { // go through entire list
// Log.e("tile # ", Integer.toString(small));
// Log.e("selected? ", allTilesInt[largeTile-1][small] == 0 ? "no" : "yes");
if (allTilesInt[largeTile-1][small] == 0) { // if the tile was not selected
buttonList[largeTile-1][small].setText(""); // remove the letter from the button
} else {
continue;
}
}
// if word was not in the dictionary
} else {
mSoundPool.play(mSoundIncorrect, mVolume, mVolume, 1, 0, 1f); // quack when incorrect
}
}
/**
* Shuffle the word pulled from the word bank
* @param text word from the word bank to be shuffled
* @return the shuffled string
*/
public static String shuffle(String text) {
char[] characters = text.toCharArray();
for (int i = 0; i < characters.length; i++) {
int randomIndex = (int)(Math.random() * characters.length);
char temp = characters[i];
characters[i] = characters[randomIndex];
characters[randomIndex] = temp;
}
return new String(characters);
}
// private void switchTurns() {
// mPlayer = mPlayer == Tile.Owner.X ? Tile.Owner.O : Tile
// .Owner.X;
// }
//
// /**
// * Set owner of small tile to current player
// * @param large index of large tile being moved
// * @param small index of small tile being moved
// */
// private void makeMove(int large, int small) {
// mLastLarge = large;
// mLastSmall = small;
// Tile smallTile = mSmallTiles[large][small];
// Tile largeTile = mLargeTiles[large];
// smallTile.setOwner(mPlayer); // set owner of small tile to current player
// setAvailableFromLastMove(small);
// Tile.Owner oldWinner = largeTile.getOwner();
// Tile.Owner winner = largeTile.findWinner(); // see if there's a winner for the board containing the small tile
// if (winner != oldWinner) {
// largeTile.setOwner(winner);
// }
// winner = mEntireBoard.findWinner(); // see if someone has won the entire board
// mEntireBoard.setOwner(winner);
// updateAllTiles();
// if (winner != Tile.Owner.NEITHER) { // if someone has won
// ((GameActivity)getActivity()).reportWinner(winner);
// }
// }
// /**
// * Redraw all boards
// */
// public void restartGame() {
// initGame();
// initViews(getView());
// updateAllTiles();
//
// }
// // REPLACE???
//// if (isAvailable(smallTile)) {
//// makeMove(fLarge, fSmall);
//// switchTurns();
//// }
/**
* Sets up data structures
*/
public void initGame() {
Log.d("UT3", "init game");
mEntireBoard = new Tile(this);
// Create all the tiles
for (int large = 0; large < 9; large++) {
mLargeTiles[large] = new Tile(this);
for (int small = 0; small < 9; small++) {
mSmallTiles[large][small] = new Tile(this);
}
mLargeTiles[large].setSubTiles(mSmallTiles[large]);
}
mEntireBoard.setSubTiles(mLargeTiles);
// If the player moves first, set which spots are available
mLastSmall = -1;
mLastLarge = -1;
setAvailableFromLastMove(mLastSmall);
}
/**
* Clears the available list and then marks unoccupied tiles
* at the available large board as available
* @param small
*/
private void setAvailableFromLastMove(int small) {
clearAvailable();
// Make all the tiles at the destination available
if (small != -1) {
for (int dest = 0; dest < 9; dest++) {
Tile tile = mSmallTiles[small][dest];
if (tile.getOwner() == Tile.Owner.NEITHER)
addAvailable(tile);
}
}
// If there were none available, make all squares available
if (mAvailable.isEmpty()) {
setAllAvailable();
}
}
/**
* If there are no possible moves, mark all unoccupied tiles as available
*/
private void setAllAvailable() {
for (int large = 0; large < 9; large++) {
for (int small = 0; small < 9; small++) {
Tile tile = mSmallTiles[large][small];
if (tile.getOwner() == Tile.Owner.NEITHER)
addAvailable(tile);
}
}
}
/**
* Updates the state of the tile representing the overall board,
* then each of the large tiles for the first 9 level boards,
* then the small tiles that contain the letters
*/
private void updateAllTiles() {
mEntireBoard.updateDrawableState();
for (int large = 0; large < 9; large++) {
mLargeTiles[large].updateDrawableState();
for (int small = 0; small < 9; small++) {
mSmallTiles[large][small].updateDrawableState();
}
}
}
/**
* Create a string containing the state of the game.
* @return
*/
public String getState() {
StringBuilder builder = new StringBuilder();
builder.append(mLastLarge);
builder.append(',');
builder.append(mLastSmall);
builder.append(',');
for (int large = 0; large < 9; large++) {
for (int small = 0; small < 9; small++) {
builder.append(mSmallTiles[large][small].getOwner().name());
builder.append(',');
}
}
return builder.toString();
}
/**
* Restore the state of the game from the given string.
* @param gameData
*/
public void putState(String gameData) {
String[] fields = gameData.split(",");
int index = 0;
mLastLarge = Integer.parseInt(fields[index++]);
mLastSmall = Integer.parseInt(fields[index++]);
for (int large = 0; large < 9; large++) {
for (int small = 0; small < 9; small++) {
Tile.Owner owner = Tile.Owner.valueOf(fields[index++]);
mSmallTiles[large][small].setOwner(owner);
}
}
setAvailableFromLastMove(mLastSmall);
updateAllTiles();
}
}