Hi,
I am trying to create a simon-says type game, where there are four icons of different colors. One icon lights up. The user clicks it. Next, a new icon lights up. The user clicks the sequence... etc, etc, etc.
Right now, the first round works. Unfortunately, the icons do not change after that point. Any help would be appreciated. Thanks!
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class IanSays extends JFrame implements ActionListener
{
ImageIcon dark [] = {new ImageIcon("darkRed.png"),
new ImageIcon("darkBlue.png"), new ImageIcon("darkGreen.png"),
new ImageIcon("darkYellow.png")};
ImageIcon light [] = {new ImageIcon("red.png"),
new ImageIcon("blue.png"), new ImageIcon("green.png"),
new ImageIcon("yellow.png")};
JButton btns [] = {new JButton(dark[0]),new JButton(dark[1]),
new JButton(dark[2]), new JButton(dark[3])};
Container cont;
String code = "";
String guess = "";
int guesses = 0;
public IanSays()
{
super("Ian Says");
setSize(415,425);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
cont = getContentPane();
cont.setLayout(null);
for(int i = 0; i < btns.length; i++)
{
cont.add(btns[i]);
btns[i].addActionListener(this);
if(i<2)
{
btns[i].setBounds(i*200,0,200,200);
}
else
{
btns[i].setBounds((i-2)*200,200,200,200);
}
}
play();
}
public void play()
{
System.out.println("play");
for(int i = 0; i < code.length(); i++)
{
System.out.println(" in play loop");
reset();
cont.validate();
try
{
Thread.sleep(250);
}
catch(Exception e){}
char letter = code.toCharArray()[i];
if(letter=='r')
{
System.out.println(" RED");
btns[0].setIcon(light[0]);
}
if(letter=='b')
{
System.out.println(" BLUE");
btns[1].setIcon(light[1]);
}
if(letter=='g')
{
System.out.println(" GREEN");
btns[2].setIcon(light[2]);
}
if(letter=='y')
{
System.out.println(" YELLOW");
btns[3].setIcon(light[3]);
}
cont.validate();
try
{
Thread.sleep(250);
}
catch(Exception e){}
}
try
{
Thread.sleep(250);
}
catch(Exception e){}
newColor();
try
{
Thread.sleep(250);
}
catch(Exception e){}
reset();
System.out.println(" end of play");
}
public void newColor()
{
int rand = (int)(Math.random()*4);
if(rand==0)
{
btns[rand].setIcon(light[rand]);
code+="r";
}
if(rand==1)
{
btns[rand].setIcon(light[rand]);
code+="b";
}
if(rand==2)
{
btns[rand].setIcon(light[rand]);
code+="g";
}
if(rand==3)
{
btns[rand].setIcon(light[rand]);
code+="y";
}
cont.validate();
}
public void reset()
{
for(int i = 0; i < btns.length; i++)
{
btns[i].setIcon(dark[i]);
}
}
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < btns.length; i++)
{
if(e.getSource()==btns[i])
{
if(i==0)
guess+="r";
if(i==1)
guess+="b";
if(i==2)
guess+="g";
if(i==3)
guess+="y";
guesses++;
break;
}
}
String codeSeg = code.substring(0,guesses);
System.out.println("code "+code);
System.out.println("codeseg "+codeSeg);
System.out.println("guess "+guess);
if(!codeSeg.equals(guess))
{
System.out.println("lose!");
//You Lose
}
else
{
if(guesses==code.length())
{
System.out.println("next rnd!");
guess = "";
guesses = 0;
play();
}
}
}
public static void main (String[]args)
{
new IanSays();
}
}