Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TODO: comment this file
- import acm.program.*;
- import acm.util.*;
- import java.io.*; // for File
- import java.util.*; // for Scanner
- public class Snowman extends SnowmanProgram {
- public void run() {
- int winCount = 0;
- int gameCount = 0;
- int bestGuessGame = 0;
- intro();
- String file = promptUserForFile("Directory filename? ", "res");
- boolean play = true;
- while (play) {
- int remainingGuesses = playOneGame(getRandomWord(file));
- if (remainingGuesses > 0) {
- winCount++;
- }
- gameCount++;
- bestGuessGame = bestGame(remainingGuesses, bestGuessGame);
- if (readBoolean("Play again (Y/N)?", "y", "n") == false) {
- play = false;
- }
- }
- stats(gameCount, winCount, bestGuessGame);
- }
- /*
- * This method prints an introductory message to the snowman game
- * Post: An introductory message will be displayed.
- */
- private void intro() {
- println("CS106A Snowman!");
- println("I will think of a random word.");
- println("You'll try to guess its letters.");
- println("Every time you guess a letter");
- println("that isn't in my word, a new");
- println("piece of the snowman appears.");
- println("Guess correctly to avoid");
- println("bringing him to life in the sun!");
- println();
- }
- // TODO: comment this method
- private int playOneGame(String secretWord) {
- boolean keepPlaying = true;
- boolean win = false;
- boolean lose = false;
- String guessedLetters = "";
- int guessCount = 0;
- boolean letterInWord = false;
- displaySnowman(guessCount);
- println("#######");
- while (keepPlaying) {
- println("Secret Word : " + createHint(secretWord, guessedLetters));
- println("Your Guesses: " + guessedLetters);
- println("Guesses Left: " + (8 - guessCount));
- char guess = readGuess(guessedLetters);
- for (int i = 0; i < secretWord.length(); i++) {
- if (secretWord.charAt(i) == (guess)) {
- letterInWord = true;
- }
- }
- if (letterInWord) {
- guessedLetters += guess;
- println("Correct!");
- letterInWord = false;
- if (createHint(secretWord, guessedLetters).equals(secretWord)) {
- keepPlaying = false;
- win = true;
- } else {
- displaySnowman(guessCount);
- println("#######");
- }
- } else {
- guessedLetters += guess;
- guessCount++;
- println("Incorrect.");
- displaySnowman(guessCount);
- println("#######");
- if (8 - guessCount == 0) {
- keepPlaying = false;
- lose = true;
- }
- }
- }
- if (win) {
- println("You win! My word was \"" + secretWord + "\".");
- } else if (lose) {
- println("You lose! My word was \"" + secretWord + "\".");
- }
- return 8 - guessCount;
- }
- // TODO: comment this method
- private String createHint(String secretWord, String guessedLetters) {
- String hint = "";
- for (int i = 0; i < secretWord.length(); i++) {
- hint += "-";
- }
- for (int i = 0; i < guessedLetters.length(); i++) {
- for (int j = 0; j < secretWord.length(); j++) {
- if (guessedLetters.charAt(i) == secretWord.charAt(j)) {
- hint = hint.substring(0, j) + secretWord.charAt(j) + hint.substring(j + 1);
- }
- }
- }
- return hint;
- }
- // TODO: comment this method
- private char readGuess(String guessedLetters) {
- String guess = readLine("Your guess? ");
- boolean invalidGuess = true;
- boolean checkGuessedLetters = true;
- char finalGuess = ' ';
- while (invalidGuess) {
- if (guess.length() > 1 || isNumber(guess) || guess.length() == 0) {
- println("Type a single letter from A-Z.");
- guess = readLine("Your guess? ");
- } else {
- guess = guess.toUpperCase();
- checkGuessedLetters = true;
- for (int i = 0; i < guessedLetters.length(); i++) {
- if (Character.toString(guessedLetters.charAt(i)).equalsIgnoreCase(guess)) {
- checkGuessedLetters = false;
- }
- }
- if (checkGuessedLetters == false) {
- println("You already guessed that letter.");
- guess = readLine("Your guess? ");
- } else {
- finalGuess = guess.charAt(0);
- invalidGuess = false;
- }
- }
- }
- return finalGuess;
- }
- /*
- * This method displays an different image of a snowman, depending on the amount of guesses the player has taken.
- * Pre: The player must incorrectly guess a letter of the secret word
- * Post: An image of a snowman will be displayed.
- */
- private void displaySnowman(int guessCount) {
- try {
- Scanner input = new Scanner(new File("res/display" + (8 - guessCount) + ".txt")); //opens file of needed image
- while (input.hasNextLine()) {
- String line = input.nextLine();
- println(line); //prints each line of the image one by one
- }
- } catch (FileNotFoundException ex) {
- println("Error reading the file: " + ex); //displays an error if the file is not found
- }
- }
- /*
- * This program prints a set of statistics about the games of snowman that have been played
- * Pre: The player must choose not to play another game
- * Post: The statistics for all games played will be displayed
- */
- private void stats(int gamesCount, int gamesWon, int best) {
- println(" ");
- println("Overall statistics:");
- println("Games played: " + gamesCount);
- println("Games won: " + gamesWon);
- println("Win percent: " + ((double) gamesWon / gamesCount * 100) + "%");
- println("Best game: " + best + " guess(es) remaining");
- println("Thanks for playing!");
- }
- /*
- * This method randomly selects and retrieves a word from a file full of words.
- * Pre: A valid file name is provided.
- * Post: A random word will be returned from the file
- */
- private String getRandomWord(String filename) {
- String word = " ";
- try {
- Scanner input = new Scanner(new File(filename)); //opens the file of words
- String digit = input.nextLine(); //stores the first line (which is a digit) in a string
- int randInt = Integer.parseInt(digit); //converts the digit from a string to an integer
- int number = RandomGenerator.getInstance().nextInt(1, randInt); //generates a random number between 1 and the number of words in the file
- for (int i = 1; i <= number; i++) {
- word = input.nextLine(); //moves to the word corresponding to the random number and stores it as a string
- }
- input.close();
- } catch (FileNotFoundException ex) {
- print("Unable to open that file. Try again."); //displays an error if the file is not found
- }
- return word;
- }
- private boolean isNumber(String input) {
- try {
- Integer.parseInt(input);
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- private int bestGame(int guesses, int bestGuesses){
- int best = 0;
- if(guesses > bestGuesses){
- best = guesses;
- } else {
- best = bestGuesses;
- }
- return best;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement