Advertisement
Guest User

Untitled

a guest
Mar 17th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. package com.noisycloud;
  2.  
  3. import java.io.IOException;
  4. import java.util.Random;
  5.  
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8.  
  9. import com.google.appengine.api.datastore.*;
  10.  
  11.  
  12. @SuppressWarnings("serial")
  13. public class Shakespearemonkey extends HttpServlet {
  14.    
  15.     static final String lstAlphabet     = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  16.     static final int    intNumLoops     = 1000;
  17.     static String       Shakespeare     = "All your base are belong to me";
  18.     static Key          theResultsKey   = KeyFactory.createKey("Results","tblMonkeyResults");
  19.     DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  20.    
  21.     private static char generateRandomLetter(){
  22.         //I generate a random character and return it.
  23.         Random rnd = new Random();
  24.         char strChar = lstAlphabet.charAt(rnd.nextInt(lstAlphabet.length()));
  25.         return strChar;
  26.     }
  27.    
  28.     private void writeResultsToFile(int intKeyStrokes, String strBestGuess){
  29.         //I write the number of keystrokes and the best guess so far to the datastore
  30.         Entity objDaoOut = new Entity("Results", "tblMonkeyResults");
  31.        
  32.         objDaoOut.setProperty("intKeyStrokes",intKeyStrokes);
  33.         objDaoOut.setProperty("strBestGuess", strBestGuess);
  34.  
  35.         datastore.put(objDaoOut);
  36.     }
  37.    
  38.     private MonkeyResults readResultsFromFile() {
  39.         //I read results from the datastore and return an instance of class MonkeyResults
  40.         MonkeyResults monkeyResults = new MonkeyResults();
  41.         long intStrokes = 0;
  42.         String strGuess = "";
  43.        
  44.         try{
  45.             Entity objDaoIn = datastore.get(theResultsKey);
  46.        
  47.             intStrokes  = (long) objDaoIn.getProperty("intKeyStrokes");
  48.             strGuess    = (String) objDaoIn.getProperty("strBestGuess");
  49.         }catch(EntityNotFoundException e){
  50.             //e.printStackTrace();
  51.         }finally{
  52.             monkeyResults.setKeyStrokes((int)intStrokes);
  53.             monkeyResults.setBestGuess(strGuess);
  54.         }
  55.        
  56.         return monkeyResults;
  57.     }
  58.  
  59.     private void trackProgress(int intKeyStrokes, String strBestGuess) {
  60.         String              strBestGuessFromFile    = "";
  61.         int                 intKeyStrokesFromFile   = 0;
  62.         String              strNewBestGuess         = "";
  63.         int                 intNewStrokes           = 0;
  64.         MonkeyResults       monkeyResults           = readResultsFromFile();
  65.        
  66.         if(monkeyResults.getKeyStrokes() != 0 && monkeyResults.getBestGuess() != ""){
  67.             strBestGuessFromFile    = (String) monkeyResults.getBestGuess();
  68.             intKeyStrokesFromFile   = (int) monkeyResults.getKeyStrokes();
  69.         }
  70.  
  71.         //whichever string is longer, this is the new best guess
  72.         if(strBestGuess.length() > strBestGuessFromFile.length()){
  73.             strNewBestGuess     = strBestGuess;
  74.         }else{
  75.             strNewBestGuess     = strBestGuessFromFile;
  76.         }
  77.        
  78.         //number of key strokes is cumulative
  79.         intNewStrokes           = intKeyStrokes + intKeyStrokesFromFile;
  80.        
  81.         writeResultsToFile(intNewStrokes,strNewBestGuess);
  82.     }
  83.    
  84.    
  85.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
  86.         int         intCount                = 0;
  87.         String      strSubString            = "";
  88.         String      strShakespeare          = Shakespeare.replaceAll("[^a-zA-Z]", ""); //Shakespeare without spaces etc
  89.         String      strMonkeyString         = "";
  90.         String      strBestSoFar            = "";
  91.         response.setContentType("text/html");
  92.        
  93.         while (intCount < intNumLoops) {
  94.             intCount++;
  95.            
  96.             //generate the random guess, one keystroke at a time
  97.             strMonkeyString = strMonkeyString + Character.toString(generateRandomLetter());
  98.            
  99.             strSubString    =   strShakespeare.substring(0,strMonkeyString.length());
  100.            
  101.             //check if we've guessed correctly so far
  102.             if(strMonkeyString.equalsIgnoreCase(strSubString)){
  103.                 //Is this our best guess so far
  104.                 if(strSubString.length() > strBestSoFar.length()){
  105.                     strBestSoFar    =   strSubString;
  106.                 }
  107.             }else{
  108.                 //incorrect guess, start again
  109.                 strMonkeyString = "";
  110.             }
  111.         }
  112.        
  113.         trackProgress(intCount,strBestSoFar);
  114.        
  115.         //purely for output, re-read the latest and update user on progress
  116.         try {
  117.             response.getWriter().println("Good Morning, I am your monkey! I will be trying to guess the string: " + strShakespeare + "<br />");
  118.             MonkeyResults monkeyresults = readResultsFromFile();
  119.             response.getWriter().println("My best guess so far is: ");
  120.             response.getWriter().println((String) monkeyresults.getBestGuess());
  121.             response.getWriter().println("<br />I have made ");
  122.             response.getWriter().println((int) monkeyresults.getKeyStrokes());
  123.             response.getWriter().println("keystrokes");
  124.         } catch (IOException e) {
  125.             e.printStackTrace();
  126.         }
  127.        
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement