Question: You must complete the code so that it works as follows: 1 . The code in the initialize ( ) procedure of the Gosu window

You must complete the code so that it works as follows:
1. The code in the initialize() procedure of the Gosu window should be completed to set up cells that are connected to each other with variables joining each cell to its neighbours (using references).
2. The user should be able to left click on cells on the screen to create mazes (and later in the Maze Search task we will use recursion to find a path through the maze).
3. Each cell clicked on should turn yellow.
4. Once this is working (or perhaps before) add code to print out each cell and indicate whether the reference to the neighbour on each side is nil or not, as per the following:
In this case there is no neighbour to the north or the west.
(NB: whether the east or the west neighbour is nil will depend on your perspective i.e is your perspective looking into the screen, or out of the screen)
Your submitted screenshot should look something like the following:
the code in question:
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP =*0..2
end
MAP_WIDTH =200
MAP_HEIGHT =200
CELL_DIM =20
class Cell
# have a pointer to the neighbouring cells
attr_accessor :north, :south, :east, :west, :vacant, :visited, :on_path
def initialize()
# Set all the pointers to nil
@north = nil
@south = nil
@east = nil
@west = nil
# record whether this cell is vacant
# default is not vacant i.e is a wall.
@vacant = false
# this stops cycles - set when you travel through a cell
@visited = false
@on_path = false
end
end
# Instructions:
# Left click on cells to create a maze with at least one path moving from
# left to right. The right click on a cell for the program to find a path
# through the maze. When a path is found it will be displayed in red.
class GameWindow Gosu::Window
# initialize creates a window with a width an a height
# and a caption. It also sets up any variables to be used.
# This is procedure i.e the return value is 'undefined'
def initialize
super MAP_WIDTH, MAP_HEIGHT, false
self.caption = "Map Creation"
@path = nil
x_cell_count = MAP_WIDTH / CELL_DIM
y_cell_count = MAP_HEIGHT / CELL_DIM
@columns = Array.new(x_cell_count)
column_index =0
# first create cells for each position
while (column_index x_cell_count)
row = Array.new(y_cell_count)
@columns[column_index]= row
row_index =0
while (row_index y_cell_count)
cell = Cell.new()
@columns[column_index][row_index]= cell
row_index +=1
end
column_index +=1
end
# now set up the neighbour links
# You need to do this using a while loop with another
# nested while loop inside.
end
# this is called by Gosu to see if should show the cursor (or mouse)
def needs_cursor?
true
end
# Returns an array of the cell x and y coordinates that were clicked on
def mouse_over_cell(mouse_x, mouse_y)
if mouse_x = CELL_DIM
cell_x =0
else
cell_x =(mouse_x / CELL_DIM).to_i
end
if mouse_y = CELL_DIM
cell_y =0
else
cell_y =(mouse_y / CELL_DIM).to_i
end
[cell_x, cell_y]
end
# start a recursive search for paths from the selected cell
# it searches till it hits the East 'wall' then stops
# it does not necessarily find the shortest path
# Completing this function is NOT NECESSARY for the Maze Creation task
# complete the following for the Maze Search task - after
# we cover Recusion in the lectures.
# But you DO need to complete it later for the Maze Search task
def search(cell_x ,cell_y)
dead_end = false
path_found = false
if (cell_x ==((MAP_WIDTH / CELL_DIM)-1))
if (ARGV.length >0) # debug
puts "End of one path x: "+ cell_x.to_s +" y: "+ cell_y.to_s
end
[[cell_x,cell_y]] # We are at the east wall - exit
else
north_path = nil
west_path = nil
east_path = nil
south_path = nil
if (ARGV.length >0) # debug
puts "Searching. In cell x: "+ cell_x.to_s +" y: "+ cell_y.to_s
end
# INSERT MISSING CODE HERE!! You need to have 4'if' tests to
# check each surrounding cell. Make use of the attributes for
# cells such as vacant, visited and on_path.
# Cells on the outer boundaries will always have a nil on the
# boundary side
if (column_index ==0)
cell.west = nil
else
cell.west = @columns[column_index -1][row_index]
end
if (column_index ==(x_cell_count -1))
cell.west = nil
else
cell.west = @columns[column_index -1][row_index]
end
# pick one of the possible paths that is not nil (if any):
if (north_path != nil)
path = north_path
elsif (south_path != nil)
path = south_path
elsif (east_path != nil)
path = east_path
elsif (west_path != nil)
path = west_path
end
# A path was found:
if (path != nil)
if (ARGV.length >0) # debug
puts "Added x: "+ cell_x.to_s +" y: "+ cell_y.to_s
end
[[cell_x,cell_y]].concat(path)
else
if (ARGV.length >0) # debug
puts "Dead end x: "+ cell_x.to_s +" y: "+ cell_y.to_s
end
nil # dead end
end
end
end
# Reacts to button press
# left button marks a cell vacant
# Right button
 You must complete the code so that it works as follows:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!