PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/mona/src/minc/TmazeMaker.cpp

#
C++ | 189 lines | 164 code | 15 blank | 10 comment | 37 complexity | b84019e8f3579a34631eaea91f0275d3 MD5 | raw file
  1. // For conditions of distribution and use, see copyright notice in minc.hpp
  2. /*
  3. * Tmaze-making program.
  4. * Generate T-mazes using the given parameters and output in the
  5. * LENS neural network example file format.
  6. */
  7. #include "Tmaze.hpp"
  8. #include "TmazeGrammar.hpp"
  9. // Mazes.
  10. vector<Tmaze *> Mazes;
  11. char *Usage[] =
  12. {
  13. (char *)"Tmaze_maker\n",
  14. (char *)" -mazeSeed <maze random seed>\n",
  15. (char *)" -numMazeMarks <number of maze marks (must be >= 2)>\n",
  16. (char *)" -numMazes <number of mazes to generate>\n",
  17. (char *)" [-outputFile <output file name> (otherwise to standard output)]\n",
  18. NULL
  19. };
  20. void printUsage()
  21. {
  22. for (int i = 0; Usage[i] != NULL; i++)
  23. {
  24. fprintf(stderr, "%s", Usage[i]);
  25. }
  26. }
  27. int
  28. main(int argc, char *argv[])
  29. {
  30. int i, j, m, mazeSeed, numMazeMarks, numMazes, intervals;
  31. char *outputFile;
  32. Tmaze *maze;
  33. FILE *fp;
  34. // Get options.
  35. mazeSeed = numMazeMarks = numMazes = -1;
  36. outputFile = NULL;
  37. for (i = 1; i < argc; i++)
  38. {
  39. if (strcmp(argv[i], "-mazeSeed") == 0)
  40. {
  41. i++;
  42. if (i >= argc)
  43. {
  44. printUsage();
  45. return(1);
  46. }
  47. mazeSeed = atoi(argv[i]);
  48. continue;
  49. }
  50. if (strcmp(argv[i], "-numMazeMarks") == 0)
  51. {
  52. i++;
  53. if (i >= argc)
  54. {
  55. printUsage();
  56. return(1);
  57. }
  58. numMazeMarks = atoi(argv[i]);
  59. if (numMazeMarks < 2)
  60. {
  61. printUsage();
  62. return(1);
  63. }
  64. continue;
  65. }
  66. if (strcmp(argv[i], "-numMazes") == 0)
  67. {
  68. i++;
  69. if (i >= argc)
  70. {
  71. printUsage();
  72. exit(1);
  73. }
  74. numMazes = atoi(argv[i]);
  75. if (numMazes < 0)
  76. {
  77. printUsage();
  78. return(1);
  79. }
  80. continue;
  81. }
  82. if (strcmp(argv[i], "-outputFile") == 0)
  83. {
  84. i++;
  85. if (i >= argc)
  86. {
  87. printUsage();
  88. return(1);
  89. }
  90. outputFile = argv[i];
  91. continue;
  92. }
  93. printUsage();
  94. return(1);
  95. }
  96. if ((mazeSeed < 0) || (numMazeMarks < 0) || (numMazes < 0))
  97. {
  98. printUsage();
  99. return(1);
  100. }
  101. // Generate mazes.
  102. TmazeGrammar *tmg = new TmazeGrammar(mazeSeed, numMazeMarks);
  103. for (m = 0; m < numMazes; m++)
  104. {
  105. maze = tmg->generateMaze();
  106. assert(maze != NULL);
  107. Mazes.push_back(maze);
  108. }
  109. // Output mazes.
  110. if (outputFile != NULL)
  111. {
  112. if ((fp = fopen(outputFile, "w")) == NULL)
  113. {
  114. fprintf(stderr, "Cannot open maze output file %s for writing\n", outputFile);
  115. return(1);
  116. }
  117. }
  118. else
  119. {
  120. fp = stdout;
  121. }
  122. intervals = 0;
  123. for (m = 0; m < numMazes; m++)
  124. {
  125. maze = Mazes[m];
  126. if (intervals < (int)maze->path.size())
  127. {
  128. intervals = (int)maze->path.size();
  129. }
  130. fprintf(fp, "name: { ");
  131. for (i = 0; i < (int)maze->path.size(); i++)
  132. {
  133. fprintf(fp, "%d ", maze->path[i].mark);
  134. }
  135. fprintf(fp, "} %d\n", (int)maze->path.size());
  136. for (i = 0; i < (int)maze->path.size(); i++)
  137. {
  138. fprintf(fp, "I:");
  139. for (j = 0; j < numMazeMarks; j++)
  140. {
  141. if (j == maze->path[i].mark)
  142. {
  143. fprintf(fp, " 1");
  144. }
  145. else
  146. {
  147. fprintf(fp, " 0");
  148. }
  149. }
  150. fprintf(fp, " T: ");
  151. if (maze->path[i].direction)
  152. {
  153. fprintf(fp, "0 1");
  154. }
  155. else
  156. {
  157. fprintf(fp, "1 0");
  158. }
  159. if (i < (int)maze->path.size() - 1)
  160. {
  161. fprintf(fp, "\n");
  162. }
  163. else
  164. {
  165. fprintf(fp, ";\n");
  166. }
  167. }
  168. }
  169. if (outputFile != NULL)
  170. {
  171. fclose(fp);
  172. }
  173. return(0);
  174. }