Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.io.*;
- public class HangMan {
- /**
- * Program: HangMan.java
- * Purpose: Traditional word guessing game, uses files to store data
- * Creator: Chris Clarke
- * Created: 12.11.2012
- */
- /*
- - Create a list of possible words called "words.txt"
- - The games suppose to display the word to be guessed as a series of "*"'s.
- - If the letters guessed correctly, the letter becomes displayed amongst the series of "*"'s.
- - Display all the letters used (correctly & incorrectly) above the word.
- - If the words guessed correctly add up the score (5 points per letters and -5 off the
- final score per word that was guessing incorrectly e.g. - if the words "blue" then it'll be
- worth 20 points, 3 wrong guesses makes the final score 5).
- - If the player gets 5 wrong guesses, it's an instant loss, and they get a score of 0.
- - If the player looses, prompt them to input their name and have it stored in another text file
- labelled "scores.txt".
- - And then, start again with a new word.
- */
- public static void main(String[] args) {
- File f = new File("words.txt");
- Scanner scan;
- int wordCount = 0;
- ArrayList<String> words = new ArrayList<String>();
- try {
- scan = new Scanner(f); // to read file
- while (scan.hasNext()) {
- wordCount++;
- words.add(scan.next());
- }
- scan.close();
- } catch (FileNotFoundException x) {
- System.out.println("File not found!");
- System.exit(1);
- }
- Scanner input = new Scanner(System.in); // for user input
- int wordNumber = 0;
- String theWord;
- String letter;
- do {
- System.out.println("Can you guess the word?");
- theWord = words.get(wordNumber);
- int len = theWord.length();
- boolean[] discovered = new boolean[len];
- boolean solved = false;
- String usedLetters = "";
- int wrongGuesses = 0;
- score = 0;
- do {
- System.out.println("Used letters: "+usedLetters);
- showState(theWord, discovered);
- do {
- System.out.print("Guess a letter: ");
- letter = input.nextLine().toLowerCase();
- letter = letter.substring(0, 1);
- } while (contains(letter, usedLetters));
- usedLetters += letter;
- if (guessLetter(letter, theWord, discovered)) {
- solved = true;
- for (int i=0; i<theWord.length(); i++) {
- if (!discovered[i]) {
- solved = false;
- }
- }
- if (solved) break;
- } else {
- wrongGuesses++;
- if (wrongGuesses==MAX_WRONG_GUESSES) {
- instantLoss(input);
- break;
- }
- }
- } while (true);
- if (solved) {
- System.out.println(theWord);
- System.out.println("Congratulations! You got it!");
- score -= (wrongGuesses * POINTS_PER_LETTER);
- System.out.println("Score: "+score+"\n");
- } else {
- System.out.println("Try again.");
- }
- wordNumber++;
- } while (wordNumber<wordCount);
- System.out.println("Game Over");
- } // end main
- public static boolean contains(String s0, String s1) {
- for (int i=0; i<s1.length(); i++) {
- if (s0.equals(s1.substring(i, i+1))) {
- return true;
- }
- }
- return false;
- } // end contains
- public static boolean guessLetter(String letter, String theWord, boolean[] discovered) {
- boolean found1 = false;
- for (int i=0; i<theWord.length(); i++) {
- if (letter.equals(theWord.substring(i, i+1))) {
- if (!discovered[i]) {
- discovered[i] = true;
- score += POINTS_PER_LETTER;
- found1 = true;
- }
- }
- }
- return found1;
- } // end guessLetter
- public static void saveText(String n, String filename) {
- try {
- FileWriter fw = new FileWriter(new File(filename));
- fw.write(n);
- fw.close();
- } catch (IOException x) {
- System.out.println("Trouble writing to file!");
- System.exit(1);
- }
- } // end saveText
- public static void showState(String w, boolean[] discovered) {
- for (int i=0; i< w.length(); i++) {
- if (discovered[i]) {
- System.out.print(w.substring(i, i+1));
- } else {
- System.out.print("*");
- }
- }
- System.out.println();
- } // end showState
- public static void instantLoss(Scanner input) {
- System.out.println("You lose. Your score is zero.");
- System.out.println("Enter your name: ");
- String name = input.nextLine();
- saveText(name, "scores.txt");
- System.out.println("File \"scores.txt\" created successfully.");
- } // end instantLoss
- private static int score = 0;
- private static final int MAX_WRONG_GUESSES = 5;
- private static final int POINTS_PER_LETTER = 5;
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment