Source Code Game Hangman

import java.awt.*;
import java.applet.*;
import javax.swing.*;

public class Hangman extends Applet{
// guessWord is when the users guesses the entire word at once
// guessList is which characters the user has guessed so far
String hiddenWord="", guessWord, guessList;
// number of wrong letter guesses
int missCount;
// maximum number of misses allowed
int maxMisses;
// mouseOver is boolean to the Mouse being over the "new game" button
boolean win, gameOver, mouseOver = true;
// this varible is needed to tell MouseMove not to repaint() if already hanged
boolean hanged;
// array corresponding to the hiddenWord, defining user's knowledge of each character
boolean[] knownChars;
// background color
Color bgColor = new Color(0x00dddddd);

public void init()
{
// adjust window size, and background color
setSize(1000,625);
setBackground(bgColor);
// petunjuk untuk pemain / pengguna
JOptionPane.showMessageDialog(this, "Gunakan keyboard untuk memasukkan / menginput kata dalam menjawab soal.", "Game Hangman", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(this, "Click pada layar untuk mulai main, semua berhubungan dengan ibu kota negara di dunia!", "Game Hangman", JOptionPane.INFORMATION_MESSAGE);
// start up a new game
newGame();
}

public void newGame()
{
// set the random hidden word
hiddenWord = getHiddenWord();
guessList = "";
guessWord = "";
// define size of knownChars by the length of the hidden word
knownChars = new boolean[hiddenWord.length()];
// make known characters false unless it happens to be a space
for (int i=0; i<=(hiddenWord.length()-1)*2; i++)
{
if (i%2 == 0)
{
if (hiddenWord.charAt(i/2) != ' ')
g.drawLine(i*15+225,100,(i+1)*15+225,100);
if (knownChars[i/2] == true)
g.drawString(""+hiddenWord.charAt(i/2), i*15+224, 95);
}
}
// draw figure according to number of misses
switch (missCount)
{
// string 'em up
case 7: animateHang(g); break;
// left arm
case 6: g.drawLine(375,270,300,280);
// right arm
case 5: g.drawLine(375,270,450,280);
// left leg
case 4: g.drawLine(375,400,325,450);
// right leg
case 3: g.drawLine(375,400,425,450);
// torso
case 2: g.drawLine(375,250,375,400);
// face
case 1: {g.drawOval(349,199,51,51); g.setColor(new Color(0x00ffcc99)); g.fillOval(350,200,50,50);}
}
// display list of all character guesses
g.setColor(Color.black);
for(int i=0; i<=(hiddenWord.length()-1)*2; i++)
{
if (i%2 == 0)
{
g.drawString(""+hiddenWord.charAt(i/2), i*15+224, 95);
}
}
}
// display losing acknowledgements, and display correct answer
if (missCount == maxMisses)
{
g.setColor(Color.red);
g.drawString("You Lose!",600,200);
gameOver = true;
for(int i=0; i<=(hiddenWord.length()-1)*2; i++)
{
if (i%2 == 0)
{
g.drawString(""+hiddenWord.charAt(i/2), i*15+224, 95);
}
}
}

// display number of misses
g.setColor(Color.black);
g.setFont(new Font("Helvetica", Font.BOLD, 16));
g.drawString("Misses: "+missCount,155,168);

}

public String getHiddenWord() {
// possible hidden words
String[] wordList = {"Abijan","Abu Dhabi","Addis Ababa","Aden","AI Kuwait","AI Manama","Akra","Algier",
"Alma Ata","Amman","Amsterdam","Andora","Ankara","Apia","Ashakabad","Asmara","Asuncion","Athena",
"Bagdad","Bairiki","Baku","Bamako","Bandar Seri Begawan","Bangkok","Bangui","Banyul","Beijing",
"Beirut","Belmopan","Beograd","Berlin","Bern","Besse Terre","Bissau","Bogota","Brasilia","Bratislava",
"Brazzaville","Bridgetown","Brussel","Budapest","Buenos Aires","Bujumbura","Bukarest","Canberra",
"Caracas","Dakar","Dakka","Damaskus","Dar`es Salam","Dili","Doha","Dublin","Dushanbe","El Alun",
"Fort De France","Freetown","Frunze","Funafuti","Gabourone","Georgetown","Grozny","Guatemala","Hanoi",
"Harare","Havana","Helsinki","Honiara","Islamabad","Jakarta","Jibouti","Kabul","Kairo","Kampala",
"Kathmandu","Kayenne","Khartoum","Kiev","Kigali","Kingstone","Kinshasa","Kishinev","Kolombo","Konakri",
"Kopenhagen","Koror","Kuala Lumpur","La Paz","La Valetta","Lagos","Libreville","Lilongwe","Lima",
"Lisabon","Ljubljana","Lome","London","Luanda","Lusaka","Luxemburg","Madrid","Malabo","Male","Managua",
"Manila","Maputo","Maseru","Mbabane","Mexico City","Minsk","Mogadishiu","Monaco","Monrovia","Montevideo",
"Moskow","Muskat","Nairobi","Ndjamena","New Delhi","Niamey","Nikosia","Normea","Nouackohott","Nukualofa",
"Oslo","Ottawa","Ouagadoudou","Panama","Pappete","Paramaribo","Paris","Phnom Penh","Ponape",
"Port au Prince","Port Louis","Port Moresby","Port of Span","Port Vila","Porto Novo","Praha","Pretoria",
"Pyongyang","Quito","Rabat","Rangoon","Reykyavik","Riga","Riyadh","Roma","San Juan","San Salvador",
"San Yose","San'a","Santiago","Santo Domingo","Sarajevo","Seoul","Singapura","Skopje","Sofia",
"St. George's","St. John's","Stockholm","Suva","Taipeh","Tallinu","Tananarive","Tashkent"," Tegucigalpa",
"Teheran","Tel Aviv","Thimbu","Tirana","Tokyo","Tripoli","Tunis","Ulanbator","Vatikan","Vientiane",
"ViIna","Warsawa","Washington","Wellington","Wina","Windhoek","Yaonde","Zagrib"
};
// chose one of those and generalize it with toLowerCase()
hiddenWord = wordList[(int)(Math.random()*(wordList.length+1))];
hiddenWord = hiddenWord.toLowerCase();

return hiddenWord;
}

public boolean validateGuess(String guess)
{
if (guess.equalsIgnoreCase(hiddenWord))
return true;
else
return false;
}

public void guessTheWord()
{
guessWord = JOptionPane.showInputDialog(null, "Guess the entire word:");
if (validateGuess(guessWord))
{
win = true;
}
else
{
// you lose
missCount = maxMisses;
}
}

public boolean keyDown(Event e, int k)
{
if (!gameOver)
{
boolean rightGuess = false;
// Cast the "key pressed" to a character
char keyChar = (char) k;
if (keyChar != ' ')
{
// if character has previously been choosen, then get out of this method
for(int i=0; i
Kemudian kita jalankan pada browser,akan tampak seperti berikut :
Tampilan awal Game :

Tampilan ketika memenangkan game :

Tampilan ketika kalah pada game :

date Senin, 02 Mei 2011

0 komentar to “Game Hangman”

Leave a Reply: