Version 10/11/17
Educational Objectives: After completing this assignment, the student should be able to accomplish the following:
Operational Objectives: Implement a random maze generator as a client of fsu::Partition.
Deliverables:
ranmaze.cpp # client program of Partition <> generating random mazes makefile # creates targets ranmaze.x, solvemaze.x log.txt # development and testing log
The official development/testing/assessment environment is specified in the Course Organizer. Code should compile without warnings or errors.
In order not to confuse the submit system, create and work within a separate subdirectory cop4531/proj5.
Maintain your work log in the text file log.txt as documentation of effort, testing results, and development history. This file may also be used to report on any relevant issues encountered during project development.
General requirement on genericity. Use generics whenever possible. For example:
In short: re-use components from LIB (or your own versions) whenever possible. Don't re-invent the wheels.
Begin by copying all of the files from LIB/proj5 into your proj5 directory along with relevant executables from area51. You should see at least the following:
deliverables.sh # submission configuration file ranmaze_i.x # sample executable mazemaster.x # maze analyzer
Execute ranmaze_i.x to generate maze files and mazemaster.x to analyze the resulting maze files. You will follow the same process in testing your own ranmaze and its resulting maze files. Also pay careful attention to the detailed I/O behavior of ranmaze, this is how your program should behave.
Create the file ranmaze.cpp, a client of Partition<>. Using mazemaster.x, test thoroughly to be sure the mazes it generates are self-consistent and that the behavior mimics that of LIB/area51/ranmaze_i.x, including the file name extensions for output files.
Check that ranmaze.cpp meets the specifications given below.
Be sure that you have established the submit script LIB/scripts/submit.sh as a command in your ~/.bin directory.
Warning: Submit scripts do not work on the program and linprog servers. Use shell.cs.fsu.edu or quake.cs.fsu.edu to submit projects. If you do not receive the second confirmation with the contents of your project, there has been a malfunction.
Output maze files according to the spec in the Union-Find notes Appendix: Maze Technology. Take care that all maze files are syntactically and semantically correct - mazemaster.x is helpful in testing your output.
The maze file should have the start cell at the beginning (left-most cell) of the middle row and the goal at the end (right-most cell) of the same row.
ranmaze should expect three command line arguments: number of rows, number of columns, and filename.
ranmaze should output two versions of the random maze: (1) the first time goal is reachable from start, output the maze to the file "filename.[components]" where [components] is the actual number of components in the maze; and (2) after all cells are reachable from start, output the maze to the file "filename.1".
Take care that your mazes are correctly "random" in the sense that all walls have the same probability of being selected for inspection at each step. This is mostly a matter of judicious design, especially at boundary cases. For example: be sure that a cell face on an interior cell has the same chance of selection as a cell face of a boundary cell, where fewer faces are eligible.
A very handy way to generate unsigned integers is with the fsu random object generator. Include these files:
#include <xran.h> #include <xran.cpp>
and declare an object and use it like this:
static fsu::Random_unsigned_int ran; ... x = ran(a,b); // x is a random unsigned int in the range a <= x < b y = ran(0,4); // y is a random choice of 0,1,2,3 (N,E,S,W) z = ran(0,2); // z is a random choice of 0 or 1 (coin flip)
This generator uses the Marsaglia KISS generator as its underlying engine.
The keyword static in this context has the meaning inherited from C: The object will be initialized the first time it comes into scope and remain alive during the remaining execution, going in and out of scope as needed.
An effective way to organize ranmaze.cpp is to write a function
void Connect( size_t beg , size_t end , // start and goal cells size_t numrows, size_t numcols, // maze dimensions fsu::Vector<uint8_t>& maze , // walls codes for cells fsu::Partition<size_t>& p // tracks component structure );
that can be called from main() once for the "first pass" maze and subsequently as often as needed for the "one component" maze. Note that the maze data and partition data are passed by reference so that modifications made in a call to Connect will affect the data in the calling process. The organization is then left to function main:
int main (int argc, char* argv[]) { ... size_t numcells = numrows * numcols; start = ??; goal = ??; fsu::Vector<uint8_t> maze (numcells, 15); // all closed boxes fsu::Partition<size_t> p (numcells); // all singletons // ensure goal is reachable from start: std::cout << " components after 0 passes: " << p.Components() << '\n'; Connect (start, goal, numrows, numcols, maze, p); size_t components = p.Components(); std::cout << " components after 1 pass: " << components << '\n'; if (components > 1) // output intermediate result the first time goal is reachable { // build filename filec ... // output first pass maze out1.open(filec); WriteMaze(out1, start, goal, numrows, numcols, maze); out1.close(); out1.clear(); std::cout << "1-pass maze written to file " << filec << '\n'; } // continue until all cells are reachable if (components > 1) { for (size_t cell = 1; cell < numcells; ++cell) { Connect (0, cell, numrows, numcols, maze, p); } } std::cout << " components after all passes: " << p.Components() << '\n'; out1.open(file1); WriteMaze(out1, start, goal, numrows, numcols, maze); out1.close(); out1.clear(); std::cout << "n-pass maze written to file " << file1 << '\n'; ... }
Building the filename is just the metedious [meticulous + tedious] work of creating the correct size null-terminated character array and putting all the characters in the right place. You need two such file names "filec" and "file1" each of which is the user input filename "file" with an extension appended. To get the extension for filec, unfortunately there is no "itoa" in the library, so you need the number of digits = 1 + (size_t)log10(components) and a loop that finds the digits:
for (size_t i = 0; i < digits; ++i) { char digit = '0' + (char)(components % 10); // put this digit in the correct place in filec components /= 10; }This loop calculates the digits. You have to put them in the correct place in the file name array. Don't forget the null terminator. (You can also use std::stringstream if you prefer.)
Here is a way to get started on the main program, including the handy constants for four atomic walls:
const uint8_t NORTH = 0x01; const uint8_t EAST = 0x02; const uint8_t SOUTH = 0x04; const uint8_t WEST = 0x08; ... int main (int argc, char* argv[]) { if (argc < 4) { std::cout << "** command line arguments required:\n" << " 1: number of rows\n" << " 2: number of cols\n" << " 3: file name for maze\n" return 0; } size_t numrows = atoi(argv[1]); size_t numcols = atoi(argv[2]); ... }
The wall constants are global, hence accessible from Connect()
The following image is of a maze generated by ranmaze. Start and goal cells marked red/green, respectively.
The executable area51/mazemaster.x is useful in analysing small maze files while the maze programs are under development. The mazebuilder-olson and mazebuilder-brown javascripts (written by former students) are useful in creating small mazes for testing.
If you have web space on ww2.cs.fsu.edu, you can set up a quick maze viewer as follows:
Put a script "mazegen.sh" in your space with contents:
#!/bin/sh now=($( date '+%F %H:%M')) DATE=${now[0]} TIME=${now[1]} TIME_STAMP=${DATE}_${TIME} ranmaze.x $1 $2 _TEMPMAZE cp _TEMPMAZE maze$1x$2.$TIME_STAMP # saves copy of maze printmaze.x -h12 -w12 < _TEMPMAZE.1 > _TEMPMAZE.ps pstopnm -stdout -yborder=0 -xborder=0 -portrait -xsize=1000 _TEMPMAZE.ps | pnmtopng > maze.png chmod 644 maze.png rm _TEMPMAZE* echo "maze graphic in maze.png"
Make a simple web page "viewmaze.html" with contents as follows:
<html> <head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252"> <title>ViewMaze</title> <style type = "text/css"></style> </head> <body link="black" vlink="black" alink="#808080"> <br> <blockquote> <IMG SRC="maze.png" ALT="maze.png"><br> Maze generated by ranmaze. Solution obtained using Path_BFS in graph model. </body> </html>
Put a copy of printmaze.x in the same directory.
Change permissions of *.x and *.sh to 700.
Change permissions of viewmaze.html to 644.
Then running "mazegen.sh $1 $2" followed by a refresh of the page will display the newly generated maze. Here is a working example: mazegen2.
Alternatively you can drag maze.png to your desktop and view it there.