From 8adaa6b7585c8c5a50025e20ae7a8795967d1c59 Mon Sep 17 00:00:00 2001 From: AmanV26 <35691065+AmanV26@users.noreply.github.com> Date: Mon, 26 Feb 2018 02:22:45 +0530 Subject: [PATCH 1/5] King move added --- src/ChessAI.java | 114 +++++++++++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 43 deletions(-) diff --git a/src/ChessAI.java b/src/ChessAI.java index 272edc1..4acf34e 100755 --- a/src/ChessAI.java +++ b/src/ChessAI.java @@ -1,29 +1,14 @@ -/** - * - * @author ashutosh maurya - */ import UserInterface.GUI; //API for Chess GUI import javax.swing.*; //Java Swing Library - +/** + * + * @author Aman Verma + */ public class ChessAI { - /** - * @param args - * @param i - * @return - */ - /* The Chess Board is represented by a 8*8 2D Array - r/R : Rook - k/K : King - b/B : Bishop - q/Q : Queen - a/A : King - p/P : Pawn - Future Task : Bitboards can be used to implement chess board, which are much faster than arrays - */ - static String chessBoard[][] = { + static String chessBoard[][] = { {"r","k","b","q","a","b","k","r"}, {"p","p","p","p","p","p","p","p"}, {" "," "," "," "," "," "," "," "}, @@ -32,26 +17,30 @@ public class ChessAI { {" "," "," "," "," "," "," "," "}, {"P","P","P","P","P","P","P","P"}, {"R","K","B","Q","A","B","K","R"}}; - public static void main(String[] args) { + + static int kingpositionC,kingpositionL; + public static void main(String[] args) { + // TODO code application logic here /* - GUI ui=new GUI(); - JFrame f=new JFrame("ChessAI"); - f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - f.add(ui); - f.setSize(500, 500); - f.setVisible(true); - */ + GUI ui=new GUI(); + JFrame f=new JFrame("ChessAI"); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.add(ui); + f.setSize(500, 500); + f.setVisible(true); ++ */ +System.out.println(possibleMoves()); } /* - MOVE GENERATION - The following method returns a string which tells the next best possible move - String Format : r1 c1 r2 c2 CAPTURED-PIECE - This means that the chess piece moves from Row1 , Column1 to Row2, Column2 which captured the stated CAPTURED-PIECE - a space means no capture - Example : "1234b" represents row1,column2 moves to row3 column4 and capture b - */ ++ MOVE GENERATION ++ The following method returns a string which tells the next best possible move ++ String Format : r1 c1 r2 c2 CAPTURED-PIECE ++ This means that the chess piece moves from Row1 , Column1 to Row2, Column2 which captured the stated CAPTURED-PIECE ++ a space means no capture ++ Example : "1234b" represents row1,column2 moves to row3 column4 and capture b ++ */ public static String possibleMoves() { String move=""; for(int i=0;i<64;i++){ @@ -74,26 +63,65 @@ public static String possibleMoves() { } public static String possibleMoves_Pawn(int i) { - return ""; + String list=""; + return list; } public static String possibleMoves_Rook(int i) { - return ""; - } - + String list=""; + return list; + } + public static String possibleMoves_Knight(int i) { - return ""; + String list=""; + return list; } public static String possibleMoves_Bishop(int i) { - return ""; + String list=""; + return list; } public static String possibleMoves_King(int i) { - return ""; + String list=""; + return list; } public static String possibleMoves_Queen(int i) { - return ""; + String list="",oldpiece; + int r=i/8,c=i%8; + for(int j=0;j<9;j++) + { + if(j!=4) + { try{ + if(Character.isLower(chessBoard[r-1+j/3][c-1+j%3])||" ".equals(chessBoard[r-1+j/3][c-1+j%3])) + { + oldpiece=chessBoard[r-1+j/3][c-1+j%3]; + chessboard[r][c]=" "; + chessBoard[r-1+j/3][c-1+j%3]="A"; + int kingtemp=kingpositionC; + kingpositionC=i+(j/3)*8 +j%3-9; + if(iskingsafe()){ + list=list+r+c+(r-1+j/3)+(c-1+j%3)+oldpiece; + } + chessboard[r][c]="A"; + chessBoard[r-1+j/3][c-1+j%3]=oldpiece; + kingpositionC=kingtemp; + } + }catch(Exception e){} + } + } + //need to add casting later; + return list; + } + + + +public static boolean iskingsafe() + { + return true; + } + + } From d462cd1a87c6295060b58292ac64b79d9356708d Mon Sep 17 00:00:00 2001 From: Ashutosh Maurya Date: Wed, 21 Mar 2018 23:24:51 +0530 Subject: [PATCH 2/5] Update ChessAI.java --- src/ChessAI.java | 208 +++++++++++++++++++++++++---------------------- 1 file changed, 111 insertions(+), 97 deletions(-) diff --git a/src/ChessAI.java b/src/ChessAI.java index 4acf34e..17ef013 100755 --- a/src/ChessAI.java +++ b/src/ChessAI.java @@ -1,127 +1,141 @@ -import UserInterface.GUI; //API for Chess GUI -import javax.swing.*; //Java Swing Library /** * - * @author Aman Verma + * @author ashutosh maurya + * @author aman verma */ +import UserInterface.GUI; //API for Chess GUI +import javax.swing.*; //Java Swing Library + public class ChessAI { + + /** + * @param args + * @param i + * @return + */ + + /* The Chess Board is represented by a 8*8 2D Array + r/R : Rook + k/K : King + b/B : Bishop + q/Q : Queen + a/A : King + p/P : Pawn - - static String chessBoard[][] = { - {"r","k","b","q","a","b","k","r"}, - {"p","p","p","p","p","p","p","p"}, - {" "," "," "," "," "," "," "," "}, - {" "," "," "," "," "," "," "," "}, - {" "," "," "," "," "," "," "," "}, - {" "," "," "," "," "," "," "," "}, - {"P","P","P","P","P","P","P","P"}, - {"R","K","B","Q","A","B","K","R"}}; + Future Task : Bitboard can be used to implement the chess board, + which is much faster than a 2-D array + */ + static int kingPosL, kingPosC; + static String chessBoard[][] = { + {"r", "k", "b", "q", "a", "b", "k", "r"}, + {"p", "p", "p", "p", "p", "p", "p", "p"}, + {" ", " ", " ", " ", " ", " ", " ", " "}, + {" ", " ", " ", " ", " ", " ", " ", " "}, + {" ", " ", " ", " ", " ", " ", " ", " "}, + {" ", " ", " ", " ", " ", " ", " ", " "}, + {"P", "P", "P", "P", "P", "P", "P", "P"}, + {"R", "K", "B", "Q", "A", "B", "K", "R"}}; - static int kingpositionC,kingpositionL; - public static void main(String[] args) { - // TODO code application logic here - /* - GUI ui=new GUI(); - JFrame f=new JFrame("ChessAI"); - f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - f.add(ui); - f.setSize(500, 500); - f.setVisible(true); -+ */ -System.out.println(possibleMoves()); + public static void main(String[] args) { + + /* GUI ui = new GUI(); + JFrame f = new JFrame("ChessAI"); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.add(ui); + f.setSize(500, 500); + f.setVisible(true); */ + System.out.println(possibleMoves()); } - + /* -+ MOVE GENERATION -+ The following method returns a string which tells the next best possible move -+ String Format : r1 c1 r2 c2 CAPTURED-PIECE -+ This means that the chess piece moves from Row1 , Column1 to Row2, Column2 which captured the stated CAPTURED-PIECE -+ a space means no capture -+ Example : "1234b" represents row1,column2 moves to row3 column4 and capture b -+ */ + MOVE GENERATION + The following method returns a string which tells the next best possible move + String Format : r1 c1 r2 c2 CAPTURED-PIECE + This means that the chess piece moves from Row1 , Column1 to Row2, Column2 + which captured the stated CAPTURED-PIECE + a space means no capture + Example : "1234b" represents row1,column2 moves to row3 column4 and capture b + */ public static String possibleMoves() { - String move=""; - for(int i=0;i<64;i++){ - switch(chessBoard[i/8][i%8]) { - case "P" : move+=possibleMoves_Pawn(i); + String move = ""; + for (int i = 0; i < 64; i++) { + switch (chessBoard[i / 8][i % 8]) { + case "P": + move += possibleMovesPawn(i); break; - case "R" : move+=possibleMoves_Rook(i); + case "R": + move += possibleMovesRook(i); break; - case "K" : move+=possibleMoves_Knight(i); + case "K": + move += possibleMovesKnight(i); break; - case "B" : move+=possibleMoves_Bishop(i); + case "B": + move += possibleMovesBishop(i); break; - case "Q" : move+=possibleMoves_Queen(i); + case "Q": + move += possibleMovesQueen(i); break; - case "A" : move+=possibleMoves_King(i); + case "A": + move += possibleMovesKing(i); break; } } return move; } - - public static String possibleMoves_Pawn(int i) { - String list=""; - return list; + + public static String possibleMovesPawn(int i) { + return ""; } - - public static String possibleMoves_Rook(int i) { - String list=""; - return list; - } - - public static String possibleMoves_Knight(int i) { - String list=""; - return list; + + public static String possibleMovesRook(int i) { + return ""; } - - public static String possibleMoves_Bishop(int i) { - String list=""; - return list; + + public static String possibleMovesKnight(int i) { + return ""; } - - public static String possibleMoves_King(int i) { - String list=""; - return list; + + public static String possibleMovesBishop(int i) { + return ""; } - - public static String possibleMoves_Queen(int i) { - String list="",oldpiece; - int r=i/8,c=i%8; - for(int j=0;j<9;j++) - { - if(j!=4) - { try{ - if(Character.isLower(chessBoard[r-1+j/3][c-1+j%3])||" ".equals(chessBoard[r-1+j/3][c-1+j%3])) - { - oldpiece=chessBoard[r-1+j/3][c-1+j%3]; - chessboard[r][c]=" "; - chessBoard[r-1+j/3][c-1+j%3]="A"; - int kingtemp=kingpositionC; - kingpositionC=i+(j/3)*8 +j%3-9; - if(iskingsafe()){ - list=list+r+c+(r-1+j/3)+(c-1+j%3)+oldpiece; - } - chessboard[r][c]="A"; - chessBoard[r-1+j/3][c-1+j%3]=oldpiece; - kingpositionC=kingtemp; - } - }catch(Exception e){} - } - } - //need to add casting later; + + public static String possibleMovesKing(int i) { + String list = "", oldPiece; + int r = i / 8, c = i % 8; + for (int j = 0; j < 9; j++) { + if (j != 4) { + try { + String ch = chessBoard[r - 1 + j / 3][c - 1 + j % 3]; + if (Character.isLowerCase(ch.charAt(0)) || " ".equals(ch)) { + oldPiece = ch; + chessBoard[r][c] = " "; + chessBoard[r - 1 + j / 3][c - 1 + j % 3] = "A"; + int temp = kingPosC; + kingPosC = i + (j / 3) * 8 + j % 3 - 9; + if (isKingSafe()) { + list = list + Integer.toString(r) + Integer.toString(c) + + Integer.toString(r - 1 + j / 3) + + Integer.toString(c - 1 + j % 3) + + oldPiece; + } + chessBoard[r][c] = "A"; + chessBoard[r - 1 + j / 3][c - 1 + j % 3] = oldPiece; + kingPosC = temp; + } + } catch (Exception e) { + /* Do Nothing */ } + } + } return list; + } + public static boolean isKingSafe() { + return true; } - - -public static boolean iskingsafe() - { - return true; - } - - + public static String possibleMovesQueen(int i) { + return ""; + } } From 1e30d57180721cee9e5ad448988970e0d594640e Mon Sep 17 00:00:00 2001 From: AmanV26 <35691065+AmanV26@users.noreply.github.com> Date: Thu, 22 Mar 2018 03:30:56 +0530 Subject: [PATCH 3/5] Queen moves added --- src/ChessAI.java | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/ChessAI.java b/src/ChessAI.java index 17ef013..5895c52 100755 --- a/src/ChessAI.java +++ b/src/ChessAI.java @@ -136,6 +136,45 @@ public static boolean isKingSafe() { } public static String possibleMovesQueen(int i) { - return ""; + String list="",oldPiece; + int r=i/8,c=i%8; + int temp=1; + for(int j=-1;j<=1;j++) + { + for(int k=1;k<=1;k++) + { + try + { + while(" ".equals(chessBoard[r+temp*j][c+temp*k])) + { + oldPiece=chessBoard[r+temp*j][c+temp*k]; + chessBoard[r][c]=" "; + chessBoard[r+temp*j][c+temp*k]="Q"; + if(isKingSafe()) + { + list=list+r+c+(r+temp*j)+(c+temp*k)+oldPiece; + } + chessBoard[r][c]="Q"; + chessBoard[r+temp*j][c+temp*k]=oldPiece; + temp++; + } + if(Character.isLowerCase(chessBoard[r+temp*j][c+temp*k].charAt(0))) + { + oldPiece=chessBoard[r+temp*j][c+temp*k]; + chessBoard[r][c]=" "; + chessBoard[r+temp*j][c+temp*k]="Q"; + if(isKingSafe()) + { + list=list+r+c+(r+temp*j)+(c+temp*k)+oldPiece; + } + chessBoard[r][c]="Q"; + chessBoard[r+temp*j][c+temp*k]=oldPiece; + + } + }catch(Exception e){} + temp=1; + } + } + return list; } } From ec8e1bb5dcafdeaf6bd7b83f720ce94414d96c25 Mon Sep 17 00:00:00 2001 From: AmanV26 <35691065+AmanV26@users.noreply.github.com> Date: Thu, 22 Mar 2018 03:33:13 +0530 Subject: [PATCH 4/5] Queen moves updated --- src/ChessAI.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ChessAI.java b/src/ChessAI.java index 5895c52..c58aba9 100755 --- a/src/ChessAI.java +++ b/src/ChessAI.java @@ -141,7 +141,7 @@ public static String possibleMovesQueen(int i) { int temp=1; for(int j=-1;j<=1;j++) { - for(int k=1;k<=1;k++) + for(int k=-1;k<=1;k++) { try { From 9742bde644d628aefaeb5302843760418f59d92d Mon Sep 17 00:00:00 2001 From: AmanV26 <35691065+AmanV26@users.noreply.github.com> Date: Wed, 28 Mar 2018 02:09:03 +0530 Subject: [PATCH 5/5] King Safety added --- src/ChessAI.java | 300 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 250 insertions(+), 50 deletions(-) diff --git a/src/ChessAI.java b/src/ChessAI.java index c58aba9..3f573dc 100755 --- a/src/ChessAI.java +++ b/src/ChessAI.java @@ -23,10 +23,10 @@ public class ChessAI { a/A : King p/P : Pawn - Future Task : Bitboard can be used to implement the chess board, + Idea : Bitboard can be used to implement the chess board, which is much faster than a 2-D array */ - static int kingPosL, kingPosC; + static int kingPosL, kingPosC,kingSafeC; static String chessBoard[][] = { {"r", "k", "b", "q", "a", "b", "k", "r"}, {"p", "p", "p", "p", "p", "p", "p", "p"}, @@ -39,6 +39,8 @@ public class ChessAI { public static void main(String[] args) { +while (!"A".equals(chessBoard[kingPosC/8][kingPosC%8])) {kingPosC++;}//get King's location + while (!"a".equals(chessBoard[kingPosL/8][kingPosL%8])) {kingPosL++;}//get king's location /* GUI ui = new GUI(); JFrame f = new JFrame("ChessAI"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -89,16 +91,183 @@ public static String possibleMovesPawn(int i) { return ""; } - public static String possibleMovesRook(int i) { - return ""; + public static String possibleMovesKnight(int i) { + String list = "", oldPiece; + int r = i / 8, c = i % 8; + for (int j = -1; j <= 1; j += 2) { + for (int k = -1; k <= 1; k += 2) { + try { + if (" ".equals(chessBoard[r + j][c + k * 2]) + || Character.isLowerCase(chessBoard[r + j][c + k * 2].charAt(0))) { + oldPiece = chessBoard[r + j][c + k * 2]; + chessBoard[r][c] = " "; + chessBoard[r + j][c + k * 2] = "K"; + if (isKingSafe()) { + list = list + r + c + (r + j) + (c + k * 2) + oldPiece; + } + chessBoard[r][c] = "K"; + chessBoard[r + j][c + k * 2] = oldPiece; + } + } catch (Exception e) { + } + try { + if (" ".equals(chessBoard[r + j * 2][c + k]) + || Character.isLowerCase(chessBoard[r + j * 2][c + k].charAt(0))) { + oldPiece = chessBoard[r + j * 2][c + k]; + chessBoard[r][c] = " "; + chessBoard[r + j * 2][c + k] = "K"; + if (isKingSafe()) { + list = list + r + c + (r + j * 2) + (c + k) + oldPiece; + } + chessBoard[r][c] = "K"; + chessBoard[r + j * 2][c + k] = oldPiece; + } + } catch (Exception e) { + } + } + } + return list; } - public static String possibleMovesKnight(int i) { - return ""; + public static String possibleMovesRook(int i) { + String list = "", oldPiece; + int r = i / 8, c = i % 8; + int temp = 1; + for (int j = -1; j <= 1; j += 2) { + try { + while (" ".equals(chessBoard[r][c + temp * j])) { + oldPiece = chessBoard[r][c + temp * j]; + chessBoard[r][c] = " "; + chessBoard[r][c + temp * j] = "R"; + if (isKingSafe()) { + list = list + r + c + (r) + (c + temp * j) + oldPiece; + } + chessBoard[r][c] = "R"; + chessBoard[r][c + temp * j] = oldPiece; + temp++; + } + if (Character.isLowerCase(chessBoard[r][c + temp * j].charAt(0))) { + oldPiece = chessBoard[r][c + temp * j]; + chessBoard[r][c] = " "; + chessBoard[r][c + temp * j] = "R"; + if (isKingSafe()) { + list = list + r + c + (r) + (c + temp * j) + oldPiece; + } + chessBoard[r][c] = "R"; + chessBoard[r][c + temp * j] = oldPiece; + + } + } catch (Exception e) { + /*Do Nothing*/ } + // Now reset temp to 1 + temp = 1; + try { + while (" ".equals(chessBoard[r + temp * j][c])) { + oldPiece = chessBoard[r + temp * j][c]; + chessBoard[r][c] = " "; + chessBoard[r + temp * j][c] = "R"; + if (isKingSafe()) { + list = list + r + c + (r + temp * j) + (c) + oldPiece; + } + chessBoard[r][c] = "R"; + chessBoard[r + temp * j][c] = oldPiece; + temp++; + } + if (Character.isLowerCase(chessBoard[r + temp * j][c].charAt(0))) { + oldPiece = chessBoard[r + temp * j][c]; + chessBoard[r][c] = " "; + chessBoard[r + temp * j][c] = "R"; + if (isKingSafe()) { + list = list + r + c + (r + temp * j) + (c) + oldPiece; + } + chessBoard[r][c] = "R"; + chessBoard[r + temp * j][c] = oldPiece; + + } + } catch (Exception e) { + /*Do Nothing*/ } + // Now reset temp to 1 + temp = 1; + } + return list; + } public static String possibleMovesBishop(int i) { - return ""; + String list = "", oldPiece; + int r = i / 8, c = i % 8; + int temp = 1; + for (int j = -1; j <= 1; j += 2) { + for (int k = -1; k <= 1; k += 2) { + try { + while (" ".equals(chessBoard[r + temp * j][c + temp * k])) { + oldPiece = chessBoard[r + temp * j][c + temp * k]; + chessBoard[r][c] = " "; + chessBoard[r + temp * j][c + temp * k] = "B"; + if (isKingSafe()) { + list = list + r + c + (r + temp * j) + (c + temp * k) + oldPiece; + } + chessBoard[r][c] = "B"; + chessBoard[r + temp * j][c + temp * k] = oldPiece; + temp++; + } + if (Character.isLowerCase(chessBoard[r + temp * j][c + temp * k].charAt(0))) { + oldPiece = chessBoard[r + temp * j][c + temp * k]; + chessBoard[r][c] = " "; + chessBoard[r + temp * j][c + temp * k] = "B"; + if (isKingSafe()) { + list = list + r + c + (r + temp * j) + (c + temp * k) + oldPiece; + } + chessBoard[r][c] = "B"; + chessBoard[r + temp * j][c + temp * k] = oldPiece; + + } + } catch (Exception e) { + /*Do Nothing*/ } + // Now reset temp to 1 + temp = 1; + } + } + return list; + } + + public static String possibleMovesQueen(int i) { + String list = "", oldPiece; + int r = i / 8, c = i % 8; + int temp = 1; + for (int j = -1; j <= 1; j++) { + for (int k = -1; k <= 1; k++) { + if (j != 0 || k != 0) { + try { + while (" ".equals(chessBoard[r + temp * j][c + temp * k])) { + oldPiece = chessBoard[r + temp * j][c + temp * k]; + chessBoard[r][c] = " "; + chessBoard[r + temp * j][c + temp * k] = "Q"; + if (isKingSafe()) { + list = list + r + c + (r + temp * j) + (c + temp * k) + oldPiece; + } + chessBoard[r][c] = "Q"; + chessBoard[r + temp * j][c + temp * k] = oldPiece; + temp++; + } + if (Character.isLowerCase(chessBoard[r + temp * j][c + temp * k].charAt(0))) { + oldPiece = chessBoard[r + temp * j][c + temp * k]; + chessBoard[r][c] = " "; + chessBoard[r + temp * j][c + temp * k] = "Q"; + if (isKingSafe()) { + list = list + r + c + (r + temp * j) + (c + temp * k) + oldPiece; + } + chessBoard[r][c] = "Q"; + chessBoard[r + temp * j][c + temp * k] = oldPiece; + + } + } catch (Exception e) { + /*Do Nothing*/ } + temp = 1; + } + } + } + return list; } public static String possibleMovesKing(int i) { @@ -132,49 +301,80 @@ public static String possibleMovesKing(int i) { } public static boolean isKingSafe() { - return true; - } - - public static String possibleMovesQueen(int i) { - String list="",oldPiece; - int r=i/8,c=i%8; + //bishop/queen int temp=1; - for(int j=-1;j<=1;j++) - { - for(int k=-1;k<=1;k++) - { - try - { - while(" ".equals(chessBoard[r+temp*j][c+temp*k])) - { - oldPiece=chessBoard[r+temp*j][c+temp*k]; - chessBoard[r][c]=" "; - chessBoard[r+temp*j][c+temp*k]="Q"; - if(isKingSafe()) - { - list=list+r+c+(r+temp*j)+(c+temp*k)+oldPiece; - } - chessBoard[r][c]="Q"; - chessBoard[r+temp*j][c+temp*k]=oldPiece; - temp++; - } - if(Character.isLowerCase(chessBoard[r+temp*j][c+temp*k].charAt(0))) - { - oldPiece=chessBoard[r+temp*j][c+temp*k]; - chessBoard[r][c]=" "; - chessBoard[r+temp*j][c+temp*k]="Q"; - if(isKingSafe()) - { - list=list+r+c+(r+temp*j)+(c+temp*k)+oldPiece; - } - chessBoard[r][c]="Q"; - chessBoard[r+temp*j][c+temp*k]=oldPiece; - - } - }catch(Exception e){} - temp=1; - } - } - return list; + for (int i=-1; i<=1; i+=2) { + for (int j=-1; j<=1; j+=2) { + try { + while(" ".equals(chessBoard[kingPosC/8+temp*i][kingPosC%8+temp*j])) {temp++;} + if ("b".equals(chessBoard[kingPosC/8+temp*i][kingPosC%8+temp*j]) || + "q".equals(chessBoard[kingPosC/8+temp*i][kingPosC%8+temp*j])) { + return false; + } + } catch (Exception e) {} + temp=1; + } + } + //rook/queen + for (int i=-1; i<=1; i+=2) { + try { + while(" ".equals(chessBoard[kingPosC/8][kingPosC%8+temp*i])) {temp++;} + if ("r".equals(chessBoard[kingPosC/8][kingPos%8+temp*i]) || + "q".equals(chessBoard[kingPosC/8][kingPosC%8+temp*i])) { + return false; + } + } catch (Exception e) {} + temp=1; + try { + while(" ".equals(chessBoard[kingPosC/8+temp*i][kingPosC%8])) {temp++;} + if ("r".equals(chessBoard[kingPosC/8+temp*i][kingPosC%8]) || + "q".equals(chessBoard[kingPosC/8+temp*i][kingPosC%8])) { + return false; + } + } catch (Exception e) {} + temp=1; + } + //knight + for (int i=-1; i<=1; i+=2) { + for (int j=-1; j<=1; j+=2) { + try { + if ("k".equals(chessBoard[kingPosC/8+i][kingPosC%8+j*2])) { + return false; + } + } catch (Exception e) {} + try { + if ("k".equals(chessBoard[kingPosC/8+i*2][kingPosC%8+j])) { + return false; + } + } catch (Exception e) {} + } + } + //pawn + if (kingPositionC>=16) { + try { + if ("p".equals(chessBoard[kingPosC/80-1][kingPosC%8-1])) { + return false; + } + } catch (Exception e) {} + try { + if ("p".equals(chessBoard[kingPosC/80-1][kingPosC%8+1])) { + return false; + } + } catch (Exception e) {} + //king + for (int i=-1; i<=1; i++) { + for (int j=-1; j<=1; j++) { + if (i!=0 || j!=0) { + try { + if ("a".equals(chessBoard[kingPosC/8+i][kingPosC%8+j])) { + return false; + } + } catch (Exception e) {} + } + } + } + } + + return true; } }