-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
137 lines (125 loc) · 3.44 KB
/
Board.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
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author Jeb Kilfoyle
* @version 1.0
* Board class which holds a representation of letters which have specified coordinates (x,y).
* In addition Board stores the current word being constructed.
* 4/2/2017
* CPSC 224, Project Boggle
*/
public class Board {
private int size;
private ArrayList<ArrayList<Letter>> board_letters;
private ArrayList<Letter> cur_word;
/**
* Default constructor creates a board of size 4.
*/
public Board(){
this(4);
}
/**
* Specialized Constructor creates a n x n board.
* Each Letter has an associated grid position as well as a character.
* @param n
*/
public Board(int n){
size = n;
board_letters = new ArrayList<ArrayList<Letter>>();
cur_word = new ArrayList<Letter>();
for(int i = 0; i < n; i++){
ArrayList<Letter> row = new ArrayList<Letter>();
board_letters.add(row);
for(int j = 0; j < n; j++){
Letter c = new Letter(j,i);
row.add(c);
}
}
//checkVowels(); Currently unnecessary.
}
/**
* Returns the letter at the x,y coordinate.
* @param x The x-coordinate.
* @param y The y-coordinate.
* @return The letter at coordinates (x,y).
*/
public Letter getLetter(int x, int y){
return board_letters.get(y).get(x);
}
/**
* Returns a string representation of the board.
*/
public String toString(){
String s_board = "";
for(int i = 0; i < size; i++){
s_board += "\n";
for(int j = 0; j < size; j++){
s_board += getLetter(j,i).getCharacter() + " ";
}
}
return s_board;
}
/**
* A method to check the current letters on the board, and if there are not enough vowels,
* create a new set of letters until there are enough letters. Used at the end of the constructor after all
* letters have been initialized.
*/
/* Removed method for now. May bring back later.
* private void checkVowels(){
boolean enough_vowels = false;
while(!enough_vowels){
/*Check selection of letters to see if their are at least 4 vowels.
*If their are not enough vowels, roll a new set of letters until
* their exists size number of vowels.
*/
/*int total_vowels = 0;
int total_consonants = 0;
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j ++){
String x = getLetter(j,i).getCharacter();
if (x.equalsIgnoreCase("a")|x.equalsIgnoreCase("e")|x.equalsIgnoreCase("i")
|x.equalsIgnoreCase("o")|x.equalsIgnoreCase("u")|x.equalsIgnoreCase("y")){
total_vowels++;
} else {
total_consonants++;
}
}
}
if(total_vowels >= size){
enough_vowels = true;
} else {
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
board_letters.get(j).get(i).reRoll();
}
}
}
}
}
*/
/**
* Returns the string representation of the currently selected letters.
* @return The string representation of the currently selected letters.
*/
public void pickLetter(int x, int y){
cur_word.add(board_letters.get(y).get(x));
}
public void removeLetter(){
cur_word.remove(cur_word.size()-1);
}
public Letter getLastLetter(){
return cur_word.get(cur_word.size()-1);
}
public String getCurWordString() {
String string_current_word = "";
for(int i = 0; i < cur_word.size(); i++){
string_current_word+=cur_word.get(i).getCharacter();
}
return string_current_word;
}
//clears the current word
public void clearCurWord(){
cur_word = new ArrayList<Letter>();
}
}