package lyj.chess.main; /** * 五子棋判断输赢类 * @author LYJ * */ public class CheckWhoWin { private static int HOW_MANY=5; /** * 判断棋子横线是否五连珠 * @param row 横坐标 * @param column 纵坐标 * @param chessType * @return */ public boolean acrossLine(int [][]chess,int row,int column,int chessType){ int count=0; int origalX=row,origalY=column; while(column>=0&&chess[column][row]==chessType){ count++; column--; if(count==HOW_MANY){ return true; } } column=origalY+1; while(column<chess[0].length&&chess[column][row]==chessType){ count++; column++; if(count==HOW_MANY){ return true; } } row=origalX;column=origalY; return false; } /** * 判断纵线是否五连珠 * @param chess * @param row * @param column * @param chessType * @return */ public boolean upRightLine(int [][]chess,int row,int column,int chessType){ int count=0; //往上 int origalX=row,origalY=column; while(row>=0&&chess[column][row]==chessType){ count++; row--; if(count==HOW_MANY){ return true; } } row=origalX+1; //往下 while(row<chess.length&&chess[column][row]==chessType){ count++; row++; if(count==HOW_MANY){ return true; } } row=origalX;column=origalY; return false; } /** * 判断左斜 * @param chess * @param row * @param column * @param chessType * @return */ public boolean leftXie(int [][]chess,int row,int column,int chessType){ int count=0; int origalX=row,origalY=column; while(row>=0&&column<=chess[0].length-1&&chess[column][row]==chessType){ count++; column++; row--; if(count==HOW_MANY){ return true; } } row=origalX+1; column=origalY-1; while(row<=chess.length-1&&column>=0&&chess[column][row]==chessType){ count++; row++; column--; if(count==HOW_MANY){ return true; } } row=origalX;column=origalY; return false; } /** * 判断右斜 * @param chess * @param row * @param column * @param chessType * @return */ public boolean rightXie(int [][]chess,int row,int column,int chessType){ int count=0; int origalX=row,origalY=column; while(row<=chess.length-1&&column<chess[0].length-1&&chess[column][row]==chessType){ row++; column++; count++; if(count==HOW_MANY){ return true; } } row=origalX-1; column=origalY-1; while(row>=0&&column>=0&&chess[column][row]==chessType){ row--; column--; count++; if(count==HOW_MANY){ return true; } } row=origalX;column=origalY; return false; } }
最近下载更多