Advertisement
Guest User

Untitled

a guest
Mar 17th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1.  
  2.  
  3.     import java.util.Random;
  4.     import java.io.*;
  5.  
  6.     import org.json.simple.JSONObject;
  7.     import org.json.simple.parser.JSONParser;
  8.     import org.json.simple.parser.ParseException;
  9.  
  10.     public class hamletsMonkey {
  11.  
  12.         static final String lstAlphabet     = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  13.         static final int    intNumLoops     = 100;
  14.         static String       Shakespeare     = "All your base are belong to me";
  15.         static String       strFileName     = "monkeyRed.txt";
  16.        
  17.        
  18.         private static char generateRandomLetter(){
  19.             Random rnd = new Random();
  20.             char strChar = lstAlphabet.charAt(rnd.nextInt(lstAlphabet.length()));
  21.             return strChar;
  22.         }
  23.        
  24.         @SuppressWarnings("unchecked")
  25.         private static void writeResultsToFile(long intKeyStrokes, String strBestGuess){
  26.            
  27.             //create json object
  28.             JSONObject obj = new JSONObject();
  29.             obj.put("intKeyStrokes", intKeyStrokes);
  30.             obj.put("strBestGuess", strBestGuess);
  31.            
  32.             //write it to file
  33.             try {
  34.                 FileWriter file = new FileWriter(strFileName);
  35.                 file.write(obj.toJSONString());
  36.                 file.flush();
  37.                 file.close();
  38.          
  39.             } catch (IOException e) {
  40.                 e.printStackTrace();
  41.             }
  42.         }
  43.        
  44.         private static JSONObject readResultsFromFile(){
  45.             JSONParser parser = new JSONParser();
  46.             JSONObject jsonObject = new JSONObject();
  47.            
  48.             try {
  49.                 //read from file
  50.                 Object obj = parser.parse(new FileReader(strFileName));
  51.                 jsonObject = (JSONObject) obj;
  52.          
  53.             } catch (FileNotFoundException e) {
  54.                 e.printStackTrace();
  55.             } catch (IOException e) {
  56.                 e.printStackTrace();
  57.             } catch (ParseException e) {
  58.                 e.printStackTrace();
  59.             }
  60.             return jsonObject;
  61.         }
  62.        
  63.        
  64.         private static void trackProgress(int intKeyStrokes, String strBestGuess){
  65.             JSONObject  jsonObject              = readResultsFromFile();
  66.             String      strBestGuessFromFile    = "";
  67.             long        intKeyStrokesFromFile   = 0;
  68.             String      strNewBestGuess         = "";
  69.             long        intNewStrokes           = 0;
  70.            
  71.             if(jsonObject.get("intKeyStrokes") != null && jsonObject.get("strBestGuess") != null){
  72.                 strBestGuessFromFile    = (String) jsonObject.get("strBestGuess");
  73.                 intKeyStrokesFromFile   = (long) jsonObject.get("intKeyStrokes");
  74.             }
  75.      
  76.             //whichever string is longer, this is the new best guess
  77.             if(strBestGuess.length() > strBestGuessFromFile.length()){
  78.                 strNewBestGuess     = strBestGuess;
  79.             }else{
  80.                 strNewBestGuess     = strBestGuessFromFile;
  81.             }
  82.            
  83.             //number of key strokes is cumulative
  84.             intNewStrokes           = intKeyStrokes + intKeyStrokesFromFile;
  85.            
  86.             writeResultsToFile(intNewStrokes,strNewBestGuess);
  87.             System.out.println("Finishing - " + intNewStrokes + " keystrokes-" + strNewBestGuess);
  88.         }
  89.        
  90.         public static void main(String[] args) {
  91.             int         intCount                = 0;
  92.             String      strSubString            = "";
  93.             String      strShakespeare          = Shakespeare.replaceAll("[^a-zA-Z]", ""); //Shakespeare without spaces etc
  94.             String      strMonkeyString         = "";
  95.             String      strBestSoFar            = "";
  96.            
  97.             while (intCount < intNumLoops) {
  98.                 intCount++;
  99.                
  100.                 //generate the random guess, one keystroke at a time
  101.                 strMonkeyString = strMonkeyString + Character.toString(generateRandomLetter());
  102.                
  103.                 strSubString    =   strShakespeare.substring(0,strMonkeyString.length());
  104.                
  105.                 System.out.print(strMonkeyString + "==" + strSubString);
  106.        
  107.                 //check if we're done
  108.                 if(strMonkeyString.equalsIgnoreCase(strSubString)){
  109.                     System.out.println(" ** Nice guess, move along please.");
  110.                     if(strSubString.length() > strBestSoFar.length()){
  111.                         strBestSoFar    =   strSubString;
  112.                     }
  113.                 }else{
  114.                     System.out.println("");
  115.                     strMonkeyString = "";
  116.                 }
  117.             }
  118.            
  119.             trackProgress(intCount,strBestSoFar);
  120.            
  121.         }
  122.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement