-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputValidation.java
211 lines (182 loc) · 6.87 KB
/
InputValidation.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
// Create an instantiable class to manage input validation.
// Import Scanner class.
import java.util.Scanner;
public class InputValidation{
// Declare/initialise instance variables.
private char randomLetter;
private String word;
private String previousWord = " ";
private String previousCorrectWord = " ";
private boolean valid;
private String vowels = "aeiou";
private int numberOfA = 0;
private int numberOfE = 0;
private int numberOfI = 0;
private int numberOfO = 0;
private int numberOfU = 0;
private int score = 0;
// Declare a constructor with 2 parameters that take in the word and previous word.
public InputValidation(String fpWord, String fpPreviousWord) {
this.word = fpWord;
this.previousWord = fpPreviousWord;
}
// Setter method to assign a value to the instance variable randomLetter.
public void setRandomLetter(char randomLetter){
this.randomLetter = randomLetter;
}
// Getter
public int getScore() {
return score;
}
// Method to validate words (takes in 1 parameter - word).
public void validateWord(String word){
// Create object of LimitedVocabulary class if a word is input for validation.
if (!word.equals("-")){
LimitedVocabulary limitedVocabulary = new LimitedVocabulary(word);
// If a previous word entered that isn't correct, we will use the previous correct word to compare words against.
if (!previousCorrectWord.equals(" ")) {
previousWord = previousCorrectWord;
}
// If no previous word (For the first turn of the first round).
if (previousWord.equals(" ")){
// The word is validated against a computer generated letter, not any previous word.
if (limitedVocabulary.verifyWord(word) && word.length() >= 3 && randomLetter == word.charAt(0)){
// Display valid word message.
System.out.println("Well done! " + word + " is a valid word!");
System.out.println("");
System.out.println("//////");
System.out.println(" O O");
System.out.println(" < ");
System.out.println(" U ");
System.out.println("");
// Boolean to confirm word validated.
valid = true;
// Add points for correct word.
// Traverse String entered by player to compare word against string of vowels and if a vowel exists, add 1 to the relevant number of that vowel.
for (int x = 0; x < word.length(); x++) {
if ( word.charAt(x) == vowels.charAt(0) ) {
numberOfA = numberOfA +1;
}
if ( word.charAt(x) == vowels.charAt(1) ) {
numberOfE = numberOfE +1;
}
if ( word.charAt(x) == vowels.charAt(2) ) {
numberOfI = numberOfI +1;
}
if ( word.charAt(x) == vowels.charAt(3) ) {
numberOfO = numberOfO +1;
}
if ( word.charAt(x) == vowels.charAt(4) ) {
numberOfU = numberOfU +1;
}
}
// Add points for the number of repeated vowels.
if ( numberOfA > 1) {
score = score + numberOfA;
}
if ( numberOfE > 1) {
score = score + numberOfE;
}
if ( numberOfI > 1) {
score = score + numberOfI;
}
if ( numberOfO > 1) {
score = score + numberOfO;
}
if ( numberOfU > 1) {
score = score + numberOfU;
}
// Display points won for word and total points.
System.out.println("You won " + score + " points!");
System.out.println();
} else {
System.out.println(word + " is an invalid word! No points won.");
System.out.println("");
System.out.println("//////");
System.out.println(" - -");
System.out.println(" < ");
System.out.println(" n ");
System.out.println("");
System.out.println("Remember that the word must start with the computer's letter and be at least 3 characters long.");
System.out.println("Please try again!");
System.out.println("");
// Scanner wordInput3 = new Scanner(System.in);
// word = wordInput3.nextLine().toLowerCase();
// InputValidation inputValidation = new InputValidation(word, previousWord);
// inputValidation.validateWord(word);
// if (word.equals(true)){
// previousCorrectWord = word;
// }
}
// Else statement for all turns other than the first turn.
} else {
// If statement to compare word to previous word - The characters at specified indexes are compared as per the rules.
if (limitedVocabulary.verifyWord(word) && word.length() >= 3 && previousWord.charAt( previousWord.length()-1) == word.charAt(1) && previousWord.charAt(previousWord.length() - 2) == word.charAt(0) ){
// Display message.
System.out.println("Well done! " + word + " is a valid word!");
System.out.println("");
System.out.println("//////");
System.out.println(" O O");
System.out.println(" < ");
System.out.println(" U ");
System.out.println("");
// Boolean to determine if valid.
valid = true;
// Add points for correct word.
// Traverse String entered by player to compare word against string of vowels and if a vowel exists, add 1 to the relevant number of that vowel.
for (int x = 0; x < word.length(); x++) {
if ( word.charAt(x) == vowels.charAt(0) ) {
numberOfA = numberOfA +1;
}
if ( word.charAt(x) == vowels.charAt(1) ) {
numberOfE = numberOfE +1;
}
if ( word.charAt(x) == vowels.charAt(2) ) {
numberOfI = numberOfI +1;
}
if ( word.charAt(x) == vowels.charAt(3) ) {
numberOfO = numberOfO +1;
}
if ( word.charAt(x) == vowels.charAt(4) ) {
numberOfU = numberOfU +1;
}
}
// Add points for the number of repeated vowels.
if ( numberOfA > 1) {
score = score + numberOfA;
}
if ( numberOfE > 1) {
score = score + numberOfE;
}
if ( numberOfI > 1) {
score = score + numberOfI;
}
if ( numberOfO > 1) {
score = score + numberOfO;
}
if ( numberOfU > 1) {
score = score + numberOfU;
}
// Display points won for word and total points.
System.out.println("You won " + score + " points!");
System.out.println();
} else {
System.out.println(word + " is an invalid word! No points won.");
System.out.println("");
System.out.println("//////");
System.out.println(" - -");
System.out.println(" < ");
System.out.println(" n ");
System.out.println("");
System.out.println("Remember that the word must being with the last 2 letters of the previous word and be at least 3 characters long.");
System.out.println("Please try again!");
System.out.println("");
}
}
}
}
// Create Getter to see if words are valid or not.
public boolean getValid(){
return valid;
}
}