Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

help me how to start The game of Snowman In this assignment, you implement a player for a familiar childrens game in which your program

help me how to start

The game of Snowman

In this assignment, you implement a player for a familiar childrens game in which your program tries to guess a secret word one letter at a time. For example, if the secret word is snowman, then the player begins with the letters hidden: *******. Each guess that does not appear in the word is a miss and scores one point. Each guessed letter which does appear in the word replaces the corresponding asterisks with the correct letter. For example, a guesss of n, results in *n****n. The fewer misses your program does overall, the better. The guessing ends when all of the letters have been guessed, to reveal the hidden word, in this case snowman.

To be able to write and execute this assignment, you need to download the following files and save them to the same directory in which you write your project:

words.txt, the 2.5 megabyte raw text file containing the list of possible words in the English language, one word per line. From this dictionary, only the proper nouns that contain only lowercase letters are used in the game.

SnowmanRunner.java, the automated tester for your Snowman code.

[page1image18360]

Class specification and methods to write

You must write and submit on D2L exactly one Java sourcecode file named SnowmanPlayer.java. This class must have all four of the following methods, with names and signatures exactly as specified below. (Otherwise, our SnowmanRunner tester class can't be compiled and executed with your class.) In addition, you may write in your class whatever private utility methods and data fields you wish. Furthermore, you are allowed to use and call any existing classes and methods in the Java standard library, such as ArrayList or various String processing methods.

Your methods must operate in silence and not print anything on the console, so as not to mess up the output of the tester class. Of course you can do some debugging prints during the development phase, but before submitting your SnowmanPlayer, you must absolutely comment out all output statements in it.

public static void startGame(String[] words, int minLength, int maxLength, String allowedChars)

This method is guaranteed to be called exactly once by SnowmanRunner at the beginning of the program execution, and after that, never again. Inside this method, you can do whatever initialization computations you wish to perform to optimize your success in the game, such as counting the individual letter frequencies in words. The parameters are as follows:

words is an array of Strings that contains all possible words of English that the secret words are randomly chosen from.

minLength and maxLength are the minimum and maximum lengths of the secret words, respectively. Your code can safely assume that the secret word length is between these two, inclusive.

allowedChars is a String that contains all possible characters that can occur in the secret words. (This would be the 26 lowercase letters of the English alphabet.)

public static void startNewWord(int length)

The method startNewWord(int) is called by SnowmanRunner at the start of each new round (which will have a new secret word). Inside this method, you can perform whatever initialization you need for guessing each individual secret word. The parameter length tells you the length of the secret word (so that you can ignore all the potential secret words that have a different length).

public static char guessLetter(String pattern, String previousGuesses)

The method guessLetter(String, String) is called by SnowmanRunner to ask your code what letter it would like to guess next for the current secret word. Your method answers by returning a char. The parameters of guessLetter() are as follows:

pattern is a String that contains the current secret word so that each unknown letter is displayed as the character SnowmanRunner.BLANK (currently the asterisk character), and each known letter is displayed as that letter. For example, if the secret word is seven characters long, and you have previously made the successful guesses 'n' and 's', the pattern might be "sn****n".

previousGuesses is a String that contains the characters that you have already guessed for this particular secret word, including both hits and misses. (Of course, it would be rather silly for your method to return as your guess some character from previousGuesses, or some character that is not in allowedChars.)

import java.util.*; import java.io.*;

public class SnowmanRunner {

/* You are allowed to modify the following three class variables. */ private static int SEED = 654321; private static final int ROUNDS = 20; private static final boolean VERBOSE = true; /* DO NOT MODIFY ANYTHING BELOW THIS LINE !!!! */ public static final int MINLENGTH = 5; public static final int MAXLENGTH = 15; private static final String allowed = "abcdefghijklmnopqrstuvwxyz"; public static final char BLANK = '*'; private static List wordList = new ArrayList(); private static String[] wordArray; public static void main(String[] args) { Scanner s; try { s = new Scanner(new File("words.txt")); } catch(Exception e) { System.out.println("Error opening file words.txt: " + e); return; } int totalCount = 0; outer: while(s.hasNext()) { String word = s.next(); totalCount++; if(word.length() < MINLENGTH || word.length() > MAXLENGTH) { continue; } for(int i = 0; i < word.length(); i++) { if(allowed.indexOf(word.charAt(i)) == -1) { continue outer; } } wordList.add(word); } if(VERBOSE) { System.out.printf("Read in %d words, of which %d remain after filtering. ", totalCount, wordList.size()); System.out.printf("Using seed %d, with %d rounds. ", SEED, ROUNDS); } wordArray = new String[wordList.size()]; for(int i = 0; i < wordList.size(); i++) { wordArray[i] = wordList.get(i); } Random rng = new Random(SEED); SnowmanPlayer.startGame(wordArray, MINLENGTH, MAXLENGTH, allowed); int totalMisses = 0; for(int i = 0; i < ROUNDS; i++) { int currentMisses = 0; String secretWord = wordList.get(rng.nextInt(wordList.size())); SnowmanPlayer.startNewWord(secretWord.length()); String previousGuesses = ""; if(VERBOSE) { System.out.print(secretWord + ": "); } int previousCorrect = -1; do { String pattern = ""; int correct = 0; for(int j = 0; j < secretWord.length(); j++) { char c = secretWord.charAt(j); if(previousGuesses.indexOf(c) > -1) { pattern += c; correct++; } else { pattern += BLANK; } } if(correct == secretWord.length()) { break; } if(correct == previousCorrect) { currentMisses++; if(VERBOSE) { System.out.print('!'); } } char guess = SnowmanPlayer.guessLetter(pattern, previousGuesses); if(VERBOSE) { System.out.print(guess); } previousCorrect = correct; previousGuesses += guess; } while(currentMisses < allowed.length()); totalMisses += currentMisses; if(VERBOSE) { System.out.println(" (" + currentMisses + ")"); } } System.out.println("Code by '" + SnowmanPlayer.getAuthor() + "' made a total of " + totalMisses + " misses over " + ROUNDS + " words."); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

0764532545, 978-0764532542

More Books

Students also viewed these Databases questions

Question

How can shaping be used to encourage desirable work behaviors?

Answered: 1 week ago