//******************************************************************** // Class Name: HangedMan.java // Assignment: Program #5: Introduction to Arrays // // Author: Prof. C Scheftic // Email: scheftic@csc.calpoly.edu // URL: www.csc.calpoly.edu/~scheftic/courses/101/Pgms/Pgm5/program_5.html // Course: CSC/CPE 101, Sections: 03 and 06 // Term Spring 2001 // School: California Polytechnic State University, // San Luis Obispo, CA 93407 USA // // History: // 11/2000 Nico developed original program; Scheftic reviewed it per specs // 3/2001 Scheftic: minor modifications, per current style guidelines // 5/2001 Scheftic: added drawWord() method and additional documentation // //******************************************************************** // // Class Description and Design Overview: // create players for a hangman game, log errors for the player, and // permit access to and display of appropriate status elements. // // Imported Classes: // java.lang (by default) // // Imported Methods: // System.out.println() // // Included Methods: // Constructor: // public HangedMan() // initializes badness for each new player object instantiated // Accessor: // public void drawPicture() // draws appropriately imperiled stick figure (for the current badness) // public void drawWord(String word, boolean[] guessed) // displays correct letter-guesses in relevant position(s) within word and // displays underscores to represent unguessed letters // public boolean isDead() // returns true if the number of errors logged (via badness) // has passed the limit (established by DEAD) // Mutator: // public void increaseBadness() // increments the number of errors (badness) for the HangedMan player // // Instance Variables (Fields): // private int howbad // represents how many errors have been logged for this player // // Class constants: // private static final int DEAD=6 // represents the maximum number of errors permitted per game // value corresponds to number of bodyparts permitted in drawPicture() // (one head, one trunk, two arms, two legs) // // Potential Improvements: // Could extend a round by drawing more body parts (e.g., 2 hands, 2 feet) // and increasing the corresponding value of DEAD. // // Pending Problems: // None known as of 23 May 2001. //******************************************************************** public class HangedMan { private static final int DEAD=7; // should match max valid switch value private int howbad; public HangedMan() { howbad=0; } public void drawPicture() { // build up to this: // ______ // | | // | O // | /|\ // | | // | / \ // |________ switch (howbad) { case 0: System.out.println(" ______ "); System.out.println(" | | "); System.out.println(" | "); System.out.println(" | "); System.out.println(" | "); System.out.println(" | "); System.out.println(" |________ "); break; case 1: System.out.println(" ______ "); System.out.println(" | | "); System.out.println(" | O "); System.out.println(" | "); System.out.println(" | "); System.out.println(" | "); System.out.println(" |________ "); break; case 2: System.out.println(" ______ "); System.out.println(" | | "); System.out.println(" | O "); System.out.println(" | | "); System.out.println(" | "); System.out.println(" | "); System.out.println(" |________ "); break; case 3: System.out.println(" ______ "); System.out.println(" | | "); System.out.println(" | O "); System.out.println(" | | "); System.out.println(" | | "); System.out.println(" | "); System.out.println(" |________ "); break; case 4: System.out.println(" ______ "); System.out.println(" | | "); System.out.println(" | O "); System.out.println(" | /| "); System.out.println(" | | "); System.out.println(" | "); System.out.println(" |________ "); break; case 5: System.out.println(" ______ "); System.out.println(" | | "); System.out.println(" | O "); System.out.println(" | /|\\ "); System.out.println(" | | "); System.out.println(" | "); System.out.println(" |________ "); break; case 6: System.out.println(" ______ "); System.out.println(" | | "); System.out.println(" | O "); System.out.println(" | /|\\ "); System.out.println(" | | "); System.out.println(" | / "); System.out.println(" |________ "); break; case 7: System.out.println(" ______ "); System.out.println(" | | "); System.out.println(" | O "); System.out.println(" | /|\\ "); System.out.println(" | | "); System.out.println(" | / \\ "); System.out.println(" |________ "); break; default: System.out.println("Don't understand badness="+howbad); break; } } public void drawWord(String word, boolean[] guessed) { char c; System.out.print("\n\t"); for(int i = 0; i < word.length(); i++) { c = word.charAt(i); if ( guessed[c - 'a'] ) // this letter has been guessed { System.out.print(c); } else { System.out.print("_"); } } System.out.println(); System.out.println(); } public void increaseBadness() { howbad++; } public boolean isDead() { return (howbad >= DEAD); } }