Functional Requirements
- Words
The game has to randomly choose a word from a list of 20 words stored in an array of a word.
The following words should be part of 20 word list: litter, beanbag, opening, settlement, monkey, turtle, bigbang, comic, statement, iteration, null, integer, float, iPhone, smartphone, version, control, a, i, the
- Display
Display the game as per the example screens.
- Lives
There is no need to show a hang man. Instead each time a letter is picked reshow the lives left.
- Number of characters
Display the number of characters in the word.
- Input letter
Ask the user to input a letter. They cannot input the same letter twice. If the letter is not part of the word they loose a life. If it is correct show the letter in its position in the word. Keep asking for letters until the user runs out of lives. At the end of the game ask if they want to play again
- Javabook Classes
Program must use the javabook classes for data entry and displaying the hangman screens.
- Pictures
A text based version of hangman, using the javabook classes is what is required NOT a picture version
Stage 1 Components
- List of 20 words, array constructed
- Random pick of a word
- Enter letter with error checking
- Enter zero with error checking and exit
Stage 2 Components
- Check if letter used before, if yes ask to re-enter
- Check for multiple character matches
- Keep match position for word update
- if no match – deduct a life, show screen
- if no match and if lives == 0, show screen
- if match – show screen, re-enter at ask for another character
- if match and word guessed, show screen
- Process to place guessed words into blank word to guess
- Check used letters and characters in word loops correct
Stage 3 Components
- Play again screen from won, with error checking
- Play again screen from lost, with error checking
- Play again picks word from list of 20 does not rebuild list
Game Logic
App Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
/* * Author: Agnieszka Przygocka * Student ID: x13121138 * Date: 2013/12/04 */ package hangman; import javabook.*; /** * This class Represents the startup point for the Game */ public class App { /** * @param args the command line arguments */ public static void main(String[] args) { //Objects Declaration and Initialisation MainWindow mWindow = new MainWindow(); InputBox iBox = new InputBox(mWindow); OutputBox oBox = new OutputBox(mWindow); String letters = new String(); String yesOrNo = new String(); //Variable declaration boolean hasWon; boolean exitGame; Game myGame = new Game(); oBox.setVisible(true); oBox.print(myGame.toString()); do { hasWon = false; exitGame = false; letters = iBox.getString("Guess a letter or enter 0 to exit: "); //Clear Game message myGame.setGameMessage(""); if(letters.length() > 1) { do { yesOrNo = iBox.getString("Do you want to guess the whole word? [y/n]"); if (yesOrNo.equalsIgnoreCase("y")) { hasWon = myGame.guessWord(letters); } else { myGame.setGameMessage("Please enter one character!!!"); } }while(!yesOrNo.equalsIgnoreCase("y") && !yesOrNo.equalsIgnoreCase("n") ); } else if (letters.length() == 1) { try { int num = Integer.parseInt(letters); if(num != 0) myGame.setGameMessage("Please enter a valid character !!!"); else exitGame = true; } catch(NumberFormatException nfe) { hasWon = myGame.checkLetter(letters.substring(0,1)); } } else { myGame.setGameMessage("Please enter at least one letter!"); } //Refresh the screen after a letter is entered oBox.clear(); oBox.print(myGame.toString()); if(myGame.getLives() == 0 || hasWon) { do { yesOrNo = iBox.getString("Do you want to play again? [y/n]"); if(yesOrNo.equalsIgnoreCase("y")) { myGame.reset(); //Refresh the screen after reset the game oBox.clear(); oBox.print(myGame.toString()); } else { exitGame = true; } }while(!yesOrNo.equalsIgnoreCase("y") && !yesOrNo.equalsIgnoreCase("n") ); } }while(!exitGame); oBox.println("Bye Bye!!!"); } } |
Game Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
package hangman; /** * This class represents the Interactions with the Game Hangman */ public class Game { //Private Variables Declaration private int lives; //Number of lives left private int lettersGuessedCount; //Number of distinct letters entered private int lettersFoundCount; //Number of letters found in the word private int wordToGuessLength; //Length of the word to guess private Word wordToGuess; //Word to guess (Word type) private StringBuffer lettersGuessed; //Distinct letters entered so far private WordList wordList; //World List object private String gameMessage; //Message to pass to the game /** * Game Class Constructor */ public Game() { this.reset(); } /** * Returns the number of Lives * @return number of lives */ public int getLives() { return this.lives; } /** * Returns the Letters entered by the user * @return String of letters entered by the user */ public String getLettersGuessed() { if(this.lettersGuessedCount > 0) return this.lettersGuessed.toString(); //return String.valueOf(this.lettersFoundCount); else return "none"; } /** * Returns the length of the word to guess * @return length of the word to guess */ public int getWordToGuessLength() { return this.wordToGuessLength; } /** * Compares a word entered by the user with the word of the game * @param word String containing a word * @return boolean value whether the word matches or not. */ public boolean guessWord(String word) { if(this.wordToGuess.compareWord(word)) { this.gameMessage = getYouWinMessage(); return true; } else { this.lives = 0; this.gameMessage = getYouLoseMessage(); return false; } } /** * Takes a live from the user */ public void takeLive() { this.lives--; } /** * Checks if a letter exists in the a word * @param letter single character * @return returns boolean value whether the user won or not */ public boolean checkLetter(String letter) { boolean foundLetter = false; boolean hasWon = false; //If the word was not entered before we will check this out. if(!this.lettersGuessed.toString().contains(letter)) { addLetterGuessed(letter); this.lettersGuessedCount++; for(int i = 0; i < wordToGuessLength; i++) { if(this.wordToGuess.getPlainWord().toLowerCase().charAt(i) == letter.toLowerCase().charAt(0)) { //FOUND A MATCH this.wordToGuess.setLetterFound(i); foundLetter = true; // if(foundLetter) this.lettersFoundCount++; if(lettersFoundCount == this.wordToGuessLength) { this.gameMessage = getYouWinMessage(); hasWon = true; } } } //IF NO MATCH WAS FOUND THEN TAKE LIFE. if(!foundLetter) { this.takeLive(); if(this.lives == 0) this.gameMessage = getYouLoseMessage(); } } else { setGameMessage("Letter already entered! Please re-enter another one!"); } return hasWon; } /** * Adds letter to the String of letters entered by the user * @param letter Single character */ private void addLetterGuessed(String letter) { if(lettersGuessed.length() > 0) { this.lettersGuessed.append(","+letter); } else { this.lettersGuessed.append(letter); } } public void setGameMessage(String message) { this.gameMessage = message; } /** * Returns the Win Message * @return String containing the winning message */ private String getYouWinMessage() { return "You Won!!! Congratulations"; } /** * Returns the Losing Message * @return String containing the losing message */ private String getYouLoseMessage() { return "You Lose!\nThe word was: "+this.wordToGuess.getPlainWord(); } /** * Prepares the game to start from the beginning */ public void reset() { this.wordList = new WordList(); this.wordToGuess = new Word(this.wordList.getRandomWord()); this.lettersGuessed = new StringBuffer(); this.wordToGuessLength = this.wordToGuess.getWordLength(); this.lives = 6; this.lettersGuessedCount = 0; this.lettersFoundCount = 0; this.gameMessage = new String(); } /** * Override implementation of the toString method * @return Customised String implemented */ @Override public String toString() { return "\n----------------------------"+ "\nLives: "+ this.getLives() + "\nWord: "+this.wordToGuess.toString().toUpperCase() + "\nLetters guessed so far: "+this.getLettersGuessed().toUpperCase()+ "\nMessage: "+ this.gameMessage+ "\n----------------------------\n"; } } |
Letter Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package hangman; /** * This class represents a letter in a Word */ public class Letter { //Private Variables Declaration private char character; private boolean visible; /** * Letter Class Constructor * @param character single character from a word */ public Letter(char character) { this.character = character; this.visible = false; } /** * Sets the visibility of the Letter object. * @param visible boolean value to set the object visible or not */ public void setVisible(boolean visible) { this.visible = visible; } /** * Override of the method toString * @return Returns String build from a new implementation of the method toString */ @Override public String toString() { if(this.visible) return String.valueOf(this.character); else return "_"; } } |
Word Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
package hangman; /** * This class represents a Word from a list of Words */ public class Word { //Private Variables Declaration private Letter[] word; private String plainWord; private StringBuilder sb; /** * Word Class Constructor * @param word String containing a plain word */ public Word(String word) { this.word = new Letter[word.length()]; this.plainWord = word; for(int i=0; i<word.length(); i++) { this.word[i] = new Letter(word.charAt(i)); } } /** * Returns the Length of the Word * @return */ public int getWordLength() { return this.word.length; } /** * Compares an inputed word against the game word * @param word word entered by the user * @return Returns a boolean value whether the word is equal or not */ public boolean compareWord(String word) { if(word.equals(this.plainWord)) { for(int i=0;i<this.getWordLength();i++) { this.word[i].setVisible(true); } return true; } else { return false; } } /** * Returns a plain String game word * @return String type word */ public String getPlainWord() { return this.plainWord; } /** * Sets the letter Found by the user * @param position */ public void setLetterFound(int position) { this.word[position].setVisible(true); } /** * Returns an override implementation of the method toString * @return a new String */ @Override public String toString() { sb = new StringBuilder(); for(int i=0; i<this.word.length; i++) { sb.append(this.word[i].toString()); } return sb.toString(); } } |
Word List Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package hangman; import java.util.Random; /** * This class represents the list of Words */ public class WordList { //Private Variables Declaration String[] words; Random r; /** * WorldList Class Constructor */ public WordList() { r = new Random(); words = new String[]{"litter" ,"beanbag" ,"opening" ,"settlement" ,"monkey" ,"turtle" ,"bigbang" ,"comic" ,"statement" ,"iteration" ,"null" ,"integer" ,"float" ,"iphone" ,"smartphone" ,"version" ,"control" ,"a" ,"i" ,"the"}; } /** * Returns a Random Word from a list of Words * @return String word */ public String getRandomWord() { return this.words[r.nextInt(this.words.length)]; } } |