Skip to content

Instantly share code, notes, and snippets.

@kenechiokolo
Created April 12, 2015 14:36
Show Gist options
  • Save kenechiokolo/8a3d9091e93e43fde405 to your computer and use it in GitHub Desktop.
Save kenechiokolo/8a3d9091e93e43fde405 to your computer and use it in GitHub Desktop.
Hangman Game (CS106A Assignment 4 Part I)
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.awt.*;
public class Hangman_II extends ConsoleProgram {
public HangmanLexicon lexicon = new HangmanLexicon(); // creates an object of type HangmanLexicon
private final static int N_HANGMAN_PARTS = 8; // number of parts to draw to complete hangman drawing
// runs the program
public void run() {
welcomeMessage();
setup();
gameplay();
}
// sets up the game by choosing a word and creating its hidden version
private void setup() {
pickWord();
drawHiddenWord();
}
// simple welcome message
private void welcomeMessage() {
println("Welcome to Hangman. ");
}
// picks a word at random from the lexicon
private void pickWord() {
int index = rgen.nextInt(0, lexicon.getWordCount() - 1);
word = lexicon.getWord(index);
}
// creates String of dashes same length as chosen word
private void drawHiddenWord() {
for (int i = 0; i < word.length(); i++) {
hiddenWord = hiddenWord + "-";
}
}
// Plays the game of hangman. Assumes word has been chosen in setup.
private void gameplay() {
while (nWrong < N_HANGMAN_PARTS && win == false) {
showProgress();
playerGuess();
checkGuess();
if (correctGuess == true) {
logCorrectGuess();
revealInstances();
}
if (correctGuess == false) {
logWrongGuess();
addSegment();
}
turns++;
if (hiddenWord.equals(word) == true) {
win = true;
}
}
if (win == false) {
lossMessage();
}
if (win == true) {
victoryMessage();
}
playAgain();
}
// replace dashes in hiddenWord with any instances of user's guess
private void revealInstances() {
String result = ""; // creates new String 'result'
/* Checks all letters in word one by one (until i = word length)
* at each index position i, checks to see if char is the same as guess (a correct instance)
* if it is - the user's guess is added to a new String 'result'
* if it isn't, this method adds to result a dash or previously revealed letter
* depending on what is present at that index in the hidden/partially hidden word hiddenWord
*/
for (int i=0; i < word.length(); i++) {
if (word.charAt(i) == guess) {
result += guess;
} else {
result += hiddenWord.charAt(i);
}
}
hiddenWord = result; // once operation is complete, sets hiddenWord equal to result
}
// adds incorrect guess to a String containing all the incorrect guesses
private void logWrongGuess() {
wrongGuess = wrongGuess + " - " + guess;
nWrong++;
println("There are no "+guess+"'s in the word. ");
}
private void logCorrectGuess() {
revealedLetters = revealedLetters + " - " + guess;
println("Your guess is correct! ");
}
// logs that a new segment has been added to the hangman drawing
private void addSegment() {
segmentsDrawn++;
}
// checks guess and assigns correctGuess as either true or false
private void checkGuess() {
guess = userGuess.charAt(0);
guess = Character.toUpperCase(guess);
int anyInstance = word.indexOf(guess);
if (anyInstance != -1) {
correctGuess = true;
}
if (anyInstance == -1) {
correctGuess = false;
}
}
// displays word progress and number of turns left
private void showProgress() {
println("The word now looks like this: "+hiddenWord+"");
int guessesLeft = N_HANGMAN_PARTS - nWrong;
println("You have "+guessesLeft+" guesses left");
}
// Asks user for guess and logs response. Forces guesses to adhere to game rules
private void playerGuess() {
boolean validGuess = false;
while (validGuess == false) {
userGuess = readLine("Your guess is: ");
if (userGuess.length() > 1) {
println("You have entered more than one letter, please try again. ");
}
if (userGuess == null) {
println("You did not register a response. Please try again. ");
}
if (userGuess.length() == 1) {
validGuess = true;
}
}
}
// User lost the game. Sad times :(. Displays a message to that effect
private void lossMessage() {
println("You lost!");
println("The word was "+word+"");
println("Better luck next time!");
}
// Woop woop! The user won! This message lets them know
private void victoryMessage() {
println("Congratulations! You won! The correct word is "+word+"");
}
// asks the user if they want to play again, y indicates yes, any other key ends the program and prompts them to close the window
private void playAgain() {
String rematch = readLine("Press 'y' if you would like to play again. ");
char y = 'y';
if (rematch.indexOf(y) != -1) {
resetParameters();
setup();
gameplay();
} else {
println("You have chosen not to play again. You may now close this window.");
}
}
// resets the parameters if the user chooses to play again
private void resetParameters() {
win = false;
nWrong = 0;
turns = 0;
hiddenWord = "";
}
private RandomGenerator rgen = RandomGenerator.getInstance(); // random generator to choose word from lexicon at random
private String word; // the randomly chosen word
private String hiddenWord = ""; // hidden version (with dashes) of randomly chosen word
private boolean win = false; // whether the user has won the game or not
private boolean correctGuess; // whether the user's guess is correct or not
private int turns; // number of turns taken in the game
private String userGuess; // the user's guess in the form of a String
private char guess; // the user's guess truncated to a char
private String wrongGuess = ""; // record of all wrong guesses made by user
private int nWrong; // number of wrong guesses
private String revealedLetters = ""; // record of letters that have been revealed so far
private int segmentsDrawn = 0; // record of segments of hangman drawing drawn so far
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment