Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You must complete the code so that it works as follows: 1 . Once the user has used the Left Mouse Key to create at
You must complete the code so that it works as follows:
Once the user has used the Left Mouse Key to create at least one path from one side of the screen to the other, the user should then be able to Right Click the mouse on a yellow cell on the left most side of the screen.
The program should then search using the recursive search function to find a path to the right side of the screen following one of the users created paths.
Each cell on the path should then be displayed in red.
A found path through the maze may look something like: Map Creation
please fix the following code:
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP
end
MAPWIDTH
MAPHEIGHT
CELLDIM
class Cell
attraccessor :north, :south, :east, :west, :vacant, :visited, :onpath
def initialize
@north nil
@south nil
@east nil
@west nil
@vacant false
@visited false
@onpath false
end
end
class GameWindow Gosu::Window
def initialize
super MAPWIDTH, MAPHEIGHT, false
self.caption "Map Creation"
@path nil
@startcells
@endcells
xcellcount MAPWIDTH CELLDIM
ycellcount MAPHEIGHT CELLDIM
@columns Array.newxcellcount Array.newycellcount Cell.new
# Set the neighbours of each cell.
# Loop through each column, then each row.
@columns.eachwithindex do column columnindex
column.eachwithindex do cell rowindex
cell.north @columnscolumnindexrowindex if rowindex
cell.south @columnscolumnindexrowindex if rowindex ycellcount
cell.east @columnscolumnindex rowindex if columnindex xcellcount
cell.west @columnscolumnindex rowindex if columnindex
end
end
end
def needscursor?
true
end
def mouseovercellmousex mousey
cellx mousex CELLDIM
celly mousey CELLDIM
cellx celly
end
def buttondownid
if id Gosu::MsLeft
# Get the cell the mouse is over.
cellx celly mouseovercellmousex mousey
cell @columnscellxcelly
# Toggle the cell's vacancy.
cell.vacant cell.vacant
elsif id Gosu::MsRight && @startcells.empty?
# Get the cell the mouse is over.
cellx celly mouseovercellmousex mousey
cell @columnscellxcelly
# Find a path from any of the start cells to this cell.
pathfound false
@startcells.each do startcell
if searchstartcell, cell
pathfound true
break
end
end
# Color the cells in the path red.
if pathfound
cell @path.east
while cell @path
cell.onpath true
cell cell.west
end
cell.onpath true
end
end
end
# A modified DFS algorithm that searches for a path from the start cell to
# the end cell, returning true if one is found, false otherwise.
def searchstartcell, destination
# Your search algorithm implementation here
end
end
# Create an instance of the GameWindow class and run the game loop
window GameWindow.new
window.show
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