home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / HanoiApple.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-07-12  |  3.5 KB  |  87 lines

  1. package org.apache.cocoon.components.flow.apples.samples;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Stack;
  6. import org.apache.avalon.framework.logger.AbstractLogEnabled;
  7. import org.apache.cocoon.ProcessingException;
  8. import org.apache.cocoon.components.flow.apples.AppleController;
  9. import org.apache.cocoon.components.flow.apples.AppleRequest;
  10. import org.apache.cocoon.components.flow.apples.AppleResponse;
  11.  
  12. public class HanoiApple extends AbstractLogEnabled implements AppleController {
  13.    public static final int NONE = -1;
  14.    public static final int SRC = 0;
  15.    public static final int AUX = 1;
  16.    public static final int DST = 2;
  17.    public Stack[] stacks;
  18.    public Object floatingDisk = null;
  19.    public int moves = 0;
  20.    public int puzzleSize = 0;
  21.  
  22.    public String toString() {
  23.       return "HanoiApple[ stacks=" + this.stacks + " | floatingDisk=" + this.floatingDisk + " | moves = " + this.moves + "]";
  24.    }
  25.  
  26.    public void process(AppleRequest req, AppleResponse res) throws ProcessingException {
  27.       if (this.stacks == null) {
  28.          String requestSize = req.getCocoonRequest().getParameter("size");
  29.          if (requestSize != null) {
  30.             try {
  31.                int size = Integer.parseInt(requestSize);
  32.                this.intializeStacks(size);
  33.             } catch (NumberFormatException var5) {
  34.             }
  35.          }
  36.       } else {
  37.          String requestStack = req.getCocoonRequest().getParameter("stack");
  38.          if (requestStack != null) {
  39.             try {
  40.                int stackNdx = Integer.parseInt(requestStack);
  41.                if (this.floatingDisk != null) {
  42.                   if (this.stacks[stackNdx].size() == 0 || (Integer)this.floatingDisk < (Integer)this.stacks[stackNdx].peek()) {
  43.                      this.stacks[stackNdx].push(this.floatingDisk);
  44.                      this.floatingDisk = null;
  45.                      ++this.moves;
  46.                   }
  47.                } else if (this.stacks[stackNdx].size() != 0) {
  48.                   this.floatingDisk = this.stacks[stackNdx].pop();
  49.                }
  50.             } catch (RuntimeException var6) {
  51.             }
  52.          }
  53.       }
  54.  
  55.       this.getLogger().debug(this.toString());
  56.       if (this.stacks == null) {
  57.          res.sendPage("hanoi/intro.jx", (Object)null);
  58.       } else {
  59.          Map bizdata = new HashMap();
  60.          bizdata.put("stacks", this.stacks);
  61.          bizdata.put("moves", "" + this.moves);
  62.          bizdata.put("floatingDisk", this.floatingDisk);
  63.          bizdata.put("nextMove", this.floatingDisk == null ? "Lift it!" : "Drop it!");
  64.          bizdata.put("puzzleSize", "" + this.puzzleSize);
  65.          res.sendPage("hanoi/hanoi.jx", bizdata);
  66.       }
  67.  
  68.    }
  69.  
  70.    private void intializeStacks(int size) {
  71.       if (size > 2) {
  72.          this.stacks = new Stack[3];
  73.  
  74.          for(int i = 0; i < 3; ++i) {
  75.             this.stacks[i] = new Stack();
  76.          }
  77.  
  78.          for(int i = size; i > 0; --i) {
  79.             this.stacks[0].push(new Integer(i));
  80.          }
  81.  
  82.          this.puzzleSize = size;
  83.       }
  84.  
  85.    }
  86. }
  87.