Here you can find the source of checkWinPossibility(int[][] board, Point p1, Point p2, int playerID)
public static int checkWinPossibility(int[][] board, Point p1, Point p2, int playerID)
//package com.java2s; //License from project: Open Source License import java.awt.Point; public class Main { public static int checkWinPossibility(int[][] board, Point p1, Point p2, int playerID) { int TwoOpenEndsRetVal = 3; int xDistance = p1.x - p2.x; int yDistance = p1.y - p2.y; int length = (Math.abs(xDistance) > Math.abs(yDistance) ? Math .abs(xDistance) : Math.abs(yDistance)) + 1; // length of combo between p1 and p2 //Check horizontally if (p1.y == p2.y) { int openLeft = 0, openRight = 0; //Check to the left for (int dx = 1; p1.x - dx >= 0; dx++) { if (board[p1.x - dx][p1.y] == 0) { // || board[p1.x - dx][p1.y] == playerID openLeft++;/*from w w w .j ava 2 s . com*/ } else { break; } } //Check to the right for (int dx = 1; p2.x + dx < board.length; dx++) { if (board[p2.x + dx][p2.y] == 0) { // || board[p2.x + dx][p2.y] == playerID openRight++; } else { break; } } //System.out.println("Max: " + (openLeft + length + openRight) + ", openLeft: " + openLeft + ", openRight: " + openRight); if (openLeft + length + openRight < 5) { //System.out.println("Five in a row is not possible."); return 0; } else if (openLeft >= 1 && openRight >= 1) { //System.out.println("Open in both directions."); return TwoOpenEndsRetVal; } else if (openLeft >= 1 || openRight >= 1) { //System.out.println("Open in one direction."); return 1; } } //Check vertically else if (p1.x == p2.x) { int openUp = 0, openDown = 0; //Check above for (int dy = 1; p1.y - dy >= 0; dy++) { if (board[p1.x][p1.y - dy] == 0) { // || board[p1.x][p1.y - dy] == playerID openUp++; } else { break; } } //Check below for (int dy = 1; p2.y + dy < board[0].length; dy++) { if (board[p2.x][p2.y + dy] == 0) { // || board[p2.x][p2.y + dy] == playerID openDown++; } else { break; } } //System.out.println("Max: " + (openAbove + length + openBelow) + ", openAbove: " + openAbove + ", openBelow: " + openBelow); if (openUp + length + openDown < 5) { //System.out.println("Five in a row is not possible."); return 0; } else if (openUp >= 1 && openDown >= 1) { //System.out.println("Open in both directions."); return TwoOpenEndsRetVal; } else if (openUp >= 1 || openDown >= 1) { //System.out.println("Open in one direction."); return 1; } }//Check diagonal 1 ( \ ) else if (p1.x < p2.x && p1.y < p2.y) { int openUpLeft = 0, openDownRight = 0; //Check up left for (int dxy = 1; (p1.x - dxy >= 0 && p1.y - dxy >= 0); dxy++) { if (board[p1.x - dxy][p1.y - dxy] == 0) { //|| board[p1.x - dxy][p1.y - dxy] == playerID openUpLeft++; } else { break; } } //Check down right for (int dxy = 1; (p2.x + dxy < board.length && p2.y + dxy < board[0].length); dxy++) { if (board[p2.x + dxy][p2.y + dxy] == 0) { // || board[p2.x + dxy][p2.y + dxy] == playerID openDownRight++; } else { break; } } //System.out.println("Max: " + (openUpLeft + length + openDownRight) + ", openUpLeft: " + openUpLeft + ", openDownRight: " + openDownRight); if (openUpLeft + length + openDownRight < 5) { //System.out.println("Five in a row is not possible."); return 0; } else if (openUpLeft >= 1 && openDownRight >= 1) { //System.out.println("Open in both directions."); return TwoOpenEndsRetVal; } else if (openUpLeft >= 1 || openDownRight >= 1) { //System.out.println("Open in one direction."); return 1; } }//Check diagonal 2 ( / ) else if (p1.x > p2.x && p1.y < p2.y) { int openUpRight = 0, openDownLeft = 0; //Check up right for (int dxy = 1; (p1.x + dxy < board.length && p1.y - dxy >= 0); dxy++) { if (board[p1.x + dxy][p1.y - dxy] == 0) { // || board[p1.x + dxy][p1.y - dxy] == playerID openUpRight++; } else { break; } } //Check down left for (int dxy = 1; (p2.x - dxy >= 0 && p2.y + dxy < board[0].length); dxy++) { if (board[p2.x - dxy][p2.y + dxy] == 0) { // || board[p2.x - dxy][p2.y + dxy] == playerID openDownLeft++; } else { break; } } //System.out.println("Max: " + (openUpRight + length + openDownLeft) + ", openUpRight: " + openUpRight + ", openDownLeft: " + openDownLeft); if (openUpRight + length + openDownLeft < 5) { //System.out.println("Five in a row is not possible."); return 0; } else if (openUpRight >= 1 && openDownLeft >= 1) { //System.out.println("Open in both directions."); return TwoOpenEndsRetVal; } else if (openUpRight >= 1 || openDownLeft >= 1) { //System.out.println("Open in one direction."); return 1; } } return 0; } }