home *** CD-ROM | disk | FTP | other *** search
- package org.apache.cocoon.components.flow.apples.samples;
-
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Stack;
- import org.apache.avalon.framework.logger.AbstractLogEnabled;
- import org.apache.cocoon.ProcessingException;
- import org.apache.cocoon.components.flow.apples.AppleController;
- import org.apache.cocoon.components.flow.apples.AppleRequest;
- import org.apache.cocoon.components.flow.apples.AppleResponse;
-
- public class HanoiApple extends AbstractLogEnabled implements AppleController {
- public static final int NONE = -1;
- public static final int SRC = 0;
- public static final int AUX = 1;
- public static final int DST = 2;
- public Stack[] stacks;
- public Object floatingDisk = null;
- public int moves = 0;
- public int puzzleSize = 0;
-
- public String toString() {
- return "HanoiApple[ stacks=" + this.stacks + " | floatingDisk=" + this.floatingDisk + " | moves = " + this.moves + "]";
- }
-
- public void process(AppleRequest req, AppleResponse res) throws ProcessingException {
- if (this.stacks == null) {
- String requestSize = req.getCocoonRequest().getParameter("size");
- if (requestSize != null) {
- try {
- int size = Integer.parseInt(requestSize);
- this.intializeStacks(size);
- } catch (NumberFormatException var5) {
- }
- }
- } else {
- String requestStack = req.getCocoonRequest().getParameter("stack");
- if (requestStack != null) {
- try {
- int stackNdx = Integer.parseInt(requestStack);
- if (this.floatingDisk != null) {
- if (this.stacks[stackNdx].size() == 0 || (Integer)this.floatingDisk < (Integer)this.stacks[stackNdx].peek()) {
- this.stacks[stackNdx].push(this.floatingDisk);
- this.floatingDisk = null;
- ++this.moves;
- }
- } else if (this.stacks[stackNdx].size() != 0) {
- this.floatingDisk = this.stacks[stackNdx].pop();
- }
- } catch (RuntimeException var6) {
- }
- }
- }
-
- this.getLogger().debug(this.toString());
- if (this.stacks == null) {
- res.sendPage("hanoi/intro.jx", (Object)null);
- } else {
- Map bizdata = new HashMap();
- bizdata.put("stacks", this.stacks);
- bizdata.put("moves", "" + this.moves);
- bizdata.put("floatingDisk", this.floatingDisk);
- bizdata.put("nextMove", this.floatingDisk == null ? "Lift it!" : "Drop it!");
- bizdata.put("puzzleSize", "" + this.puzzleSize);
- res.sendPage("hanoi/hanoi.jx", bizdata);
- }
-
- }
-
- private void intializeStacks(int size) {
- if (size > 2) {
- this.stacks = new Stack[3];
-
- for(int i = 0; i < 3; ++i) {
- this.stacks[i] = new Stack();
- }
-
- for(int i = size; i > 0; --i) {
- this.stacks[0].push(new Integer(i));
- }
-
- this.puzzleSize = size;
- }
-
- }
- }
-