arrays - ConnectFour Program Issues in Java -


i'm creating connect 4 program scratch practice , having trouble checkalignment() method, or win condition. works in rows not all, , doesn't work other direction (vertically, diagonally forwards, diagonally backwards).

public char checkalignment(int row, int column) { char color = board[row][column]; char[][] current = getboard();  // horizontal left-to-right check - - - - - - - - - - if (column + 4 <= columns) {     (int = 1; < 4; i++) {         if (current[row][column + i] != color) {             return none;         }     }     return color; }  // horizontal right-to-left check - - - - - - - - if (column - 4 > -1) {     (int = 1; < 4; i++) {         if (current[row][column - i] != color) {             return none;         }     }     return color; }  //  vertical top-to-bottom check - - - - - - - if (row + 4 <= rows) {     (int = 1; < 4; i++) {         if (current[row + i][column] != color) {             return none;         }     }     return color; }  // vertical bottom-to-top check - - - - - - - - if (row - 4 > -1) {     (int = 1; < 4; i++) {         if (current[row - i][column] != color) {             return none;         }     }     return color; }  // main diagonal backwards check - - - - - - - - - - if (column - 4 > -1 && row - 4 > -1) {     (int = 1; < 4; i++) {         (int j = 1; j < 4; j++) {             if (current[row - i][column - j] != color) {                 return none;             }         }     }     return color; }  // main diagonal forwards check - - - - - - - - - - if (column + 4 <= columns && row + 4 <= rows) {     (int = 1; < 4; i++) {         (int j = 1; j < 4; j++) {             if (current[row + i][column + j] != color) {                 return none;             }         }     }     return color; }  // secondary diagonal backwards check - - - - - - - - - if (column - 4 > -1 && row + 4 <= rows) {     (int = 1; < 4; i++) {         (int j = 1; j < 4; j++) {             if (current[row + i][column - j] != color) {                 return none;             }         }     }     return color; } // secondary diagonal forwards check - - - - - - - - - - if (column + 4 <= columns && row - 4 > -1) {     (int = 1; < 4; i++) {         (int j = 1; j < 4; j++) {             if (current[row - i][column + j] != color) {                 return none;             }         }     }     return color; } return none; 

}

can me out?

edited/adjusted:

public char checkalignment(int row, int column) {     char color = board[row][column];     char[][] current = getboard();      // horizontal left-to-right check     if (column + 4 <= num_cols) {         (int = 0; < 4; i++) {             if (current[row][column + i] != color) {                 return none;             }         }         return color;     }      // horizontal right-to-left check     if (column - 4 > -1) {         (int = 0; < 4; i++) {             if (current[row][column - i] != color) {                 return none;             }         }         return color;     }      //  vertical top-to-bottom check     if (row + 4 <= num_rows) {         (int = 0; < 4; i++) {             if (current[row + i][column] != color) {                 return none;             }         }         return color;     }      // vertical bottom-to-top check     if (row - 4 > -1) {         (int = 0; < 4; i++) {             if (current[row - i][column] != color) {                 return none;             }         }         return color;     }      // main diagonal backwards check     if (column - 4 > -1 && row - 4 > -1) {         (int = 0; < 4; i++) {             (int j = 0; j < 4; j++) {                 if (current[row - i][column - j] != color) {                     return none;                 }             }         }         return color;     }      // main diagonal forwards check - - - - - - - - - -     if (column + 4 <= num_cols && row + 4 <= num_rows) {         (int = 0; < 4; i++) {             (int j = 0; j < 4; j++) {                 if (current[row + i][column + j] != color) {                     return none;                 }             }         }         return color;     }      // secondary diagonal backwards check - - - - - - - - -     if (column - 4 > -1 && row + 4 <= num_rows) {         (int = 0; < 4; i++) {             (int j = 0; j < 4; j++) {                 if (current[row + i][column - j] != color) {                     return none;                 }             }         }         return color;     }     // secondary diagonal forwards check - - - - - - - - - -     if (column + 4 <= num_cols && row - 4 > -1) {         (int = 0; < 4; i++) {             (int j = 0; j < 4; j++) {                 if (current[row - i][column + j] != color) {                     return none;                 }             }         }         return color;     }     return none; } 

by looks of every 1 of loops use i=1 , go upto 4, running 4 times each. row , column checked +/- 4 away current. if reading correctly, checking 1 more need in cases. either make i=0 or go upto 3 (as checked own square current).

edit: gave second look

my first statement above not problem. real problem think each item checking 4 in single direction away 'current' one. not true shown in following example 'y' last piece put grid.

x x y x 

so in case can try following code checks total number left , right of 'current' selected token. if there 4 or more in both directions togeather connect-4:

//horizantal check //right side int connected = 0; int = 1; while (column + <= columns) {     if (current[row + i][column] != color) {         break;     }else{         connected++;     }     i++; }    = 1; //left side while (column - <= columns) {     if (current[row - i][column] != color) {         break;     }else{         connected++;     }     i++; } if(connected >= 4){//total connected     return color; }else{     return none; } 

on review vertical plain check looks fine. recall in connect-4 drop tokens down grid can never drop new token slot has token above (on turn dropped it) meaning doing vertical check upwards not necessary.

//vertical plain check - 1 dropped must on top if (row - 4 > -1) {     for(int i=1; i<4;i++){         if (current[row - i][column] != color) {             return none;         }     }     return color; } 

hopefully helps in getting these 2 directions correct. diagonals need have horizontal plain.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -