import java.io.*; import java.util.Random; import java.util.Scanner; public class WordGame { public static void main(String[] args) throws IOException { final int RANDOM_LETTERS_COUNT = 10; final int TRIALS = 10; int score = 0; boolean invalidLetter; char[] letterPool = new char[RANDOM_LETTERS_COUNT]; String[] userEntries = new String[TRIALS]; // Initialise dictionary. System.out.print("\n\nLoading dictionary... "); Dictionary dictionary = new Dictionary("brit-a-z.txt"); System.out.println("done\nDictionary contains " + dictionary.words.length + " words."); String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int index = (int)Math.floor(Math.random() * 26.0); String name; String word; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); // Read all the words from the dictionary (brit-a-z.txt) into an array BufferedReader fr = new BufferedReader(new FileReader("brit-a-z.txt")); Random r = new Random(); char[] letters = new char[10]; int[] points = new int[10]; for(int j = 0; j < 10; j++){ //System.out.print(alphabet.charAt(r.nextInt(alphabet.length()))); letters[j] = alphabet.charAt(r.nextInt(alphabet.length())); System.out.print("\b"); } for(int trial = 0; trial < TRIALS; trial++) { System.out.println("\nTrial " + (trial + 1) + " of " + TRIALS + ": "); System.out.println(letters); String[] wordsUsed = new String[10]; System.out.print("Enter word: "); name = br.readLine(); word = name.toUpperCase(); for(int k=trial; k < TRIALS; k++) { wordsUsed[k] = word; if (wordsUsed[k].indexOf(word) !=-1){ //if the word is present System.out.println(word + " not in dictionary "); trial = k; break; } else if (wordsUsed[k].indexOf(word) ==-1){ //word is not in array /*String[] search = readWords("brit-a-z.txt");*/ System.out.println(word + " was entered earlier"); trial = k; break; } /*letters[k] = if (letters[k].indexOf(word) !=-1){ System.out.println(word + "is an invalid word"); }*/ } /*for(int i=0; i<word.length(); i++) { word[i] = word[i].trim(); }*/ //search array in word //if statement - if word is already used, - let user try new word + not increase trials //is word is not used then continue WordGame.validWord(word, letters, score); points[trial] = word.length(); score += word.length(); } // Report total score at the end of the game. System.out.println("\nTotal score: " + score); } // Returns a random letter from the alphabet. static char getRandomLetter() { String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int index = (int)Math.floor(Math.random() * 26.0); return alphabet.charAt(index); } // Checks is a word can be formed from a set of letters. static boolean validWord(String word, char[] letters, int score) { boolean invalidLetter; char[] copyOfLetters = letters.clone(); for(int i = 0; i < word.length(); i++) { invalidLetter = true; for(int j = 0; j < copyOfLetters.length; j++) { if(word.charAt(i) == copyOfLetters[j]) { invalidLetter = false; copyOfLetters[j] = '*'; break; } } if(invalidLetter) { return false; } //number of points } int points = word.length(); System.out.println(points + " points for " + word); return true; } }