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

Write a program in java to play a word-guessing game like Hangman. Also please p

ID: 3692104 • Letter: W

Question

Write a program in java to play a word-guessing game like Hangman. Also please provide a output for this program it's the only way I can know for certain the program works.

Requirements:

It must randomly choose a word from a list of words. All words must be read from a data text file which contains at least 15 words. Each word includes at least 8 letters because it’s relatively easier to guess.

It must stop when all the letters are guessed. Program will ask player whether to keep going or quit.

It must give them limited tries and stop after they run out. (20 tries limitation)

It must display letters they have already guessed (either only the incorrect guesses or all guesses).

It must record player’s score and write the score into the score text file before user quit the game if the score belongs to the top 5 scores. That means the score file will store the 5 highest scores and the irrelative player names. At the beginning of the game, the top 5 scores list will be shown on the screen.

Score rules: 3 points for no missed letter,

2 points for 4 or less than 4 missed letters,

1 point for equal to or less than 7 tries.

Strategy:

(1) Define some array variables (or use ArrayList better) to contain word list, top five scores, and top five names.

(2) Define some String variables to contain guessed letters. Use string methods to print out every step guess.

(3) Define a variable to contain the score.

(4) At the beginning, program will read data file and score file and show score file content on the screen.

(5) Before program quits, it will compare the score player got to the top 5 scores. If the score is bigger than the fifth highest record, the top 5 records will be updated and the score file will be updated as well.

Hint:You may need to compare two string, but if they are not same type, a compare function can be created, in which program can compare each element and return a boolean value.

Hint: For input, Scanner can be used, but when program needs to read data from file, and when to read data from keyboard, the parameters are different. For example: Read data from file: File dataFile = new File(“data.txt”); Scanner input = new Scanner(dataFile); Read data from keyboard: Scanner input = new Scanner(System.in);

Hint: For output, Form atter can be used. For example: Formatter output = new Formatter(“score.txt”); output.format(“%d%d%s ", i+1, top5ScoreList.get(i),top5NameList.get(i)); System.out.printf(“%d%d%s ", i+1, top5ScoreList.get(i),top5NameList.get(i));

Output looks like the following:

This output is just an example. You may make it better.

Hint: To insert one value into an array, you may use ArrayList<> type and use add(index, name) method. For example:List<String> top5NameList = new ArrayList<String>(); top5NameList.add(index, name);

Word:-- eciicati Guess: n Word:__ Misses: Guess: Word:- eci_icatio Miasess Guess: P Word PeC cation Miosea: Gueas: u peciication Miasest 1u Guess: Word: PeCi ication Guesn:

Explanation / Answer

Please follow the code and comments for description :

Hope this gives you the solution

CODE :

    import java.util.Scanner;
    import java.util.Random;
    import java.io.*;
    public class Hangman {
        private Scanner in = new Scanner(System.in);
        private boardPiece[] board = {new boardPiece(0),new boardPiece(0),new boardPiece(3),
                                      new boardPiece(1),new boardPiece(2),new boardPiece(0)};
        private String puzzle;
        private String puzzleWord;
        private int puzzleIndex;
        private Random generator = new Random(System.currentTimeMillis());
        private boolean win;
        private boolean over;
        private String correct = "Nice!";
        //private String guesses = "";
        private char guesses[] = new char[26];
        private int guessed;
        private int misses;
        private String puzzles[] = new String[98];
        public static void main(String [] args){
            String letter;
            String again = "y";
            Hangman game = new Hangman();
            try{
                BufferedReader in =
                    new BufferedReader(new FileReader("words.txt"));
                int count = 0;
             String theNextLine;
                while ((theNextLine = in.readLine()) != null ) { //while there is another line in the input file
                      game.puzzles[count] = theNextLine; //get line of data
                      count++; //Increment CWID counter
                    }
                in.close();
                }
                catch(IOException e){
                    System.out.println("Error");
                }
            /*for(int x=0;x<game.puzzles.length;x++)
                System.out.println("PUZZLE " + x + ": " + game.puzzles[x]);*/
            System.out.println("Welcome to HangMan! Good Luck!");
            while(again.charAt(0) == 'y'){
                game.init();
                while(!game.over){
                    game.printBoard();
                    //System.out.println("Guessed: " + game.guesses);
                    game.printGuesses();
                    System.out.println("Word: " + game.puzzle);
                    System.out.println("Guess a letter: ");
                    letter = game.in.next();
                    //game.guesses = game.guesses + " " + letter.charAt(0);
                    game.guesses[game.guessed] = letter.charAt(0);
                    game.guessed++;
                    game.sort();
                    if(game.puzzleWord.indexOf(letter.charAt(0)) != -1){
                        game.correct = game.correct + letter.charAt(0);
                        game.puzzle = game.puzzleWord.replaceAll("[^"+game.correct+" ]","-");
                        if(game.puzzleWord.replaceAll("["+game.correct+" ]","").length() == 0){
                            game.win = true;
                            game.over = true;
                        }
                    }
                    else
                        game.miss();
                }
                game.printBoard();
                System.out.println("Solution: " + game.puzzleWord);
                if(game.win)
                    System.out.println("Congratulations! You've won!");
                else
                    System.out.println("Ahahahaha, you failed!");
                System.out.println();
                System.out.println("Another round? (y/n)");
                again = game.in.next();
            }
            System.out.println("See ya!");
        }
        void init(){
            win = false;
            over = false;
            board[0].piece = "    ______ ";
            board[1].piece = "    |    | ";
            board[2].piece = "         | ";
            board[3].piece = "         | ";
            board[4].piece = "         | ";
            board[5].piece = " _______| ";
            puzzleIndex = generator.nextInt(puzzles.length);
            puzzleWord = puzzles[puzzleIndex];
            puzzle = puzzleWord.replaceAll("[A-Z,a-z]","*");
            correct = "";
            //guesses = "";
            for(int x=0;x<26;x++)
                guesses[x] = '~';
            guessed = 0;
            misses = 0;
        }
        void printBoard(){
            for(int x =0;x<5;x++)
                System.out.println(board[x].piece);
        }
        void miss(){
            misses++;
            if(misses == 1)
                board[2].piece = "    0    | ";
            else if(misses == 2)
                board[2].piece = "   \0    | ";
            else if(misses == 3)
                board[2].piece = "   \0/   | ";
            else if(misses == 4)
                board[3].piece = "    |    | ";
            else if(misses == 5){
                board[4].piece = "   / \   | ";
                over = true;
            }
        }
        void printGuesses(){
            System.out.print("Guesses: ");
            for(int x=0;x<26;x++){
                if(guesses[x] != '~')
                    System.out.print(guesses[x] + " ");
            }
            System.out.println();
        }
        void sort(){
            boolean doMore = true;
            while (doMore) {
                doMore = false; // assume this is last pass over array
                for (int i=0; i<guesses.length-1; i++) {
                    if (guesses[i] > guesses[i+1]) {
                        char temp = guesses[i];
                        guesses[i] = guesses[i+1];
                        guesses[i+1] = temp;
                        doMore = true; // after an exchange, must look again
                    }
                }
            }
        }
        class boardPiece{
            public String piece;
            public int total;
            public int used;
            boardPiece(int x){
                used = 0;
                total = x;
            }
        }
    }

The only thing i guess is that i haven't been able to achieve is anything to do with recording & adding up the players scores.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote