public class Hangman extends JPanel implements ActionListener{
private boolean used[];
private String guessword;
private int numguesses;
private boolean finished;
private Button a[];
private boolean won;
private RandomWord rw = new RandomWord();
public Hangman(){
setPreferredSize(new Dimension(350, 350));
initgame();
repaint();
}
public int getNumGuesses(){
return numguesses;
}
/*
* initialise the game
* */
public void initgame() {
//initialise game variables
rw.newGame(); //generate random word
guessword = rw.getWord();//get the random word into word buffer
won = false;
finished = false;
numguesses = 0;
used = new boolean[26];
a = new Button[26];
StringBuffer buffer;
//create keyboard buttons
for (int i = 0; i <26; i++) {
buffer = new StringBuffer();
buffer.append((char)(i+65));
a[i] = new Button(buffer.toString());
a[i].addActionListener(this);
a[i].setEnabled(true);
add(a[i]);
}
}
/*
* Paint Hangman picture
*
* */
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//draw the hangman pole
setBackground(Color.WHITE);
g2.fillRect(10, 250, 150, 20);
g2.fillRect(40,70,10,200);
g2.fillRect(40,70,60,10);
g2.setColor(Color.GRAY);
g2.fillRect(95,70,5,25);
//draw head
g2.setColor(Color.MAGENTA);
if (numguesses >=1 )
g2.drawOval(82,95,30,30);
//draw body
g2.setColor(Color.PINK);
if (numguesses >=2 )
g2.drawLine(97,125,97,150);
if (numguesses >=3 )
g2.drawLine(97,150,117,183);
if (numguesses >=4 )
g2.drawLine(97,150,77,183);
if (numguesses >=5 )
g2.drawLine(97,125,117,135);
if (numguesses >=6 )
g2.drawLine(97,125,77,135);
//fill up the used letters
StringBuffer st = new StringBuffer();
for (int l=0; l<=25; l++) {
if (used[l])
st.append((char)(l+65));
else
st.append("-");
}
g2.setColor(Color.BLUE);
Font f = new Font("Times New Roman",Font.ITALIC,14);
//display the used letters
g2.setFont(f);
g2.drawString(st.toString(),25,285);
st.delete(0, 26);
StringBuffer guessed = new StringBuffer();
Font ff = new Font("Times New Roman",Font.BOLD,24);
g2.setColor(Color.BLACK);
g2.setFont(ff);
//display the hidden word as - and letters
for (int mm = 0;mm < guessword.length();mm++) {
if (used[(int)guessword.charAt(mm)-65])
guessed.append(guessword.charAt(mm));
else
guessed.append("-");
}
g2.drawString(guessed.toString(),75,230);
guessed.delete(0, guessword.length()+1);
if (numguesses >=6) {//more than 6 tries = lose
g2.setColor(Color.WHITE);
g2.fillRect(70, 200, 200, 30);
g2.setColor(Color.BLACK);
g2.drawString(guessword.toString(),75,230);
Font fff = new Font("Times New Roman",Font.BOLD,36);
g2.setFont(fff);
g2.setColor(Color.RED);
g2.drawString("You lose!",130,150);//display you lose
finished = true;//game finish = true
}
if (won) {//if win game
Font fff = new Font("Times New Roman",Font.BOLD,36);
g2.setFont(fff);
g2.setColor(Color.GREEN);
g2.drawString("You Win!",130,150);//display you win
finished = true;//game finish = true
}
repaint();
}
/*
* Action listener for key buttons
* */
public void actionPerformed( ActionEvent ev) {
for (int i = 0; i < 26; i++) {
if (ev.getSource() == a[i]) {
checker(i);//call check letter method
a[i].setEnabled(false);//disable once pressed
}
}
}
/*
* Check letter input from user
* */
public void checker(int letter) {
if (!finished) {
boolean found = false;
boolean wrong = false;
if (!used[letter]) {//if not in the used letter array
for (int mm = 0;mm < guessword.length(); mm++) {
if (((char)(letter+65)) == guessword.charAt(mm))
{ System.out.println((char)(letter+65));
found = true;//letter matching found = true
}
}
if (!found)//if not matching
numguesses++;//number of guesses used increased
System.out.println(numguesses);
}
used[letter] = true;//set true when letter is used
//if word not matching wrong = true
for (int mm = 0;mm < guessword.length(); mm++) {
if (!used[(int)(guessword.charAt(mm))-65])
wrong = true;
}
if (!wrong)//if word is not wrong win game
won = true;
repaint();
}
}
public void resetGame(){
//initialise game variables
rw.newGame(); //generate random word
guessword = rw.getWord();//get the random word into word buffer
used = new boolean[26];
won = false;
finished = false;
numguesses = 0;
//reset letter board
for (int i = 0; i <26; i++) {
a[i].setEnabled(true);
}
}
}