Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with computing the pathing for the arrows. This is a class that, by following the arrow rules, Cupid will shoot his arrows

I need help with computing the pathing for the "arrows". This is a class that, by following the arrow rules, Cupid will shoot his arrows in order to hit the targets. The input file/map is like this: (the

Second line: #rows, #columns #num targets #quiver size Other lines: H-horizontal path, V-vertical path, C-cross path, W-wall, S-starting point, J- Target 5 5 1 5 W W S W W W W W W W W W V W W V W C V J C H C H H

image text in transcribed

image text in transcribed

image text in transcribed

You must implement the following methods in this class: StartSearch(String filename) This is the constructor for the class. It receives as input the name of the file containing the description of the map. In this method you must create an object of the class Map (described in the provided files) passing as parameter the given input file; this will display the map on the screen. Read them if you want to know the format of the input files. static void main(String[] args). This method will first create an object of the class StartSearch using the constuctor StartSearch(args[0]). Args[O] will be the name of a map file, args[1] will be the number of cells that the arrow can travel before it falls to the ground. o If and only if a size argument is provided, your algorithm should count how many targets can be found in a path that is at most args[1] long with the number of arrows determined by the map. When you run the program, you will pass as command line arguments the name of the input file (see following section after Java classes), and the number of cells that Cupid can hit from his starting point to find target along with the number of arrows. Your main method then will try to find a path from Cupid to the targets according to the restrictions specified above. o The algorithm that looks for a path from the initial cell to the destinations must use a stack and it cannot be recursive. Suggestions on how to look for this path are given in the next section. The code provided to you will show the path selected by the algorithm as it tries to reach the target cells, so you can visually verify if your program works. The main method should exit by printing out the number of targets found given the restrictions above (maximum arrow length, maximum number of arrows, Cupid won't shoot down the same path twice. . MapCell nextCell(MapCell cell). o The parameter is the current cell. o This method returns the best cell to continue the path from the current one, as specified early in the limitations. o Refer to those priorities when coding this section. o If several unmarked cells are adjacent to the current one and can be selected as part of the path (remember the arrow will stay on the same path first), then this method must return one of them in the following order: . A target cell . A cross path cell. If there are several possible cross path cells, then the one with the smallest index is returned. A vertical path cell or a horizontal path cell with smallest index Read the description of the class MapCell below to learn how to get the index of a neighbouring cell. If there are no unmarked cells adjacent to the current one that can be used to continue the path, this method returns null. o Your program must catch any exceptions that are thrown by the provided code. For each exception caught, an appropriate message must be printed. o The message must explain what caused the exception to be thrown. You can write more methods in this class but they must be declared as private. Algorithm for Computing a Path Below is a description for an algorithm that will look for a path for Cupid to the targets. It will be helpful to understand the algorithm deeply before attempting to implement it. There are better algorithms for finding targets given a maximum path length, but they may not pass all the tests. Implement the algorithm as described to make sure your code passes the test cases. You must use a stack to keep track of which cells are in the path, and it cannot be recursive. Writing your algorithm in pseudocode will make coding it in Java easier and is helpful to show to the TAs and the instructors if you need help. Initialize the number of found targets to 0. Create a stack based on the command line inputs . Get the start cell (Cupid) using the methods of the supplied class Map. Push the starting cell into the stack and mark the cell as inStack. . You will use methods of the class MapCell to mark a cell. While the stack is not empty and Cupid has arrows left, and the arrow path length has not been reached, perform the following steps: o Peek at the top of the stack to get the current cell. o Find the next unmarked neighbouring cell (use method nextCell from class StartSearch to do this). o If one exists: Push the neighbouring cell into the stack and mark it as inStack. (if max path length given) Increase the path length counter . If the best neighbouring cell is a target, increase the number of targets hit o Otherwise, since there are no unmarked neighbouring cells that can be added to the path, pop the top cell (and increase the path length counter) from the stack. While the stack is not empty perform the following: o Pop the top cell from the stack. o When popping cells adjacent to Cupid, mark as out of stack. Your program must print a message indicating how many targets were hit Note that your algorithm does not need to find the shortest path from Cupid to all the targets, or even the shortest path with the pathing restraints. Command Line Arguments Your program must read the name of the input map file from the command line. You can run the program with the following command: java StartSearch nameOfMapFile (optional)maxPathLength Where nameOfMapFile is the name of the file containing the map, and maxPathLength is the longest path that Cupid's arrow can take to find targets. You can use the following code to verify that the program was invoked with the correct number of arguments: public class StartSearch { public static void main(String args) { if (args.length

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

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago