001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.flow.apples.samples;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Stack;
022:
023: import org.apache.avalon.framework.logger.AbstractLogEnabled;
024: import org.apache.cocoon.ProcessingException;
025: import org.apache.cocoon.components.flow.apples.AppleController;
026: import org.apache.cocoon.components.flow.apples.AppleRequest;
027: import org.apache.cocoon.components.flow.apples.AppleResponse;
028:
029: /**
030: * HanoiApple shows an apple maintaining the state of the fanous puzzle.
031: */
032: public class HanoiApple extends AbstractLogEnabled implements
033: AppleController {
034:
035: public static final int NONE = -1;
036: public static final int SRC = 0;
037: public static final int AUX = 1;
038: public static final int DST = 2;
039:
040: // full state of the puzzle is in the following variables
041: public Stack[] stacks;
042: public Object floatingDisk = null;
043: public int moves = 0;
044: public int puzzleSize = 0;
045:
046: public String toString() {
047: return "HanoiApple[ stacks=" + this .stacks + " | floatingDisk="
048: + this .floatingDisk + " | moves = " + this .moves + "]";
049: }
050:
051: public void process(AppleRequest req, AppleResponse res)
052: throws ProcessingException {
053:
054: // processing
055: if (stacks == null) {
056: String requestSize = req.getCocoonRequest().getParameter(
057: "size");
058: if (requestSize != null) {
059: try {
060: int size = Integer.parseInt(requestSize);
061: intializeStacks(size);
062: } catch (NumberFormatException ignore) {
063: }
064: }
065: } else {
066: // decide selected column
067: String requestStack = req.getCocoonRequest().getParameter(
068: "stack");
069: if (requestStack != null) {
070: try {
071: int stackNdx = Integer.parseInt(requestStack);
072: if (this .floatingDisk != null) {
073: // we are in the middle of a move --> complete move if it is allowed
074: if (this .stacks[stackNdx].size() == 0
075: || ((Integer) this .floatingDisk)
076: .intValue() < ((Integer) this .stacks[stackNdx]
077: .peek()).intValue()) {
078:
079: this .stacks[stackNdx]
080: .push(this .floatingDisk);
081: this .floatingDisk = null;
082: this .moves++;
083: }
084: } else {
085: if (this .stacks[stackNdx].size() != 0) {
086: this .floatingDisk = this .stacks[stackNdx]
087: .pop();
088: }
089: }
090: } catch (RuntimeException ignore) {
091: //NUMBERFORMAT
092: //ARRAYINDEXOUTOFBOUNDS
093: }
094: }
095: }
096:
097: getLogger().debug(toString());
098:
099: //view generation
100: if (stacks == null) {
101: res.sendPage("hanoi/intro.jx", null);
102: } else {
103: Map bizdata = new HashMap();
104: bizdata.put("stacks", this .stacks);
105: bizdata.put("moves", "" + this .moves);
106: bizdata.put("floatingDisk", this .floatingDisk);
107: bizdata
108: .put("nextMove",
109: this .floatingDisk == null ? "Lift it!"
110: : "Drop it!");
111: bizdata.put("puzzleSize", "" + this .puzzleSize);
112:
113: res.sendPage("hanoi/hanoi.jx", bizdata);
114: }
115:
116: }
117:
118: private void intializeStacks(int size) {
119: if (size > 2) {
120: this .stacks = new Stack[3];
121: for (int i = 0; i < 3; i++) {
122: this .stacks[i] = new Stack();
123: }
124: for (int i = size; i > 0; i--) {
125: this .stacks[0].push(new Integer(i));
126: }
127: this.puzzleSize = size;
128: }
129: }
130:
131: }
|