예제 #1
0
 private static void burnForest(int iterations) {
   for (int i = 0; i < iterations; i++) {
     if (iterations > 1) {
       System.out.println("Iteration " + (i + 1) + " of " + iterations + ":");
     }
     forest.plant();
     do {
       forest.nextFrame();
       if (verbose) {
         System.out.println(forest);
       }
     } while (forest.isBurning());
     if (!verbose) {
       System.out.println(forest);
     }
   }
 }
예제 #2
0
 /**
  * A long series of {@code if} and {@code switch} statements which try to acertain what on earth
  * the user wants to do.
  *
  * @param args Series of tokens constituting a single command
  */
 private static void executeCommand(String[] args) {
   if (args.length == 0) { // There is no command to execute
     return;
   }
   switch (args[0]) {
     case "quit":
     case "q":
     case "exit":
       System.exit(0);
       break;
     case "set":
       // Is there an argument of "set"?
       if (args.length < 2) {
         printHelp("set");
       } else if (args.length < 3) {
         // Does the argument of set have an argument?
         String[] validArgs = {"size", "spread", "verbose", "start"};
         for (String validArg : validArgs) {
           if (validArg.equals(args[1])) {
             printHelp("set/" + validArg);
             return;
           }
         }
         // Never mind, the specified setting doesn't even exist
         printHelp("set");
       } else
         switch (args[1]) { // Woo! Something we can work with
           case "size":
             // Needs two arguments
             if (args.length < 4) {
               printHelp("set/size");
               break;
             }
             // Try to turn those args into a new size
             int[] newSize = new int[2];
             for (int i = 0; i < 2; i++) {
               try {
                 newSize[i] = Integer.parseInt(args[i + 2]);
               } catch (NumberFormatException e) { // User had one job
                 printHelp("set/size");
                 break;
               }
             }
             forest.resize(newSize[0], newSize[1]);
             break;
           case "spread":
             try {
               forest.setCriticalNumber(Double.parseDouble(args[2]));
             } catch (NumberFormatException e) {
               printHelp("set/spread");
             } catch (IllegalArgumentException e) {
               System.out.println("Specified spread probability is out " + "of bounds.");
             }
             break;
           case "verbose":
             // These are all the possible inputs interpreted as "yes" or
             // "no"
             String[] yes = {"true", "t", "yes", "y", "1"};
             String[] no = {"false", "f", "no", "n", "0"};
             if (memberOf(args[2], yes)) {
               verbose = true;
             } else if (memberOf(args[2], no)) {
               verbose = false;
             } else {
               printHelp("set/verbose");
             }
             break;
           case "start":
             String[] middles = {
               "c", "centred", "centered", "centre", "center", "midle", "mid", "m"
             };
             if (memberOf(args[2], middles)) {
               forest.centerStartingPoint();
             } else {
               if (args.length < 4) {
                 printHelp("set/start");
               } else {
                 int[] newCenter = new int[2];
                 for (int i = 0; i < 2; i++) {
                   try {
                     newCenter[i] = Integer.parseInt(args[i + 2]);
                   } catch (NumberFormatException e) {
                     printHelp("set/start");
                     break;
                   }
                 }
                 try {
                   forest.setStartingPoint(newCenter[0], newCenter[1]);
                 } catch (IllegalArgumentException e) {
                   System.out.println("Specified center is out of " + "bounds.");
                   break;
                 }
               }
             }
             break;
           default:
             printHelp("set");
         }
       break; // End of "set"
     case "stat":
       if (args.length < 2) {
         printHelp("stat");
       }
       switch (args[1]) {
         case "grab":
           stat.takeSnapshot();
           break;
         case "list":
           if (args.length < 3 || !args[2].equalsIgnoreCase("-v")) {
             System.out.println("Frames saved: " + stat.size());
           } else {
             char[][][] snaps = stat.getList();
             for (int i = 0; i < snaps.length; i++) {
               System.out.println("Snapshot " + i + ":\n" + Forest.renderTrees(snaps[i]));
             }
           }
           break;
         case "drop":
           if (args.length < 3) {
             printHelp("stat/drop");
           } else {
             try {
               stat.dropFrame(parseIndex(args[2], stat.size(), "stat/drop"));
             } catch (NumberFormatException e) {
             }
           }
           break;
         case "clear":
           stat.clear();
           break;
         case "print":
           if (args.length < 3) {
             printHelp("stat/print");
           } else {
             try {
               Forest.renderTrees(stat.getFrame(parseIndex(args[2], stat.size(), "stat/print")));
             } catch (NumberFormatException e) {
             }
           }
           break;
         case "calc":
           if (args.length < 3) {
             System.out.println(stat.summary());
           } else {
             try {
               System.out.println(stat.summary(parseIndex(args[2], stat.size(), "stat/calc")));
             } catch (NumberFormatException e) {
             }
           }
           break;
         default:
           printHelp("stat");
       }
       break; // End of "stat"
     case "burn":
       if (args.length < 2) {
         burnForest(1);
       } else {
         int iterations;
         try {
           iterations = Integer.parseInt(args[1]);
         } catch (NumberFormatException e) {
           printHelp("burn");
           break;
         }
         burnForest(iterations);
       }
       break;
     case "help":
       if (args.length < 2) {
         printHelp("general");
         break;
       } else {
         String[] opts = {"set", "burn", "exit", "quit", "q", "help", "about"};
         if (memberOf(args[1], opts)) {
           printHelp(args[1]);
         } else {
           System.out.println(args[1] + " is not a valid command");
           printHelp("general");
         }
       }
       break;
     case "about":
       printHelp("about");
       break;
     default:
       System.out.println(args[0] + " is not a valid command.");
       printHelp("general");
       break;
   }
 }