Answered step by step
Verified Expert Solution
Link Copied!

Question

00
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:
1.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.
2.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.
3.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 =*0..2
end
MAP_WIDTH =200
MAP_HEIGHT =200
CELL_DIM =20
class Cell
attr_accessor :north, :south, :east, :west, :vacant, :visited, :on_path
def initialize
@north = nil
@south = nil
@east = nil
@west = nil
@vacant = false
@visited = false
@on_path = false
end
end
class GameWindow Gosu::Window
def initialize
super MAP_WIDTH, MAP_HEIGHT, false
self.caption = "Map Creation"
@path = nil
@start_cells =[]
@end_cells =[]
x_cell_count = MAP_WIDTH / CELL_DIM
y_cell_count = MAP_HEIGHT / CELL_DIM
@columns = Array.new(x_cell_count){ Array.new(y_cell_count){ Cell.new }}
# Set the neighbours of each cell
@columns.each_with_index do |column, column_index|
column.each_with_index do |cell, row_index|
cell.north = @columns[column_index][row_index -1] if row_index >0
cell.south = @columns[column_index][row_index +1] if row_index y_cell_count -1
cell.east = @columns[column_index +1][row_index] if column_index x_cell_count -1
cell.west = @columns[column_index -1][row_index] if column_index >0
end
end
end
def needs_cursor?
true
end
def mouse_over_cell(mouse_x, mouse_y)
cell_x = mouse_x / CELL_DIM
cell_y = mouse_y / CELL_DIM
[cell_x, cell_y]
end
def button_down(id)
case id
when Gosu::MsLeft
cell_x, cell_y = mouse_over_cell(mouse_x, mouse_y)
cell = @columns[cell_x][cell_y]
cell.vacant = true
@start_cells cell if cell_x ==0 # Add cell to start cells if on left edge
when Gosu::MsRight
cell_x, cell_y = mouse_over_cell(mouse_x, mouse_y)
start_cell = @columns[cell_x][cell_y]
if !@start_cells.empty? && start_cell.vacant
search(start_cell, @columns.last[cell_y]) # Find path to right edge
end
end
end
def search(current_cell, end_cell)
return false if current_cell.nil? || current_cell.visited || current_cell.vacant
current_cell.visited = true
if current_cell == end_cell
current_cell.on_path = true
return true
end
[current_cell.north, current_cell.south, current_cell.east, current_cell.west].each do |neighbor|
if search(neighbor, end_cell)
current_cell.on_path = true
return true
end
end
false
end
def draw
@columns.each_with_index do |column, column_index|
column.each_with_index do |cell, row_index|
color = cell.vacant ? Gosu::Color::YELLOW : Gosu::Color::GREEN
color = Gosu::Color::RED if cell.on_path
Gosu.draw_rect(column_index * CELL_DIM, row_index * CELL_DIM, CELL_DIM, CELL_DIM, color, ZOrder::TOP, mode=:default)
end
end
end
end
window = GameWindow.new
window.show Maze_creation.rb
require 'gosu'
module zorder
BACKGROUND, MIDDLE, TOP =**0..2
end
MAP_WIDTH =200
MAP_HEIGHT =200
CELL_DIM =20
class Cell
attr_accessor :north, :south, :east, :west, :vacant, :visited, :on_path
def initialize
@north = nil
@south = nil
@east = nil
ewest = nil
@vacant = false
@visited = false
@on_path = false
end
end
class GameWindow Gosu::Window
def initialize
super MAP_WIDTH, MAP_HEIGHT, false
self.caption = "Map Creation"
@path = nil
@start_cells =[]
@end_cells =[]
x_cell_count = MAP_WIDTH / CELL_DIM
y_cell_count = MAP_HEIGHT / CELL_DIM
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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