pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Groovy pastebin - collaborative debugging tool View Help


Posted by Chanwit Kaewkasi on Wed 17 Sep 06:32 (modification of post by Chanwit Kaewkasi view diff)
report abuse | View followups from Chanwit Kaewkasi | download | new post

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
  3. <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
  4.  
  5. <?page zscriptLanguage="GroovyGrails"?>
  6.  
  7. <zk>
  8.   <zscript>
  9.     class Hangman {
  10.      
  11.       def wrong
  12.       boolean gameOver = false
  13.       char[] buffer
  14.       def correct = 0
  15.       def win = false
  16.      
  17.       def getRevealedWord() {
  18.         def r=''
  19.         this.buffer.each {
  20.           r += "${it} "
  21.         }
  22.         return r
  23.       }                                        
  24.      
  25.       def word
  26.       void setWord(value) {
  27.         this.word = value
  28.         this.buffer = new char[value.length()]
  29.         this.word.eachWithIndex {obj, i ->
  30.           this.buffer[i] = '_'
  31.         }
  32.         this.wrong = 0
  33.         this.gameOver = false
  34.         this.correct = 0
  35.       }
  36.            
  37.       def guess = { ch ->
  38.         def right = false
  39.         word.eachWithIndex { it, i ->
  40.           if(it == ch) {
  41.             buffer[i] = it
  42.             correct++
  43.             right = true
  44.           }                                    
  45.         }
  46.         if(!right) {
  47.           wrong++
  48.           if(wrong == 12) gameOver = true
  49.         } else {
  50.           if(correct == word.length()) win = true
  51.         }                              
  52.       }                                                        
  53.     }
  54.    
  55.     words = ['polyglot','alliance','web']
  56.     a_to_m = ('a'..'m') as String[]
  57.     n_to_z = ('n'..'z') as String[]
  58.        
  59.     hangman = new Hangman(word:words[new Random().nextInt(3)])
  60.  
  61.     nextstep = {
  62.       if(hangman.win || hangman.gameOver) return
  63.       hangman.guess self.label
  64.       answer.value = hangman.revealedWord                      
  65.       if(hangman.win) {
  66.         count.style = 'color: green'
  67.         count.value = 'YOU WIN !!!'
  68.       } else if(hangman.gameOver) {
  69.         count.style = 'color: red'
  70.         count.value = 'GAME OVER'
  71.       } else {
  72.         imgStep.src = "images/hangman/step${hangman.wrong+1}.gif"
  73.         count.value = hangman.wrong
  74.       }
  75.     }          
  76.   </zscript>
  77.   <vbox>
  78.     <hbox>
  79.       Number of Wrong Guess:
  80.       <label id="count" value="${hangman.wrong}"/>             
  81.     </hbox>
  82.     <image id="imgStep" src="images/hangman/step1.gif"/>
  83.     <hbox>
  84.       The Word:
  85.       <label id="answer" value="${hangman.revealedWord}"/>
  86.     </hbox>
  87.     Chracters:
  88.     <hbox>
  89.       <button label="${each}" width="28px" forEach="${a_to_m}" onClick="nextstep()"/>
  90.     </hbox>
  91.     <hbox>
  92.       <button label="${each}" width="28px" forEach="${n_to_z}" onClick="nextstep()"/>
  93.     </hbox>
  94.     <box height="20px"/>
  95.     <label value="New Game?"
  96.             style="cursor: pointer; text-decoration: underline; color: maroon;"
  97.             onClick="Executions.sendRedirect 'index.zul'"/>
  98.   </vbox>
  99. </zk>

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me