Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.noisycloud;
- import java.io.IOException;
- import java.util.Random;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import com.google.appengine.api.datastore.*;
- @SuppressWarnings("serial")
- public class Shakespearemonkey extends HttpServlet {
- static final String lstAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- static final int intNumLoops = 1000;
- static String Shakespeare = "All your base are belong to me";
- static Key theResultsKey = KeyFactory.createKey("Results","tblMonkeyResults");
- DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
- private static char generateRandomLetter(){
- //I generate a random character and return it.
- Random rnd = new Random();
- char strChar = lstAlphabet.charAt(rnd.nextInt(lstAlphabet.length()));
- return strChar;
- }
- private void writeResultsToFile(int intKeyStrokes, String strBestGuess){
- //I write the number of keystrokes and the best guess so far to the datastore
- Entity objDaoOut = new Entity("Results", "tblMonkeyResults");
- objDaoOut.setProperty("intKeyStrokes",intKeyStrokes);
- objDaoOut.setProperty("strBestGuess", strBestGuess);
- datastore.put(objDaoOut);
- }
- private MonkeyResults readResultsFromFile() {
- //I read results from the datastore and return an instance of class MonkeyResults
- MonkeyResults monkeyResults = new MonkeyResults();
- long intStrokes = 0;
- String strGuess = "";
- try{
- Entity objDaoIn = datastore.get(theResultsKey);
- intStrokes = (long) objDaoIn.getProperty("intKeyStrokes");
- strGuess = (String) objDaoIn.getProperty("strBestGuess");
- }catch(EntityNotFoundException e){
- //e.printStackTrace();
- }finally{
- monkeyResults.setKeyStrokes((int)intStrokes);
- monkeyResults.setBestGuess(strGuess);
- }
- return monkeyResults;
- }
- private void trackProgress(int intKeyStrokes, String strBestGuess) {
- String strBestGuessFromFile = "";
- int intKeyStrokesFromFile = 0;
- String strNewBestGuess = "";
- int intNewStrokes = 0;
- MonkeyResults monkeyResults = readResultsFromFile();
- if(monkeyResults.getKeyStrokes() != 0 && monkeyResults.getBestGuess() != ""){
- strBestGuessFromFile = (String) monkeyResults.getBestGuess();
- intKeyStrokesFromFile = (int) monkeyResults.getKeyStrokes();
- }
- //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);
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
- int intCount = 0;
- String strSubString = "";
- String strShakespeare = Shakespeare.replaceAll("[^a-zA-Z]", ""); //Shakespeare without spaces etc
- String strMonkeyString = "";
- String strBestSoFar = "";
- response.setContentType("text/html");
- while (intCount < intNumLoops) {
- intCount++;
- //generate the random guess, one keystroke at a time
- strMonkeyString = strMonkeyString + Character.toString(generateRandomLetter());
- strSubString = strShakespeare.substring(0,strMonkeyString.length());
- //check if we've guessed correctly so far
- if(strMonkeyString.equalsIgnoreCase(strSubString)){
- //Is this our best guess so far
- if(strSubString.length() > strBestSoFar.length()){
- strBestSoFar = strSubString;
- }
- }else{
- //incorrect guess, start again
- strMonkeyString = "";
- }
- }
- trackProgress(intCount,strBestSoFar);
- //purely for output, re-read the latest and update user on progress
- try {
- response.getWriter().println("Good Morning, I am your monkey! I will be trying to guess the string: " + strShakespeare + "<br />");
- MonkeyResults monkeyresults = readResultsFromFile();
- response.getWriter().println("My best guess so far is: ");
- response.getWriter().println((String) monkeyresults.getBestGuess());
- response.getWriter().println("<br />I have made ");
- response.getWriter().println((int) monkeyresults.getKeyStrokes());
- response.getWriter().println("keystrokes");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement