-1

I have this array which i want to check on 4 in a row (the game). I can do it with hundreds of if else but how can I do it in a loop? Is this even possible? This is my code. Thanks in advance!

int array[][] = {{0, 1, 1, 1, 1, 0, 0},
                 {0, 0, 0, 1, 0, 1, 1},
                 {0, 0, 0, 1, 0, 1, 1},
                 {0, 1, 1, 0, 1, 0, 1},
                 {1, 0, 0, 1, 0, 1, 1},
                 {1, 0, 0, 1, 0, 1, 0}};

for (int rows = 0; rows < 6; rows++) {
    for(int columns = 0; columns < 7; columns++) {
        System.out.print(array[rows][columns] + " ");
    }
    System.out.println();
}

if (array[0][0] == 0) {
    if(array[0][1] == 0) {
        if (array[0][2] == 0) {
            if (array[0][3] == 0) {
                System.out.println("Connect Four!");
            }
        }
    }
}
Vikas Gupta
  • 10,779
  • 4
  • 35
  • 42
  • u want to check on a row if all 4 columns are having 0 value correct ? – Dreamweaver Jan 13 '16 at 13:02
  • Eehm hard to explain but i just want to check for four zero's or one's who are next to or above each other. Like the game connect four – Fabian van Rooyen Jan 13 '16 at 13:06
  • 1
    Wouldn't you need 3 states? IMO state possibilities are: 0=blank, 1=player1, 2=player2. When it's blank, ingore it, otherwise use loops to check the adjacent array-items for player1 or player2. – Johannes Jan 13 '16 at 13:16

2 Answers2

4

Something like this?

int[] dx = {0, 1, 0, -1}; // I think you only need the first two, because
int[] dy = {1, 0, -1, 0}; // otherwise you check things twice

for (int y = 0; y < array.length; y++) {
    for (int x = 0; x < array[y].length; x++) {
        int start_value = array[y][x];
        for (int i = 0; i < dx.length; i++) {
            for (int j = 1; j < 4; j++) {
                // check if there are enough cells in y direction
                if (y + dy[i] * j >= array.length) break;
                // check if there are enough cells in x direction
                if (x + dx[i] * j >= array[y].length) break;
                // check if the value is the same
                if (array[y + dy[i] * j][x + dx[i] * j] != start_value) {
                    break;
                }
                // the next three elements in a direction are the same
                if (j == 3) {
                    System.out.println("4 in a row");
                    return;
                }
            }
        }
    }
}
System.out.println("not 4 in a row");

Add more values to dx and dy if you want to check in more directions, for example the diagonal.

martijnn2008
  • 3,552
  • 5
  • 30
  • 40
  • Thanks! Is there a way to make the places where there is 4 in a row bold or red or something? – Fabian van Rooyen Jan 13 '16 at 13:25
  • I guess you need the know the indices of the elements that are part of the four in a row. I don't know what kind of GUI thing you have, but since I posted this answer 15 minutes ago. I think you should first try out more yourself or hire some to do it for you. – martijnn2008 Jan 13 '16 at 13:27
  • I thought in the first place maybe it is'nt even possible. If that would be true then i don't even have to try it. But thanks – Fabian van Rooyen Jan 13 '16 at 13:37
0

I would introduce a counter and increase it every time a 0 is in your current "line" and reset it if the current number is a 1. If the counter reaches 4, you have 4 connected.

Take a look at this:

int array[][] = {{0, 1, 1, 1, 1, 0, 0},
                 {0, 0, 0, 1, 0, 1, 1},
                 {0, 0, 0, 1, 0, 1, 1},
                 {0, 1, 1, 0, 1, 0, 1},
                 {1, 0, 0, 1, 0, 1, 1},
                 {1, 0, 0, 1, 0, 1, 0}};

//Search rows
for (int i = 0; i < array.length; i++) {
    int rowCount = 0;                   
    for (int j = 0; j < array[i].length; j++) {
        if (array[i][j] == 0) {
            rowCount++;
        } else {
            rowCount = 0;
        }
        if (rowCount == 4) {
            System.out.println("yay");
        }
    }
}

This is not the complete code, only for rows. But it should give you an idea on how to solve your problem for rows and even diagonals, too.

Jörn Buitink
  • 2,906
  • 2
  • 22
  • 33
  • @[Fabian van Rooyen](http://stackoverflow.com/users/4973740/fabian-van-rooyen) I am curious why do you consider this answer to be better? – martijnn2008 Jan 13 '16 at 15:04