Need help finishing a Java Word Game program with provided skeleton code below.
ID: 3692695 • Letter: N
Question
Need help finishing a Java Word Game program with provided skeleton code below. More methods can and should be added as necessary. Please help, and make sure to just add to the code already provided!
// Prototype for a Code Word game in CS1
import java.util.Scanner;
public class CodeWord {
public static final int WORDLENGTH = 5;
public static final int DICTIONARYSIZE = 8548;
// Holds all words
public static String[] dictionary = new String[DICTIONARYSIZE];
// Indicates whether words can logically be ruled out
public static boolean[] inPlay = new boolean[DICTIONARYSIZE];
public static void main(String[] args){
readDictionary(); // reads in dictionary
Scanner console = new Scanner(System.in);
while (yesTo("Do you want to play Code Word?", console)){
// play the game...
// Since there is nothing in here yet, it will repeatedly
// prompt Do you want to play Code Word?
// An entire game should happen here, but
// write separate methods whenever appropriate
// for a task.
}
}
// more methods here -- a few headers have been provided
// you will need to fill in some of them
// and add more
/**
* reads the dictionary from the file into the dictionary array
*/
public static void readDictionary(){
// TODO
}
/**
* Compare the feedback from comparing a guess to the codeword to the feedback from
* comparing the guess to each dictionary word, and filter out all dictionary words
* with differing feedback.
*
* @param guess A user guess from the console
* @param feedback String of *'s and +'s resulting from comparing the guess to the codeword
*/
public static void removeWordsWithDifferentFeedback(String guess, String feedback) {
// TODO
}
/**
* This method takes two 5-letter words and returns a String
* of asterisks containing the feedback that the user would get
* for comparing word1 to word2. Recall that the number of asterisks (*'s)
* equals the number of in-place matches, and the number of plus signs (+'s)
* equals the number of out-of-place matches. All asterisks should
* precede all plus signs.
*
* @param word1 5-letter word, e.g. a user guess
* @param word2 Another 5-letter word, e.g. the secret code word
* @return Feedback of asterisks and plus signs indicating the number
* of in- and out-of-place matches.
*/
public static String feedback(String word1, String word2) {
return ""; //TODO
}
/**
* Utility function to ask user yes or no.
* No modifications are necessary for this method.
* It uses a forever loop -- but the loop stops when something is returned.
* @param prompt Text presented to user
* @param console Source of user input
* @return Whether user types 'y' (as opposed to 'n').
*/
public static boolean yesTo(String prompt, Scanner console) {
for (;;) {
System.out.print(prompt + " (y/n)? ");
String response = console.next().trim().toLowerCase();
if (response.equals("y"))
return true;
else if (response.equals("n"))
return false;
else
System.out.println("Please answer y or n.");
}
}
}
The requirements of the game are this:
And the final output should look like this:
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package codeword;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
/**
*
* @author Shivakumar
*/
public class CodeWord {
// TODO code application logic here
public static final int WORDLENGTH = 5;
public static final int DICTIONARYSIZE = 8548;
// Holds all words
public static String[] dictionary = new String[DICTIONARYSIZE];
// Indicates whether words can logically be ruled out
public static boolean[] inPlay = new boolean[DICTIONARYSIZE];
public static void main(String[] args) throws IOException {
Scanner console = new Scanner(System.in);
while (yesTo("Do you want to play Code Word?", console)){
// play the game...
// Since there is nothing in here yet, it will repeatedly
// prompt Do you want to play Code Word?
// An entire game should happen here, but
// write separate methods whenever appropriate
// for a task.
System.out.println("Enter number of guesses:");
int g;
try
{
g=console.nextInt();
}
catch(Exception e)
{
System.out.println("Enter integer:");
g=console.nextInt();
}
int k=readDictionary(); // reads in dictionary
Random rand=new Random();
String word2,temp=null;
String word1=dictionary[rand.nextInt(k)];
for(int t=g;t>0;t--)
{
System.out.println(t+":Guess a 5-letter Word( enter r for remaining words):");
word2=console.next();
if(word1.equals(word2))
System.out.println("You won");
else if(t==1)
System.out.println("you lost");
if(word2.equals("r"))
removeWordsWithDifferentFeedback(word1,feedback(word1,temp));
else
{
temp=word2;
System.out.println(feedback(word1,word2));
}
}
}
}
// more methods here -- a few headers have been provided
// you will need to fill in some of them
// and add more
/**
* reads the dictionary from the file into the dictionary array
*/
public static int readDictionary() throws FileNotFoundException, IOException{
// TODO
BufferedReader br=new BufferedReader(new FileReader("H:/wordList.txt"));
String line; int k=0;
while((line=br.readLine())!=null)
{
int i;
String[] arr=line.split(" ");
for(i=0;i<arr.length;i++)
{
dictionary[k]=arr[i];
k++;
}
}
return k;
}
/**
* Compare the feedback from comparing a guess to the codeword to the feedback from
* comparing the guess to each dictionary word, and filter out all dictionary words
* with differing feedback.
*
* @param guess A user guess from the console
* @param feedback String of *'s and +'s resulting from comparing the guess to the codeword
*/
public static void removeWordsWithDifferentFeedback(String guess, String feedback) {
// TODO
if(feedback.contains("*") || feedback.contains("+"))
System.out.println("has clue in the existing word");
}
/**
* This method takes two 5-letter words and returns a String
* of asterisks containing the feedback that the user would get
* for comparing word1 to word2. Recall that the number of asterisks (*'s)
* equals the number of in-place matches, and the number of plus signs (+'s)
* equals the number of out-of-place matches. All asterisks should
* precede all plus signs.
*
* @param word1 5-letter word, e.g. a user guess
* @param word2 Another 5-letter word, e.g. the secret code word
* @return Feedback of asterisks and plus signs indicating the number
* of in- and out-of-place matches.
*/
public static String feedback(String word1, String word2) {
String rtn=null;
char[] wrd1=word1.toCharArray();
char[] wrd2=word2.toCharArray();
int inplace=0,outplace=0;
if(word1.equals(word2))
{
System.out.println("Congrats You Worn");
}
else
{
for(int c=0;c<WORDLENGTH;c++)
{
if(wrd1[c]==wrd2[c])
{
inplace++;
}
else
{
for(int sub=0;sub<WORDLENGTH;sub++)
{
if(wrd1[c]==wrd2[sub])
outplace++;
}
}
}
}
if(inplace>=2)
{
for(int j=0;j<inplace;j++)
rtn=rtn+word2+"*";
return rtn;
}
else if(outplace>2)
{
for(int l=0;l<outplace;l++)
rtn=rtn+word2+"+";
return rtn;
}
else
return word2;
}
/**
* Utility function to ask user yes or no.
* No modifications are necessary for this method.
* It uses a forever loop -- but the loop stops when something is returned.
* @param prompt Text presented to user
* @param console Source of user input
* @return Whether user types 'y' (as opposed to 'n').
*/
public static boolean yesTo(String prompt, Scanner console) {
System.out.print(prompt + " (y/n)? ");
String response = console.next().trim().toLowerCase();
if (response.equals("y"))
return true;
else if (response.equals("n"))
return false;
else
System.out.println("Please answer y or n.");
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.