I am making a connect four type game for my end of the year project in my programming class. I am about to start building off of the console based version I have made and add a GUI, but I feel sort of if-y about the win algorithms and am wondering if I could improve them. The code for those is below:
public void checkWins(int player) { int count = 0; vertical: for(int y = 0; y<height; y++) { //System.out.println("Vertical test "+y+" of "+height+"-1"); for(int x=0; x<width; x++) { if(board[x][y] == player) { count++; if(count>=4) { System.out.println("Player "+player+" won!"); win = true; break vertical; } } else { count = 0; } } } count = 0; horizontal: for(int x = 0; x<width; x++) { //System.out.println("Horizontal test "+x+" of "+width+"-1"); for(int y = 0; y<height; y++) { if(board[x][y] == player) { count++; // If there is a break in this code, count will be reset if(count>=4) { System.out.println("Player "+player+" won!"); win = true; break horizontal; } } else { count = 0; } } } count = 0; int y = height-1; int x; diagonalDownRightA: for(int loop = 0; y>=0; loop++) { //System.out.println("diagonalDownRightA test "+loop); x = 0; int otherY = y; while(x<=width-1 && otherY<=height-1) { // Go diagonally right downwards //System.out.println("dDRA testing at "+x+","+otherY); if (board[x][otherY] == player) { count++; //System.out.println("Found a result, count++"); if(count>=4) { System.out.println("Player "+player+" won!"); win = true; break diagonalDownRightA; } } else { count = 0; } x++; otherY++; y = height-1; // fixes skipping rows bug } y-=loop; // start at (0,height-1) // go up one row, x++ y++ until reached (width-1||height-1) // go up one row, add one to x and y until reached a point of no return // repeat above until reached the top // when done checking for diagonals starting at (0,0) // go to the next collumn add one to x and y until reached a point of no return // go to the next collumn add one to x and y until reached a point of no return // same thing until done } count = 0; x = width-1; diagonalDownRightB: for(int loop = 0; x>=0; loop++) { //System.out.println("diagonalDownRightB test "+loop); y = 0; int otherX = x; // =x while(y<=height-1 && otherX<=width-1) { // Go diagonally right downwards for other half of the board //System.out.println("dDRB testing at "+otherX+","+y); if (board[otherX][y] == player) { count++; if(count>=4) { System.out.println("Player "+player+" won!"); win = true; break diagonalDownRightB; } } else { count = 0; } y++; otherX++; x = width-1; } x-=loop; } y = height-1; diagonalDownLeftA: for(int loop = 0; y>=0; loop++) { //System.out.println("diagonalDownLeftA test "+loop); x = width-1; int otherY = y; while(x>=0 && otherY<=height-1) { //System.out.println("dDLA testing at "+x+","+otherY); if (board[x][otherY] == player) { count++; //System.out.println("Found a result, count++"); if(count>=4) { System.out.println("Player "+player+" won!"); win = true; break diagonalDownLeftA; } } else { count = 0; } x--; otherY++; y = height-1; // fixes skipping rows bug } y-=loop; } count = 0; x = width-1; diagonalDownLeftB: for(int loop = 0; x>=0; loop++) { //System.out.println("diagonalDownLeftB test "+loop); y = 0; int otherX = x; while(y<=height-1 && otherX>=0) { //System.out.println("dDLB testing at "+otherX+","+y); if (board[otherX][y] == player) { count++; if(count>=4) { System.out.println("Player "+player+" won!"); win = true; break diagonalDownLeftB; } } else { count = 0; } y++; otherX--; x = width-1; } x-=loop; } } }