Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.inksaver.java;
- /*
- Hangman console version Java
- You will need all 3 class files:
- Main.java http://pastebin.com/nbuy3y90
- Hangman.java http://pastebin.com/chsL0dbY
- Dictionary.java http://pastebin.com/BKkvSap3
- get dictionary.txt from github or elsewhere call it "dictionary.txt" in same folder as src
- https://github.com/dmcguinness/Hangman/blob/master/dictionary.txt
- The methods used here may not be the most efficient, but have been chosen to
- match as far as possible the code used in the Lua and Python versions,
- so they can be directly compared and contrasted.
- */
- import java.io.BufferedReader;
- import java.util.ArrayList;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Random;
- public class Dictionary {
- private static String report = "OK";
- private static boolean fileExists = false;
- //create a list of lists called wordLength
- //create 25 lists to add to the wordLength[] list
- static ArrayList<ArrayList<String>> wordLength = new ArrayList<ArrayList<String>>(25);
- static ArrayList<String> wordList01 = new ArrayList<String>();
- static ArrayList<String> wordList02 = new ArrayList<String>();
- static ArrayList<String> wordList03 = new ArrayList<String>();
- static ArrayList<String> wordList04 = new ArrayList<String>();
- static ArrayList<String> wordList05 = new ArrayList<String>();
- static ArrayList<String> wordList06 = new ArrayList<String>();
- static ArrayList<String> wordList07 = new ArrayList<String>();
- static ArrayList<String> wordList08 = new ArrayList<String>();
- static ArrayList<String> wordList09 = new ArrayList<String>();
- static ArrayList<String> wordList10 = new ArrayList<String>();
- static ArrayList<String> wordList11 = new ArrayList<String>();
- static ArrayList<String> wordList12 = new ArrayList<String>();
- static ArrayList<String> wordList13 = new ArrayList<String>();
- static ArrayList<String> wordList14 = new ArrayList<String>();
- static ArrayList<String> wordList15 = new ArrayList<String>();
- static ArrayList<String> wordList16 = new ArrayList<String>();
- static ArrayList<String> wordList17 = new ArrayList<String>();
- static ArrayList<String> wordList18 = new ArrayList<String>();
- static ArrayList<String> wordList19 = new ArrayList<String>();
- static ArrayList<String> wordList20 = new ArrayList<String>();
- static ArrayList<String> wordList21 = new ArrayList<String>();
- static ArrayList<String> wordList22 = new ArrayList<String>();
- static ArrayList<String> wordList23 = new ArrayList<String>();
- static ArrayList<String> wordList24 = new ArrayList<String>();
- static ArrayList<String> wordList25 = new ArrayList<String>();
- //note a single Random object is reused here
- static Random randomGenerator = new Random();
- // NOT static: can be called by instances of this class
- public String getReport() {
- return report;
- }
- public String getWord(int length){
- String word = "";
- int listLength = 0;
- if (length > 0 && length < 26){//word must be between 1 and 25 characters
- listLength = (wordLength.get(length).size());
- if (listLength > 0) {
- //return string.upper(self.wordLength[length][math.random(1, listLength)])
- int randomInt = randomGenerator.nextInt(listLength + 1);
- word = wordLength.get(length).get(randomInt);
- }
- else {
- word = "";
- }
- }
- return word.toUpperCase();
- }
- //check if dictionary file in place
- private static boolean createWordlist() {
- BufferedReader br = null;
- fileExists = true;
- try {
- br = new BufferedReader(new FileReader("dictionary.txt"));
- String line;
- line = br.readLine();
- System.out.println("Processing 40,000 word dictionary. Please wait...");
- while (line != null) {
- if (line != "") { //ignore empty lines
- if (!line.contains("-") && !line.contains("'")) { //ignore hyphenated and apostrophe words
- switch (line.length() -1){
- case 1:
- wordList01.add(line);
- break;
- case 2:
- wordList02.add(line);
- break;
- case 3:
- wordList03.add(line);
- break;
- case 4:
- wordList04.add(line);
- break;
- case 5:
- wordList05.add(line);
- break;
- case 6:
- wordList06.add(line);
- break;
- case 7:
- wordList07.add(line);
- break;
- case 8:
- wordList08.add(line);
- break;
- case 9:
- wordList09.add(line);
- break;
- case 10:
- wordList10.add(line);
- break;
- case 11:
- wordList11.add(line);
- break;
- case 12:
- wordList12.add(line);
- break;
- case 13:
- wordList13.add(line);
- break;
- case 14:
- wordList14.add(line);
- break;
- case 15:
- wordList15.add(line);
- break;
- case 16:
- wordList16.add(line);
- break;
- case 17:
- wordList17.add(line);
- break;
- case 18:
- wordList18.add(line);
- break;
- case 19:
- wordList19.add(line);
- break;
- case 20:
- wordList20.add(line);
- break;
- case 21:
- wordList21.add(line);
- break;
- case 22:
- wordList22.add(line);
- break;
- case 23:
- wordList23.add(line);
- break;
- case 24:
- wordList24.add(line);
- break;
- case 25:
- wordList25.add(line);
- break;
- }
- }
- }
- line = br.readLine();
- }
- wordLength.add(wordList01);
- wordLength.add(wordList02);
- wordLength.add(wordList03);
- wordLength.add(wordList04);
- wordLength.add(wordList05);
- wordLength.add(wordList06);
- wordLength.add(wordList07);
- wordLength.add(wordList08);
- wordLength.add(wordList09);
- wordLength.add(wordList10);
- wordLength.add(wordList11);
- wordLength.add(wordList12);
- wordLength.add(wordList13);
- wordLength.add(wordList14);
- wordLength.add(wordList15);
- wordLength.add(wordList16);
- wordLength.add(wordList17);
- wordLength.add(wordList18);
- wordLength.add(wordList19);
- wordLength.add(wordList20);
- wordLength.add(wordList21);
- wordLength.add(wordList22);
- wordLength.add(wordList23);
- wordLength.add(wordList24);
- wordLength.add(wordList25);
- //now have ArrayList called wordLength of 25 empty ArrayLists
- }
- catch (IOException e){
- e.printStackTrace();
- }
- finally {
- try {
- br.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- System.out.println(" ");
- return fileExists;
- }
- //Class constructor
- Dictionary() {
- if (!createWordlist()) {
- report = "File 'Dictionary.txt' not found";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement