-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWord.java
101 lines (82 loc) · 2.08 KB
/
Word.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
public class Word {
private int row, column; // first letter position
private boolean isHorizontal; // true = horizontal, false = vertical
private String letters;
private String designatedLetters;
Word(int row, int column, boolean isHorizontal, String letters) {
this.row = row;
this.column = column;
this.isHorizontal = isHorizontal;
this.letters = letters;
this.designatedLetters = letters;
}
Word(int row, int column, boolean isHorizontal, String letters, String blankDesignations) {
this.row = row;
this.column = column;
this.isHorizontal = isHorizontal;
this.letters = letters;
// merge blank designations into the letters
StringBuilder designatedLettersBuilder = new StringBuilder(letters);
int j =0 ;
for (int i=0; i<letters.length(); i++) {
if (letters.charAt(i) == Tile.BLANK) {
designatedLettersBuilder.setCharAt(i,blankDesignations.charAt(j));
j++;
}
}
designatedLetters = designatedLettersBuilder + "";
}
// getRow pre-condition: isHorizontal is true
public int getRow() {
return row;
}
// getColumn pre-condition: isHorizonal is flase
public int getColumn() {
return column;
}
public int getFirstRow() {
return row;
}
public int getLastRow() {
if (isHorizontal) {
return row;
} else {
return row + letters.length() - 1;
}
}
public int getFirstColumn() {
return column;
}
public int getLastColumn() {
if (!isHorizontal) {
return column;
} else {
return column + letters.length() - 1;
}
}
public String getLetters() {
return letters;
}
public char getLetter(int i) {
return letters.charAt(i);
}
public String getDesignatedLetters() {
return designatedLetters;
}
public char getDesignatedLetter(int index) {
return designatedLetters.charAt(index);
}
public int length() {
return letters.length();
}
public boolean isHorizontal() {
return isHorizontal;
}
public boolean isVertical() {
return !isHorizontal;
}
@Override
public String toString() {
return letters;
}
}