![]() |
|
|
#126 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
|
|
|
|
|
|
#127 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
fixed that little issue. i had to move the letterArray and secretWordArray creation to the actionPerformed section. i also moved the if statement that compares the two arrays to the actionPerformed method.
now i just need to get it to actually write the input letter to the mask which is hiding the secret word. for that i have... Code:
for(int i = 0; i < inputString.length(); i++)
{
if(secretWordArray[i] == letterArray[j])
{
temp += textfieldString;
}
else
{
temp += inputMask;
}
}
Code:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TimeBomb extends JFrame implements ActionListener
{
public String inputString = "";
public String textfieldString = "";
public String inputMask = "*";//mask
public String temp = "";
int i,j;
JTextField tfInput = new JTextField(1);
JLabel lblMessage = new JLabel("Guess A Letter: ");
JLabel maskedWord = new JLabel();
JLabel guessedLetter = new JLabel();
JButton btnOK = new JButton("OK");
JLabel timer = new JLabel("*-----");
public TimeBomb() //constructor for GUI
{
super("Time Bomb Game");
inputString = JOptionPane.showInputDialog("Enter Secret Word: ");//user enters secret word
for(int i = 0; i < inputString.length(); i++)//gets the length of the secret word
{
temp += inputMask;//creates a mask of the secret word of the appropriate length
maskedWord = new JLabel(temp);
}
Font f1 = new Font("Dialog", Font.PLAIN, 24);//set main font
Font f2 = new Font("Dialog", Font.PLAIN, 32);//set larger font
lblMessage.setFont(f1);
tfInput.setFont(f1);
guessedLetter.setFont(f1);
btnOK.setFont(f1);
timer.setFont(f2);
maskedWord.setFont(f1);
getContentPane().setLayout(new BorderLayout(1,1));//simple gui layout
JPanel northPanel = new JPanel();
northPanel.add(lblMessage);
northPanel.add(tfInput);
northPanel.add(btnOK);
btnOK.addActionListener(this);
getContentPane().add(northPanel, "North");
JPanel centerPanel = new JPanel();
centerPanel.add(maskedWord);
centerPanel.add(guessedLetter);
getContentPane().add(centerPanel, "Center");
JPanel southPanel = new JPanel();
southPanel.add(timer);//count down to explosion
getContentPane().add(southPanel, "South");
setSize(350, 200);//should be large enough on all platforms
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if ("OK".equals(e.getActionCommand()))//OK button gets entered letter and puts it in the south panel
{
textfieldString = tfInput.getText();//string created from JTextField
int len2 = textfieldString.length();//gets the length of the letter
char[] letterArray = new char[len2];//creates array using len2
for(int j = 0; j < len2; j++)
{
letterArray[j] = textfieldString.charAt(j);//populates array using textfieldString
}
int len = inputString.length();//gets the length of the secret word and stores it in len
char[] secretWordArray = new char[len];//create array using len
for(int i = 0; i < len; i++)
{
secretWordArray[i] = inputString.charAt(i);//populate array using inputString
}
for(int i = 0; i < inputString.length(); i++)
{
if(secretWordArray[i] == letterArray[j])
{
temp += textfieldString;
}
else
{
temp += inputMask;
}
}
}
}//end of actionPerformed
public static void main(String[] args)
{
new TimeBomb();
}
}//end TimeBomb class
|
|
|
|
|
|
#128 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
You need to do something with "temp" once it exits the for loop (probably assign to both your equivilent of _Reveal and also to the text display).
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#129 | |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
Quote:
edit! ok so the command is maskedWord.setText(temp); however it is a little glitchy lol! it creates a whole new masked array with only the letter guessed revealed. so my code isnt perfect yet obviously. if i guess a letter wrong it creates a whole new ****** and if i guess a letter right it creates s***** and then when i guess another letter right it creates a new se**** so it jus t keeps making them and my field has ******s******e****** just running on. so i need to have it override the original maskedWord and keep the letters correctly guessed. Last edited by Easy Rhino; Oct 22, 2009 at 07:46 AM. |
|
|
|
|
|
|
#130 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
You have to update your equvilent of _Reveal so every time the code is executed, it is comparing the _Secret to the current _Reveal.
If you are dealing with strings: Get: (something) = myvar; Set: myvar = (something); set variable = get variable
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#131 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
uhm, ok let me work on this a bit.
Last edited by Easy Rhino; Oct 22, 2009 at 05:19 PM. |
|
|
|
|
|
#132 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
well i cant seem to troubleshoot this issue. here is my actionPerformed()
you can see the arrays are populated here and then compared using a for statement. temp is actually equal to the masked string which happens in the constructor. so then i want textFieldString, which is the letter entered in the JTextField, to += temp. but it only puts out the guessed letter and keeps the initial temp mask. Code:
public void actionPerformed(ActionEvent e)
{
if ("OK".equals(e.getActionCommand()))//OK button gets entered letter and puts it in the south panel
{
textfieldString = tfInput.getText();//string created from JTextField
int len2 = textfieldString.length();//gets the length of the letter
char[] letterArray = new char[len2];//creates array using len2
for(int j = 0; j < len2; j++)
{
letterArray[j] = textfieldString.charAt(j);//populates array using textfieldString
}
int len = inputString.length();//gets the length of the secret word and stores it in len
char[] secretWordArray = new char[len];//create array using len
for(int i = 0; i < len; i++)
{
secretWordArray[i] = inputString.charAt(i);//populate array using inputString
}
for(int i = 0; i < inputString.length(); i++)
{
if(secretWordArray[i] == letterArray[j])
{
temp += textfieldString;
}
else
{
temp += inputMask;
timer.setText(boom.getText());
}
}
maskedWord.setText(temp);
}
}//end of actionPerformed
|
|
|
|
|
|
#133 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
You could use String.getChar() instead of copying it manually.
e.g. Code:
textfieldString.getChars(0, textfieldString.length() - 1, letterArray, 0); Once you have your temp populated, you'll want to do "new JTextField(temp)" to update that JTextField or, if possible, .setText("temp"). "temp" should be defined as a String.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } Last edited by FordGT90Concept; Oct 22, 2009 at 07:59 PM. |
|
|
|
|
|
#134 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
i did a little trouble shooting by adding
System.out.println(temp) to my if statement. like so... Code:
for(int i = 0; i < inputString.length(); i++)
{
if(secretWordArray[i] == letterArray[j])
{
System.out.println(temp);
temp += textfieldString;
System.out.println(temp);
}
else
{
temp += inputMask;
System.out.println(temp);
}
}
maskedWord.setText(temp);
****** <--- this is temp before textfieldString is += to it ******s <--- this is temp after textfieldString is += to it ******s* <--- and the next 5 lines are what happens in the else statement temp += inputMask ******s** ******s*** ******s**** ******s***** im not sure why it runs both the if and else part of the code. isnt it either or??? (if the letter does match a letter in the secret word it runs both, if it doesnt match it only runs else) and this explains that with temp += textfieldString it is only adding the entered letter onto the mask, NOT replacing the * with the letter. so ive screwed something up somewhere. Last edited by Easy Rhino; Oct 22, 2009 at 08:32 PM. |
|
|
|
|
|
#135 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
temp needs to be cleared (temp = "") before it is used (the for loop).
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#136 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
|
|
|
|
|
|
#137 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
It can never do if and else with the same evaluation. It has to be reevaluated to send it on a different code path.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#138 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
|
|
|
|
|
|
#139 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
It is an impossibility for it to do both. The syntax is correct.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#140 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
|
|
|
|
|
|
#141 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
In a String, you can't replace just one letter. That's why you append it. You are creating a new string one charater at a time.
Edit: Did you put that temp = ""; in there yet? Code:
temp = "";
for(int i = 0; i < inputString.length(); i++)
{
if(secretWordArray[i] == letterArray[j])
{
temp += textfieldString;
}
else
{
temp += inputMask;
timer.setText(boom.getText());
}
}
maskedWord.setText(temp);
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#142 | |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
Quote:
i want this to happen. secret word is secret it creates a mask ****** i guess s the output on the gui is now s***** i guess e the output on the gui is now se**e* i guess t the output on the gui is now se**et and so on. so with my current code it totally replaces ****** with s and then when i enter e it reads ee and when i enter t it reads t so it isnt 1) replacing * with the correct letter 2) storing that value in memory NOTE: my algorithm is going to blow my professor away lol. |
|
|
|
|
|
|
#143 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
Code:
secretWordArray[i] == letterArray[j]
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#144 | |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
Quote:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 at TimeBomb.actionPerformed(TimeBomb.java:107) at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012) at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335) at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404) at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:253) at java.awt.Component.processMouseEvent(Component.jav a:6108) at javax.swing.JComponent.processMouseEvent(JComponen t.java:3276) at java.awt.Component.processEvent(Component.java:587 3) at java.awt.Container.processEvent(Container.java:210 5) at java.awt.Component.dispatchEventImpl(Component.jav a:4469) at java.awt.Container.dispatchEventImpl(Container.jav a:2163) at java.awt.Component.dispatchEvent(Component.java:42 95) at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4461) at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4125) at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4055) at java.awt.Container.dispatchEventImpl(Container.jav a:2149) at java.awt.Window.dispatchEventImpl(Window.java:2478 ) at java.awt.Component.dispatchEvent(Component.java:42 95) at java.awt.EventQueue.dispatchEvent(EventQueue.java: 604) at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275) at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200) at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138) |
|
|
|
|
|
|
#145 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
Why are they not equal in length?
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#146 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
|
|
|
|
|
|
#147 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
If that is the case, then you aren't comparing your equivlent of _Secret and _Reveal. You must perform that comparison in order to update your display (temp). Without it, you would get "s*****" on the first one then "*e**e*" on the second instead of "se**e*"
If the array is only one character long, it should be a char instead of an array. String.charAt(0) to get the first character of a String.
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
|
#148 | |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
Quote:
Code:
if(secretWordArray[i] == textfieldString.charAt(0)) right? |
|
|
|
|
|
|
#149 |
|
Linux Advocate
Join Date: Nov 2006
Posts: 10,224 (4.29/day)
Thanks: 1,207
Thanked 2,775 Times in 1,793 Posts
|
my prof just told me that it would be easier to use the indexof() and lastindexof() methods to determine if the letter is part of the word. so i should go with strings rather than arrays. bah,
now i am totally lost. |
|
|
|
|
|
#150 | |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,581 (6.28/day)
Thanks: 1,755
Thanked 2,598 Times in 1,962 Posts
|
Quote:
I converted Magic to Java for you: Code:
private Boolean Magic(char letter)
{
Boolean hit = false;
String temp = "";
for (int i = 0; i < secretWordArray.length(); i++)
{
if (textfieldString.charAt(i) == inputMask.charAt(i))
{
if (letter == secretWordArray.charAt(i))
{
temp += secretWordArray.charAt(i);
hit = true;
}
else
{
temp += inputMask.charAt(i);
}
}
else
{
temp += textfieldString.charAt(i);
}
}
textfieldString = temp;
return hit;
}
After running that code, you would still have to update the GUI with the text. What IDE are you using to create this?
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding a "Malicious Software Tips, Help and Discussion" thread to the forums | Black Panther | Comments & Feedback | 29 | Apr 2, 2008 06:34 PM |