cooldude1561
05-16-2006, 09:41 AM
basicly i need to make a basic hangman applet and i dont even know where to start. this is what my teacher said.
using the decision making statments that we have learned so far, create a program that asks the user to guess a word. they have 6 chances to guess the word before they are hung. give them a chance to play two different games
can someone please help this is due soon.
agent_x91
05-18-2006, 04:25 PM
depends what decision making statements you have learned so far...?
from the sounds of it you just want a simple, non-graphical application to ask users to guess the word itself rather than a full-scale hangman application... is that right, or would you like a complete hangman program which lets you guess letters rather than entire words?
agent_x91
05-18-2006, 04:41 PM
If it is just a completely non-graphical guessing game you're after, you can simply do something like this:
class Hangman
{
String[] words={"word1","word2"}; //array of words to use
BufferedReader user_in;
public static void main(String[] args)
{
String word_used=words[Math.floor(Math.random()*words.length)];
try
{
user_in=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<6;i++)
{
String guess=user_in.readLine();
if(guess.equals(word_used))
{
System.out.println("Correct!");
System.exit(0);
}
else
{
System.out.print((i!=5)?"No, try again\n":"No, ");
}
}
System.out.print("You're hung!");
System.exit(0);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Sorry for any minor errors in that, I just hurriedly put that together in the quick reply box:)
cooldude1561
05-22-2006, 09:23 AM
i need it to look like this:
http://img105.imageshack.us/img105/7184/hgjhg0gt.png
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class hangman extends Applet implements ActionListener {
//this is the used letter array
private boolean usd[] = new boolean[26];
private String guessme;
private int numguesses=0;
private boolean finished = false;
private boolean won = false;
private Button a[];
public void init() {
int i;
StringBuffer buffer;
// setLayout( new GridLayout( 2,13) );
a = new Button[26];
// create all 26 buttons
for (i = 0; i <26; i++) {
buffer = new StringBuffer();
buffer.append((char)(i+65));
a[i] = new Button(buffer.toString());
a[i].addActionListener( this );
add(a[i]);
}
// make the guessed word uppercase
guessme=getParameter("wrd").toUpperCase();
}
public void paint(Graphics g) {
//draw gallows and rope
setBackground(Color.white);
g.fillRect(10, 250, 150, 20);
g.fillRect(40,70,10,200);
g.fillRect(40,70,60,10);
g.setColor(Color.yellow);
g.fillRect(95,70,5,25);
g.setColor(Color.orange);
if (numguesses >=1 )
g.drawOval(82,95,30,30);
g.setColor(Color.green);
if (numguesses >=2 )
g.drawLine(97,125,97,150);
if (numguesses >=3 )
g.drawLine(97,150,117,183);
if (numguesses >=4 )
g.drawLine(97,150,77,183);
if (numguesses >=5 )
g.drawLine(97,125,117,135);
if (numguesses >=6 )
g.drawLine(97,125,77,135);
StringBuffer st = new StringBuffer();
for (int l=0; l<=25; l++) {
if (usd[l]) st.append((char)(l+65));
else st.append(".");
}
g.setColor(Color.blue);
Font f = new Font("Courier",Font.ITALIC,14);
g.setFont(f);
g.drawString(st.toString(),25,285);
StringBuffer guessed = new StringBuffer();
Font ff = new Font("Courier",Font.BOLD,24);
g.setColor(Color.black);
g.setFont(ff);
for (int mm=0;mm<guessme.length();mm++) {
if (usd[(int)guessme.charAt(mm)-65])
guessed.append(guessme.charAt(mm));
else
guessed.append(".");
}
g.drawString(guessed.toString(),75,230);
if (numguesses >=6) {
g.setColor(Color.white);
g.fillRect(70, 200, 200, 30);
g.setColor(Color.black);
g.drawString(guessme.toString(),75,230);
Font fff = new Font("Helvetica",Font.BOLD,36);
g.setFont(fff);
g.setColor(Color.red);
g.drawString("You lose!",200,100);
finished = true;
}
if (won) {
Font fff = new Font("Helvetica",Font.BOLD,36);
g.setFont(fff);
// Color red=new Color.red
g.setColor(Color.red);
g.drawString("You Win!",200,100);
finished = true;
}
}
public void rer(int lett) {
if (!finished) {
boolean found=false;
boolean www=false;
if (!usd[lett]) {
for (int mm =0;mm<guessme.length();mm++) {
if (guessme.charAt(mm)==((char)(lett+65))) found=true;
}
if (!found) numguesses++;
}
usd[lett] = true;
for (int mm =0;mm<guessme.length();mm++) {
if (!usd[(int)(guessme.charAt(mm))-65]) www=true;
}
if (!www) won=true;
repaint();
}
}
public void actionPerformed( ActionEvent ev) {
int i;
for (i = 0; i < 26; i++) {
if (ev.getSource() == a[i]) { rer(i); }
}
}
the only problem is that i cant figure out where the words go and whenever i load it, it says nonintieted.