Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: this is part of the coding for a game and its not working so please help.

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default this is part of the coding for a game and its not working so please help.

    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);


    }
    });

    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,409
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: this is part of the coding for a game and its not working so please help.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    How are you debugging the code?
    counter is not changing back to 1
    Add some println() statements to show the values of the variables that control when that should happen.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: this is part of the coding for a game and its not working so please help.

    Yeow! Too much code posted improperly!

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    And please, please, please, post your code correctly using code or highlight tags per the above link.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default this is part of the coding for a game and its not working so please help.it may seem long but it isnt, most of it is the same cause i have to create many buttons with the same function.

    the problem is inside the for loop and a variable called counter is supposed to turn back to 1 but doesnt so please check it out.



    [code=java]
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Game extends JFrame

    {
    JButton game;
    JLabel label;
    JTextField textfield1,textfield2,textfield3,textfield4,textfi eld5;
    int n;
    String colour1,colour2,colour3,colour4;
    int counter=1;
    static boolean b=true;
    public Game()
    {
    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);
    final JButton a=new JButton("00");
    panel2.add(a);
    final JButton a1=new JButton("01");
    panel2.add(a1);
    final JButton a2=new JButton("02");
    panel2.add(a2);
    final JButton a3=new JButton("03");
    panel2.add(a3);
    final JButton a4=new JButton("10");
    panel2.add(a4);
    final JButton a5=new JButton("11");
    panel2.add(a5);
    final JButton a6=new JButton("12");
    panel2.add(a6);
    final JButton a7=new JButton("13");
    panel2.add(a7);
    final JButton a8=new JButton("20");
    panel2.add(a8);
    final JButton a9=new JButton("21");
    panel2.add(a9);

    final JButton a10=new JButton("22");
    panel2.add(a10);
    final JButton a11=new JButton("23");
    panel2.add(a11);
    final JButton a12=new JButton("30");
    panel2.add(a12);
    final JButton a13=new JButton("31");
    panel2.add(a13);
    final JButton a14= new JButton("32");
    panel2.add(a14);
    final JButton a15= new JButton("33");
    panel2.add(a15);

    [COLOR="#FF0000"] for(int i=0;i<16&&counter<=2;i++,counter++)
    {

    System.out.println(counter);

    if(counter==1 && colour1.compareTo("red")==0)
    {

    if(b==true)
    {

    a.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a.setBackground(Color.red);



    }
    });
    b=false;
    }



    if(b==true)
    {
    a1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a1.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a2.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a2.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a3.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a3.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a4.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a4.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a5.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a5.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)

    {

    a6.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a6.setBackground(Color.red);


    }

    });
    b=false;

    }


    if(b==true)
    {

    a7.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a7.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a8.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a8.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a9.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a9.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a10.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a10.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a11.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a11.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a12.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a12.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a13.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a13.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a14.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a14.setBackground(Color.red);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a15.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a15.setBackground(Color.red);

    }
    });
    b=false;
    }



    }
    else if(counter==1 && colour1.compareTo("green")==0)
    {
    if(b==true)
    {

    a.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a1.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a2.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a2.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a3.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a3.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a4.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a4.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a5.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a5.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a6.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a6.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a7.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a7.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a8.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a8.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {


    a9.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a9.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {


    a10.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a10.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a11.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a11.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a12.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a12.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {


    a13.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a13.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a14.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a14.setBackground(Color.green);


    }
    });
    b=false;
    }

    if(b==true)
    {

    a15.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a15.setBackground(Color.green);


    }
    });
    b=false;
    }



    }

    if( counter==2 && colour2.compareTo("red")==0)
    {
    if(b==true)
    {


    a.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a1.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a2.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a2.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a3.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a3.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a4.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a4.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a5.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a5.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a6.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a6.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a7.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a7.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a8.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a8.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a9.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a9.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a10.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a10.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a11.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a11.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a12.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a12.setBackground(Color.red);



    }
    });

    b=false;
    }

    if (b==true)
    {

    a13.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a13.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a14.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a14.setBackground(Color.red);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a15.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a15.setBackground(Color.red);



    }
    });

    b=false;
    }




    }
    else if(counter==2&& colour2.compareTo("green")==0)
    {

    if(b==true)
    {

    a.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a1.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a2.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a2.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a3.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a3.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a4.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a4.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a5.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a5.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a6.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a6.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a7.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a7.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a8.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a8.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a9.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a9.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {
    a10.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a10.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {
    a11.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a11.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a12.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a12.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a13.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a13.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a14.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a14.setBackground(Color.green);



    }
    });

    b=false;
    }

    if(b==true)
    {

    a15.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    a15.setBackground(Color.green);



    }
    });

    b=false;
    }




    }
    b=true;
    counter=1;

    }

    }
    });
    }
    });

    }
    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"));
    }

    }
    });
    }
    });
    }}

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: this is part of the coding for a game and its not working so please help.

    Please do not post multiple threads on the same topic, post your code correctly, post only the code necessary to show where the problem is, preferably runnable to demonstrate the problem, and describe the problem and its location better.

    Threads merged.

Similar Threads

  1. Help with simple math game coding
    By jmd in forum Java Theory & Questions
    Replies: 17
    Last Post: February 28th, 2013, 10:49 PM
  2. Confused on getWidth() / getHeight() for GCanvas as part of Hangman game
    By Thedomes in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2012, 08:13 PM
  3. [2 Part Qestion] Saving Game Data & Hiding GUI from taskbar
    By Jello36 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2012, 09:38 PM
  4. Replies: 1
    Last Post: June 28th, 2011, 07:34 AM
  5. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM