Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im writing a Hangman java program with two implementations. On one implementatio

ID: 3686500 • Letter: I

Question

Im writing a Hangman java program with two implementations. On one implementation the game will be played in standard form. In the other implementation the game is played in a fasion that the computer cheats(EvilHangman). After each guess the computer will split the dictionary into different families based on the position of the guessed letter. The computer now picks a word from the new dictionary list and so on. The computer never settles on a word until the game is over hopefully never allowing the user to win. My code for evil hangam is shown below. I had the UpdateWithGuess method explained to me on how to write the code but I'm still confused on how to start. The commented section in my basic logic that I need to write.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class EvilHangman implements HangmanGame{
   
    public final char BLANK = '-';            //char used for blank spaces in puzzle.
    public char[] knownLetters;         //array of letters in word.
    public char letter;                 //letter being guessed.
    public int guesses;                 //Number of guesses.
    public String puzzle;               //String representation of the puzzle.
    private String secretWord;          //The word we are trying to guesse.
    private int secretWordLength;
    private Random word = new Random(); //Random word chosen from dictionary.
   
    //Collection of possible words.
    public LinkedList<String> dictionary = new LinkedList<String>();
    //Collection of letters already guessed.
    public Collection<Character> guessedLetters = new ArrayList<Character>();
   
    public Map<char [], List<String>> wordFamilyMap = new HashMap<>();

    /**
     * Constructor for a Fair game of Hangman.
     * Reads in dictionaryFile.
     * @param dictionaryFile The file contaning a list of possible words.
     */
    public EvilHangman(String dictionaryFile) {
        InputStream w1 = FairHangman.class.getResourceAsStream(dictionaryFile);
        InputStreamReader w2 = new InputStreamReader(w1);
        BufferedReader w3 = new BufferedReader(w2);
        String line;
        try{
            while((line = w3.readLine()) != null){
                dictionary.add(line);
                System.out.println(line);
            }
        }catch(Exception error){
        }    
    }
   
    /**
     * Initialize a new game.
     * Reset all bookkeeping.
     * @param secretWord The word picked from dictionary.
     * @param knownLetters The letters of the word.
     * @param guesses Number of guesses remaining.
     */
    public void initGame(int guesses) {
        guessedLetters = new ArrayList<Character>();
        this.guesses = guesses;
        guessedLetters = new ArrayList<Character>();
        secretWord = dictionary.get(word.nextInt(dictionary.size()));
        secretWordLength = secretWord.length();
        knownLetters = new char[secretWord.length()];
        for(int i = 0; i < knownLetters.length; i++) {
            knownLetters[i] = BLANK;
        }
       
    }

    /**
     * How many guesses left until the player loses?
     * @param guesses The number of guesses remaining.
     * @return guesses.
     */
    public int getGuessesRemaining() {
        return guesses;
    }
   
    /**
     * What letters has the player already guessed?
     * @param guessedLetters The collection of letters already guessed.
     * @return GuessedLetters.
     */
    public Collection<Character> getGuessedLetters(){
        return guessedLetters;
    }
     
    /**
     * What is the current state of the puzzle.
     * @param knownLetters Letters of the puzzle.
     * @return String representation of the puzzle.
     */
    public String getPuzzle() {
        return new String(knownLetters);
    }

    /**
     * Selects a random word from the dictionaryFile.
     * @return randomly chosen word from the dictionary.
     */
    public String getSecretWord() {
        return secretWord;
    }
   
    /**
     * Checks to see if the puzzle has been completed or
     * all of the blanks have been filled in.
     * @param knownLetters The letters of the puzzle.
     * @return True if puzzle is completed.
     * @return False if any blanks remain.
     */
    public boolean isComplete() {
        for(int i = 0;i < knownLetters.length;i++){
            if(knownLetters[i] == BLANK){
                return false;
            }
        }
        return true;
    }

    /**
     * Checks if the game is over.
     * Game is over if we run out of guesses or if the puzzle is completed.
     * @param guesses The number of guesses remaining.
     * @return True if game is over, false if not.
     */
    public boolean isGameOver() {
        if((guesses == 0) || isComplete() == true)
            return true;
        else{
            return false;
        }
    }

    /**
     * Respond to player's guess, updating internal bookkeeping
     * appropriately.
     * @param letter The guessed letter.
     * @param guesses The number of guesses remaining.
     * @param secretWord The secret word we are guessing.
     * @param True if letter was found in the word.
     */
    public boolean updateWithGuess(char letter) {
        boolean found = false;
       
        for(int i = 0;i < puzzle.length();i++){
            if(secretWord.charAt(i) == letter)
            char[] guessedWord = new char[i];
        }
        // iterate over each position in the puzzle
            // make a new char array with the guessed letter inserted at that position
            // create new temp list of words
            // iterate over all the words in the current dictionary (words left at this guess)
                // if the letter at this position is the guess, add it to the temp list of words
            // put(char array, temp list of words) in map
       
        // add another value to the map that consists of all the words without the guessed letter at all
       
        // iterate over every key,value in the map
            // find the largest value set (i.e. collection of words for that pattern)
            // this becomes the new dictionary
            // if this set contains the letter guessed, return true, else false
        wordFamilyMap.put(key, value);
       
//        u___ {}
//        _u__ {jump, bump, hump}
//        __u_ {.}
        // ___ {}
       
        return found;
    }
}

