Programming Assignment 08

Deadline: November 22 2024 – Friday, 11:30pm

This assignment is part 1 of a 2-part assignment. In this assignment you will write code to create a word search game. In the next assignment you will write code for a user to play that game.

Make word search game

In this assignment, your goal is to create a word search grid based on an input text file.

Example of input file:

20 20
amazing
zoo
house
mouse
croissant
coffee
milk
tea
scone
brownie
napkin
fudge

The first line of the file will always give you the dimensions of the word search grid. The other lines will contain one word per line.

Here’s an example output based on the input file given above (your output does not have to match this exactly, but all words need to be there):

X H N R C B V D P S R P X L O T L C X O 
K M I L K K Y B J T G L T P Y K F R N C 
Y G Q K T A M A Z I N G W U T B C J I A 
Q A M W P L P T O U R I V R P J U K G W 
H I J D X K B P O Y N A P K I N J D W I 
Y O B P B W G M V I Y M X Y U D G V A R 
N E W W R N R B L D F U D G E W J V E T 
J J S C O N E D J D O S U V A W X I H R 
A L A T W M I C R O I S S A N T D N C R 
W L B Q N Q X O X X T F K E B G R D C H 
M I L K I I D F G R E T W Y B K M W P U 
O P I C E F D F I P N F O Y T N T G D D 
T E A H U N C E H X H K K D U U P U S F 
E W D L Q S J E I R B C Y R V W Y W S Q 
R T I L G G R X A P G O M K W O B M M I 
E F J X L Y L J W Y G H O U S E K J Q B 
N O H E I C U X V B Y Q U J I L L Q A C 
Y G E C J Y L B J C Y K S Y N F G K T J 
M S Y I A N D Q G I A H E V G C W W A B 
B U O R T Y K E D X Y K A X K T I X T Q
  • amazing is at 2 5
  • zoo is at 2 8
  • house is at 15 11
  • mouse is at 14 12
  • croissant is at 8 7
  • coffee is at 8 7
  • milk is at 10 0
  • tea is at 12 0
  • scone is at 7 2
  • brownie is at 5 4
  • napkin is at 4 10
  • fudge is at 6 10

You are to write out an output file that has ouput_ in front of the file name. For example, if the input file is named input.txt the output file should be named output_input.txt.

You are require to write a WordSearch.java class that has a main method, and the first argument provided is the input file name. All the other classes and methods are up to you. The autograder will be checking the output text file you create.

Diagonal words

Some of your words should be diagonal (in addition to horizontal and vertical words). Here’s an example with diagonal words.

K M J M P S P L O Y Y H J T U R O Y J H 
H K L C O K Q M C P Y A H U F C V F W H 
T D U Y F U A C I G D T Q H L C W R Y N 
X L V Q A C S M E F S T B X R N G S X T 
V G L E T L R E X C Y U A M A Z I N G X 
G L Q G C N T O E H M N K P R O T Q D C 
U L Y E R J J E I J O F G Y O O Y B K X 
K M W L A K L X A S E U Y E F C H R I M 
J C C B P Q R S S D S X S E A S C O N E 
E N J P U M B V R L K A Q E M A W W X I 
E F F H P W O U M S B E N V X K X N R Q 
U H X X Q V I W O C S J Q T M U U I W O 
G J N B C M L U Q B W U X M G H N E E A 
L J B L G R S Q Y J K O P M Q Y J S W M 
T P X E T D C I D S G O Y C N E D V V X 
N A P K I N F I O L Q G M D T L T D V K 
W B C B K M U C O F F E E S O S L G F L 
F U D G E U E M L N O P F Y U S I K U E 
I B C O M T G M I L K G R Q B R E R V A 
G W D Y O D L J Q A O B R D R F Q D P C 
  • amazing is at 4 12 (horizontal)
  • zoo is at 4 15 (vertical)
  • house is at 5 9 (diagonal)
  • mouse is at 0 3 (diagonal)
  • croissant is at 3 5 (diagonal)
  • coffee is at 16 7 (horizontal)
  • milk is at 18 7 (horizontal)
  • tea is at 5 6 (diagonal)
  • scone is at 8 15 (horizontal)
  • brownie is at 6 17 (vertical)
  • napkin is at 15 0 (horizontal)
  • fudge is at 17 0 (horizontal)

Writing a file

Here’s sample code on how to write a file (you don’t have to do this exactly like the example below, the autograder will be opening and reading the file you create):

String filename = args[0];
Grid myGrid = setUpGame(filename);
FileWriter fileWriter = new FileWriter("output_" + filename);
fileWriter.write(myGrid.toString());
fileWriter.close();

Your file name should come from the args array. The autograder will run the main method in WordSearch.java.

Ouput file format

Your output file should contain only your grid, and nothing else.

Example:

K M J M P S P L O Y Y H J T U R O Y J H
H K L C O K Q M C P Y A H U F C V F W H
T D U Y F U A C I G D T Q H L C W R Y N
X L V Q A C S M E F S T B X R N G S X T
V G L E T L R E X C Y U A M A Z I N G X
G L Q G C N T O E H M N K P R O T Q D C
U L Y E R J J E I J O F G Y O O Y B K X
K M W L A K L X A S E U Y E F C H R I M
J C C B P Q R S S D S X S E A S C O N E
E N J P U M B V R L K A Q E M A W W X I
E F F H P W O U M S B E N V X K X N R Q
U H X X Q V I W O C S J Q T M U U I W O
G J N B C M L U Q B W U X M G H N E E A
L J B L G R S Q Y J K O P M Q Y J S W M
T P X E T D C I D S G O Y C N E D V V X
N A P K I N F I O L Q G M D T L T D V K
W B C B K M U C O F F E E S O S L G F L
F U D G E U E M L N O P F Y U S I K U E
I B C O M T G M I L K G R Q B R E R V A
G W D Y O D L J Q A O B R D R F Q D P C

Test your output file

Run the provided JUnit test to check if your output file is in the correct format and that all words are found in the grid (the autograder will run a similar test, but it will also check that you have at least one word in each orientation).

Gradescope submission

Package information:

package com.gradescope.wordsearch;

Decomposition

  • Points will be taken off for copy, pasted, and edited code that should have been encapsulated in a method.

  • This program should use fewer than 5 .java files but at least two .java files Each of these files should be (<300 lines). Do NOT include hard code any specific words at specific coordinates.

  • Each method should be less than 30 lines. This INCLUDES comments, but not the method header. It is easier to read a function if it can all fit on one screen.

Code Clarity

  • YOU should be able to read, understand, and explain your own code to someone else a couple days after you wrote it.

    • No magic numbers
    • No methods written to just get the test cases to work
  • There needs to be a balance between no comments in the body of the methods and a comment for every line in the program. Either extreme will result in points off.

  • The file header should include instructions on how someone would use this program. To use the program, one would need to know the input file format. One would also need to know the command line arguments that should be passed to the program.

  • Use meaningful variable names. Loop iterators can be simple (i for integers, s for strings, n for numbers, etc.).

  • The clearest code examples will be anonymously shown in class.

  • The most obfuscated code examples will be anonymously shown in class with suggestions for improvement.