Source code: com/jdwsoftware/wfh/model/NumberGame.java
1 package com.jdwsoftware.wfh.model;
2
3 import javax.swing.JComponent;
4 import java.awt.Graphics;
5 import java.awt.Image;
6 import java.awt.Color;
7 import java.awt.Font;
8
9 import java.util.Hashtable;
10 import java.util.Random;
11
12 /**
13 * <p>Title: Woody's Fishing Hole</p>
14 * <p>Description: Educational Fishing Game</p>
15 * <p>Copyright: Copyright (C) 2003 JDW Software, LLC</p>
16 * <p>Company: JDW Software, LLC</p>
17 *
18 * @author <a href="mailto:jdw@jdwsoftware.com">Jim Woodgate</a>
19 * @version $Revision: 1.6 $
20 */
21
22 /*
23 * This file is part of Woodys Fishing Hole.
24 *
25 * Woodys Fishing Hole is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version 2 of
28 * the License, or (at your option) any later version.
29 *
30 * Woodys Fishing Hole is distributed in the hope that it will be
31 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
32 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with Woodys Fishing Hole; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 * USA
39 */
40
41 public class NumberGame extends GameType {
42 private static final String COPYRIGHT = com.jdwsoftware.util.Copyright.COPYRIGHT;
43 private static final String VERSION = "$Id: NumberGame.java,v 1.6 2003/11/24 16:39:55 jdw Exp $";
44
45
46 private static final String[] NUMBERS = new String[] {"0","1","2","3","4","5","6","7","8","9"};
47
48 private Hashtable _images;
49
50 private String _question;
51 private static Random r = new Random();
52
53 public NumberGame(JComponent caller) {
54 super(caller);
55
56 _images = new Hashtable();
57
58 Font bigFont = new java.awt.Font("Monospaced", 1, 36);
59 for (int i=0; i<NUMBERS.length; i++) {
60 String key = NUMBERS[i];
61 Image image = _caller.createImage(getWidth(), getHeight());
62 Graphics g = image.getGraphics();
63 g.setColor(Color.black);
64 g.fillRect(0,0,getWidth(),getHeight());
65 g.setColor(Color.white);
66 g.fillRect(BORDER, BORDER, getWidth()-2*BORDER, getHeight()-2*BORDER);
67 g.setColor(Color.black);
68 g.setFont(bigFont);
69 g.drawString(key, BORDER*2, BORDER+bigFont.getSize());
70 _images.put(key, image);
71 }
72 _question = NUMBERS[r.nextInt(NUMBERS.length)];
73 }
74
75 public void newQuestion() {
76 _question = NUMBERS[r.nextInt(NUMBERS.length)];
77 }
78
79 public Image getQuestion() {
80 return (Image)_images.get(_question);
81 }
82
83 public String getAnswer() {
84 return _question;
85 }
86
87 public String[] getValidAnswers() {
88 return NUMBERS;
89 }
90
91 public Image getAnswer(String s) {
92 return (Image)_images.get(s);
93 }
94
95 }