Filter:   InfoImg
download Hangman.java
Language: Java
Copyright: (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
LOC: 346
Project Info
JavaML
Server: SourceForge
Type: cvs
...ml\javaml\java‑ml\examples\
   .cvsignore
   Abacus.java
   AlphaBullet.java
   Animator.java
   AnInterface.java
   AnonymousClass.java
   AnotherApplet.java
   Assignments.java
   AudioItem.java
   Banner.java
   BigInit.java
   Blah.java
   BounceItem.java
   Bubbles.java
   cannon.java
   Chart.java
   ComplexApplet.java
   ComplexApplet2.java
   Continued.java
   crossword.java
   DiningPhilosophers.java
   escherpaint.java
   fillTriangle.java
   FirstApplet.java
   generic_paint.java
   Hangman.java
   ImageLoop.java
   ImageLoopItem.java
   ImageTape.java
   ImageTest.java
   JackhammerDuke.java
   LED.java
   LEDMessage.java
   Letters.java
   OuterClass.java
   Pythagoras.java
   RotateFilter.java
   Script.java
   SimpAnim.html
   SimpAnim.java
   SimpleAnimator.java
   stars.java
   SystemInfo.java
   TumbleItem.java
   voltage.java
   WordMatch.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
 * @(#)Hangman.java	1.5  03 Apr 1996 10:56:25
 *
 * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
 * without fee is hereby granted. 
 * Please refer to the file http://java.sun.com/copy_trademarks.html
 * for further important copyright and trademark information and to
 * http://java.sun.com/licensing.html for further important licensing
 * information for the Java (tm) Technology.
 * 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 * 
 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
 * HIGH RISK ACTIVITIES.
 *
 * @author Patrick Chan
 * modified to use MediaTracker by
 * @author Herb Jellinek
 * modified for 1.1 by
 * @author Chris Bucchere
 *
 */

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;

public class Hangman 
extends java.applet.Applet 
implements Runnable, MouseListener, KeyListener {
  
  /* This is the maximum number of incorrect guesses. */
  final int maxTries = 5;
  
  /* This is the maximum length of a secret word. */
  final int maxWordLen = 20;
  
  /* This buffer holds the letters in the secret word. */
  char secretWord[];
  
  /* This is the length of the secret word. */
  int secretWordLen;
  
  /* This buffer holds the letters which the user typed
     but don't appear in the secret word. */
  char wrongLetters[];
  
  /* This is the current number of incorrect guesses. */
  int wrongLettersCount;
  
  /* This buffer holds letters that the user has successfully
     guessed. */
  char word[];
  
  /* Number of correct letters in 'word'. */
  int wordLen;
  
  /* This is the font used to paint correctly guessed letters. */
  Font wordFont;
  FontMetrics wordFontMetrics;
  
  /* This is the MediaTracker that looks after loading the images. */
  MediaTracker tracker;
  
  /* These are the various classes of images we load */
  static final int DANCECLASS = 0;
  static final int HANGCLASS = 1;
  
  /* This is the sequence of images for Duke hanging on the gallows. */
  Image hangImages[];
  final int hangImagesWidth = 39;
  final int hangImagesHeight = 58;
  
  // Dancing Duke related variables
  
  /* This thread makes Duke dance. */
  Thread danceThread;
  
  /* These are the images that make up the dance animation. */
  Image danceImages[];
  private int danceImageWidths[] = { 70, 85, 87, 90, 87, 85, 70 };
  
  /* This is the maximum width and height of all the dance images. */
  int danceHeight = 68;
  
  /* This variable holds the number of valid images in danceImages. */
  int danceImagesLen = 0;
  
  /* These offsets refer to the dance images.  The dance images
     are not of the same size so we need to add these offset 
     in order to make the images "line" up. */
  private int danceImageOffsets[] = { 8, 0, 0, 8, 18, 21, 27 };
  
  /* This represents the sequence to display the dance images
     in order to make Duke "dance".  */
  private int danceSequence[] = { 3, 4, 5, 6, 6, 5, 6, 6, 5, 4, 3, 
				  2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2 };
  
  /* This is the current sequence number.  -1 implies
     that Duke hasn't begun to dance. */
  int danceSequenceNum = -1;
  
  
  /* This variable is used to adjust Duke's x-position while
     he's dancing. */
  int danceX = 0;
  
  /* This variable specifies the currently x-direction of
     Duke's dance.  1=>right and -1=>left. */
  int danceDirection = 1;
  
  /* This is the stream for the dance music. */
  AudioClip danceMusic;
  
  /**
   * Initialize the applet. Resize and load images.
   */
  public void init() {
    int i;
    
    // create tracker
    tracker = new MediaTracker(this);
    
    // load in dance animation
    danceMusic = getAudioClip(getDocumentBase(), "audio/dance.au");
    danceImages = new Image[40];
    
    for (i = 1; i < 8; i++) {
      Image im = getImage(getDocumentBase(), "images/dancing-duke/T" + i + ".gif");
      tracker.addImage(im, DANCECLASS);
      danceImages[danceImagesLen++] = im;
    }
    
    // load in hangman image sequnce
    hangImages = new Image[maxTries];
    for (i=0; i<maxTries; i++) {
      Image im = getImage(getDocumentBase(), "images/hanging-duke/h"+(i+1)+".gif");
      tracker.addImage(im, HANGCLASS);
      hangImages[i] = im;
    }
    
    // initialize the word buffers.
    wrongLettersCount = 0;
    wrongLetters = new char[maxTries];
    
    secretWordLen = 0;
    secretWord = new char[maxWordLen];
    
    word = new char[maxWordLen];
    
    wordFont = new java.awt.Font("Courier", Font.BOLD, 24);
    wordFontMetrics = getFontMetrics(wordFont);
    
    resize((maxWordLen+1) * wordFontMetrics.charWidth('M') + maxWordLen * 3,
	   hangImagesHeight * 2 + wordFontMetrics.getHeight());
    
    addMouseListener(this);
    addKeyListener(this);
  }
  
  /**
   * Paint the screen.
   */
  public void paint(Graphics g) {
    int imageW = hangImagesWidth;
    int imageH = hangImagesHeight;
    int baseH = 10;
    int baseW = 30;
    Font font;
    FontMetrics fontMetrics;
    int i, x, y;
    
    // draw gallows pole
    g.drawLine(baseW/2, 0, baseW/2, 2*imageH - baseH/2);
    g.drawLine(baseW/2, 0, baseW+imageW/2, 0);
    
    // draw gallows rope
    g.drawLine(baseW+imageW/2, 0, baseW+imageW/2, imageH/3);
    
    // draw gallows base
    g.fillRect(0, 2*imageH-baseH, baseW, baseH);
    
    
    // draw list of wrong letters
    font = new java.awt.Font("Courier", Font.PLAIN, 15);
    fontMetrics = getFontMetrics(font);
    x = imageW + baseW;
    y = fontMetrics.getHeight();
    g.setFont(font);
    g.setColor(Color.red);
    for (i=0; i<wrongLettersCount; i++) {
      g.drawChars(wrongLetters, i, 1, x, y);
      x += fontMetrics.charWidth(wrongLetters[i]) 
+ fontMetrics.charWidth(' ');
    }
    
    if (secretWordLen > 0) {
      // draw underlines for secret word
      int Mwidth = wordFontMetrics.charWidth('M');
      int Mheight = wordFontMetrics.getHeight();
      g.setFont(wordFont);
      g.setColor(Color.black);
      x = 0;
      y = getSize().height - 1;
      for (i=0; i<secretWordLen; i++) {
	g.drawLine(x, y, x + Mwidth, y);
	x += Mwidth + 3;
      }
      
      // draw known letters in secret word
      x = 0;
      y = getSize().height - 3;
      g.setColor(Color.blue);
      for (i=0; i<secretWordLen; i++) {
	if (word[i] != 0) {
	  g.drawChars(word, i, 1, x, y);
	}
	x += Mwidth + 3;
      }
      
      if (wordLen < secretWordLen && wrongLettersCount > 0) {
	// draw Duke on gallows
	g.drawImage(hangImages[wrongLettersCount-1], 
                    baseW, imageH/3, this);
      }
    }
    
    }
  
  public void update(Graphics g) {
    if (wordLen == 0) {
      g.clearRect(0, 0, getSize().width, getSize().height);
      paint(g);
    } else if (wordLen == secretWordLen) {
      if (danceSequenceNum < 0) {
	g.clearRect(0, 0, getSize().width, getSize().height);
	paint(g);
	danceSequenceNum = 0;
      }
      updateDancingDuke(g);
    } else {
      paint(g);
    }
  }
  
  void updateDancingDuke(Graphics g) {
    int baseW = 30;
    int imageH = hangImagesHeight;
    int danceImageNum = danceSequence[danceSequenceNum];
    
    // first, clear Duke's current image
    g.clearRect(danceX+baseW, imageH*2 - danceHeight, 
		danceImageOffsets[danceImageNum]+danceImageWidths[danceImageNum], 
		danceHeight);
    
    // update dance position
    danceX += danceDirection;
    if (danceX < 0) {
      danceX = danceDirection = (int)Math.floor(Math.random() * 12) + 5;
    } else if (danceX + baseW > getSize().width / 2) {
      //danceDirection = -(int)Math.floor(Math.random() * 12) - 5;
      danceDirection *= -1;
    } else if (Math.random() > .9f) {
      danceDirection *= -1;
    }
    
    // update dance sequence
    danceSequenceNum++;
    if (danceSequenceNum >= danceSequence.length) {
      danceSequenceNum = 0;
    }
    
    // now paint Duke's new image
    danceImageNum = danceSequence[danceSequenceNum];
    if ((danceImageNum < danceImagesLen) && (danceImages[danceImageNum] != null)) {
      g.drawImage(danceImages[danceImageNum], 
		  danceX+baseW+danceImageOffsets[danceImageNum], 
		  imageH*2 - danceHeight, this);
    }
  }
  
  /**
   * Starts a new game.  Chooses a new secret word
   * and clears all the buffers
   */
  public void newGame() {
    int i;
    
    // stop animation thread.
    danceThread = null;
    
    // pick secret word
    String s = wordlist[(int)Math.floor(Math.random() * wordlist.length)];
    
    secretWordLen = Math.min(s.length(), maxWordLen);
    for (i=0; i<secretWordLen; i++) {
      secretWord[i] = s.charAt(i);
    }
    
    // clear word buffers
    for (i=0; i<maxWordLen; i++) {
      word[i] = 0;
    }
    wordLen = 0;
    for (i=0; i<maxTries; i++) {
      wrongLetters[i] = 0;
    }
    wrongLettersCount = 0;
    
    repaint();
  }
  
  /**
   * Start the applet.
   */
  public void start() {
    requestFocus();
    try {
      tracker.waitForID(HANGCLASS);
    } catch (InterruptedException e) {}
    tracker.checkAll(true);
    // Start a new game only if user has won or lost; otherwise
    // retain the same game.
    if (secretWordLen == wordLen || wrongLettersCount == maxTries) {
      newGame();
    }
  }
  
  /**
   * Stop the applet.  Stop the danceThread.
   */
  public void stop() {
    danceThread = null;
  }
  
  /**
   * Run Duke's dancing animation. This method is called by class Thread.
   * @see java.lang.Thread
   */
  public void run() {
    try {
      tracker.waitForID(DANCECLASS);
    } catch (InterruptedException e) {
    }
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
    // start the dancing music.
    danceMusic.loop();
    
    // increment the sequence count and invoke the paint method.
    while (getSize().width > 0 && getSize().height > 0 && danceThread != null) {
      repaint();
      try {Thread.sleep(100);} catch (InterruptedException e){}
    }
    
    // The dance is done so stop the music.
    danceMusic.stop();
  }
  
  /**
   * Starts Duke's dancing animation.
   */
  private void startDukeDancing () {
    if (danceThread == null) {
      danceThread = new Thread(this);
      danceThread.start();
    }
  }
  
  // Added by Kevin A. Smith 10/25/95
  public String getAppletInfo() {
    return "Author: Patrick Chan\nVersion 1.5";
  }
  
  
  /* This is the hangman's limited word list. */
  String wordlist[] = {
    "abstraction",
    "ambiguous",
    "arithmetic",
    "backslash",
    "bitmap",
    "circumstance",
    "combination",
    "consequently",
    "consortium",
    "decrementing",
    "dependency",
    "disambiguate",
    "dynamic",
    "encapsulation",
    "equivalent",
    "expression",
    "facilitate",
    "fragment",
    "hexadecimal",
    "implementation",
    "indistinguishable",
    "inheritance",
    "internet",
    "java",
    "localization",
    "microprocessor",
    "navigation",
    "optimization",
    "parameter",
    "patrick",
    "pickle",
    "polymorphic",
    "rigorously",
    "simultaneously",
    "specification",
    "structure",
    "lexical",
    "likewise",
    "management",
    "manipulate",
    "mathematics",
    "hotjava",
    "vertex",
    "unsigned",
    "traditional"};
  
  /* key tracking methods */
  public void keyPressed(KeyEvent e) {
  }

  public void keyReleased(KeyEvent e) { 
    int i;
    boolean found = false;
    char key = e.getKeyChar();
      
    // start new game if user has already won or lost.
    if (secretWordLen == wordLen || wrongLettersCount == maxTries) {
      newGame();
      e.consume();
      return;
    }
    
    // check if valid letter
    if (key < 'a' || key > 'z') {
      play(getDocumentBase(), "audio/beep.au");
      e.consume();
      return;    
    }
    // check if already in secret word
    for (i=0; i<secretWordLen; i++) {
      if (key == word[i]) {
	found = true;
	play(getDocumentBase(), "audio/ding.au");
	e.consume();
	return;
      }
    }
    // check if already in wrongLetters
    if (!found) {
      for (i=0; i<maxTries; i++) {
	if (key == wrongLetters[i]) {
	  found = true;
	  play(getDocumentBase(), "audio/ding.au");
	  e.consume();
	  return;
	}
      }
    }
    // is letter in secret word? If so, add it.
    if (!found) {
      for (i=0; i<secretWordLen; i++) {
	if (key == secretWord[i]) {
	  word[i] = (char)key;
	  wordLen++;
	  found = true;
	}
      }
      if (found) {
	if (wordLen == secretWordLen) {
	  play(getDocumentBase(), "audio/whoopy.au");
	  startDukeDancing();
	} else {
	  play(getDocumentBase(), "audio/ah.au");
	}
      }
    }
    // wrong letter; add to wrongLetters
    if (!found) {
      if (wrongLettersCount < wrongLetters.length) {
	wrongLetters[wrongLettersCount++] = (char)key;
	if (wrongLettersCount < maxTries) {
	  play(getDocumentBase(), "audio/ooh.au");
	} else {
	  // show the answer
	  for (i=0; i<secretWordLen; i++) {
	    word[i] = secretWord[i];
	  }
	  play(getDocumentBase(), "audio/scream.au");
	}
      }
    }
    if (wordLen == secretWordLen) {
      danceSequenceNum = -1;
    }
    repaint();
    e.consume();
    return;
  }

  public void keyTyped(KeyEvent e) {  
  }
  
  /* mouse tracking methods */
  public void mouseClicked(MouseEvent e) {
  }
  
  public void mouseReleased(MouseEvent e) {
  }
  
  /**
   * Grab the focus and restart the game.
   */  
  public void mousePressed(MouseEvent e) {
    int i;
    
    // grab focus to get keyDown events
    requestFocus();
    
    if (secretWordLen > 0 && 
	(secretWordLen == wordLen || wrongLettersCount == maxTries)) {
      newGame();
    } else {
      play(getDocumentBase(), "audio/beep.au");
    }
    e.consume();
  }
         
  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

}