Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.io.*;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- public class hamletsMonkey {
- static final String lstAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- static final int intNumLoops = 100;
- static String Shakespeare = "All your base are belong to me";
- static String strFileName = "monkeyRed.txt";
- private static char generateRandomLetter(){
- Random rnd = new Random();
- char strChar = lstAlphabet.charAt(rnd.nextInt(lstAlphabet.length()));
- return strChar;
- }
- @SuppressWarnings("unchecked")
- private static void writeResultsToFile(long intKeyStrokes, String strBestGuess){
- //create json object
- JSONObject obj = new JSONObject();
- obj.put("intKeyStrokes", intKeyStrokes);
- obj.put("strBestGuess", strBestGuess);
- //write it to file
- try {
- FileWriter file = new FileWriter(strFileName);
- file.write(obj.toJSONString());
- file.flush();
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static JSONObject readResultsFromFile(){
- JSONParser parser = new JSONParser();
- JSONObject jsonObject = new JSONObject();
- try {
- //read from file
- Object obj = parser.parse(new FileReader(strFileName));
- jsonObject = (JSONObject) obj;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return jsonObject;
- }
- private static void trackProgress(int intKeyStrokes, String strBestGuess){
- JSONObject jsonObject = readResultsFromFile();
- String strBestGuessFromFile = "";
- long intKeyStrokesFromFile = 0;
- String strNewBestGuess = "";
- long intNewStrokes = 0;
- if(jsonObject.get("intKeyStrokes") != null && jsonObject.get("strBestGuess") != null){
- strBestGuessFromFile = (String) jsonObject.get("strBestGuess");
- intKeyStrokesFromFile = (long) jsonObject.get("intKeyStrokes");
- }
- //whichever string is longer, this is the new best guess
- if(strBestGuess.length() > strBestGuessFromFile.length()){
- strNewBestGuess = strBestGuess;
- }else{
- strNewBestGuess = strBestGuessFromFile;
- }
- //number of key strokes is cumulative
- intNewStrokes = intKeyStrokes + intKeyStrokesFromFile;
- writeResultsToFile(intNewStrokes,strNewBestGuess);
- System.out.println("Finishing - " + intNewStrokes + " keystrokes-" + strNewBestGuess);
- }
- public static void main(String[] args) {
- int intCount = 0;
- String strSubString = "";
- String strShakespeare = Shakespeare.replaceAll("[^a-zA-Z]", ""); //Shakespeare without spaces etc
- String strMonkeyString = "";
- String strBestSoFar = "";
- while (intCount < intNumLoops) {
- intCount++;
- //generate the random guess, one keystroke at a time
- strMonkeyString = strMonkeyString + Character.toString(generateRandomLetter());
- strSubString = strShakespeare.substring(0,strMonkeyString.length());
- System.out.print(strMonkeyString + "==" + strSubString);
- //check if we're done
- if(strMonkeyString.equalsIgnoreCase(strSubString)){
- System.out.println(" ** Nice guess, move along please.");
- if(strSubString.length() > strBestSoFar.length()){
- strBestSoFar = strSubString;
- }
- }else{
- System.out.println("");
- strMonkeyString = "";
- }
- }
- trackProgress(intCount,strBestSoFar);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement