The Artima Developer Community
Sponsored Link

Java Answers Forum
hangman

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dominic francis

Posts: 1
Nickname: whatadom
Registered: Apr, 2005

hangman Posted: Apr 15, 2005 6:32 AM
Reply to this message Reply
Advertisement
im trying to make agame of hangman because im learning java at the moment and if any one can help in anyway it would be most appreciated.

i would like to be able to have the game show wrong letters when the user selects and in correct letter , perhaps using drawchar.

and also i seem to have a problem at the moment when you get a word correctit appears that there is no way of starting the game again and look for a new word.

if any one has any avice it would be appreciated

all the best dom

heres the code

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class Hangman extends Applet implements ActionListener {

String string1="", string2="", temp="";

Button ready,go,guess;

Label label1,label2;

Label word[];
TextField text1,text2;
int x,y,z,w,l;
String []wordlist = {"BURNLEY", "ARSENAL", "LEEDS"};
java.util.Random rand = new java.util.Random();
String toGuess = wordlist[rand.nextInt(wordlist.length)];
Font f = new Font("Arial", Font.BOLD,24);
boolean nextGame;

public void init(){
ready = new Button("New Game");
go = new Button("try");
guess = new Button("Guess");
label1 = new Label("Secret word or words 20 characters max");
label2 = new Label(" ");
for(int i=0; i<toGuess.length(); i++) temp +="*";
text1 = new TextField(temp,20);
text2 = new TextField("",2);
add(label1);
add(text1);
add(ready);
ready.addActionListener(this);
go.addActionListener(this);
go.setEnabled(false);
label1= new Label("Guess: 1 letter");
add(label1);
add(text2);
add(go);
add(label2);
add("south",guess);
guess.setEnabled(false);
guess.addActionListener(this);
Label []word = new Label[toGuess.length()];
this.word=word;
for(int i=0;i<toGuess.length();i++){
word=new Label(" ");
word.setBackground(Color.cyan);
word.setForeground(Color.blue);
word.setFont(f);
add(word);
}
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==ready){
setBackground(Color.white);
if (nextGame){
for (int i=0;i<toGuess.length();i++)remove(word);
toGuess = wordlist[rand.nextInt(wordlist.length)];
for (int i=0;i<toGuess.length();i++) temp +="*";
text1.setText(temp);
Label []word = new Label[toGuess.length()];
this.word=word;
for(int i=0;i<toGuess.length();i++){
word=new Label(" ");
word.setBackground(Color.cyan);
word.setForeground(Color.blue);
word.setFont(f);
this.add(word);
}
validate();
}
string1=toGuess;
x=string1.length();
w=x;
z=x;
guess.setEnabled(true);
text2.setEditable(true);
for(y=0;y<x;y++){
if (string1.charAt(y)==' '){
word[y].setText(" - ");
}
else word[y].setText(" ? ");
}
text1.setText("");
text1.setEditable(false);
ready.setEnabled(false);
go.setEnabled(true);
nextGame=true;
}
if (e.getSource()==go) {
string2=text2.getText().toUpperCase();
text2.setText("");
if (string2.length()==1){
for(y=0;y<string1.length();y++){
if (string2.charAt(0)==string1.charAt(y)){
word[y].setText(string2);
z-=1;
}
}
if (w==z){
l++;
getAppletContext().showStatus("Bad Luck");
}
else{
w=z;
getAppletContext().showStatus(":D well done");
}
}
else if(string2.length()==0)
getAppletContext().showStatus("So ... is there a letter ?!?");
else
getAppletContext().showStatus("Hmmm, never mind, try again");
repaint();
}
if (e.getSource()==guess)
l=11;
if (l>=11){
getAppletContext().showStatus("You lose a life, bad luck");
for (y=0;y<x;y++){
word[y].setText("***");
}
text1.setText(string1);
text2.setEditable(false);
text2.setText("");
go.setEnabled(false);
ready.setEnabled(true);
text1.setEditable(true);
guess.setEnabled(false);
l=0;
}
}
public void paint(Graphics g) {
if (l>0) {
g.setColor(Color.black);
g.drawLine(160,240,230,240);
g.drawLine(160,238,230,238);
}
if (l>1) {
g.setColor(Color.black);
g.drawLine(160,240,160,150);
g.drawLine(158,240,158,150);
}
if (l>2) {
g.setColor(Color.black);
g.drawLine(160,150,200,150);
g.drawLine(160,148,200,148);
}
if (l>3) {
g.setColor(Color.black);
g.drawLine(160,175,175,150);
g.drawLine(158,173,173,148);
}
if (l>4) {
g.setColor(Color.red);
g.fillArc(175,150,25,25,0,360);

}
if (l>5) {
g.setColor(Color.blue);
g.drawLine(187,175,187,210);
g.drawLine(189,175,189,210);

}
if (l>6) {
g.setColor(Color.green);
g.drawLine(170,193,187,193);
g.drawLine(170,194,187,194);
}
if (l>7) {
g.setColor(Color.green);
g.drawLine(189,193,207,193);
g.drawLine(189,194,207,194);

}
if (l>8) {
g.setColor(Color.black);
g.drawLine(187,210,174,235);
g.drawLine(188,211,175,236);
}
if (l>9) {
g.setColor(Color.black);
g.drawLine(188,211,201,236);
g.drawLine(189,210,202,235);

}


}

}

Topic: Waiting for Thread/Runnable Close/Stop confirmation before enabling Start Previous Topic   Next Topic Topic: Holding on to Threads/Runnables (ArrayList) to be able to .STOP them

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use