Answered step by step
Verified Expert Solution
Link Copied!

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:
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: Map Creation
please fix the following code:
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
@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.
# Loop through each column, then each row.
@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)
if id == Gosu::MsLeft
# Get the cell the mouse is over.
cell_x, cell_y = mouse_over_cell(mouse_x, mouse_y)
cell = @columns[cell_x][cell_y]
# Toggle the cell's vacancy.
cell.vacant =!cell.vacant
elsif id == Gosu::MsRight && !@start_cells.empty?
# Get the cell the mouse is over.
cell_x, cell_y = mouse_over_cell(mouse_x, mouse_y)
cell = @columns[cell_x][cell_y]
# Find a path from any of the start cells to this cell.
path_found = false
@start_cells.each do |start_cell|
if search0(start_cell, cell)
path_found = true
break
end
end
# Color the cells in the path red.
if path_found
cell = @path.east
while cell != @path
cell.on_path = true
cell = cell.west
end
cell.on_path = 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 search0(start_cell, 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
image text in transcribed

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

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions

Question

What has been your desire for leadership in CVS Health?

Answered: 1 week ago

Question

Question 5) Let n = N and Y Answered: 1 week ago

Answered: 1 week ago