Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes between Version 5 and Version 6 of RecursiveMazeSolverExample

Show
Ignore:
Author:
nascent (IP: 67.110.217.136)
Timestamp:
05/30/10 21:57:35 (11 years ago)
Comment:

Updated to be version agnostic.

Legend:

Unmodified
Added
Removed
Modified
  • RecursiveMazeSolverExample

    v5 v6  
    4545import std.string; 
    4646 
    47 /* 
    48  * Created 7-Feb-2006; 
    49  * Author: Jesse Phillips; 
    50  */ 
    51  
    5247int main() { 
    5348  Maze myMaze = new Maze(); 
    5449 
    55   writefln(myMaze); 
     50  writefln("%s", myMaze); 
    5651 
    5752  if(myMaze.solveMaze()) { 
    6156    writefln("The maze could not be solved."); 
    6257 
    63   writefln(myMaze); 
     58  writefln("%s", myMaze); 
    6459   
    6560  return 0; 
    9590    /// Prints the maze to the screen. 
    9691    /// I will look for a Java's toString() equivalent 
    97     char[] toString()  
     92    override string toString()  
    9893    // Using Contracts (in{}out{}body{}) 
    9994    in { 
    10499      assert(maze); 
    105100    }body { 
    106       char[] result = format("Maze Size: %dx%d\n", size[X], size[Y]); 
     101      string result = format("Maze Size: %dx%d\n", size[X], size[Y]); 
    107102      // change the array length to about 200 because I know it will be large 
    108103      result ~= format("Starting Postion: (%d,%d)\n",start[X],start[Y]); 
    116111    } 
    117112 
    118     bit solveMaze() 
     113    bool solveMaze() 
    119114    in { 
    120115      assert(maze); 
    138133      char[][] charSize =  split(fileIn[line]); 
    139134 
    140       // std.string.atoi() converts strings to int 
    141       result[X] = atoi(charSize[X]); 
    142       result[Y] = atoi(charSize[Y]); 
     135      version(D_Version2) { 
     136         result[X] = std.conv.to!int(charSize[X]); 
     137         result[Y] = std.conv.to!int(charSize[Y]); 
     138      } else { 
     139         // std.string.atoi() converts strings to int 
     140         result[X] = atoi(charSize[X]); 
     141         result[Y] = atoi(charSize[Y]); 
     142      } 
    143143 
    144144      return result; 
    148148    ///   fileIn was the string array created by std.file.read() 
    149149    char[][] buildMaze() { 
    150       return fileIn[3..length][0..length]; 
     150      return fileIn[3..$][0..$]; 
    151151    } 
    152152 
    153153    /// Makes an attempt to move into x,y and continue to the exit 
    154     bit traverseMaze(int x, int y) { 
     154    bool traverseMaze(int x, int y) { 
    155155      if(validMove(x, y)) 
    156156        if(atEnd(x, y)) { 
    175175 
    176176    /// Checks to see if we are at the ending location 
    177     bit atEnd(int curX, int curY) { 
     177    bool atEnd(int curX, int curY) { 
    178178      return (curX == end[X] && curY == end[Y]); 
    179179    } 
    180180 
    181181    /// Checks to see if we are in a wall, out of bounds, or somewhere we have been 
    182     bit validMove(int x, int y) { 
     182    bool validMove(int x, int y) { 
    183183      // Within the maze bounds? 
    184184      if(y < 0 || x < 0 || y >= maze.length || x >= maze[y].length) 
    191191    } 
    192192} 
     193 
    193194}}} 
    194195==== output ====