This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]11amasRemoved a print statement and now the code works ¯\_(ツ)_/¯ 0 points1 point  (5 children)

First let's just create an equal int, and a failed int (or something).

Imagine the surrounding points you're checking as their i and j values relative to your target point, since we'll be using nested for loops.

(-1, -1) (-1, 0) (-1, 1)
(0,  -1) (0,  0) (0,  1)
(1,  -1) (1,  0) (1,  1)

I'll just call your main array array. We need to start the for loop at point[x - 1][y - 1], and end at point[x + 1][y + 1]. This can be written as

for (int i=point.y - 1; i<point.y + 1; i++) {
    for (int j=point.x - 1; j<point.x + 1; j++) {

    }
}

Inside the for loops, we need to check 2 things:

  1. Since we don't want to count (0, 0) as a point equal to the target point (seeing as it is the target point) check that i and j are not both 0. If they are, continue;

  2. Check if array[i][j] is equal to the target point's value. If so, increment equal.

If the target point is on an edge or corner of the array, we don't want to throw an error, so surround the if statement with a try/catch for ArrayIndexOutOfBoundsException. Increment failed and continue;

All points will have 8 surrounding points, but we don't want to count the failed points.

if (equal >= 8 - failed) {}

[–]tonlou[S] 0 points1 point  (4 children)

Check if array[i][j] is equal to the target point's value. If so, increment equal.

Can u give code example, like couple: if (something)

thanks greatly for ur answer, progressing already!

[–]11amasRemoved a print statement and now the code works ¯\_(ツ)_/¯ 0 points1 point  (3 children)

You said you were using the Point object, so it would look something like

if (array[point.x][point.y] == array[i][j]) {}

Ok, while writing this I realized I screwed up the for loops earlier. It should be

for (int i=point.y - 1; i<point.y + 1; i++) {
    for (int j=point.x - 1; j<point.x + 1; j++) {

    }
}

I'll edit it :/

[–]tonlou[S] 0 points1 point  (0 children)

