KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > Magic8Ball

1 package example;
3 import com.caucho.util.L10N;
4 import java.util.logging.Logger JavaDoc;
5 import java.util.logging.Level JavaDoc;
7 import java.util.Random JavaDoc;
8 import java.util.HashMap JavaDoc;
10 /**
11  * Professor Trelawney's magic 8 ball.
12  * The magic 8 ball will return a prophecy with getProphecy().
13  *
14  * <p>The default prophet is Professor Trelawney (of course). If you wish to
15  * change the prophet that provides the answer, use
16  * setProphet(name), where name is one of "trelawney", "classic", or "kingwu".
17  */

18 public class Magic8Ball {
19   static protected final Logger JavaDoc log =
20     Logger.getLogger(Magic8Ball.class.getName());
21   static final L10N L = new L10N(Magic8Ball.class);
23   private final static String JavaDoc DEFAULT_PROPHET = "trelawney";
25   private Random JavaDoc _random = new Random JavaDoc();
27   private String JavaDoc _prophet = DEFAULT_PROPHET;
29   private HashMap JavaDoc _prophets = new HashMap JavaDoc();
31   public Magic8Ball()
32   {
33     _prophets.put("trelawney",new String JavaDoc[] {
34       "The future for you is very dire",
35       "Within a fortnight you will surely die",
36       "Grave trouble awaits you",
37       "You must not leave the room",
38       "The stars are aligned for grave injury",
39     });
40     _prophets.put("classic",new String JavaDoc[] {
42       "Signs point to yes",
43       "Yes",
44       "Reply hazy, try again",
45       "Without a doubt",
46       "My sources say no",
47       "As I see it, yes",
48       "You may rely on it",
49       "Concentrate and ask again",
50       "Outlook not so good",
51       "It is decidedly so",
52       "Better not tell you now",
53       "Very doubtful",
54       "Yes - definitely",
55       "It is certain",
56       "Cannot predict now",
57       "Most likely",
58       "Ask again later",
59       "My reply is no",
60       "Outlook good",
61       "Don't count on it",
62     });
63     _prophets.put("kingwu",new String JavaDoc[] {
64 "THE CREATIVE works sublime success, Furthering through perseverance.",
65 "THE RECEPTIVE brings about sublime success, Furthering through the perseverance of a mare. If the superior man undertakes something and tries to lead, He goes astray; But if he follows, he finds guidance. It is favorable to find friends in the west and south, To forego friends in the east and north. Quiet perseverance brings good fortune.",
66 "DIFFICULTY AT THE BEGINNING works supreme success, Furthering through perseverance. Nothing should be undertaken. It furthers one to appoint helpers.",
67 "YOUTHFUL FOLLY has success. It is not I who seek the young fool; The young fool seeks me. At the first oracle I inform him. If he asks two or three times, it is importunity. If he importunes, I give him no information. Perseverance furthers.",
68 "WAITING. If you are sincere, You have light and success. Perseverance brings good fortune. It furthers one to cross the great water.",
69 "CONFLICT. You are sincere And are being obstructed. A cautious halt halfway brings good fortune. Going through to the end brings misfortune. It furthers one to see the great man. It does not further one to cross the great water.",
70 "THE ARMY. The army needs perseverance And a strong man. Good fortune without blame.",
71 "HOLDING TOGETHER brings good fortune. Inquire of the oracle once again Whether you possess sublimity, constancy, and perseverance; Then there is no blame. Those who are uncertain gradually join. Whoever come too late Meets with misfortune.",
72 "THE TAMING POWER OF THE SMALL Has success. Dense clouds, no rain from our western region. ",
73 "TREADING. Treading upon the tail of the tiger. It does not bite the man. Success.",
74 "PEACE. The small departs, The great approaches. Good fortune. Success.",
75 "STANDSTILL. Evil people do not further The perseverance of the superior man. The great departs; the small approaches.",
76 "FELLOWSHIP WITH MEN in the open. Success. It furthers one to cross the great water. The perseverance of the superior man furthers.",
77 "POSSESSION IN GREAT MEASURE. Supreme success.",
78 "MODESTY creates success. The superior man carries things through.",
79 "ENTHUSIASM. It furthers one to install helpers And to set armies marching.",
80 "FOLLOWING has supreme success. Perseverance furthers. No blame.",
81 "WORK ON WHAT HAS BEEN SPOILED Has supreme success. It furthers one to cross the great water. Before the starting point, three days. After the starting point, three days.",
82 "APPROACH has supreme success. Perseverance furthers. When the eighth month comes, There will be misfortune.",
83 "CONTEMPLATION. The ablution has been made, But not yet the offering. Full of trust they look up to him.",
84 "BITING THROUGH has success. It is favorable to let justice be administered.",
85 "GRACE has success. In small matters It is favorable to undertake something.",
86 "SPLITTING APART. IT does not further one To go anywhere.",
87 "RETURN. Success. Going out and coming in without error. Friends come without blame. To and fro goes the way. On the seventh day comes return. It furthers one to have somewhere to go.",
88 "INNOCENCE. Supreme success. Perseverance furthers. If someone is not as he should be, He has misfortune, And it does not further him To undertake anything.",
89 "THE TAMING POWER OF THE GREAT. Perseverance furthers. Not eating at home brings good fortune. It furthers one to cross the great water.",
90 "THE CORNERS OF THE MOUTH. Perseverance brings good fortune. Pay heed to the providing of nourishment And to what a man seeks To fill his own mouth with.",
91 "PREPONDERANCE OF THE GREAT. The ridgepole sags to the breaking point. It furthers one to have somewhere to go. Success.",
92 "The Abysmal repeated. If you are sincere, you have success in your heart, And whatever you do succeeds.",
93 "THE CLINGING. Perseverance furthers. It brings success. Care of the cow brings good fortune.",
94 "Influence. Success. Perseverance furthers. To take a maiden to wife brings good fortune.",
95 "DURATION. Success. No blame. Perseverance furthers. It furthers one to have somewhere to go.",
96 "RETREAT. Success. In what is small, perseverance furthers.",
97 "THE POWER OF THE GREAT. Perseverance furthers. ",
98 "PROGRESS. The powerful prince Is honored with horses in large numbers. In a single day he is granted audience three times.",
99 "DARKENING OF THE LIGHT. In adversity It furthers one to be persevering.",
100 "THE FAMILY. The perseverance of the woman furthers. ",
101 "OPPOSITION. In small matters, good fortune.",
102 "OBSTRUCTION. The southwest furthers. The northeast does not further. It furthers one to see the great man. Perseverance brings good fortune.",
103 "DELIVERANCE. The southwest furthers. If there is no longer anything where one has to go, Return brings good fortune. If there is still something where one has to go, Hastening brings good fortune.",
104 "DECREASE combined with sincerity Brings about supreme good fortune Without blame. One may be persevering in this. It furthers one to undertake something. How is this to be carried out? One may use two small bowls for the sacrifice.",
105 "INCREASE. It furthers one To undertake something. It furthers one to cross the great water.",
106 "BREAK-THROUGH. One must resolutely make the matter known At the court of the king. It must be announced truthfully. Danger. It is necessary to notify one's own city. It does not further to resort to arms. It furthers one to undertake something.",
107 "COMING TO MEET. The maiden is powerful. One should not marry such a maiden.",
108 "GATHERING TOGETHER. Success. The king approaches his temple. It furthers one to see the great man. This brings success. Perseverance furthers. To bring great offerings creates good fortune. It furthers one to undertake something.",
109 "PUSHING UPWARD has supreme success. One must see the great man. Fear not. Departure toward the south Brings good fortune.",
110 "OPPRESSION. Success. Perseverance. The great man brings about good fortune. No blame. When one has something to say, It is not believed.",
111 "THE WELL. The town may be changed, But the well cannot be changed. It neither decreases nor increases. They come and go and draw from the well. If one gets down almost to the water And the rope does not go all the way, Or the jug breaks, it brings misfortune.",
112 "REVOLUTION. On your own day You are believed. Supreme success, Furthering through perseverance. Remorse disappears.",
113 "THE CALDRON. Supreme good fortune. Success.",
114 "SHOCK brings success. Shock comes-oh, oh! Laughing words -ha, ha! The shock terrifies for a hundred miles, And he does not let fall the sacrificial spoon and chalice.",
115 "KEEPING STILL. Keeping his back still So that he no longer feels his body. He goes into his courtyard And does not see his people. No blame.",
116 "DEVELOPMENT. The maiden Is given in marriage. Good fortune. Perseverance furthers.",
117 "THE MARRYING MAIDEN. Undertakings bring misfortune. Nothing that would further.",
118 "ABUNDANCE has success. The king attains abundance. Be not sad. Be like the sun at midday.",
119 "THE WANDERER. Success through smallness. Perseverance brings good fortune To the wanderer.",
120 "THE GENTLE. Success through what is small. It furthers one to have somewhere to go. It furthers one to see the great man.",
121 "THE JOYOUS. Success. Perseverance is favorable.",
122 "DISPERSION. Success. The king approaches his temple. It furthers one to cross the great water. Perseverance furthers.",
123 "LIMITATION. Success. Galling limitation must not be persevered in.",
124 "INNER TRUTH. Pigs and fishes. Good fortune. It furthers one to cross the great water. Perseverance furthers.",
125 "PREPONDERANCE OF THE SMALL. Success. Perseverance furthers. Small things may be done; great things should not be done. The flying bird brings the message: It is not well to strive upward, It is well to remain below. Great good fortune.",
126 "AFTER COMPLETION. Success in small matters. Perseverance furthers. At the beginning good fortune. At the end disorder.",
127 "BEFORE COMPLETION. Success. But if the little fox, after nearly completing the crossing, Gets his tail in the water, There is nothing that would further.",
128     });
129   }
131   /**
132    * Set the prophet to use.
133    */

134   public void setProphet(String JavaDoc prophet)
135   {
136     if (prophet == null)
137       _prophet = DEFAULT_PROPHET;
138     else
139       _prophet = prophet;
140   }
142   /**
143    * Get the prophet that will be used.
144    */

145   public String JavaDoc getProphet()
146   {
147     return _prophet;
148   }
150   /**
151    * Get a prophecy.
152    */

153   public String JavaDoc getProphecy()
154   {
155     String JavaDoc[] prophecies = (String JavaDoc[]) _prophets.get(_prophet);
157     if (prophecies == null) {
158       return "a false prophet is never wise";
159     }
160     else {
161       int i = _random.nextInt(prophecies.length);
162       return prophecies[i];
163     }
164   }
165 }
Popular Tags