Explanation / Answer

Hangman java program with two implementations

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class EvilHangman implements HangmanGame{
   
    public final char BLANK = '-';            //char used for blank spaces in puzzle.
    public char[] knownLetters;         //array of letters in word.
    public char letter;                 //letter being guessed.
    public int guesses;                 //Number of guesses.
    public String puzzle;               //String representation of the puzzle.
    private String secretWord;          //The word we are trying to guesse.
    private int secretWordLength;
    private Random word = new Random(); //Random word chosen from dictionary.
   
    //Collection of possible words.
    public LinkedList<String> dictionary = new LinkedList<String>();
    //Collection of letters already guessed.
    public Collection<Character> guessedLetters = new ArrayList<Character>();
   
    public Map<char [], List<String>> wordFamilyMap = new HashMap<>();

    /**
     * Constructor for a Fair game of Hangman.
     * Reads in dictionaryFile.
     * @param dictionaryFile The file contaning a list of possible words.
     */
    public EvilHangman(String dictionaryFile) {
        InputStream w1 = FairHangman.class.getResourceAsStream(dictionaryFile);
        InputStreamReader w2 = new InputStreamReader(w1);
        BufferedReader w3 = new BufferedReader(w2);
        String line;
        try{
            while((line = w3.readLine()) != null){
                dictionary.add(line);
                System.out.println(line);
            }
        }catch(Exception error){
        }    
    }
   
    /**
     * Initialize a new game.
     * Reset all bookkeeping.
     * @param secretWord The word picked from dictionary.
     * @param knownLetters The letters of the word.
     * @param guesses Number of guesses remaining.
     */
    public void initGame(int guesses) {
        guessedLetters = new ArrayList<Character>();
        this.guesses = guesses;
        guessedLetters = new ArrayList<Character>();
        secretWord = dictionary.get(word.nextInt(dictionary.size()));
        secretWordLength = secretWord.length();
        knownLetters = new char[secretWord.length()];
        for(int i = 0; i < knownLetters.length; i++) {
            knownLetters[i] = BLANK;
        }
       
    }

    /**
     * How many guesses left until the player loses?
     * @param guesses The number of guesses remaining.
     * @return guesses.
     */
    public int getGuessesRemaining() {
        return guesses;
    }
   
    /**
     * What letters has the player already guessed?
     * @param guessedLetters The collection of letters already guessed.
     * @return GuessedLetters.
     */
    public Collection<Character> getGuessedLetters(){
        return guessedLetters;
    }
     
    /**
     * What is the current state of the puzzle.
     * @param knownLetters Letters of the puzzle.
     * @return String representation of the puzzle.
     */
    public String getPuzzle() {
        return new String(knownLetters);
    }

    /**
     * Selects a random word from the dictionaryFile.
     * @return randomly chosen word from the dictionary.
     */
    public String getSecretWord() {
        return secretWord;
    }
   
    /**
     * Checks to see if the puzzle has been completed or
     * all of the blanks have been filled in.
     * @param knownLetters The letters of the puzzle.
     * @return True if puzzle is completed.
     * @return False if any blanks remain.
     */
    public boolean isComplete() {
        for(int i = 0;i < knownLetters.length;i++){
            if(knownLetters[i] == BLANK){
                return false;
            }
        }
        return true;
    }

    /**
     * Checks if the game is over.
     * Game is over if we run out of guesses or if the puzzle is completed.
     * @param guesses The number of guesses remaining.
     * @return True if game is over, false if not.
     */
    public boolean isGameOver() {
        if((guesses == 0) || isComplete() == true)
            return true;
        else{
            return false;
        }
    }

    /**
     * Respond to player's guess, updating internal bookkeeping
     * appropriately.
     * @param letter The guessed letter.
     * @param guesses The number of guesses remaining.
     * @param secretWord The secret word we are guessing.
     * @param True if letter was found in the word.
     */
    public boolean updateWithGuess(char letter) {
        boolean found = false;
       
        for(int i = 0;i < puzzle.length();i++){
            if(secretWord.charAt(i) == letter)
            char[] guessedWord = new char[i];
        }
        // iterate over each position in the puzzle
            // make a new char array with the guessed letter inserted at that position
            // create new temp list of words
            // iterate over all the words in the current dictionary (words left at this guess)
                // if the letter at this position is the guess, add it to the temp list of words
            // put(char array, temp list of words) in map
       
        // add another value to the map that consists of all the words without the guessed letter at all
       
        // iterate over every key,value in the map
            // find the largest value set (i.e. collection of words for that pattern)
            // this becomes the new dictionary
            // if this set contains the letter guessed, return true, else false
        wordFamilyMap.put(key, value);
       
//        u___ {}
//        _u__ {jump, bump, hump}
//        __u_ {.}
        // ___ {}
       
        return found;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////