the problem is that the counter is not changing back to 1 so please check into it.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Game2 extends JFrame
{
JButton game;
JLabel label;
JTextField textfield1,textfield2,textfield3,textfield4,textfi eld5;
int n;
String colour1,colour2,colour3,colour4;
int counter=1;
boolean b=true;
JButton a=new JButton("00");
JButton a1=new JButton("01");
JButton a2=new JButton("02");
JButton a3=new JButton("03");
public Game2()
{
super("Game");
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
game=new JButton("Game");
game.setToolTipText("click here to play the game");
frame.add(game);
frame.setVisible(true);
game.setBackground(Color.pink);
game.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JFrame frame2=new JFrame();
frame2.setSize(500,500);
final JPanel panel=new JPanel();
textfield1=new JTextField(15);
label=new JLabel("number of players");
frame2.add(panel);
panel.add(label);
panel.add(textfield1);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
frame2.setVisible(true);
JButton submit=new JButton("Submit");
submit.setToolTipText("click to submit the number of players");
panel.add(submit);
submit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String number=textfield1.getText();
n=Integer.parseInt(number);
final JFrame frame3=new JFrame();
final JPanel panel1=new JPanel();
frame3.add(panel1);
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
final JButton submit1=new JButton("Submit");
final JFrame frame4=new JFrame();
final JPanel panel2=new JPanel();
frame4.add(panel2);
frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
JFrame frame5=new JFrame();
JPanel panel3=new JPanel();
frame5.add(panel3);
frame5.setSize(700,300);
frame5.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
JButton ok=new JButton("OK");
if(n==2)
{
frame5.setVisible(true);
panel3.add(new JLabel("INSTRUCTIONS FOR TWO PLAYER GAME"));
panel3.add(new JLabel("1.The next step would be to choose colours and only two colours Red and Green are available."));
panel3.add(new JLabel("2.Each player chooses one colour."));
panel3.add(new JLabel("3.The aim of the game is to connect four boxes vertically,horizontally or diagonally."));
panel3.add(new JLabel("4.The two players take turns clicking on boxes and the colours change according to the colour they chose in the beginning."));
panel3.add(new JLabel("5.Once a box's colour has changed the players can't click on it again."));
panel3.add(new JLabel("6.The player to get four boxes in a row first wins."));
panel3.add(ok);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
frame3.setVisible(true);
frame3.setSize(200,200);
textfield2=new JTextField(15);
panel1.add(new JLabel("Enter the colours"));
panel1.add(new JLabel("player 1"));
panel1.add(textfield2);
textfield3=new JTextField(15);
panel1.add(new JLabel("player 2"));
panel1.add(textfield3);
panel1.add(submit1);
submit1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String colour1 =textfield2.getText();
String colour2 =textfield3.getText();
frame4.setVisible(true);
frame4.setSize(250,175);
panel2.add(a);
panel2.add(a1);
panel2.add(a2);
panel2.add(a3);
for(int i=0;i<16&&counter<=2;i++, counter=counter+1)
{
System.out.println(counter);
if(counter==1 && colour1.compareTo("red")==0)
{
if(b==true)
{
function();
}
b=false;
if(b==true)
{
function1();
}
b=false;
if(b==true)
{
function2();
}
b=false;
if(b==true)
{
function3();
}
b=false;
}
else if(counter==1 && colour1.compareTo("green")==0)
{
if(b==true)
{
function4();
}
b=false;
if(b==true)
{
function5();
}
b=false;
if(b==true)
{
function6();
}
b=false;
if(b==true)
{
function7();
}
b=false;
}
if( counter==2 && colour2.compareTo("red")==0)
{
if(b==true)
{
function8();
}
counter=1;
b=false;
if(b==true)
{
function9();
}
counter=1;
b=false;
if(b==true)
{
function10();
}
counter=1;
b=false;
if(b==true)
{
function11();
}
counter=1;
b=false;
}
else if(counter==2&& colour2.compareTo("green")==0)
{
if(b==true)
{
function12();
}
counter=1;
b=false;
if(b==true)
{
function13();
}
counter=1;
b=false;
if(b==true)
{
function14();
}
counter=1;
b=false;
if(b==true)
{
function15();
}
counter=1;
b=false;
}
b=true;
}
}
});
}
});
}
else if(n==3)
{
frame5.setVisible(true);
panel3.add(new JLabel("INSTRUCTIONS FOR THREE PLAYER GAME"));
panel3.add(new JLabel("1.The next step would be to choose colours and only colours available are Red,Green,Blue."));
panel3.add(new JLabel("2.Each player chooses one colour."));
panel3.add(new JLabel("3.The aim of the game is to connect four boxes vertically,horizontally or diagonally."));
panel3.add(new JLabel("4.The players take turns clicking on boxes and the colours change according to the colour they chose in the beginning."));
panel3.add(new JLabel("5.Once a box's colour has changed the players can't click on it again."));
panel3.add(new JLabel("6.The player to get four boxes in a row first wins."));
panel3.add(ok);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
frame3.setVisible(true);
frame3.setSize(200,250);
panel1.add(new JLabel("enter the colour you want"));
textfield2=new JTextField(15);
panel1.add(new JLabel("player 1"));
panel1.add(textfield2);
textfield3=new JTextField(15);
panel1.add(new JLabel("player 2"));
panel1.add(textfield3);
textfield4=new JTextField(15);
panel1.add(new JLabel("player 3"));
panel1.add(textfield4);
panel1.add(submit1);
submit1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String colour1=textfield2.getText();
String colour2=textfield3.getText();
String colour3=textfield4.getText();
frame4.setVisible(true);
frame4.setSize(300,200);
JButton a=new JButton("00");
panel2.add(a);
JButton a1=new JButton("01");
panel2.add(a1);
JButton a2=new JButton("02");
panel2.add(a2);
JButton a3=new JButton("03");
panel2.add(a3);
JButton a4=new JButton("04");
panel2.add(a4);
JButton a5=new JButton("10");
panel2.add(a5);
JButton a6=new JButton("11");
panel2.add(a6);
JButton a7=new JButton("12");
panel2.add(a7);
JButton a8=new JButton("13");
panel2.add(a8);
JButton a9=new JButton("14");
panel2.add(a9);
JButton a10=new JButton("20");
panel2.add(a10);
JButton a11=new JButton("21");
panel2.add(a11);
JButton a12=new JButton("22");
panel2.add(a12);
JButton a13=new JButton("23");
panel2.add(a13);
JButton a14= new JButton("24");
panel2.add(a14);
JButton a15= new JButton("30");
panel2.add(a15);
JButton a16= new JButton("31");
panel2.add(a16);
JButton a17= new JButton("32");
panel2.add(a17);
JButton a18= new JButton("33");
panel2.add(a18);
JButton a19= new JButton("34");
panel2.add(a19);
JButton a20= new JButton("40");
panel2.add(a20);
JButton a21= new JButton("41");
panel2.add(a21);
JButton a22= new JButton("42");
panel2.add(a22);
JButton a23= new JButton("43");
panel2.add(a23);
JButton a24= new JButton("44");
panel2.add(a24);
}
});
}
});
}
else if(n==4)
{
frame5.setVisible(true);
panel3.add(new JLabel("INSTRUCTIONS FOR FOUR PLAYER GAME"));
panel3.add(new JLabel("1.The next step would be to choose colours and only colours available are Red,Green,Blue and Pink"));
panel3.add(new JLabel("2.Each player chooses one colour."));
panel3.add(new JLabel("3.The aim of the game is to connect four boxes vertically,horizontally or diagonally."));
panel3.add(new JLabel("4.The players take turns clicking on boxes and the colours change according to the colour they chose in the beginning."));
panel3.add(new JLabel("5.Once a box's colour has changed the players can't click on it again."));
panel3.add(new JLabel("6.The player to get four boxes in a row first wins."));
panel3.add(ok);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
frame3.setVisible(true);
frame3.setSize(200,275);
panel1.add(new JLabel("enter the colour you want"));
textfield2=new JTextField(15);
panel1.add(new JLabel("player 1"));
panel1.add(textfield2);
textfield3=new JTextField(15);
panel1.add(new JLabel("player 2"));
panel1.add(textfield3);
textfield4=new JTextField(15);
panel1.add(new JLabel("player 3"));
panel1.add(textfield4);
textfield5=new JTextField(15);
panel1.add(new JLabel("player 4"));
panel1.add(textfield5);
panel1.add(submit1);
submit1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String colour1=textfield2.getText();
String colour2=textfield3.getText();
String colour3=textfield4.getText();
String colour4=textfield5.getText();
frame4.setVisible(true);
frame4.setSize(350,230);
JButton a=new JButton("00");
panel2.add(a);
JButton a1=new JButton("01");
panel2.add(a1);
JButton a2=new JButton("02");
panel2.add(a2);
JButton a3=new JButton("03");
panel2.add(a3);
JButton a4=new JButton("04");
panel2.add(a4);
JButton a5=new JButton("05");
panel2.add(a5);
JButton a6=new JButton("10");
panel2.add(a6);
JButton a7=new JButton("11");
panel2.add(a7);
JButton a8=new JButton("12");
panel2.add(a8);
JButton a9=new JButton("13");
panel2.add(a9);
JButton a10=new JButton("14");
panel2.add(a10);
JButton a11=new JButton("15");
panel2.add(a11);
JButton a12=new JButton("20");
panel2.add(a12);
JButton a13=new JButton("21");
panel2.add(a13);
JButton a14= new JButton("22");
panel2.add(a14);
JButton a15= new JButton("23");
panel2.add(a15);
JButton a16= new JButton("24");
panel2.add(a16);
JButton a17= new JButton("25");
panel2.add(a17);
JButton a18= new JButton("30");
panel2.add(a18);
JButton a19= new JButton("31");
panel2.add(a19);
JButton a20= new JButton("32");
panel2.add(a20);
JButton a21= new JButton("33");
panel2.add(a21);
JButton a22= new JButton("34");
panel2.add(a22);
JButton a23= new JButton("35");
panel2.add(a23);
JButton a24= new JButton("40");
panel2.add(a24);
JButton a25= new JButton("41");
panel2.add(a25);
JButton a26= new JButton("42");
panel2.add(a26);
JButton a27= new JButton("43");
panel2.add(a27);
JButton a28= new JButton("44");
panel2.add(a28);
JButton a29= new JButton("45");
panel2.add(a29);
JButton a30= new JButton("50");
panel2.add(a30);
JButton a31= new JButton("51");
panel2.add(a31);
JButton a32= new JButton("52");
panel2.add(a32);
JButton a33= new JButton("53");
panel2.add(a33);
JButton a34= new JButton("54");
panel2.add(a34);
JButton a35= new JButton("55");
panel2.add(a35);
}
});
}
});
}
else
{
frame3.setSize(200,200);
panel1.add(new JLabel("follow the rules"));
}
}
});
}
});
}
public void function()
{
a.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a.setBackground(Color.red);
}
});
}
public void function1()
{
a1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a1.setBackground(Color.red);
}
});
}
public void function2()
{
a2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a2.setBackground(Color.red);
}
});
}
public void function3()
{
a3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a3.setBackground(Color.red);
}
});
}
public void function4()
{
a.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a.setBackground(Color.green);
}
});
}
public void function5()
{
a1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a1.setBackground(Color.green);
}
});
}
public void function6()
{
a2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a2.setBackground(Color.green);
}
});
}
public void function7()
{ a3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a3.setBackground(Color.green);
}
});
}
public void function8()
{
a.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a.setBackground(Color.red);
}
});
}
public void function9()
{
a1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a1.setBackground(Color.red);
}
});
}
public void function10()
{ a2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a2.setBackground(Color.red);
}
});
}
public void function11()
{ a3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a3.setBackground(Color.red);
}
});
}
public void function12()
{ a.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a.setBackground(Color.green);
}
});
}
public void function13()
{
a1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a1.setBackground(Color.green);
}
});
}
public void function14()
{
a2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a2.setBackground(Color.green);
}
});
}
public void function15()
{
a3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
a3.setBackground(Color.green);
}
});
}
}