Question: 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:
code in question:
equire '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
@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
case id
when Gosu::MsLeft
cellx celly mouseovercellmousex mousey
cell @columnscellxcelly
cell.vacant true
@startcells cell if cellx # Add cell to start cells if on left edge
when Gosu::MsRight
cellx celly mouseovercellmousex mousey
startcell @columnscellxcelly
if @startcells.empty? && startcell.vacant
searchstartcell, @columns.lastcelly # Find path to right edge
end
end
end
def searchcurrentcell, endcell
return false if currentcell.nil? currentcell.visited currentcell.vacant
currentcell.visited true
if currentcell endcell
currentcell.onpath true
return true
end
currentcell.north, currentcell.south, currentcell.east, currentcell.westeach do neighbor
if searchneighbor endcell
currentcell.onpath true
return true
end
end
false
end
def draw
@columns.eachwithindex do column columnindex
column.eachwithindex do cell rowindex
color cell.vacant Gosu::Color::YELLOW : Gosu::Color::GREEN
color Gosu::Color::RED if cell.onpath
Gosu.drawrectcolumnindex CELLDIM, rowindex CELLDIM, CELLDIM, CELLDIM, color, ZOrder::TOP, mode:default
end
end
end
end
window GameWindow.new
window.show Mazecreation.rb
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
ewest 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
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
