Menu

Part 1 Recursion Text File Input Java Problem Might Take Time Solve Dont Know Policy Need Q43906773

Part 1: Recursion and text file input (Java problem…this mighttake time to solve but i dont know the policy. I need solutionbadly)

This problem will ask you to find a path through a maze whoseform is given to you via an input file. The input file willindicate the broad outline of a square Maze that you will find yourway out of.

Such a maze in the file might look like the following (allcharacters are separate by a single space).

S

.

.

#

#

#

#

#

#

.

#

#

.

.

D

.

#

.

.

R

.

#

#

.

#

.

#

#

.

#

#

.

.

.

#

#

#

#

#

.

#

.

.

R

#

#

#

.

#

#

#

.

.

.

#

.

#

#

#

.

#

E

.

.

The Legend for this maze is the follows:

S => Start Point, E => End Point (Our Goal),‘’.’’   => Open Path (Cost is 1), # => StoneImpassable, R =>Rabbit (Cost is 10), D => Dragon (Cost is75)

The idea is that you are an adventurer trying to find your waythrough this maze using the least expensive path.   Ifyour path takes you on a “.” square then the cost of this path is1. However, if you encounter a Rabbit then you have to fight therabbit for the square at a cost of 10. Finally if you encounter aDragon then a major fight will ensue at a cost of 75.  Different paths from S to E will cost different amount intotal.

As adventurers we should really be taking the other path.However, this portion of the assignment does not require you tofind the shortest path, but just one path through the maze.

Solution Design

               Your program is to implement a Recursive solution to the problem offinding a path through the maze.   In order to do this weare going to ask you to implement a GameCell Class and a GameBoardClass. A GameBoard is to be composed of a 2D square array ofGameCell Entries. Each of these GameCell entries will correspond toone square in the maze.   The GameBoard will thenrepresent the entirety of the maze. UML diagrams for these classescan be found on the next pages.

GameCell

-cVal:char

-nRow:int

-nCol:int

-bVisited:boolean

+Rabbit_Cost:int

+Dragon_Cost:int

+Blocked_Cost:int

+Open_Cost:int

+GameCell(cVal:char,nRow:int,nCol:int)

+isVisited():boolean

+setVisited(bVal:boolean)

+getCurrentCost():int

+isBlocked():boolean

+isStart():boolean

+isEnd():Boolean

+toString():String

+getRow():int

+getCol():int

+getVal():char

The Methods are described as following:

               GameCell – This method takes the current value that the square isto be associated with in the maze (i.e. ‘R’ is rabbit, ‘.’ Is open)and the row and the column position of the cell in the maze.

isVisited,setVisited – These are used for determination ofwhether a square has been visited.

               getCurrentCost – This returns the current cost for a square (i.e. 1open, 75 for dragon).

               isStart,isEnd – These indicate whether or not the current square isa start or end square (no cost).

               getRow,getCol – Return the corresponding row and column values inthe maze.

               getVal – Returns the corresponding character in that cell.

               toString – This will just return a string consisting of the currentcharacter value and a space character.

               The following page will list the GameBoard UML Diagram

GameBoard

-aBoard:GameCell[][]

-nSquareSize:int

-nStartRow:int

-nStartCol:int

+GameBoard(String:sFileName)

#loadBoard(String:sFileName)

#getSize(String:sFileName):int

+playGame():void

-findStart():void

#findPath(nRow:int,nCol:int):Vector

#canGoUp(nRow:int, nCol:int):boolean

#canGoLeft(nRow:int, nCol:int):boolean

#canGoRight(nRow:int,nCol:int):boolean

#canGoDown(nRow:int,nCol:int):boolean

+pathCost(obPath:Vector):int

+printBoard():void

The Methods are described as follows:

               GameBoard – This is the constructor that takes the name of the filethat is supposed to hold a representation of the maze (see Notes oninput later).

               loadBoard – Helper routine which will handle the actual details ofreading the file and instantiating the corresponding GameCells.

               getSize – This routine will return the number of lines in theassociated file. Note that we will only be working with squarematrices and the number of lines in the file is the same as thenumber of rows and columns in the max.

               findStart – This is a helper routine which sets the nStartRow andnStartCol values. Note that these values do not have to be onborder square.

               playGame – This routine will print out

  • If there is a path from the start point to the end
  • The cost of path that was found
  • The Cell positions (row,column) on the path path.

It works by determining the startingposition and then calling the recursive findPath routine.

findPath – This is the recursiveroutine that will walk the maze. In a nutshell this routine willdetermine if we can go left, right, up, or down and then callitself again in any valid directions. It will return the path fromits current position to the end or null if no path from the currentpoint to the end position exists.

canGoLeft, canGoRight, canGoUp,canGoDown – These routines will, when given the current cellposition as an argument, indicate if it is ok to go in one of theindicated directions. A cell is valid to visit if

  • It is in the game boundary (i.e. you can’t go left if you arein column 0).
  • It is not blocked (i.e. associated value is ‘#’)
  • You have not visited the cell before.

pathCost – This routine given a path(as stored in a Vector) will return the cost of all squares on thatpath.

printBoard() – This function willprint out the contents of the current game board to the screen. Theoutput should look like the following:

Game Board

# . . # # # # #

# . # # . . D .

# . . R . # # .

# # # # . # # S

. . # # # # # .

# . . R # # # .

# # # . . . # .

# # # . # E . .

Input notes:

               Input files will consist of a series of lines that each describes arow in our maze. Each character (square) in that row will beseparated from the next by a single space character. Note that the# of rows and columns in the maze is assumed to be a square.

               The ‘S’ character may not necessarily be at position (0,0) in thefile.

Part 2: ShortestPath                                                                                                         

               Modify the code as given above to actually find the shortest path(if 2 or more path exists).  

               The logic of the findPath routine is going to have to change –essentially you cannot just ask “has this cell been visitedbefore”.   You must ask “On the current path, have Ivisited this cell before”.

if you keep track of the cells you are visiting on the currentpath, you would recognize that this cell would not have beenvisited (using the right/left/up/down strategy) and could proceedto be part of a new path.

       One possible solution is toa pass along a current path variable of type ArrayList andadd/delete cells as you visit/leave them. So a Signature forFindShortestPath Might Look like:

Public ArrayList FindShortestPath(int nRow, int nCol, ArrayListobCurrPath)

Expert Answer


Answer to Part 1: Recursion and text file input (Java problem…this might take time to solve but i dont know the policy. I need s…

OR