Skip to content

Instantly share code, notes, and snippets.

@rahulsmehta
Created August 15, 2012 02:12
Show Gist options
  • Save rahulsmehta/3354931 to your computer and use it in GitHub Desktop.
Save rahulsmehta/3354931 to your computer and use it in GitHub Desktop.
EvilHangman Facade Example
//Assume that all necessary packages are imported
public class EvilHangman implements HangmanInterface {
//Assume all private member variables including dictionary and letters guessed,
//num guesses, etc have been instantiated
private int guessesRemaining,wordLength;
//Now here's the important piece: we're going to have the EvilHangman game
//maintain a private, REGULAR hangman game.
private MyHangmanGame realHangman;
public EvilHamgnan(int length,int numGuess){
//Assume all of the private member variables have been set
guessesRemaining = numGuess;
wordLength = wordLength;
realHangman = null;
}
//So now let's take a look at the most important method - makeGuess
public void makeGuess(char guessed){
//Don't worry about this if statement right now. I explain it lower on the page.
if(realHangman != null) realHangman.makeGuess(guessed);
//Do all of the other dictionary processing, etc. We don't need to see that here
//I'm just throwing a dummy method in here -
//assume this method returns true if the committing condition has been reached
//(that is, if the dictionary cannot be "pruned" any farther, or no more words can
//be removed without deleting the entire dictionary)
if(mustCommitToAWord()){
String secretWord = getSecretWord(); // <-- Randomly picks a word from the dictionary
//Now here's the facade part: when the game has to commit, it instantiates a REGULAR
//hangman game. This game will play regular, non-evil hangman until one of the end
//conditions is reached (win or loss).
//We construct the MyHangmanGame with the secret word we randomly picked, as well as
//pass along the number of guesses left. This means that the game seamlessly switches
//into the normal hangman game instance, and passes all of the information over.
realHangman = new MyHangmanGame(secretWord,guessesRemaining);
}
}
public boolean isWin(){
//Here's the magic: we set realHangman to null in the constructor.
//This provides us a really easy way to see whether or not we are playing
//real hangman. At the beginning of every other hangman game method, we check to
//see if the real hangman game has been instantiated. If it has been, we know
//that the game has committed to a word and is playing regular hangman.
//If this is happening, we pass along the information for the method call
//to methods of realHangman. The trick here is that we know MyHangmanGame
//and EvilHangman are both implementing the SAME HangmanGame interface. This means
//that we can call all of the same methods without worrying about something breaking.
if(realHangman != null) return realHangman.isWin();
//Go through the actual logic of determining a win here.
//I stubbed it out because it'd get too verbose to rewrite
//that here.
return (true or false);
}
//I provided a couple more examples of facade methods here. This
//is how you would implement the facade concept on all of the other
//methods in your EvilHangman class.
public String getSecretWord(){
if(realHangman != null) return realHangman.getSecretWord();
return someString;
}
public int numGuessesRemaining(){
if(realHangman != null) return realHangman.numGuessesRemaining();
return guessesRemaining;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment