add hangman assignment from WCC CS 145 Spring 2019.
This commit is contained in:
		
							parent
							
								
									fd6cfb770d
								
							
						
					
					
						commit
						46f17fac95
					
				| 
						 | 
				
			
			@ -0,0 +1,85 @@
 | 
			
		|||
// Class HangmanMain is the driver program for the Hangman program.  It reads a
 | 
			
		||||
// dictionary of words to be used during the game and then plays a game with
 | 
			
		||||
// the user.  This is a cheating version of hangman that delays picking a word
 | 
			
		||||
// to keep its options open.  You can change the setting for SHOW_COUNT to see
 | 
			
		||||
// how many options are still left on each turn.
 | 
			
		||||
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.io.*;
 | 
			
		||||
 | 
			
		||||
public class HangmanMain {
 | 
			
		||||
	public static final String DICTIONARY_FILE = "dictionary2.txt";
 | 
			
		||||
	public static final boolean DEBUG = true; // show words left
 | 
			
		||||
 | 
			
		||||
	public static void main(String[] args) throws FileNotFoundException {
 | 
			
		||||
		System.out.println("Welcome to the cs 145 hangman game.");
 | 
			
		||||
		System.out.println();
 | 
			
		||||
 | 
			
		||||
		// open the dictionary file and read dictionary into an ArrayList
 | 
			
		||||
		Scanner input = new Scanner(new File(DICTIONARY_FILE));
 | 
			
		||||
		List<String> dictionary = new ArrayList<String>();
 | 
			
		||||
		while (input.hasNext()) {
 | 
			
		||||
			dictionary.add(input.next().toLowerCase());
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// set basic parameters
 | 
			
		||||
		Scanner console = new Scanner(System.in);
 | 
			
		||||
		System.out.print("What length word do you want to use? ");
 | 
			
		||||
		int length = console.nextInt();
 | 
			
		||||
		System.out.print("How many wrong answers allowed? ");
 | 
			
		||||
		int max = console.nextInt();
 | 
			
		||||
		System.out.println();
 | 
			
		||||
 | 
			
		||||
		// set up the HangmanManager and start the game
 | 
			
		||||
		List<String> dictionary2 = Collections.unmodifiableList(dictionary);
 | 
			
		||||
		HangmanManager hangman = new HangmanManager(dictionary2, length, max);
 | 
			
		||||
		if (hangman.words().isEmpty()) {
 | 
			
		||||
			System.out.println("No words of that length in the dictionary.");
 | 
			
		||||
		} else {
 | 
			
		||||
			playGame(console, hangman);
 | 
			
		||||
			showResults(hangman);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Plays one game with the user
 | 
			
		||||
	public static void playGame(Scanner console, HangmanManager hangman) {
 | 
			
		||||
		while (hangman.guessesLeft() > 0 && hangman.pattern().contains("-")) {
 | 
			
		||||
			System.out.println("guesses : " + hangman.guessesLeft());
 | 
			
		||||
			if (DEBUG) {
 | 
			
		||||
				System.out.println(hangman.words().size() + " words left: "
 | 
			
		||||
						+ hangman.words());
 | 
			
		||||
			}
 | 
			
		||||
			System.out.println("guessed : " + hangman.guesses());
 | 
			
		||||
			System.out.println("current : " + hangman.pattern());
 | 
			
		||||
			System.out.print("Your guess? ");
 | 
			
		||||
			char ch = console.next().toLowerCase().charAt(0);
 | 
			
		||||
			if (hangman.guesses().contains(ch)) {
 | 
			
		||||
				System.out.println("You already guessed that");
 | 
			
		||||
			} else {
 | 
			
		||||
				int count = hangman.record(ch);
 | 
			
		||||
				if (count == 0) {
 | 
			
		||||
					System.out.println("Sorry, there are no " + ch + "'s");
 | 
			
		||||
				} else if (count == 1) {
 | 
			
		||||
					System.out.println("Yes, there is one " + ch);
 | 
			
		||||
				} else {
 | 
			
		||||
					System.out.println("Yes, there are " + count + " " + ch
 | 
			
		||||
							+ "'s");
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			System.out.println();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// reports the results of the game, including showing the answer
 | 
			
		||||
	public static void showResults(HangmanManager hangman) {
 | 
			
		||||
		// if the game is over, the answer is the first word in the list
 | 
			
		||||
		// of words, so we use an iterator to get it
 | 
			
		||||
		String answer = hangman.words().iterator().next();
 | 
			
		||||
		System.out.println("answer = " + answer);
 | 
			
		||||
		if (hangman.guessesLeft() > 0) {
 | 
			
		||||
			System.out.println("You beat me");
 | 
			
		||||
		} else {
 | 
			
		||||
			System.out.println("Sorry, you lose");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,224 @@
 | 
			
		|||
/*
 | 
			
		||||
* Matt Jensen
 | 
			
		||||
* CS145 - Lab 4
 | 
			
		||||
* 5/21/19
 | 
			
		||||
*
 | 
			
		||||
// runs the hangman game
 | 
			
		||||
*/
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
public class HangmanManager {
 | 
			
		||||
 | 
			
		||||
    private List<String> dictionary;
 | 
			
		||||
    private Set<String> words;
 | 
			
		||||
    private int length;
 | 
			
		||||
    private int max;
 | 
			
		||||
    private SortedSet<Character> guesses;
 | 
			
		||||
    private int tries;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    *
 | 
			
		||||
    * @param   dictionary    contains all the possible solutions.
 | 
			
		||||
    * @param   length   length of solutions.
 | 
			
		||||
    * @param   max   maximum guesses.
 | 
			
		||||
    * @return   manager
 | 
			
		||||
    *
 | 
			
		||||
    */
 | 
			
		||||
    public HangmanManager(List<String> dictionary, int length, int max) {
 | 
			
		||||
        if(length < 1 || max < 0){
 | 
			
		||||
            throw new IllegalArgumentException("length or max are incorrect");
 | 
			
		||||
        }
 | 
			
		||||
        this.setDictionary(dictionary);
 | 
			
		||||
        this.setLength(length);
 | 
			
		||||
        this.setMax(max);
 | 
			
		||||
        this.setTries(0);
 | 
			
		||||
        this.setGuesses();
 | 
			
		||||
        this.setWords();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    * set of words being considered my manager.
 | 
			
		||||
    *
 | 
			
		||||
    * @return   words
 | 
			
		||||
    *
 | 
			
		||||
    */
 | 
			
		||||
    public Set<String> words()  {
 | 
			
		||||
        return this.words;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    * guesses the player has left in the game.
 | 
			
		||||
    *
 | 
			
		||||
    */
 | 
			
		||||
    public int guessesLeft() {
 | 
			
		||||
        return this.max - this.tries;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    * past guesses by player.
 | 
			
		||||
    *
 | 
			
		||||
    * @return   guesses
 | 
			
		||||
    *
 | 
			
		||||
    */
 | 
			
		||||
    public SortedSet<Character> guesses() {
 | 
			
		||||
        return this.guesses;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    * representation of the current state of the game.
 | 
			
		||||
    *
 | 
			
		||||
    * @return   pattern
 | 
			
		||||
    *
 | 
			
		||||
    */
 | 
			
		||||
    public String pattern() {
 | 
			
		||||
        if(this.getWords().isEmpty()){
 | 
			
		||||
            throw new IllegalStateException("words are empty");
 | 
			
		||||
        }
 | 
			
		||||
        String word = this.getWords().iterator().next();
 | 
			
		||||
        return pattern(word, this.getGuesses());
 | 
			
		||||
    }
 | 
			
		||||
    // generated the string mask for a word given a set of guessses.
 | 
			
		||||
    public static String pattern(String word, Set<Character> guessMask) {
 | 
			
		||||
        String pattern = "";
 | 
			
		||||
        for( int i = 0; i < word.length(); i++) {
 | 
			
		||||
            if( guessMask.contains(word.charAt(i)) ) {
 | 
			
		||||
                pattern += " " + word.charAt(i);
 | 
			
		||||
            } else {
 | 
			
		||||
                pattern += " -";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return pattern;
 | 
			
		||||
          
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // accessor for guesses set.
 | 
			
		||||
    private SortedSet<Character> getGuesses() {
 | 
			
		||||
        return this.guesses;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
    * records a guess.
 | 
			
		||||
    *
 | 
			
		||||
    * @return   matches     number of matches of the guess.
 | 
			
		||||
    *
 | 
			
		||||
    */
 | 
			
		||||
    public int record(char guess) {
 | 
			
		||||
        if(this.guessesLeft() < 1){
 | 
			
		||||
            throw new IllegalStateException("no tries left");
 | 
			
		||||
        }
 | 
			
		||||
        if( ! this.getWords().isEmpty() && this.getGuesses().contains(guess)){
 | 
			
		||||
            throw new IllegalArgumentException();
 | 
			
		||||
        }
 | 
			
		||||
        int count = 0;
 | 
			
		||||
        this.addGuess(guess);
 | 
			
		||||
        // maps a count to and array of possibilities
 | 
			
		||||
 | 
			
		||||
        SortedMap<String, List<String>> possibilities = getPossibilities(guess);
 | 
			
		||||
        if( possibilities.size() >= 1 ) {
 | 
			
		||||
            this.setWords(mostEvil(possibilities));
 | 
			
		||||
        }
 | 
			
		||||
        count = countMatches(this.getWords().iterator().next(), guess);
 | 
			
		||||
        if(count == 0){
 | 
			
		||||
            this.addTry();
 | 
			
		||||
        }
 | 
			
		||||
        return count;
 | 
			
		||||
    }
 | 
			
		||||
    /**
 | 
			
		||||
     * counts number of chars in word
 | 
			
		||||
     * 
 | 
			
		||||
     * @param word
 | 
			
		||||
     * @param character
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    public static int countMatches(String word, char character){
 | 
			
		||||
        int count = 0;
 | 
			
		||||
        for(int i = 0; i < word.length(); i++){
 | 
			
		||||
            if(word.charAt(i) == character){
 | 
			
		||||
                count++;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return count;
 | 
			
		||||
    }
 | 
			
		||||
    /**
 | 
			
		||||
     * returns the most evil list of words in the map.
 | 
			
		||||
     * @param map   the map of all possible values.
 | 
			
		||||
     * @return  list of the most evil words.
 | 
			
		||||
     */
 | 
			
		||||
    private List<String> mostEvil(Map<String, List<String>> map){
 | 
			
		||||
        // check if the key is the smallest guess.gg
 | 
			
		||||
        String maxKey = "";
 | 
			
		||||
        int maxCount = 0;
 | 
			
		||||
        for(String key : map.keySet()){
 | 
			
		||||
            if( map.get(key).size() > maxCount) {
 | 
			
		||||
                maxKey = key;
 | 
			
		||||
                maxCount = map.get(key).size();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return map.get(maxKey);
 | 
			
		||||
    }
 | 
			
		||||
    /**
 | 
			
		||||
     * get a map with patterns as keys and words as values.
 | 
			
		||||
     * 
 | 
			
		||||
     * @param guess     the additional guess.
 | 
			
		||||
     * @return  possibilities   the possible words
 | 
			
		||||
     */
 | 
			
		||||
    private SortedMap<String, List<String>> getPossibilities(char guess) {
 | 
			
		||||
        SortedMap<String, List<String>> map = new TreeMap<String, List<String>>();
 | 
			
		||||
        String pattern = "";
 | 
			
		||||
        Set<String> keys = new HashSet<String>();
 | 
			
		||||
        for(String word : this.getWords()) {
 | 
			
		||||
            keys.add(pattern(word, this.getGuesses()));
 | 
			
		||||
        }
 | 
			
		||||
        for(String key : keys){
 | 
			
		||||
            map.put(key, new ArrayList<String>());
 | 
			
		||||
        }
 | 
			
		||||
        for(String word : this.getWords()) {
 | 
			
		||||
            pattern = pattern(word, this.getGuesses());
 | 
			
		||||
            map.get(pattern).add(word);
 | 
			
		||||
        }
 | 
			
		||||
        return map;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * Setters and Getters for object attributes.
 | 
			
		||||
     * 
 | 
			
		||||
     */
 | 
			
		||||
    
 | 
			
		||||
    private Set<String> getWords() {
 | 
			
		||||
        return this.words;
 | 
			
		||||
    }
 | 
			
		||||
    private void addGuess(char guess) {
 | 
			
		||||
        this.guesses.add(guess);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void setDictionary(List<String> dictionary) {
 | 
			
		||||
        this.dictionary = dictionary;
 | 
			
		||||
    }
 | 
			
		||||
    private void setLength(int length) {
 | 
			
		||||
        this.length = length;
 | 
			
		||||
    }
 | 
			
		||||
    private void setMax(int max) {
 | 
			
		||||
        this.max = max;
 | 
			
		||||
    }
 | 
			
		||||
    private void setTries(int tries) {
 | 
			
		||||
        this.tries = tries;
 | 
			
		||||
    }
 | 
			
		||||
    private void addTry(){
 | 
			
		||||
        this.tries++;
 | 
			
		||||
    }
 | 
			
		||||
    private void setGuesses() {
 | 
			
		||||
        this.guesses = new TreeSet<Character>();
 | 
			
		||||
    }    
 | 
			
		||||
    private void setWords() {
 | 
			
		||||
        this.setWords(this.dictionary);
 | 
			
		||||
    }
 | 
			
		||||
    private void setWords(List<String> words) {
 | 
			
		||||
        this.words = new TreeSet<String>();
 | 
			
		||||
        for(String word : words) {
 | 
			
		||||
            if( word.length() == this.length) {
 | 
			
		||||
                this.words.add(word);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
ally
 | 
			
		||||
beta
 | 
			
		||||
cool
 | 
			
		||||
deal
 | 
			
		||||
else
 | 
			
		||||
flew
 | 
			
		||||
good
 | 
			
		||||
hope
 | 
			
		||||
ibex
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 527 KiB  | 
		Loading…
	
		Reference in New Issue