1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21 package autohit.call;
22
23 import java.util.Random;
24
25 import autohit.universe.Universe;
26
27 /**
28 * RANDOM call. Returns a RANDOM integer between 0 and top.
29 *
30 * <pre>
31 * REQURIES: logger, core IGNORES: uni PARAMETERS (INPUT): top= highest integer.
32 * </pre>
33 *
34 * @author Erich P. Gatejen
35 * @version 1.0 <i>Version History</i><code>EPG - Initial - 8Jan04</code>
36 */
37 public class Call_RANDOM extends Call {
38
39 // GLOBAL RANDOM
40 static Random r;
41
42 /**
43 * Implement this to handle load time initialization. The four main fields
44 * will already be set--vmc, sc, log, and u. You must implement this, but
45 * you don't have to do anything. Remember that calls are cached per VM and
46 * reused as often as possible. There will be no thread-safety issues with
47 * the VMCore or log, but the SystemContecxt and Universe may be shared.
48 *
49 * @throws CallException
50 */
51 public void load_chain() throws CallException {
52 // Nothing to do.
53 }
54
55 /**
56 * Implement this to return the name of the CALL
57 *
58 * @return name of the CALL
59 */
60 public String name() {
61 return "RANDOM";
62 }
63
64 /**
65 * Execute it.
66 *
67 * @return the result or null if there is no result
68 */
69 public String call() throws CallException {
70
71 int value = 0;
72 String top = null;
73
74 try {
75
76 if (r == null)
77 r = new Random();
78
79 try {
80
81 top = this.requiredString("top");
82 value = r.nextInt(Integer.parseInt(top + 1));
83
84 } catch (Exception e) {
85 throw new CallException(
86 this.format("Bad call. Parameter 'top' is bad. value=" + top),
87 CallException.CODE_CALL_PROGRAM_ERROR);
88 }
89
90 } catch (CallException ce) {
91 throw ce;
92 } catch (Exception e) {
93 throw new CallException(
94 this.format("General exception. error=" + e.getMessage()),
95 CallException.CODE_CALL_FAULT,
96 e);
97 }
98
99 this.debug("Random number selected = " + value);
100
101 return Integer.toString(value);
102 }
103
104 /**
105 * Execute using the passed universe, rather than the loaded.
106 *
107 * @param uni
108 * a universe
109 * @return the result or null if there is no result
110 * @see autohit.universe.Universe
111 */
112 public String call(Universe uni) throws CallException {
113 return this.call();
114 }
115 }
|