Question
We provide you with code for implementing the following game: given a rectangle of letters, find all dictionary words that consist of consecutive letters on
We provide you with code for implementing the following game: given a rectangle of letters, find all dictionary words that consist of consecutive letters on the board (up-down, down-up, left-right, or right-left).
import java.util.ArrayList;
public class WordGame {
/*
* Returns all strings that appear
* as a consecutive horizontal or vertical sequence of letters
* (left-right, right-left, up-down, or down-up)
* in the array board and also appear in dict.
* Note that the same word may appear multiple times
* on the board, and will then be multiple times in
* the returned array.
*
* dict is assumed to be in alphabetical order
* board is assumed to be rectangular
*/
public static String[] search(String[] dict, char[][] board) {
/*The following line contains
* that the ret is an ArrayList that contains String. It is an
* ArrayList of only Strings.
*/
ArrayList
int height = board.length;
// Set width to 0 for an empty board,
// and to the width for first line otherwise,
// because we assume the board is a rectangle
int width = board.length==0 ? 0 : board[0].length;
// TODO Generate substrings of the board
// and check if they are in the dictionary
// by calling inDictionary (which you implement).
// If they are, use ret.add to add them
// to the array that gets returned.
// Character.toString, String+char, and String+=char
// can all be useful here.
// You may want to see https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
// and https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
//
// TODO (10% of your grade): if your board
// has height h and width w, how many strings
// do you need to check using inDictionary
// (assume that you do nothing to filter out
// duplicates or, equivalently, that the board
// is such that there are no duplicates)
// ANSWER:
// EXPLANATION OF THE ANSWER:
// This line converts ArrayList
return ret.toArray(new String[0]);
}
}
--------------------below is the code for the main function class -------------------
import java.io.*;
/*
* THIS IS THE FILE FOR TESTING YOUR CODE
*/
public class WordGameDriver {
public static void main(String[] args) {
String dictionaryFileName = "src"+File.separatorChar+"hw4"+File.separatorChar+"dictionary.txt";
String boardFileName = "src"+File.separatorChar+"hw4"+File.separatorChar+"board.txt";
String [] dict = WordGameHelperClass.readDictionary(dictionaryFileName);
if (dict == null) return;
char [][] board = WordGameHelperClass.readBoard(boardFileName);
if (board == null) return;
String [] result = WordGame.search(dict, board);
for (String w : result) {
System.out.println(w);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started