mpo         2003/08/29 15:31:33

  Added:       src/blocks/apples/samples/hanoi hanoi.jx intro.jx hanoi.xsl
               
src/blocks/apples/java/org/apache/cocoon/components/flow/apples/samples
                        HanoiApple.java
  Log:
  Adding Towers of Hanoi sample in the Apples block.
  
  Revision  Changes    Path
  1.1                  cocoon-2.1/src/blocks/apples/samples/hanoi/hanoi.jx
  
  Index: hanoi.jx
  ===================================================================
  <?xml version="1.0"?>
  <page xmlns:jx="http://apache.org/cocoon/templates/jx/1.0";>
  
    <title>Tower of Hanoi Puzzle</title>
  
    <content>
  
      <para>For some background on the puzzle, see: 
        <a 
href="http://www.cut-the-knot.org/recurrence/hanoi.shtml";>http://www.cut-the-knot.org/recurrence/hanoi.shtml</a>
      </para>
      
      <para>You've currently used ${moves} moves.</para>
  
  
      <para>The current state of the stacks is:</para>
      
      <hn:hanoi height="${puzzleSize}" 
xmlns:hn="http://apache.org/cocoon/apples/samples/hanoi";>
        <hn:float size="${floatingDisk}"/>
        <hn:stacks>
          <jx:forEach var="stack" items="${stacks}">
            <hn:stack>
                <jx:forEach var="disc" items="${stack}">
                  <hn:disc size="${disc}" />
                </jx:forEach>
            </hn:stack>
                </jx:forEach>           
        </hn:stacks>   
      </hn:hanoi>    
          
    </content>
  
  </page>
    
  
  
  1.1                  cocoon-2.1/src/blocks/apples/samples/hanoi/intro.jx
  
  Index: intro.jx
  ===================================================================
  <?xml version="1.0"?>
  <page xmlns:jx="http://apache.org/cocoon/templates/jx/1.0";>
  
    <title>Tower of Hanoi Puzzle</title>
  
    <content>
  
      <para>For some background on the puzzle, see: 
        <a 
href="http://www.cut-the-knot.org/recurrence/hanoi.shtml";>http://www.cut-the-knot.org/recurrence/hanoi.shtml</a>
      </para>
  
      <para>To start the puzzle interaction you need to initilaize the number 
of discs: </para>
  
      <form method="post" action="${continuation.id}.continue">
  
        <select name="size">
          <option>3</option>
          <option selected="selected">4</option>
          <option>5</option>
          <option>6</option>
          <option>7</option>
          <option>8</option>
        </select>
        
        <input type="submit" value="Start!"/>
        
      </form>          
      
    </content>
  
  </page>
  
  
  1.1                  cocoon-2.1/src/blocks/apples/samples/hanoi/hanoi.xsl
  
  Index: hanoi.xsl
  ===================================================================
  <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:hn="http://apache.org/cocoon/apples/samples/hanoi";>
  
  
  
  
    <xsl:template match="hn:hanoi">
  
      <xsl:variable name="stacks" select="hn:stacks"/> 
      <xsl:variable name="height" select="@height"/>
      <xsl:variable name="cols-per-stack" select="($height)*2 + 1"/>
      <xsl:variable name="cols-total" select="($cols-per-stack)*3"/>
      <xsl:variable name="on-the-move" select="boolean(hn:float/@size != '')"/>
      
      <center>
        <table border="0">
          <!-- fixing col-widths row -->
          <tr>
                  <xsl:call-template name="fix-cells" >
                    <xsl:with-param name="remaining" select="$cols-total" />
                  </xsl:call-template>
          </tr>
          <!-- /fixing col-widths row -->
          
          <!-- title-row -->
          <tr>
            <td colspan="{$cols-total}">
              <center>-- Towers of Hanoi Puzzle --</center>            
              <hr width="70%" />
            </td>
          </tr>
          <!-- /title-row -->
          
          <!-- moving-disc-row -->
          <tr>
            <td colspan="{$cols-total}"><center>Disc on the move:</center></td>
          </tr>
          <tr bgcolor="#ffffff" height="5px">
            <td colspan="{$cols-per-stack}"/>          
            <xsl:call-template name="disc">
              <xsl:with-param name="disc" select="hn:float" />
              <xsl:with-param name="max_size" select="$height"/>
              <xsl:with-param name="disc_color">#996633</xsl:with-param>
            </xsl:call-template>
            <td colspan="{$cols-per-stack}"/>
          </tr>
  
          <tr>
            <td colspan="{$cols-total}"><hr width="70%"/></td>
          </tr>        
                <!-- /moving-disc-row -->
                
                <!-- stack-header-row -->
            <tr>
              <xsl:choose>
                <xsl:when test="$on-the-move" >
                      <td colspan="{$cols-per-stack}"><center><a 
href="?stack=0">Drop It!</a></center></td>
                      <td colspan="{$cols-per-stack}"><center><a 
href="?stack=1">Drop It!</a></center></td>
                      <td colspan="{$cols-per-stack}"><center><a 
href="?stack=2">Drop It!</a></center></td>
                </xsl:when>
                <xsl:otherwise>
                      <td colspan="{$cols-per-stack}"><center><a 
href="?stack=0">Lift It!</a></center></td>
                      <td colspan="{$cols-per-stack}"><center><a 
href="?stack=1">Lift It!</a></center></td>
                      <td colspan="{$cols-per-stack}"><center><a 
