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 / guess-number.js < prev    next >
Encoding:
JavaScript  |  2004-07-12  |  1.6 KB  |  47 lines

  1. /*
  2.  * Copyright 1999-2004 The Apache Software Foundation.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. // simple number guessing game in Flowscript
  18. // based on the Cocoon Flow tutorial:
  19. // http://cocoon.apache.org/2.1/userdocs/flow/tutor.html
  20.  
  21. function public_startGuessNumber() {
  22.   var max = cocoon.parameters["maxValue"];
  23.   var toGuess = Math.round(Math.random() * max);
  24.   if(toGuess == 0) toGuess = 1;
  25.   var hint = "Guess a number between 1 and " + max;
  26.   var tries = 0;
  27.  
  28.   // show and process input form, until correct answer is given
  29.   while (true) {
  30.     cocoon.sendPageAndWait("number-guess/views/guess", {"toGuess" : toGuess, "hint" : hint, "tries" : tries});
  31.     var answer = parseInt( cocoon.request.get("answer") );
  32.  
  33.     tries++;
  34.  
  35.     if(answer) {
  36.      if(answer > toGuess) {
  37.           hint = "The number you entered (" + answer + ") is too big";
  38.       } else if(answer < toGuess) {
  39.           hint = "The number you entered (" + answer + ") is too small";
  40.       } else {
  41.           break;
  42.       }
  43.     }
  44.   }
  45.  
  46.   cocoon.sendPage("number-guess/views/success", {"toGuess" : toGuess, "answer" : answer, "tries" : tries} );
  47. }