forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrabble.java
More file actions
44 lines (33 loc) · 1.24 KB
/
Copy pathScrabble.java
File metadata and controls
44 lines (33 loc) · 1.24 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
import java.util.Scanner;
public class Scrabble {
public static void main(String[] args) {
String word, letters;
int[] letterHist, wordHist;
boolean result;
Scanner input = new Scanner(System.in);
System.out.println("Let's play Scrabble. Can you spell your word with the letters you have?");
System.out.print("What letters do you have? ");
letters = input.nextLine();
System.out.print("What word are you trying to spell? ");
word = input.nextLine();
letterHist = createLetterHistogram(letters);
wordHist = createLetterHistogram(word);
result = canSpell(wordHist, letterHist);
System.out.print("Can \"" + word + "\" be spelled with " + letters + "? ");
System.out.println(result);
}
public static int[] createLetterHistogram(String string) {
int[] stringHist = Ex2.letterHist(string);
return stringHist;
}
public static boolean canSpell(int[] word, int[] letters) {
boolean enoughLetters = true;
for (int i = 0; i < word.length; i++) {
if (letters[i] < word[i]) {
enoughLetters = false;
break;
}
}
return enoughLetters;
}
}