brilliant_moves

HangMan.java

Nov 12th, 2012
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.47 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. import java.io.*;
  4.  
  5. public class HangMan {
  6.  
  7.     /**
  8.     *   Program:    HangMan.java
  9.     *   Purpose:    Traditional word guessing game, uses files to store data
  10.     *   Creator:    Chris Clarke
  11.     *   Created:    12.11.2012
  12.     */
  13.  
  14. /*
  15. - Create a list of possible words called "words.txt"
  16. - The games suppose to display the word to be guessed as a series of "*"'s.
  17.  
  18. - If the letters guessed correctly, the letter becomes displayed amongst the series of "*"'s.
  19. - Display all the letters used (correctly & incorrectly) above the word.
  20. - If the words guessed correctly add up the score (5 points per letters and -5 off the
  21.  final score per word that was guessing incorrectly e.g. - if the words "blue" then it'll be
  22.  worth 20 points, 3 wrong guesses makes the final score 5).
  23. - If the player gets 5 wrong guesses, it's an instant loss, and they get a score of 0.
  24. - If the player looses, prompt them to input their name and have it stored in another text file
  25.  labelled "scores.txt".
  26. - And then, start again with a new word.
  27. */
  28.  
  29.     public static void main(String[] args) {
  30.         File f = new File("words.txt");
  31.         Scanner scan;
  32.         int wordCount = 0;
  33.         ArrayList<String> words = new ArrayList<String>();
  34.         try {
  35.             scan = new Scanner(f); // to read file
  36.             while (scan.hasNext()) {
  37.                 wordCount++;
  38.                 words.add(scan.next());
  39.             }
  40.             scan.close();
  41.         } catch (FileNotFoundException x) {
  42.             System.out.println("File not found!");
  43.             System.exit(1);
  44.         }
  45.  
  46.         Scanner input = new Scanner(System.in); // for user input
  47.  
  48.         int wordNumber = 0;
  49.         String theWord;
  50.         String letter;
  51.  
  52.         do {
  53.             System.out.println("Can you guess the word?");
  54.             theWord = words.get(wordNumber);
  55.             int len = theWord.length();
  56.             boolean[] discovered = new boolean[len];
  57.             boolean solved = false;
  58.             String usedLetters = "";
  59.             int wrongGuesses = 0;
  60.             score = 0;
  61.  
  62.             do {
  63.                 System.out.println("Used letters: "+usedLetters);
  64.                 showState(theWord, discovered);
  65.                 do {
  66.                     System.out.print("Guess a letter: ");
  67.                     letter = input.nextLine().toLowerCase();
  68.                     letter = letter.substring(0, 1);
  69.                 } while (contains(letter, usedLetters));
  70.  
  71.                 usedLetters += letter;
  72.  
  73.                 if (guessLetter(letter, theWord, discovered)) {
  74.                     solved = true;
  75.                     for (int i=0; i<theWord.length(); i++) {
  76.                         if (!discovered[i]) {
  77.                             solved = false;
  78.                         }
  79.                     }
  80.                     if (solved) break;
  81.                 } else {
  82.                     wrongGuesses++;
  83.                     if (wrongGuesses==MAX_WRONG_GUESSES) {
  84.                         instantLoss(input);
  85.                         break;
  86.                     }
  87.                 }
  88.             } while (true);
  89.  
  90.             if (solved) {
  91.                 System.out.println(theWord);
  92.                 System.out.println("Congratulations!  You got it!");
  93.                 score -= (wrongGuesses * POINTS_PER_LETTER);
  94.                 System.out.println("Score: "+score+"\n");
  95.             } else {
  96.                 System.out.println("Try again.");
  97.             }
  98.  
  99.             wordNumber++;
  100.         } while (wordNumber<wordCount);
  101.  
  102.         System.out.println("Game Over");
  103.     } // end main
  104.  
  105.     public static boolean contains(String s0, String s1) {
  106.         for (int i=0; i<s1.length(); i++) {
  107.             if (s0.equals(s1.substring(i, i+1))) {
  108.                 return true;
  109.             }
  110.         }
  111.         return false;
  112.     } // end contains
  113.  
  114.     public static boolean guessLetter(String letter, String theWord, boolean[] discovered) {
  115.         boolean found1 = false;
  116.         for (int i=0; i<theWord.length(); i++) {
  117.             if (letter.equals(theWord.substring(i, i+1))) {
  118.                 if (!discovered[i]) {
  119.                     discovered[i] = true;
  120.                     score += POINTS_PER_LETTER;
  121.                     found1 = true;
  122.                 }
  123.             }
  124.         }
  125.         return found1;
  126.     } // end guessLetter
  127.  
  128.     public static void saveText(String n, String filename) {
  129.         try {
  130.             FileWriter fw = new FileWriter(new File(filename));
  131.             fw.write(n);
  132.             fw.close();
  133.         } catch (IOException x) {
  134.             System.out.println("Trouble writing to file!");
  135.             System.exit(1);
  136.         }
  137.     } // end saveText
  138.  
  139.     public static void showState(String w, boolean[] discovered) {
  140.         for (int i=0; i< w.length(); i++) {
  141.             if (discovered[i]) {
  142.                 System.out.print(w.substring(i, i+1));
  143.             } else {
  144.                 System.out.print("*");
  145.             }
  146.         }
  147.         System.out.println();
  148.     } // end showState
  149.  
  150.     public static void instantLoss(Scanner input) {
  151.         System.out.println("You lose.  Your score is zero.");
  152.         System.out.println("Enter your name: ");
  153.         String name = input.nextLine();
  154.         saveText(name, "scores.txt");
  155.         System.out.println("File \"scores.txt\" created successfully.");
  156.     } // end instantLoss
  157.  
  158.     private static int score = 0;
  159.  
  160.     private static final int MAX_WRONG_GUESSES = 5;
  161.     private static final int POINTS_PER_LETTER = 5;
  162. } // end class
Advertisement
Add Comment
Please, Sign In to add comment