Hi sorry about asking so many questions.....and yes i have been searching on the internet, but anyways i am getting to start checking the array words with its chars and whatnot, but i dont know if will work i am confused where to put the array of words i have and in what method and just wondering if my code will eventually even work? any help will be helpfull critique as much as you want thank you
Futbol10 0 Newbie Poster
/**
* @(#)Hangman.java
*
* Hangman Applet application
*
* @author
* @version 1.00 2008/5/27
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JOptionPane;
public class MyHangman extends Applet {
private boolean usd[] = new boolean[26];
private String guess;
private int numguesses = 0;
private boolean finished = false;
private boolean won = false;
private Button b[];
char secretWord[];//holds letters in word
char word[];//holds letters guessed in word
char wrongWord[];
int wordLen;
int secretWordLen;
public void init() {
int a;
StringBuffer buff = new StringBuffer();
setLayout ( new GridLayout(83,1));
b = new Button[26];
for(a = 0; a < 26; a++)
{
buff = new StringBuffer();
buff.append((char)(a+65));
b[a] = new Button(buff.toString());
//b[a].addActionListener(this);}
add(b[a]);//adds all of the buttons
//guess = getParameter("wrd").toUpperCase();
}
}
public void paint(Graphics g)
{
setBackground(Color.orange);
g.fillRect(450, 700, 200, 20);
g.fillRect(450,550,10,100);
g.fillRect(450,300,20,400);
g.fillRect(450,300,150,20);
g.setColor(Color.blue);
g.fillRect(525,320,25,75);//draws the gallows
g.setColor(Color.white);
/*g.drawRect(535,445,10,100);
g.drawOval(515,395,50,50);
g.drawRect(545,475,30,5);
g.drawRect(505,475,30,5);
g.drawOval(525,410,5,5);
g.drawOval(540,410,5,5);
g.drawRect(530,420,6,6);
g.setColor(Color.black);
g.drawRect(530,440,18,1);*/
if ( numguesses >= 1 )
g.setColor(Color.white);
g.fillOval(515,395,50,50);
g.setColor(Color.blue);
g.fillOval(525,410,5,5);
g.fillOval(540,410,5,5);
g.drawRect(530,437,18,1);
g.drawRect(530,420,6,6);
g.setColor(Color.red);
if ( numguesses >= 2)
g.fillRect(535,445,10,100);
if ( numguesses >= 3)
g.fillRect(545,475,30,5);
if ( numguesses >= 4)
g.fillRect(505,475,30,5);
if ( numguesses >= 5)
g.drawLine(535,540,500,570);
if( numguesses >= 6)
g.drawLine(580,570,545,540);
StringBuffer wrong = new StringBuffer();
for (int l = 0; l<=25; l++) {
if (usd[l]) wrong.append((char)(l+65));
else wrong.append(".");
}
g.setColor(Color.green);//Setting up the incorrect guessed letters
Font f = new Font("Palatino",Font.BOLD+Font.ITALIC,65);
g.setFont(f);
g.drawString(wrong.toString(),300,850);//end of writing the wrong letters
StringBuffer right = new StringBuffer();
Font ff = new Font("Palatino",Font.BOLD,85);
g.setColor(Color.black);
g.setFont(ff);
for (int ii = 0; ii < guess.length(); ii++)
if(usd[(int)guess.charAt(ii)-65])
right.append(guess.charAt(ii));//Marks down the right answers
else
right.append("."); //If the same letter is used twice it won't be put down
//or counted
g.drawString(right.toString(),600,400);
if(numguesses >= 6)
{
JOptionPane.showMessageDialog(null, " Wow you just got your buddy hung nice job");
finished = true;
}
if(won)
{
JOptionPane.showMessageDialog(null, " Phew your buddy lives another day");
}
}
public void actionPerformed(ActionEvent e)
{
int i;
boolean found = false;
char but = e.getSource();
if (secretWordLen == wordLen || numguesses == 6) {
newGame();
e.consume();
return;
}//starts a new game
for (i=0; i<secretWordLen; i++) {
if (but == word[i]) {
found = true;
e.consume();
return;
}
}//checks to see if already in secret word
if (!found) {
for (i=0; i<6; i++) {
if (but == wrongWord[i]) {
found = true;
e.consume();
return;
}
}
}//check if already in wrong letters
if (!found) {
for (i=0; i<secretWordLen; i++) {
if (but == secretWord[i]) {
word[i] = (char)but;
wordLen++;
found = true;
}
}
}//adds letters to secret word if correct
if (!found) {
if (numguesses < wrongWord.length) {
wrongWord[numguesses++] = (char)but;
if (numguesses < 6) {
JOptionPane.showMessageDialog(null, "Momma's wrong again!");
} else {
// show the answer
for (i=0; i<secretWordLen; i++) {
word[i] = secretWord[i];
}
JOptionPane.showMessageDialog(null, "Here it is son!");
}
}
}
repaint();
e.consume();
return;
}
String wordList[] = {
"BEAU"
"SHERLOCK"
"ROBIN"
"LUKE"
"AMY"
"JASON"
"AUGUSTIN"
"FAITH"
"BRANDON"
"JENNIFER"
"JAVA"
"GREENE"
"MORTON"
"BLODGETT"
"HOLMES"
"PAPWORTH"
"PETERSEN"
"CHAMBERS"
"HARRAL"
"CAMPOS"
}} //end of main
PoovenM 30 Junior Poster
Hi there, just curious, why do you want to use a char array and not a String object? Possibly using a char array would save memory or be faster, but if you used strings, then there are methods that allow you to easily search the string for any substring. In particular (taken from the Java Docs):
indexOf(int ch, int fromIndex) Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
I haven't taken a good look at your code, but declaring and instantiating the String array object, you must separate each element with a comma like so:
String wordList[] = {
"BEAU",
"SHERLOCK",
"ROBIN",
...
"HARRAL",
"CAMPOS"
};
Notice that the is no comma for the last element and that there is a semicolon at the end of the declaration. The semicolon is required. Perhaps try asking a more specific question. One should always have an algorithm in mind when one is programming, perhaps share this algorithm with us and we can assist you from there... :icon_biggrin:
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
And please include the code in post with [code] [/code] tags. Many people, myself included, do not want to mess with attachments to glance at your code.
Futbol10 0 Newbie Poster
ok sorry about the code part....noobie please forgive and as the string object i havent really gone over it in class and i dont know how to use it? so basically its over my head right now lol......as for the strings i understand thee wordList but don't know where to put it?
I have a method for checking the buttons pushed and if it should be put in wrong letters right letters etc? should it go in there?
and as you stated i dont know how to check the array strings i have with the chars so if the user guesses a letter it is right? Basically what i have below is my words and a random gen to pick a random word and i need help with how to check chars and what method to put it in.....once i get that i can show you my for and what not to check the chars to see if it will work thanks for anything if anything is confusing just ask and sorry i am a noobie
String wordList[] = {
"BEAU",
"SHERLOCK",
"ROBIN",
"LUKE",
"AMY",
"JASON",
"AUGUSTIN",
"FAITH",
"BRANDON",
"JENNIFER",
"JAVA",
"GREENE",
"MORTON",
"BLODGETT",
"HOLMES",
"PAPWORTH",
"PETERSEN",
"CHAMBERS",
"HARRAL",
"CAMPOS",};
char array[]=wordList[0].toCharArray();
char array1[]=wordList[1].toCharArray();
char array2[]=wordList[2].toCharArray();
char array3[]=wordList[3].toCharArray();
char array4[]=wordList[4].toCharArray();
char array5[]=wordList[5].toCharArray();
char array6[]=wordList[6].toCharArray();
char array7[]=wordList[7].toCharArray();
char array8[]=wordList[8].toCharArray();
char array9[]=wordList[9].toCharArray();
char array10[]=wordList[10].toCharArray();
char array11[]=wordList[11].toCharArray();
char array12[]=wordList[12].toCharArray();
char array13[]=wordList[13].toCharArray();
char array14[]=wordList[14].toCharArray();
char array15[]=wordList[15].toCharArray();
char array16[]=wordList[16].toCharArray();
char array17[]=wordList[17].toCharArray();
char array18[]=wordList[18].toCharArray();
char array19[]=wordList[19].toCharArray();
char array20[]=wordList[20].toCharArray();
String randomString=wordList[(int)(wordList.length*Math.random())];
public void actionEvent(ActionListener evt)
{
}
Edited by mike_2000_17 because: Fixed formatting
warrior16 0 Newbie Poster
Are you retarded....?
Please post up the code, quit being lazy. Maybe if you make it easier for us to help, then maybe we will share our knowledge with you, maybe. IMHO your trying to hard.
Futbol10 0 Newbie Poster
I hate you sherlock....
ps this is to warriors16
warrior16 0 Newbie Poster
Interesting.
To check the array that is made of Strings, you just go
if(wordlist[1] = "whatever the name is")
Thats how you check the array of Strings you made.
Your code is very complicated.
char array[]=wordList[0].toCharArray();
What does that above line mean? Your creating an Array called array and it is of char values and of no size. Then you are saying it is equal to the value of wordList[0]. Why are you doing this? It sounds kind of redundent don't you think?
Alex Edwards 321 Posting Shark
Interesting.
To check the array that is made of Strings, you just go
if(wordlist[1] = "whatever the name is")
Not quite, you never want to use the assignment operator for a boolean expression since it will always be true if the value stored isn't null.
You may want to use syntax such as--
if(wordlist[1].equalsIgnoreCase("whatever the name is"))
Thats how you check the array of Strings you made.
Your code is very complicated.char array[]=wordList[0].toCharArray();
What does that above line mean? Your creating an Array called array and it is of char values and of no size. Then you are saying it is equal to the value of wordList[0]. Why are you doing this? It sounds kind of redundent don't you think?
Actually toCharArray() returns an array of the size of the amount of characters in the String value (not including the null terminator).
So if wordList[0] contains the String "Value" then toCharArray of that would be a char array of size 5 from indices 0-4.
The declaration char array[] means that the variable array is of type char[]. Size is later listed either directly or indirectly.
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
You don't need to declare char arrays for every value in the word list. You have a single word chosen randomly from that list and you only need to worry about the characters in that word
char[] characters = randomString.toCharArray();
or you can dispense with the array and check any character with
randomString.charAt(0)
for any position 0 to length-1.
majestic0110 commented: A relentless helper!! +3
VernonDozier 2,218 Posting Expert Featured Poster
Are you retarded....?
Please post up the code, quit being lazy. Maybe if you make it easier for us to help, then maybe we will share our knowledge with you, maybe. IMHO your trying to hard.
Overly harsh, and unnecessary, in my opinion. The OP posted code and made a slight error with the code tags. He's a noob, so cut him some slack. You made a mistake in your post, which Alex Edwards pointed out, and nobody insulted you because of it.
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
Interesting.
To check the array that is made of Strings, you just go
if(wordlist[1] = "whatever the name is")
Thats how you check the array of Strings you made.
Your code is very complicated.char array[]=wordList[0].toCharArray();
What does that above line mean? Your creating an Array called array and it is of char values and of no size. Then you are saying it is equal to the value of wordList[0]. Why are you doing this? It sounds kind of redundent don't you think?
Actually warrior16's mistake was far more serious and shows an incredible lack of java knowledge. I never post a solution that I haven't tried myself so I will know that it will work.
if(wordlist[1] = "whatever the name is")
You NEVER use '==' to compare Strings (you used '=' but let's say that it was an honest mistake).
That piece of code and your next question about the array thing shows that you have no clue about java and you shouldn't even post your so-called solutions, and YOU were the lazy one, not bothering to check the API for the toCharArray() method
PS: Don't let this guy get you Futbol10
Futbol10 0 Newbie Poster
Thanks for all your help guys and as for you warrior16, whatever man im sorry im not as smart as you at this java programming......and thanks guys i have finished my program and it works awesome thanks for all the help:)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.