/* * LuckyOccurrenceCounter.java * * Created on January 13, 2003, 7:35 PM */ package project1; /** This oracle, and its omen, are written to take advantage of code * already in LuckTester and LuckTesterOmen. * * */ public class LuckyOccurrenceCounter implements IOracle { /** Oracle will try this many times. */ private static final int maxTrials = 1000000; /** Creates a new instance of LuckyOccurrenceCounter */ public LuckyOccurrenceCounter() { } /** See if some data is suitable to be presented to the oracle. * For example, the God of Luck required initials (2 upper case characters) * as request data. * @param requestData the data, as a string; exact format and requirements * are oracle-dependent. For this oracle, the input must be a string * of exactly two uppercase English letters. * @return true iff the requestData is acceptable (can be processed without * error) by the oracle. */ public boolean checkRequestData(String requestData) { return LuckTester.checkInitials(requestData); // all the work is done there. } /** Tell the name of this oracle. * @return A short string with the name of the oracle; there should be * no leading or trailing whitespace. */ public String getName() { return "Lucky Occurrence Counter"; } /** Search the entrails and return the omen found. * It should never go into an infinite loop. * @param requestData The information (typically a name) about which the oracle * is searching for information (for an omen). * @return an omen, or null if there is no omen at all. */ public IOmen searchEntrailsForOmen(String initials) { final int matchesNeeded = maxTrials; //force it to keep going final int count = LuckTester.matchInitialsRandomly(initials, maxTrials, matchesNeeded); final String shortInterpretation = this.interpret(count); final IOmen retOmen = new LuckyOccurrencesOmen(count, shortInterpretation); return retOmen; } /** Report on how lucky a result is. @param score a value representing the number of matches found. * @return an English message explaining or evaluating the result. For example, * it could be "You are truly beloved of Fortuna" or "You need to increase * your offerings or disaster will befall", as appropriate. */ private String interpret(double score) { final double aSize = 26.0; //alphabet size final double trials = 1000000; // should probably be instance variables final double expectation = trials/(aSize*aSize); //expected number of matches if (score > 2 * expectation) { return "Incredible Luck! You are truly beloved of the gods."; } if (score > expectation) { return "The gods smile upon you."; } if (score < expectation/5) { return "EXTREME DANGER -- the gods are wrathful unto thee."; } if (score < expectation/2) { return "Caution -- luck does not favor you."; } else { return "The gods of luck are just a tiny bit displeased."; } //end interpret } }