Jump to content

Four in a row [Java help]

- - - - -

  • Please log in to reply
9 replies to this topic

#1
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hi everyone :)
I'm new here and i'm sory for not my perfect english and for my newbie java problems.
I'm trying to make a simple "Four in row" game but i'm having some problems.



for (int y = 0; y < length; y++) {

    for (int x = 0; x < width; x++) {

 

        grid[x][y].addActionListener(new ActionListener() {

             

            public void actionPerformed(ActionEvent e) {                    

                 

                int seznam[] = new int[6];

                int y = 1;

 

                if (   ((e.getSource() == grid[0][y])  )

                        || ((e.getSource() == grid[1][y]))

                        || ((e.getSource() == grid[2][y]))

                        || ((e.getSource() == grid[3][y]))

                        || ((e.getSource() == grid[4][y]))

                        || ((e.getSource() == grid[5][y]))) {

 

 

                    for (int x = 5; x > -1; x--) {

                      

                        seznam[x] = x;

                    }

 

                  

 

                            Arrays.sort(seznam);             

                            int n = seznam[seznam.length-1];

                         

 

                            if (grid[n][y].getBackground().equals(Color.gray)){

                                grid[n][y].setBackground(Color.blue);

                                 

                            }

                 

// From here the problems begine

           

                     

                }

              

 

            }

        });

    }

}




What I'm trying to do here is that when an user click the second row the last "button" on the bottom became blue, and so on. I guess you guys know how 4inrow works.
The problems is that I tried to do this with while and if loops but with no good results.
Can anyone help me.

Thankyou

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
As such?

public void actionPerformed(ActionEvent e) {          

   JButton button = (JButton) e.getSource();


   if(button.getBackGround().equals(Color.GRAY)){

      button.setBackground(Color.BLUE);

   } 

}

Or do you mean, that when the user clicks the 2nd COLUMN (vertical) it will make the lowest possible button blue?
Then I think it's something like:


boolean here = false;


for(int x=0 ; x<grid.length && !here ; x++){

  for(int y=grid[x].length-1 ; y>=0 ; y--){

    if(grid[x][y] == e.getSource() && !here){

       here = true;

       y=grid[x].length-1

    }

    if(here){

        if(grid[x][y].getBackground().equals(Color.GRAY)) {

           grid[x][y].setBackground(Color.BLUE);

           break;

        }  

    }

  }

}



#3
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hey man this is what I was searcing for. I have to do some changes but yes, this is it :)
Thx a lot :)

#4
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Soo I guess I'll need a little help again. The code that win Dc gave me is great but it's doing my 4inrow game in horizontal and not in vertical..
I need some time to understand your code in in some way I enable it to work in vertical way. But i can't fill the last column.

Here is what I've done with the code:



						boolean here = false;


						for (int y = 0; y < grid.length && !here; y++) {

							for (int x = 5; x >= 0; x--) {

								if (grid[x][y] == e.getSource() && !here) {

									here = true;


									x = grid[y].length - 1;


								}

								if (here) {

									if (grid[x - 1][y].getBackground().equals(

											Color.WHITE)) {

										grid[x - 1][y]

												.setBackground(Color.BLUE);

										break;

									}


								}


							}

						}



Posted Image

#5
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hi I still having some problems. The code that you post toninght was exelent..bu it fills my 4 in row in horizontal. I needed some time to understand the code and I've enabled that it fills in vertical but I can't fill the last column.

Here is my actually code :


	boolean here = false;


						for (int y = 0; y < grid.length && !here; y++) {

							for (int x = 5; x >= 0; x--) {


								if (grid[x][y] == e.getSource() && !here) {

									here = true;


									x = grid[y].length - 1;


								}

								if (here) {

									if (grid[x - 1][y].getBackground().equals(

											Color.WHITE)) {

										grid[x - 1][y]

												.setBackground(Color.BLUE);

										break;

									}


								}


							}

						}




#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

for (int y = 0; y < grid.length && !here; y++) {

							for (int x = 5; x >= 0; x--) {


								if (grid[x][y] == e.getSource() && !here) {

									here = true;


									x = grid[y].length[B][COLOR="orange"] - 1[/COLOR][/B];


								}

								if (here) {

									if (grid[x [COLOR="orange"][B]- 1[/B][/COLOR]][y].getBackground().equals(

											Color.WHITE)) {

										grid[x [B][COLOR="orange"]- 1[/COLOR][/B]][y]

												.setBackground(Color.BLUE);

										break;

									}


								}


							}

						}


I think you need to choose to do -1 in the first if, or -1 in the second. Not in both.

#7
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hey actually I've already do that but it doesn's work..
Then I just try something stupid and it works. The problem was :

x = grid[y].length - 1 -> this should equal 6, because 7-1 = 6, but eclipse sad java.lang.ArrayIndexOutOfBoundsException error and so on
So then I just putted x = 6 and now everything works :)
i don't understand but it's cool

Thx again man :)

#8
Chargeotto

Chargeotto

    Newbie

  • Members
  • Pip
  • 2 posts
I'm not 100% sure, but I guess that if you do x = grid[y].length - 1...
You get the length of what's inside grid[y] , not the length of the array.
I think that's why you got the java.lang.ArrayIndexOutOfBoundsException

#9
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hey guys I'm back. I decided to make in this game an option, that you can play vs PC.



	                              boolean here = false;

						

						for (int y = 0; y <= 6 && !here; y++) {

							for (int x = 5; x >= 0; x--) {


								if (grid[x][y] == e.getSource() && !here) {

									here = true;


									x = 6;

								}


								if (here) {


										if (grid[x - 1][y].getBackground()

												.equals(Color.WHITE)) {


											grid[x - 1][y]

													.setBackground(Color.BLUE);



I was thinking about this code that I've already have, but instead of

 if (grid[x][y] == e.getSource() && !here) 

I should put here what Cpu will decided

So now I have 2 questions?
If do as I sad, it will work?
And, can anywone help me with some advice, code or pseudocode how to make my PC play enought good?

Thx

#10
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Anyone?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users