Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8"?>
- <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
- <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
- <?page zscriptLanguage="GroovyGrails"?>
- <zk>
- <zscript>
- class Hangman {
- def wrong
- boolean gameOver = false
- char[] buffer
- def correct = 0
- def win = false
- def getRevealedWord() {
- def r=''
- this.buffer.each {
- r += "${it} "
- }
- return r
- }
- def word
- void setWord(value) {
- this.word = value
- this.buffer = new char[value.length()]
- this.word.eachWithIndex {obj, i ->
- this.buffer[i] = '_'
- }
- this.wrong = 0
- this.gameOver = false
- this.correct = 0
- }
- def guess = { ch ->
- def right = false
- word.eachWithIndex { it, i ->
- if(it == ch) {
- buffer[i] = it
- correct++
- right = true
- }
- }
- if(!right) {
- wrong++
- if(wrong == 12) gameOver = true
- } else {
- if(correct == word.length()) win = true
- }
- }
- }
- words = ['polyglot','alliance','web']
- a_to_m = ('a'..'m') as String[]
- n_to_z = ('n'..'z') as String[]
- hangman = new Hangman(word:words[new Random().nextInt(3)])
- nextstep = {
- if(hangman.win || hangman.gameOver) return
- hangman.guess self.label
- answer.value = hangman.revealedWord
- if(hangman.win) {
- count.style = 'color: green'
- count.value = 'YOU WIN !!!'
- } else if(hangman.gameOver) {
- count.style = 'color: red'
- count.value = 'GAME OVER'
- } else {
- imgStep.src = "images/hangman/step${hangman.wrong+1}.gif"
- count.value = hangman.wrong
- }
- }
- </zscript>
- <vbox>
- <hbox>
- Number of Wrong Guess:
- <label id="count" value="${hangman.wrong}"/>
- </hbox>
- <image id="imgStep" src="images/hangman/step1.gif"/>
- <hbox>
- The Word:
- <label id="answer" value="${hangman.revealedWord}"/>
- </hbox>
- Chracters:
- <hbox>
- <button label="${each}" width="28px" forEach="${a_to_m}" onClick="nextstep()"/>
- </hbox>
- <hbox>
- <button label="${each}" width="28px" forEach="${n_to_z}" onClick="nextstep()"/>
- </hbox>
- <box height="20px"/>
- <label value="New Game?"
- style="cursor: pointer; text-decoration: underline; color: maroon;"
- onClick="Executions.sendRedirect 'index.zul'"/>
- </vbox>
- </zk>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement