01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: NumberGuess.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.engine.testelements.continuations;
09:
10: import com.uwyn.rife.engine.Element;
11: import com.uwyn.rife.template.Template;
12: import java.util.Calendar;
13: import java.util.Random;
14:
15: public class NumberGuess extends Element {
16: private static Random sRandomNumbers = new Random();
17:
18: public void processElement() {
19: Template template = getHtmlTemplate("engine_continuation_numberguess_guess");
20:
21: Calendar start = Calendar.getInstance();
22: int answer = 0;
23: int guesses = 0;
24:
25: synchronized (this ) {
26: answer = sRandomNumbers.nextInt(101);
27: }
28:
29: int guess = -1;
30: while (guess != answer) {
31: print(template);
32:
33: pause();
34:
35: guess = getParameterInt("guess", -1);
36:
37: if (guess < 0 || guess > 100) {
38: template.setBlock("warning", "invalid");
39: continue;
40: }
41:
42: guesses++;
43:
44: if (answer < guess) {
45: template.setBlock("indication", "lower");
46: } else if (answer > guess) {
47: template.setBlock("indication", "higher");
48: }
49: }
50:
51: Calendar now = Calendar.getInstance();
52: long duration_milliseconds = now.getTime().getTime()
53: - start.getTime().getTime();
54: long duration_seconds = duration_milliseconds / 1000;
55:
56: // output the details of the played game
57: template = getHtmlTemplate("engine_continuation_numberguess_success");
58:
59: template.setValue("answer", answer);
60: template.setValue("guesses", guesses);
61: template.setValue("duration", duration_seconds);
62:
63: print(template);
64: }
65: }
|