Advertisement
Inksaver

Hangman Java class: Dictionary (2 of 3)

Sep 4th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.66 KB | None | 0 0
  1. package org.inksaver.java;
  2. /*
  3.     Hangman console version Java
  4.     You will need all 3 class files:
  5.     Main.java       http://pastebin.com/nbuy3y90
  6.     Hangman.java        http://pastebin.com/chsL0dbY
  7.     Dictionary.java     http://pastebin.com/BKkvSap3
  8.     get dictionary.txt from github or elsewhere call it "dictionary.txt" in same folder as src
  9.     https://github.com/dmcguinness/Hangman/blob/master/dictionary.txt
  10.     The methods used here may not be the most efficient, but have been chosen to
  11.     match as far as possible the code used in the Lua and Python versions,
  12.     so they can be directly compared and contrasted.
  13. */
  14. import java.io.BufferedReader;
  15. import java.util.ArrayList;
  16. import java.io.FileReader;
  17. import java.io.IOException;
  18. import java.util.Random;
  19.  
  20. public class Dictionary {  
  21.     private static String report = "OK";
  22.     private static boolean fileExists = false;
  23.    
  24.     //create a list of lists called wordLength
  25.     //create 25 lists to add to the wordLength[] list
  26.     static ArrayList<ArrayList<String>> wordLength = new ArrayList<ArrayList<String>>(25);
  27.     static ArrayList<String> wordList01 = new ArrayList<String>();
  28.     static ArrayList<String> wordList02 = new ArrayList<String>();
  29.     static ArrayList<String> wordList03 = new ArrayList<String>();
  30.     static ArrayList<String> wordList04 = new ArrayList<String>();
  31.     static ArrayList<String> wordList05 = new ArrayList<String>();
  32.     static ArrayList<String> wordList06 = new ArrayList<String>();
  33.     static ArrayList<String> wordList07 = new ArrayList<String>();
  34.     static ArrayList<String> wordList08 = new ArrayList<String>();
  35.     static ArrayList<String> wordList09 = new ArrayList<String>();
  36.     static ArrayList<String> wordList10 = new ArrayList<String>();
  37.     static ArrayList<String> wordList11 = new ArrayList<String>();
  38.     static ArrayList<String> wordList12 = new ArrayList<String>();
  39.     static ArrayList<String> wordList13 = new ArrayList<String>();
  40.     static ArrayList<String> wordList14 = new ArrayList<String>();
  41.     static ArrayList<String> wordList15 = new ArrayList<String>();
  42.     static ArrayList<String> wordList16 = new ArrayList<String>();
  43.     static ArrayList<String> wordList17 = new ArrayList<String>();
  44.     static ArrayList<String> wordList18 = new ArrayList<String>();
  45.     static ArrayList<String> wordList19 = new ArrayList<String>();
  46.     static ArrayList<String> wordList20 = new ArrayList<String>();
  47.     static ArrayList<String> wordList21 = new ArrayList<String>();
  48.     static ArrayList<String> wordList22 = new ArrayList<String>();
  49.     static ArrayList<String> wordList23 = new ArrayList<String>();
  50.     static ArrayList<String> wordList24 = new ArrayList<String>();
  51.     static ArrayList<String> wordList25 = new ArrayList<String>();
  52.    
  53.  
  54.     //note a single Random object is reused here
  55.     static Random randomGenerator = new Random();
  56.    
  57.     // NOT static: can be called by instances of this class
  58.     public String getReport() {
  59.         return report;
  60.     }
  61.    
  62.     public String getWord(int length){
  63.         String word = "";
  64.         int listLength = 0;
  65.        
  66.         if (length > 0 && length < 26){//word must be between 1 and 25 characters
  67.             listLength = (wordLength.get(length).size());
  68.             if (listLength > 0) {
  69.                 //return string.upper(self.wordLength[length][math.random(1, listLength)])
  70.                 int randomInt = randomGenerator.nextInt(listLength + 1);
  71.                 word = wordLength.get(length).get(randomInt);
  72.             }
  73.             else {
  74.                 word = "";
  75.             }
  76.         }
  77.         return word.toUpperCase();
  78.     }
  79.    
  80.     //check if dictionary file in place
  81.     private static boolean createWordlist() {
  82.         BufferedReader br = null;
  83.         fileExists = true;
  84.        
  85.         try {
  86.             br = new BufferedReader(new FileReader("dictionary.txt"));
  87.             String line;
  88.             line = br.readLine();
  89.             System.out.println("Processing 40,000 word dictionary. Please wait...");
  90.             while (line != null)  {
  91.                 if (line != "") { //ignore empty lines
  92.                     if (!line.contains("-") && !line.contains("'")) { //ignore hyphenated and apostrophe words
  93.                         switch (line.length() -1){
  94.                         case 1:
  95.                             wordList01.add(line);
  96.                             break;
  97.                         case 2:
  98.                             wordList02.add(line);
  99.                             break;
  100.                         case 3:
  101.                             wordList03.add(line);
  102.                             break;
  103.                         case 4:
  104.                             wordList04.add(line);
  105.                             break;
  106.                         case 5:
  107.                             wordList05.add(line);
  108.                             break;
  109.                         case 6:
  110.                             wordList06.add(line);
  111.                             break;
  112.                         case 7:
  113.                             wordList07.add(line);
  114.                             break;
  115.                         case 8:
  116.                             wordList08.add(line);
  117.                             break;
  118.                         case 9:
  119.                             wordList09.add(line);
  120.                             break;
  121.                         case 10:
  122.                             wordList10.add(line);
  123.                             break;
  124.                         case 11:
  125.                             wordList11.add(line);
  126.                             break;
  127.                         case 12:
  128.                             wordList12.add(line);
  129.                             break;
  130.                         case 13:
  131.                             wordList13.add(line);
  132.                             break;
  133.                         case 14:
  134.                             wordList14.add(line);
  135.                             break;
  136.                         case 15:
  137.                             wordList15.add(line);
  138.                             break;
  139.                         case 16:
  140.                             wordList16.add(line);
  141.                             break;
  142.                         case 17:
  143.                             wordList17.add(line);
  144.                             break;
  145.                         case 18:
  146.                             wordList18.add(line);
  147.                             break;
  148.                         case 19:
  149.                             wordList19.add(line);
  150.                             break;
  151.                         case 20:
  152.                             wordList20.add(line);
  153.                             break;
  154.                         case 21:
  155.                             wordList21.add(line);
  156.                             break;
  157.                         case 22:
  158.                             wordList22.add(line);
  159.                             break;
  160.                         case 23:
  161.                             wordList23.add(line);
  162.                             break;
  163.                         case 24:
  164.                             wordList24.add(line);
  165.                             break;
  166.                         case 25:
  167.                             wordList25.add(line);
  168.                             break;
  169.                         }
  170.                     }                  
  171.                 }
  172.                 line = br.readLine();
  173.             }  
  174.             wordLength.add(wordList01);
  175.             wordLength.add(wordList02);
  176.             wordLength.add(wordList03);
  177.             wordLength.add(wordList04);
  178.             wordLength.add(wordList05);
  179.             wordLength.add(wordList06);
  180.             wordLength.add(wordList07);
  181.             wordLength.add(wordList08);
  182.             wordLength.add(wordList09);
  183.             wordLength.add(wordList10);
  184.             wordLength.add(wordList11);
  185.             wordLength.add(wordList12);
  186.             wordLength.add(wordList13);
  187.             wordLength.add(wordList14);
  188.             wordLength.add(wordList15);
  189.             wordLength.add(wordList16);
  190.             wordLength.add(wordList17);
  191.             wordLength.add(wordList18);
  192.             wordLength.add(wordList19);
  193.             wordLength.add(wordList20);
  194.             wordLength.add(wordList21);
  195.             wordLength.add(wordList22);
  196.             wordLength.add(wordList23);
  197.             wordLength.add(wordList24);
  198.             wordLength.add(wordList25);
  199.             //now have ArrayList called wordLength of 25 empty ArrayLists
  200.         }  
  201.         catch (IOException e){
  202.             e.printStackTrace();
  203.         }
  204.        
  205.         finally {
  206.             try {
  207.                 br.close();
  208.             }
  209.             catch (IOException e) {
  210.                 e.printStackTrace();
  211.             }
  212.         }
  213.         System.out.println("                                                  ");
  214.         return fileExists;
  215.     }
  216.    
  217.     //Class constructor
  218.     Dictionary() {
  219.         if (!createWordlist()) {
  220.             report = "File 'Dictionary.txt' not found";
  221.         }
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement