简体   繁体   中英

Find if exists diagonally, horizontally, or vertically in 2D - values one after another

So I'm making a boardgame that's 19 by 19. It is basically connect 5, called Gomoku.

I want to make an efficient algorithm to find if there are 'n' pieces in a row. The data is stored as a 2D array of 19x19. But for question's sake, let's say it's 6x6.

0 0 0 1 0 0      0 0 0 0 0 0
0 0 0 1 0 0      0 1 0 0 0 0
0 0 0 1 0 0      0 0 1 0 0 0
0 0 0 1 0 0      0 0 0 1 0 0 
0 0 0 1 0 0      0 0 0 0 1 0
0 0 0 0 0 0      0 0 0 0 0 1

These are two examples of '5' of 1's in a row. How can i test for HORIZONTAL, VERTICAL, and both DIAGONALS?

Here's my inefficient code:

 private boolean firstDiagonalCheck(int x, int y, int num) {
    int count = 1;
    int check = 0;
    boolean rflag = true;
    boolean lflag = true;
    int pos = 1;

    check = turnHuman + 1;

    while (rflag) {
        if (x + pos >= 19 || y + pos >= 19) {
            rflag = false;
            break;
        }
        if (gb.getBoard()[x + pos][y + pos] == check) {
            count++;
            pos++;
        } else {
            rflag = false;
        }
    }

    pos = 1;

    while (lflag) {
        if (x - pos < 0 || y - pos < 0) {
            lflag = false;
            break;
        }
        if (gb.getBoard()[x - pos][y - pos] == check) {
            count++;
            pos++;
        } else {
            lflag = false;
        }
    }

    if (count == num) {
        return true;
    }
    return false;
}

This is only one method for the first Diagonal. There are 3 more.

How can I make it more efficient and check all 4 directions?

EDIT ##################

What my code does is: - Get's the position of the piece (x,y) - Check both sides (up and down if vertical) and count how many in a row there are - If the number of count matches desired, ("num"), then return true, otherwise return false.

WOULD it be more efficient if I checked the WHOLE board every time to see if there are pieces in a row?

 int count=0; boolean Check(int x,int y) { int p1; int p2; if(elementat[x+1][y+1]==1) {p1=1; p2=1;} else if(elementat[x+1][y]==1) {p1=1; p2=0;} else. if(elementat[x+1][y-1]==1) {p1=1; p2=-1;} else. if(elementat[x][y-1]==1) {p1=0;p2=-1;} else. if(elementat[x-1][y-1]==1) {p1=-1; p2=-1;} else. if(elementat[x-1][y]==1) {p1=-1; p2=0;} else. if(elementat[x-1][y+1]==1) {p1=-1; p2=1;} else. if(elementat[x][y+1]==1) {p1=0; p2=1;} Checknext(x,y); Checknextinv(x,y); If(count==5) // 5 means no. of elements to form line {return true} else {return false;} } Checknext(x,y) //19 represents the 19x19 matrix { if((x+p1)<=19 && (x+p1)>=0 && (y+p2)<=19 && (y+p2)>=0) { if(element[x+p1][y+p2]==1) { count++; checknext[x+p1][y+p2]; } } Checknextinv(x,y) { if( (x+(-1*p1))<=19 && (x+(-1*p1))>=0 && (y+(-1*p2))<=19 && (y+(-1*p2))>=0)) { if(element[x+(-1*p1)][y+(-1*p2)]==1 ) { count++; checknextinv[x+(-1*p1)][y+(-1*p2)]; } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM