#!/usr/local/bin/ruby # ######################################## # CMSC 330 - Project 1 # ######################################## #----------------------------------------------------------- # FUNCTION DECLARATIONS #----------------------------------------------------------- def parse(file) puts "Not yet implemented" end #----------------------------------------------------------- # the following is a parser that reads in a simpler version # of the maze files. Use it to get started writing the rest # of the assignment. You can feel free to move or modify # this function however you like in working on your assignment. def read_and_print_simple_file(file) line = file.gets if line == nil then return end # read 1st line, must be maze header sz, sx, sy, ex, ey = line.split(/\s/) puts "header spec: size=#{sz}, start=(#{sx},#{sy}), end=(#{ex},#{ey})" # read additional lines while line = file.gets do # begins with "path", must be path specification if line[0...4] == "path" p, name, x, y, ds = line.split(/\s/) puts "path spec: #{name} starts at (#{x},#{y}) with dirs #{ds}" # otherwise must be cell specification (since maze spec must be valid) else x, y, ds, w = line.split(/\s/,4) puts "cell spec: coordinates (#{x},#{y}) with dirs #{ds}" ws = w.split(/\s/) ws.each {|w| puts " weight #{w}"} end end end #----------------------------------------------------------- # EXECUTABLE CODE #----------------------------------------------------------- #---------------------------------- # check # of command line arguments if ARGV.length < 2 fail "usage: maze.rb " end command = ARGV[0] file = ARGV[1] maze_file = open(file) #---------------------------------- # perform command case command when "parse" parse(maze_file) when "print" read_and_print_simple_file(maze_file) else fail "Invalid command" end