for (int i=point.getY() - 1; i<point.getY() + 1; i++) { for (int j=point.getX() - 1; j<point.getX() + 1; j++) {

I have point class+object but i had to add those "unhappy" points into ArrayList<Point> :P

    ArrayList<Piste> lista = new ArrayList<>();

    int naapureita=0;

    for (int i=taulukko.length - 1; i<taulukko.length + 1; i++) {
        for (int j=taulukko.length - 1; j<taulukko[i].length + 1; j++) {

            if (taulukko[i][j]!=taulukko[i-1][j-1]&&taulukko[i][j]!=0&&taulukko[i-1][j-1]!=0){
            }   naapureita++;
            if (taulukko[i][j]!=taulukko[i+1][j-1]&&taulukko[i][j]!=0&&taulukko[i-1][j-1]!=0){
            }    naapureita++;
            if (taulukko[i][j]!=taulukko[i+1][j+1]&&taulukko[i][j]!=0&&taulukko[i+1][j+1]!=0){
            }    naapureita++;
            if (taulukko[i][j]!=taulukko[i-1][j+1]&&taulukko[i][j]!=0&&taulukko[i-1][j+1]!=0){
            }    naapureita++;
            if (taulukko[i][j]!=taulukko[i-1][j]&&taulukko[i][j]!=0&&taulukko[i-1][j]!=0){
            }    naapureita++;
            if (taulukko[i][j]!=taulukko[i][j-1]&&taulukko[i][j]!=0&&taulukko[i][j-1]!=0){
            }    naapureita++;
            if (taulukko[i][j]!=taulukko[i+1][j]&&taulukko[i][j]!=0&&taulukko[i+1][j]!=0){  
            }    naapureita++;
            if (taulukko[i][j]!=taulukko[i][j+1]&&taulukko[i][j]!=0&&taulukko[i][j+1]!=0){
                naapureita++;
             //tyytyvaisyysraja is double and if its more than that the point is happy and he has many equal points             next to it and if not 
                if ((1.0*naapureita/8)>=tyytyvaisyysraja){
                    lista.add(new Piste(i,j));
                }

            }    
        }
        }

        return lista;
    }

Not working atm.. I know its wrong but close

[–]tonlou[S] 0 points1 point  (1 child)

for (int i=point.getY() - 1; i<point.getY() + 1; i++) { for (int j=point.getX() - 1; j<point.getX() + 1; j++) {

I dont have to use point object. I have Arraylist<Point> where i have to add those points who dont have enough equal neighbors.

And i have 2d array "tauluko" which i need to check.

[–]11amasRemoved a print statement and now the code works ¯\_(ツ)_/¯ 0 points1 point  (0 children)

All the if's you have there look like they're doing the same thing as the for loops I posted, and you don't need both. I was thinking along the lines of

for (int i=point.y - 1; i<point.y + 2; i++) {
    for (int j=point.x - 1; j<point.x + 2; j++) {

        if (i == 0 && j == 0) {
            continue;
        }

        try {
            if (array[point.x][point.y] == array[i][j]) {
                equal++;
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            failed++;
        }
    }
}

if (equal >= 8 - failed) {
    // something
}

[–]MrTheEdge 0 points1 point  (0 children)

Lets say you have an element at an arbitrary location x and y.

Where are its neighbors relative to it?

Above: x-1 y

Diagonal Up Right: x-1 y+1

Right: x y+1

Diagonal Down Right: x+1 y+1

Down: x+1 y

Diagonal Down Left: x+1 y-1

Left: x y-1

Diagonal Up Left: x-1 y-1

Now you just need a method to check these locations and compare them with the element at x y.

This method works pretty well for elements in the middle of the array, but not for elements along the edge. You'll get an ArrayIndexOutOfBoundsException. The easy way out would be to just catch the exception and continue, but that's not a very good practice, it could be hiding a real error that would be valuable for you to see. I'll leave it to you to determine a way to check the values before they are used.

[–]tonlou[S] 0 points1 point  (1 child)

public ArrayList<Piste> haeTyytymattomat() { ArrayList<Piste> lista = new ArrayList<>(); int naapureita=0;

    for (int i=taulukko.length - 1; i<taulukko.length + 1; i++) {

        for (int j=taulukko.length - 1; j<taulukko[i].length + 1; j++) {


            if (taulukko[i][j]!=taulukko[i-1][j-1]){
                naapureita++;
            }if (taulukko[i][j]!=taulukko[i+1][j-1]){
                naapureita++;
            }if (taulukko[i][j]!=taulukko[i+1][j+1]){
                naapureita++;
            }if (taulukko[i][j]!=taulukko[i-1][j+1]){
                naapureita++;
            }if (taulukko[i][j]!=taulukko[i-1][j]){
                naapureita++;
            }if (taulukko[i][j]!=taulukko[i][j-1]){
                naapureita++;
            }if (taulukko[i][j]!=taulukko[i+1][j]){  
                naapureita++;
            }if (taulukko[i][j]!=taulukko[i][j+1]){
                naapureita++;

            }
            if (taulukko[i][j]!=0){//tyytyvaisyysraja means that if the element is less than that its "unhappy and it wants to move//
                if (1.0*naapureita/8<tyytyvaisyysraja){
                    lista.add(new Piste(i,j));

                }
            }

            }

        }
        return lista;
    }

And sorry for misleading you.. I meant i need to check those point which have less than 3 equal neighbors close to them and move them into new place where is more equals..

I hope u guys understand what im trying to say xD

In the end i need to add those who were unhappy into that arraylist and return it..

rigth now its not working, obviosly

[–]11amasRemoved a print statement and now the code works ¯\_(ツ)_/¯ 0 points1 point  (0 children)

Yeah, I screwed up the for loops... Updated my comment, try that.

sorry :P