Description
Word game Problem description The world is full of word games, many of them involving a word search on a gameboard. One such involves a four-by-four random arrangement of English letters and finding words in it. Although we will not implement the full game, instead we concentrate on perhaps the trickiest part: verifying a given word is on the gameboard. Consider diagram below (and ignore for now how it was created):
Individual letters on this board may be identified by their row-and-column numbers. For example, the uppercase letter ‘H’ is at the row 1 and column 2, i.e. ‘H’ is at(1, 2).
Page 2 of 5
Using this same notation, ‘L’ is at (0, 0), ‘D’ is at (2, 3), the two ‘I’s are at (0, 1) and (3, 0), etc. There are also eight letters adjacent to the lettersquare containing ‘H’:
Notice “adjacent” with respect to a letter also means squares lying on a diagonal in addition to those above, below, to the right, and to the left. The eight neighbours of ‘H’ are (clockwise from 12 o’clock) ‘T’, ‘E’, ‘S’, ‘D’, ‘E’, ‘J’, ‘N’, and ‘I’. There are many words on this board. One of them is “DESTINY”. (Another is “LINTSEED”; how long do you need to find it?!) The diagram below shows the letters in “DESTINY” alongside the actual sequence: There is an imaginary line connecting the letters in the sequence. The line is also permitted to cross over itself (although this does not actually in our example). However, a lettersquare may appear at most once in a sequence. Your main task for this assignment is to write a recursive backtracking algorithm that provided a gameboard determines whether or not a given word can be found. A word is considered found if there exists a sequence of (row, column) letter-squares generated by the algorithm which forms the letter-sequence that is the same as the word; for the gameboard above and the word “DESTINY” this sequence is: [(2,3), (2,2), (1,3), (0,2), (0,1), (1,1), (2,0)] Programming requirements You will complete source code for the GameBoard class. The class as given to you already has some crucial functionality: • The first constructor has one parameter – an array of strings – and uses that parameter to initialize the game board. This method has been written for you.
Page 3 of 5
• The second constructor has one parameter – a long integer – and uses that parameter to initialize a random-number generator whose values will guide the construction of a board. This method has been written for you. The latter constructor, when provided the same parameter value, will always produce the same random board. (Please read the code comments in that constructor if you want to learn more about how it actually uses random numbers to create a board.) • You can ignore the method named shuffle(). • The method toString() creates a human-readable version of the created gameboard. This method has been written for you. Your task is to complete the following two methods and add other private methods of your own if you need them: • public String isWord(String word): Using the parameter word, determine whether or not that word can be found as a sequence within the gameboard. The method must do this by, in part, calling findPath (mentioned below). If no sequence is found, the method returns null. If there is a sequence of letters, then this sequence of (row, column) values is returned as a string (i.e., in the format shown at the bottom of the previous page). Important: This method must not print out the sequence! • private Boolean findPath(String wordToFind, Stack Page 4 of 5
# Transcript of a WordSearch session $ java WordSearch Random seed? 4005 0 1 2 3 ——- 0: L I T E 1: R N H S 2: Y J E D 3: I A Y E
Word to search (enter ! to exit)? destiny [(2,3), (2,2), (1,3), (0,2), (0,1), (1,1), (2,0)] Word to search? collusion no path Word to search? !
# Transcript of a FindAllWords session (completed code) $ java FindAllWords 123 longwords.txt 0 1 2 3 ——- 0: A O C W 1: L G E Z 2: P O V K 3: L I S T
ALPIST: [(0,0), (1,0), (2,0), (3,1), (3,2), (3,3)] COLOVE: [(0,2), (0,1), (1,0), (2,1), (2,2), (1,2)] EGOIST: [(1,2), (1,1), (2,1), (3,1), (3,2), (3,3)] PLOSIVE: [(2,0), (1,0), (2,1), (3,2), (3,1), (2,2), (1,2)] VOLAGE: [(2,2), (2,1), (1,0), (0,0), (1,1), (1,2)]
Testing GameBoard Within GameBoard.java is a main() method already containing a little bit of code. As you write your solution, you should initially avoid working with a fully-random board and instead use the constructor to create a much simpler board. • You can begin by the simple case of searching a simple gameboard for a single-letter word. • Once single-letter words are working, move on to two-letter words. Experiment with paths of an expected word (including letters on a diagonal). • The trickiest bit of programming will involve backtracking for words longer than two letters. Construct gameboards to test for cases where all letters in a word except for the last letter can be found as a sequence; your code must properly terminate.
Page 5 of 5
• Other things to consider: use the String methods toUpperCase() and toLowerCase() as needed; think carefully about how your code handles letters that lie on a board edge; recognize that objects references given as method parameters can result in changes to those objects, such that the changes are visible outside the method; beware of adding to a sequence a lettersquare already visited in the sequence. Files to submit: a. GameBoard.java b. FindAllWords.java
Grading • Submissions are expected to follow all of the requirements detailed in the specification (assuming you are already logged into conneX, the link is http://bit.ly/2GuuVy1). • The coding conventions, specified in the “Coding Conventions for CSC 115” document on conneX (link is http://bit.ly/2D4f8oh) must be followed.