href="?stack=2">Lift It!</a></center></td>
                </xsl:otherwise>
              </xsl:choose>
            </tr>
                <!-- /stack-header-row -->
        
                
          <!-- stack-rows -->
          <xsl:call-template name="remaining-stack-rows">
            <xsl:with-param name="remaining" select="$height"/>
            <xsl:with-param name="max_size" select="$height"/>
            <xsl:with-param name="cols-per-stack" select="$cols-per-stack"/>
            <xsl:with-param name="stacks" select="$stacks"/>
          </xsl:call-template>        
          <!-- /stack-rows -->
                          
        </table>
      </center>
    </xsl:template>
  
  
    <xsl:template name="remaining-stack-rows">
      <xsl:param name="remaining"/>
      <xsl:param name="max_size"/>
      <xsl:param name="cols-per-stack"/>
      <xsl:param name="stacks"/>
  
      <xsl:call-template name="stack-row">
        <xsl:with-param name="row-number" select="$remaining"/>
        <xsl:with-param name="max_size" select="$max_size"/>
        <xsl:with-param name="cols-per-stack" select="$cols-per-stack"/>
        <xsl:with-param name="stacks" select="$stacks"/>
      </xsl:call-template>
  
      <xsl:if test="$remaining &gt; 1">
        <xsl:call-template name="remaining-stack-rows">
          <xsl:with-param name="remaining" select="$remaining - 1"/>
          <xsl:with-param name="max_size" select="$max_size"/>
          <xsl:with-param name="cols-per-stack" select="$cols-per-stack"/>
          <xsl:with-param name="stacks" select="$stacks"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:template>
  
  
    <xsl:template name="stack-row">
      <xsl:param name="row-number"/>
      <xsl:param name="max_size"/>
      <xsl:param name="cols-per-stack"/>
      <xsl:param name="stacks"/>
  
        <tr height="5px" bgcolor="#ffffff">     
        <xsl:for-each select="$stacks/hn:stack">
          <xsl:variable name="stack-ndx" select="position()"/>
  
          <xsl:call-template name="disc">
            <xsl:with-param name="disc" select="hn:disc[number($row-number)]" />
            <xsl:with-param name="max_size" select="$max_size"/>
            <xsl:with-param name="disc_color">#2C6D91</xsl:with-param>
          </xsl:call-template>
        </xsl:for-each>
      </tr>
    </xsl:template>
  
  
  
    <xsl:template name="disc">
      <xsl:param name="disc" />
      <xsl:param name="max_size" />
      <xsl:param name="disc_color" />
      
      <xsl:variable name="size" select="$disc/@size" />
  
        <xsl:choose>
        <xsl:when test="$size &gt; 0" >
          <xsl:choose>
            <xsl:when test="($max_size - $size) &gt; 0" >
              <td colspan="{$max_size - $size}" bgcolor="#ffffff"/>
              <td colspan="{$size}"             bgcolor="{$disc_color}"/>
              <td                               bgcolor="#000000"/>
              <td colspan="{$size}"             bgcolor="{$disc_color}"/>
              <td colspan="{$max_size - $size}" bgcolor="#ffffff"/>
            </xsl:when>
            <xsl:otherwise>
              <td colspan="{$size}"             bgcolor="{$disc_color}"/>
              <td                               bgcolor="#000000"/>
              <td colspan="{$size}"             bgcolor="{$disc_color}"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
          <xsl:otherwise>
            <td colspan="{$max_size}" bgcolor="#ffffff"/>
            <td                       bgcolor="#000000"/>
            <td colspan="{$max_size}" bgcolor="#ffffff"/>
          </xsl:otherwise>
        </xsl:choose>    
  
    </xsl:template>
  
  
    <xsl:template name="fix-cells">
      <xsl:param name="remaining"/>
  
        <td width="5px" />
  
      <xsl:if test="$remaining &gt; 1">
        <xsl:call-template name="fix-cells">
          <xsl:with-param name="remaining" select="$remaining - 1"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:template>
  
  
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
  
  
  </xsl:stylesheet>
  
  
  
  1.1                  
cocoon-2.1/src/blocks/apples/java/org/apache/cocoon/components/flow/apples/samples/HanoiApple.java
  
  Index: HanoiApple.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
  */
  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;
  
  /**
   * HanoiApple shows an apple maintaining the state of the fanous puzzle.
   */
  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;
      
      // full state of the puzzle is in the following variables
      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 {
  
          // processing
          if (stacks == null) {
              String requestSize = req.getCocoonRequest().getParameter("size");
              if (requestSize != null) {
                  try {
                      int size = Integer.parseInt(requestSize);
                      intializeStacks(size);
                  } catch (NumberFormatException ignore) {
                  }
              }
          } else {
              // decide selected column
              String requestStack = 
req.getCocoonRequest().getParameter("stack");
              if (requestStack != null) {
                  try {
                      int stackNdx = Integer.parseInt(requestStack);
                      if (this.floatingDisk != null) {
                          // we are in the middle of a move --> complete move 
if it is allowed
                          if (   this.stacks[stackNdx].size() == 0 
                              || ((Integer)this.floatingDisk).intValue() < 
((Integer)this.stacks[stackNdx].peek()).intValue()) {
                                  
                              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 ignore) {
                     //NUMBERFORMAT
                     //ARRAYINDEXOUTOFBOUNDS                    
                  }
              }
          }
  
          getLogger().debug(toString());
  
          //view generation
          if (stacks == null) {
              res.sendPage("hanoi/intro.jx", 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;
          }
      }
  
  }
  
  
  

Reply via email to