import java.awt.BorderLayout;
import java.awt.Component;
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.JPanel;
import javax.swing.JTextField;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class TimeBomb extends JFrame implements ActionListener
{
public String inputString;
public String inputMask = "*";//mask
public String temp = "";
public static byte user, secret;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
JTextField tfInput = new JTextField(2);
JLabel lblMessage = new JLabel("Enter A Letter: ");
JLabel guessLetter = new JLabel();
JButton btnOK = new JButton("OK");
JLabel timer = new JLabel("*-----");
public TimeBomb() throws IOException//constructor for GUI
{
super("Time Bomb Game");
//secret word
System.out.println("Enter Secret Word: ");//enter secret word into console
inputString = keyboard.readLine();//store 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
guessLetter = new JLabel(temp);
byte[] secret = inputString.getBytes();
byte[] user = keyboard.readLine().getBytes();
//set font
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);
guessLetter.setFont(f1);
btnOK.setFont(f1);
timer.setFont(f2);
//gui properties
getContentPane().setLayout(new BorderLayout(1,1));
JPanel northPanel = new JPanel();
northPanel.add(lblMessage);
getContentPane().add(northPanel, "North");
JPanel westPanel = new JPanel();
westPanel.add(timer);
getContentPane().add(westPanel, "West");
JPanel centerPanel = new JPanel();
centerPanel.add(tfInput);
getContentPane().add(centerPanel, "Center");
JPanel eastPanel = new JPanel();
eastPanel.add(btnOK);
getContentPane().add(eastPanel, "East");
btnOK.addActionListener(this);
JPanel southPanel = new JPanel();
southPanel.add(guessLetter);
getContentPane().add(southPanel, "South");
setSize(310, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private byte[] CompareSecret(byte[] secret, final byte[] user)
{
if (secret.length == user.length)
{
byte[] output = new byte[secret.length];
for (int i = 0; i < secret.length; i++)
{
if (secret[i] == user[i])
output[i] = secret[i];
else
output[i] = 42; // *
}
return output;
}
else
{
return null; // invalid
}
}
public void actionPerformed(ActionEvent e)
{
if ("OK".equals(e.getActionCommand()))//OK button gets entered letter and puts it in the south panel
{
guessLetter.setText(tfInput.getText());
}
}
public static void main(String[] args) throws IOException
{
TimeBomb x = new TimeBomb();
x.CompareSecret(secret, user);
}
}//end TimeBomb class