public int canDefend(int color) { Board b = new Board(); Castle castle = new Castle(color, 0, 0); Pawn pawn = new Pawn(color, 0, 0); Knight knight = new Knight(color, 0, 0); Bishop bishop = new Bishop(color, 0, 0); Queen queen = new Queen(color, 0, 0); int i, j, m, n, f = 0; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { b.setSquare(this.getInfo(i, j), i, j); } } Point p = b.searchKing(color); King king = new King(color, p.x, p.y); for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (b.getInfo(i, j) == 0) { } else if (b.getInfo(i, j) == (color == 1 ? 1 : 7)) { castle.x = i; castle.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (castle.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1 ? 1 : 7), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1 ? 1 : 7), i, j); b.setSquare(f, m, n); } } } } else if (b.getInfo(i, j) == (color == 1 ? 2 : 8)) { knight.x = i; knight.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (knight.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1 ? 2 : 8), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1 ? 2 : 8), i, j); b.setSquare(f, m, n); } } } } else if (b.getInfo(i, j) == (color == 1 ? 3 : 9)) { bishop.x = i; bishop.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (bishop.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1 ? 3 : 9), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1 ? 3 : 9), i, j); b.setSquare(f, m, n); } } } } else if (b.getInfo(i, j) == (color == 1 ? 4 : 10)) { queen.x = i; queen.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (queen.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1 ? 4 : 10), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1 ? 4 : 10), i, j); b.setSquare(f, m, n); } } } } else if (b.getInfo(i, j) == (color == 1 ? 5 : 11)) { for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (king.move(b, m, n) == 1) { f = b.getInfo(m, n); king.x = m; king.y = n; b.setSquare(0, i, j); b.setSquare((color == 1 ? 5 : 11), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1 ? 5 : 11), i, j); b.setSquare(f, m, n); king.x = p.x; king.y = p.y; } } } } else if (b.getInfo(i, j) == (color == 1 ? 6 : 12)) { pawn.x = i; pawn.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (pawn.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1 ? 6 : 12), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1 ? 6 : 12), i, j); b.setSquare(f, m, n); } } } } } } return(0); }
public int isChecked(Board brd2) { int c2, m; if (color == 1) { c2 = 2; } else { c2 = 1; } Board b = brd2; int i, j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { b.setSquare(brd2.getInfo(i, j), i, j); } } Pawn pawn = new Pawn(c2, 0, 0); Castle castle = new Castle(c2, 0, 0); Knight knight = new Knight(c2, 0, 0); Queen queen = new Queen(c2, 0, 0); Bishop bishop = new Bishop(c2, 0, 0); Point p = brd2.searchKing(color); King king = new King(c2, 0, 0); int r = p.x; int s = p.y; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (i == r && j == s) { continue; } m = b.getInfo(i, j); switch (m) { case 0: break; case 1: if (c2 == 1) { castle.x = i; castle.y = j; if (castle.move(b, r, s) == 1) { return(1); } } break; case 2: if (c2 == 1) { knight.x = i; knight.y = j; if (knight.move(b, r, s) == 1) { return(1); } } break; case 3: if (c2 == 1) { bishop.x = i; bishop.y = j; if (bishop.move(b, r, s) == 1) { return(1); } } break; case 4: if (c2 == 1) { queen.x = i; queen.y = j; if (queen.move(b, r, s) == 1) { return(1); } } break; case 5: if (c2 == 2) { king.x = i; king.y = j; if (king.move(b, r, s) == 1) { return(1); } } break; case 6: if (c2 == 1) { pawn.x = i; pawn.y = j; if (pawn.move(b, r, s) == 1) { return(1); } } break; case 7: if (c2 == 2) { castle.x = i; castle.y = j; if (castle.move(b, r, s) == 1) { return(1); } } break; case 8: if (c2 == 2) { knight.x = i; knight.y = j; if (knight.move(b, r, s) == 1) { return(1); } } break; case 9: if (c2 == 2) { bishop.x = i; bishop.y = j; if (bishop.move(b, r, s) == 1) { return(1); } } break; case 10: if (c2 == 2) { queen.x = i; queen.y = j; if (queen.move(b, r, s) == 1) { return(1); } } break; case 11: if (c2 == 2) { king.x = i; king.y = j; if (king.move(b, r, s) == 1) { return(1); } } break; case 12: if (c2 == 2) { pawn.x = i; pawn.y = j; if (pawn.move(b, r, s) == 1) { return(1); } } break; } } } return(0); }