Skip to content

Instantly share code, notes, and snippets.

@kenechiokolo
Created April 11, 2015 10:13
Show Gist options
  • Save kenechiokolo/6fb4c717348701b069d8 to your computer and use it in GitHub Desktop.
Save kenechiokolo/6fb4c717348701b069d8 to your computer and use it in GitHub Desktop.
CS106A Assignment 4 - Hangman
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 String pickWord() {
// int index = rgen.nextInt(0, lexicon.getWordCount() - 1);
int index = 1;
String wordOfChoice = lexicon.getWord(index);
return wordOfChoice;
}
// 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() {
for (turns = 0; turns < N_HANGMAN_PARTS; turns++) {
showProgress();
playerGuess();
checkGuess();
if (correctGuess == true) {
// revealInstances();
}
if (correctGuess == false) {
logWrongGuess();
addSegment();
}
}
if (win == false) {
lossMessage();
}
if (win == true) {
victoryMessage();
}
playAgain();
}
// adds incorrect guess to a String containing all the incorrect guesses
private void logWrongGuess() {
wrongGuess = wrongGuess + " - " + userGuess;
}
// 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 turnsLeft = N_HANGMAN_PARTS - turns;
println("You have "+turnsLeft+" turns 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! Better luck next time...");
}
// Woop woop! The user won! This message lets them know
private void victoryMessage() {
println("Congratulations! You won!");
}
// 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. ");
if (rematch == "y") {
gameplay();
} else {
println("You have chosen not to play again. You may now close this window.");
}
}
private RandomGenerator rgen = RandomGenerator.getInstance(); // random generator to choose word from lexicon at random
private String word = pickWord(); // 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 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