Java:
package games;
import java.util.Scanner;
import javax.swing.*;
import java.util.Arrays;
public class AlphaBetaChess {
static String chessBoard[][]={
{"r","k","b","q","a","b","k","r"}, //Lowercase are black
{"p","p"," ","p","p","p","p","p"},
{" "," "," "," "," "," "," "," "},
{" "," ","p","P"," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{"P","P","P"," ","P","P","P","P"},
{"R"," "," "," ","A"," "," ","R"}}; //Uppercase are white
static int kingPositionC = 60, kingPositionL = 4;
static boolean blackKingHasMoved = false, whiteKingHasMoved=false,whiteRookLeftCornerHasMoved=false, whiteRookRightCornerHasMoved=false,
blackRookLeftCornerHasMoved=false, blackRookRightCornerHasMoved=false, isWhitesTurn=true, whiteKingHasMovedTemp, rookLeftCornerHasMovedTemp2, rookRightCornerHasMovedTemp2;
static int globalDepth=4;
public static void main(String[] args) {
System.out.println(possibleMoves());
makeMove("0717 ");
System.out.println(whiteRookLeftCornerHasMoved);
//System.out.println(Arrays.deepToString(chessBoard));
undoMove("0717 ");
System.out.println(whiteRookLeftCornerHasMoved);
//System.out.println(Arrays.deepToString(chessBoard));
/* JFrame f = new JFrame("Chess Tutorial");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UserInterface ui = new UserInterface();
f.add(ui);
f.setSize(500, 500);
f.setVisible(true);*/
}
public static String alphaBeta(int depth, int beta, int alpha, String move, int player) {
//return in the form of 1234b#########
String list = possibleMoves();
if (depth==0 || list.length()==0) {return move+(rating()*(player*2-1));}
//sort later
player = 1-player; //either 1 or 0
for (int i=0; i<list.length(); i+=5) {
makeMove(list.substring(i, i+5));
flipBoard();
String returnString = alphaBeta(depth-1, beta, alpha, list.substring(i, i+5), player);
int value = Integer.valueOf(returnString.substring(5));
flipBoard();
undoMove(list.substring(i, i+5));
if (player==0) {
if (value<=beta) {beta=value; if(depth == globalDepth) {move = returnString.substring(0, 5);}}
} else {
if (value>alpha) {alpha=value; if(depth == globalDepth) {move = returnString.substring(0, 5);}}
}
if (alpha>=beta) {
if (player==0) {return move+beta;} else {return move+alpha;}
}
}
if (player==0) {return move+beta;} else {return move+alpha;}
}
public static void flipBoard() {
String temp;
for (int i=0; i<32; i++) {
int r = i/8, c = i%8;
if (Character.isUpperCase(chessBoard[r][c].charAt(0))) {
temp = chessBoard[r][c].toLowerCase();
} else {
temp = chessBoard[r][c].toUpperCase();
}
if (Character.isUpperCase(chessBoard[7-r][7-c].charAt(0))) {
chessBoard[r][c] = chessBoard[7-r][7-c].toLowerCase();
} else {
chessBoard[r][c] = chessBoard[7-r][7-c].toUpperCase();
}
chessBoard[7-r][7-c] = temp;
}
int kingTemp = kingPositionC; // Kingposition is temporarily saved
kingPositionC = 63 - kingPositionL;
kingPositionL= 63 - kingTemp;
boolean kingHasMovedTemp = whiteKingHasMoved;
whiteKingHasMoved = blackKingHasMoved;
blackKingHasMoved = kingHasMovedTemp;
boolean rookLeftCornerHasMovedTemp = whiteRookLeftCornerHasMoved;
whiteRookLeftCornerHasMoved = blackRookLeftCornerHasMoved;
blackRookLeftCornerHasMoved = rookLeftCornerHasMovedTemp;
boolean rookRightCornerHasMovedTemp = whiteRookRightCornerHasMoved;
whiteRookRightCornerHasMoved = blackRookRightCornerHasMoved;
blackRookRightCornerHasMoved = rookRightCornerHasMovedTemp;
}
public static void makeMove(String move) {
if (move.charAt(4)!='P' && move.charAt(4)!='E' && move.charAt(4)!='C') {
chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))] = chessBoard[Character.getNumericValue(move.charAt(1))][Character.getNumericValue(move.charAt(0))];
chessBoard[Character.getNumericValue(move.charAt(1))][Character.getNumericValue(move.charAt(0))] = " ";
if ("A".equals(chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))])) { //Updating KingpositionC after a Kingmove is made
kingPositionC=8*Character.getNumericValue(move.charAt(3))+Character.getNumericValue(move.charAt(2));
whiteKingHasMovedTemp = whiteKingHasMoved;
whiteKingHasMoved = true;
}
if ("R".equals(chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))]) && (" ".equals(chessBoard[7][7].charAt(0)))) { //Updating Rookhasmoved after a rookmove is made
rookRightCornerHasMovedTemp2 = whiteRookRightCornerHasMoved;
whiteRookRightCornerHasMoved = true;
}
if ("R".equals(chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))]) && (" ".equals(chessBoard[0][0].charAt(0)))) { //Updating Rookhasmoved after a rookmove is made
rookLeftCornerHasMovedTemp2 = whiteRookLeftCornerHasMoved;
whiteRookLeftCornerHasMoved = true;
}
if (move.charAt(4)=='r' && chessBoard[0][0].equals(chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))])) { //Updating Variables when enemy rook is taken
rookRightCornerHasMovedTemp2 = blackRookRightCornerHasMoved;
blackRookRightCornerHasMoved = true;
}
if (move.charAt(4)=='r' && chessBoard[0][7].equals(chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))])) { //Updating Variables when enemy rook is taken
rookLeftCornerHasMovedTemp2 = blackRookLeftCornerHasMoved;
blackRookLeftCornerHasMoved = true;
}
}
else if (move.charAt(4)=='P') {
//if pawn promotion
chessBoard[1][Character.getNumericValue(move.charAt(0))]=" ";
chessBoard[0][Character.getNumericValue(move.charAt(1))]=String.valueOf(move.charAt(3));
} else if (move.charAt(4)=='E') { //fehler
//if en passant
chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))] = "P";
chessBoard[Character.getNumericValue(move.charAt(1))][Character.getNumericValue(move.charAt(0))] = " ";
chessBoard[Character.getNumericValue(move.charAt(1))][Character.getNumericValue(move.charAt(2))] = " ";
} else if (move.charAt(4)=='C' && move.charAt(3)=='S') { //Funktioniert
//if short castle
chessBoard[7][6] = "A";
chessBoard[7][4] = " ";
chessBoard[7][7] = " ";
chessBoard[7][5] = "R";
whiteKingHasMoved = true;
kingPositionC = 62;
} else if (move.charAt(4)=='C' && move.charAt(3)=='L') { //Funktioniert
//if long castle
chessBoard[7][2] = "A";
chessBoard[7][4] = " ";
chessBoard[7][0] = " ";
chessBoard[7][3] = "R";
whiteKingHasMoved = true;
kingPositionC = 58;
}
}
public static void undoMove(String move) {
if (move.charAt(4)!='P' && move.charAt(4)!= 'P' && move.charAt(4) != 'E' && move.charAt(4) != 'C' && move.charAt(4) != 'D') {
chessBoard[Character.getNumericValue(move.charAt(1))][Character.getNumericValue(move.charAt(0))] = chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))];
chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))] = String.valueOf(move.charAt(4));
if ("A".equals(chessBoard[Character.getNumericValue(move.charAt(1))][Character.getNumericValue(move.charAt(0))])) { //Updating KingpositionC after a Kingmove is made
kingPositionC=8*Character.getNumericValue(move.charAt(1))+Character.getNumericValue(move.charAt(0));
whiteKingHasMoved = whiteKingHasMovedTemp;
}
else if (move.charAt(4)=='r' && chessBoard[0][0].equals(chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))])) { //Updating Variables when enemy rook is taken
blackRookRightCornerHasMoved = rookRightCornerHasMovedTemp2;
}
else if (move.charAt(4)=='r' && chessBoard[0][7].equals(chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))])) { //Updating Variables when enemy rook is taken
blackRookLeftCornerHasMoved = rookLeftCornerHasMovedTemp2;
}
else if ("R".equals(chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))]) && " ".equals(chessBoard[0][0])) { //Updating Rookhasmoved after a rookmove is made
whiteRookLeftCornerHasMoved = rookLeftCornerHasMovedTemp2;
}
else if ("R".equals(chessBoard[Character.getNumericValue(move.charAt(3))][Character.getNumericValue(move.charAt(2))]) && " ".equals(chessBoard[7][7])) { //Updating Rookhasmoved after a rookmove is made
whiteRookRightCornerHasMoved = rookRightCornerHasMovedTemp2;
} else if (move.charAt(4)=='P'){
//if pawn promotion
chessBoard[1][Character.getNumericValue(move.charAt(0))] = "P";
chessBoard[0][Character.getNumericValue(move.charAt(1))] = String.valueOf(move.charAt(2));
} else if (move.charAt(4)=='E') { //fehler
//if en passant
chessBoard[Character.getNumericValue(move.charAt(1))][Character.getNumericValue(move.charAt(2))] = "p";
chessBoard[Character.getNumericValue(move.charAt(1))][Character.getNumericValue(move.charAt(0))] = "P";
} else if (move.charAt(4)=='C' && move.charAt(3)=='S') { //Funktioniert
//if short castle
chessBoard[7][5] = " ";
chessBoard[7][7] = "R";
chessBoard[7][6] = " ";
chessBoard[7][4] = "A";
whiteKingHasMoved = false;
} else if (move.charAt(4)=='C' && move.charAt(3)=='L') { //Funktioniert
//if long castle
chessBoard[7][3] = " ";
chessBoard[7][0] = "R";
chessBoard[7][2] = " ";
chessBoard[7][4] = "A";
whiteKingHasMoved = false;
} else if (move.charAt(4)=='D') {
chessBoard[6][Character.getNumericValue(move.charAt(0))] = chessBoard[4][Character.getNumericValue(move.charAt(2))];
chessBoard[4][Character.getNumericValue(move.charAt(2))] = " ";
}
}
}
public static String possibleMoves() {
String list = "";
for (int i=0; i<64; i++) {
switch (chessBoard[i/8][i%8]) {
case "P": list += possibleP(i); //funktioniert
break;
case "R": list += possibleR(i); //funktioniert
break;
case "K": list += possibleK(i); //funktioniert
break;
case "B": list += possibleB(i); //funktioniert
break;
case "Q": list += possibleQ(i); //funktioniert
break;
case "A": list += possibleA(i); //funktioniert
break;
}
}
return list; //Format:sx1,sy1,dx2,dy2,captured piece
}
public static String possibleP(int i) {
String list ="", oldPiece;
int r = i/8, c = i%8;
try {
if (chessBoard[r-1][c].equals(" ") && i>=16) { // Checks if Pawn can move one forward & i >16
oldPiece = " ";
chessBoard[r][c] = " ";
chessBoard[r-1][c] = "P";
if (kingSafe()) {
list = list + c + r + c + (r-1) + oldPiece;
}
chessBoard[r-1][c] = oldPiece;
chessBoard[r][c] = "P";
}
if (r==6 && chessBoard[4][c].equals(" ")) { //Checks if Pawn is on 7th row
chessBoard[r][c] = " ";
chessBoard[4][c] = "P";
if (kingSafe()) {
list = list + c + r + c + 4 + "D";
}
chessBoard[r-2][c] = " ";
chessBoard[r][c] = "P";
}
if (Character.isLowerCase(chessBoard[r-1][c-1].charAt(0)) && i>=16) { //Checks if Pawn can take an enemy piece UND i >16
oldPiece = chessBoard[r-1][c-1];
chessBoard[r][c] = " ";
chessBoard[r-1][c-1] = "P";
if (kingSafe()) {
list = list + c + r + (c-1)+(r-1)+ oldPiece;
}
chessBoard[r-1][c-1] = oldPiece;
chessBoard[r][c] = "P";
}
if (Character.isLowerCase(chessBoard[r-1][c+1].charAt(0)) && i>=16) { //Checks if Pawn can take an enemy piece UND i >16
oldPiece = chessBoard[r-1][c+1];
chessBoard[r][c] = " ";
chessBoard[r-1][c+1] = "P";
if (kingSafe()) {
list = list + c + r + (c+1)+(r-1)+ oldPiece;
}
chessBoard[r-1][c+1] = oldPiece;
chessBoard[r][c] = "P";
}
for (int j=-1; j<=1; j+=2) {
if (Character.isLowerCase(chessBoard[r-1][c+j].charAt(0)) && i<16) { //promotion && capture
String[] temp={"Q","R","B","K"};
for (int k=0; k<4; k++) {
oldPiece=chessBoard[r-1][c+j];
chessBoard[r][c]=" ";
chessBoard[r-1][c+j]=temp[k];
if (kingSafe()) {
//column1,column2,captured-piece,new-piece,P
list=list+c+(c+j)+oldPiece+temp[k]+"P";
}
chessBoard[r][c]="P";
chessBoard[r-1][c+j]=oldPiece;
}
}
}
if (" ".equals(chessBoard[r-1][c]) && i<16) {
String[] temp={"Q","R","B","K"};
for (int k=0; k<4; k++) {
oldPiece=chessBoard[r-1][c];
chessBoard[r][c]=" ";
chessBoard[r-1][c]=temp[k];
if (kingSafe()) {
//column1,column2,captured-piece,new-piece,P
list=list+c+c+oldPiece+temp[k]+"P";
}
chessBoard[r][c]="P";
chessBoard[r-1][c]=oldPiece;
}
}
for (int k=-1; k<=1; k+=2) {
if ("D".equals(list.charAt(list.length() -1)) && r==3 && "p".equals(chessBoard[r][c+1*k]) && chessBoard[r][list.charAt(list.length()-3)].equals(chessBoard[r][c+1*k])) { //eventuell muss das geändert werden
oldPiece = "p";
chessBoard[r][c+1*k] = " ";
chessBoard[r][c] = " ";
chessBoard[r-1][c+1*k] = "P";
if (kingSafe()) {
list = list + c + r + (c+1*k)+ (r-1) + "E";
}
chessBoard[r-1][c+1*k] = " ";
chessBoard[r][c+1*k] = oldPiece;
chessBoard[r][c] = "P";
}
}
} catch (Exception e){}
return list;
}
public static String possibleR(int i) {
String list ="", oldPiece;
int r = i/8, c = i%8;
int temp2 = 1;
for (int l=-1; l<=1; l++) {
for (int m=-1; m<=1; m++) {
if((m*l)!=0) {
continue;
}
int dx = c+m*temp2;
int dy = r+l*temp2;
try {
while(" ".equals(chessBoard[dy][dx])) {
oldPiece = " ";
boolean rookLeftCornerHasMovedTemp = whiteRookLeftCornerHasMoved, rookRightCornerHasMovedTemp = whiteRookRightCornerHasMoved;
chessBoard[r][c] = " ";
if (i==56) {
whiteRookLeftCornerHasMoved = true;
}
if (i==63) {
whiteRookRightCornerHasMoved = true;
}
chessBoard[dy][dx] = "R";
if (kingSafe()) {
list = list + c + r + dx + dy + oldPiece;
}
chessBoard[r][c] = "R";
chessBoard[dy][dx] = oldPiece;
whiteRookLeftCornerHasMoved = rookLeftCornerHasMovedTemp;
whiteRookRightCornerHasMoved = rookRightCornerHasMovedTemp;
temp2++;
dx = c+temp2*m; //destination x
dy = r+temp2*l; //destination y
}
if(Character.isLowerCase(chessBoard[dy][dx].charAt(0))) {
oldPiece = chessBoard[dy][dx];
chessBoard[r][c] = " ";
chessBoard[dy][dx] = "R";
if (kingSafe()) {
list = list + c + r + dx + dy + oldPiece;
}
chessBoard[r][c] = "R";
chessBoard[dy][dx] = oldPiece;
}
} catch (Exception e){}
temp2 = 1;
}
}
return list;
}
public static String possibleK(int i) {
String list ="", oldPiece;
int r = i/8, c = i%8;
try{
for (int x=-2; x<=2; x++){
for(int y=-2; y<=2; y++){
if(Math.abs(x*y) == 2){
if(" ".equals(chessBoard[r+x][c+y]) ||
Character.isLowerCase(chessBoard[r+x][c+y].charAt(0))){
oldPiece = chessBoard[r+x][c+y];
chessBoard[r][c] = " ";
chessBoard[r+x][c+y] = "K";
if(kingSafe()){
list = list + c + r + (c+y) + (r+x) + oldPiece;
}
chessBoard[r][c] = "K";
chessBoard[r+x][c+y] = oldPiece;
}
}
}
}} catch(Exception e){}
return list;
}
public static String possibleB(int i) {
String list ="", oldPiece;
int r = i/8, c = i%8;
int temp2 = 1;
for (int n=-1; n<=1; n++) {
for (int o=-1; o<=1; o++) {
if((n * o)==0) {
continue;
}
int dx = c+o*temp2;
int dy = r+n*temp2;
try {
while(" ".equals(chessBoard[dy][dx])) {
oldPiece = " ";
chessBoard[r][c] = " ";
chessBoard[dy][dx] = "B";
if (kingSafe()) {
list = list + c + r + dx + dy + oldPiece;
}
chessBoard[r][c] = "B";
chessBoard[dy][dx] = oldPiece;
temp2++;
dx = c+temp2*o; //destination x
dy = r+temp2*n; //destination y
}
if(Character.isLowerCase(chessBoard[dy][dx].charAt(0))) {
oldPiece = chessBoard[dy][dx];
chessBoard[r][c] = " ";
chessBoard[dy][dx] = "B";
if (kingSafe()) {
list = list + c + r + dx + dy + oldPiece;
}
chessBoard[r][c] = "B";
chessBoard[dy][dx] = oldPiece;
}
} catch (Exception e){}
temp2 = 1;
}
}
return list;
}
public static String possibleQ(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++) {
int dx = c+temp*k; //destination x
int dy = r+temp*j; //destination y
try {
while(" ".equals(chessBoard[dy][dx])) {
oldPiece = " ";
chessBoard[r][c] = " ";
chessBoard[dy][dx] = "Q";
if (kingSafe()) {
list = list + c + r + dx + dy + oldPiece;
}
chessBoard[r][c] = "Q";
chessBoard[dy][dx] = oldPiece;
temp++;
dx = c+temp*k; //destination x
dy = r+temp*j; //destination y
}
if(Character.isLowerCase(chessBoard[dy][dx].charAt(0))) {
oldPiece = chessBoard[dy][dx];
chessBoard[r][c] = " ";
chessBoard[dy][dx] = "Q";
if (kingSafe()) {
list = list + c + r + dx + dy + oldPiece;
}
chessBoard[r][c] = "Q";
chessBoard[dy][dx] = oldPiece;
}
} catch (Exception e){}
temp = 1;
}
}
return list;
}
public static String possibleA(int i) {
String list ="", oldPiece;
int r = i/8, c = i%8;
for (int j=0; j<9; j++) {
if (j != 4) {
int y = r-1+j/3;
int x = c-1+j%3;
try {
if (Character.isLowerCase(chessBoard[y][x].charAt(0)) || " ".equals(chessBoard[y][x])) {
oldPiece = chessBoard[y][x];
chessBoard[r][c] = " ";
chessBoard[y][x] = "A";
int kingTemp = kingPositionC;
kingPositionC = i+(j/3)*8+j%3-9; //Diese Formel stammt nicht von mir
if (kingSafe()) {
list = list + c + r + x + y + oldPiece;
}
chessBoard[r][c] = "A";
chessBoard[y][x] = oldPiece;
//kingPosition = i ????=????!?!?!?!!?
kingPositionC = kingTemp;
}} catch (Exception e) {}
}
}
if (i==60 && whiteKingHasMoved==false && chessBoard[r][c+1].equals(" ") && chessBoard[r][c+2].equals(" ") && whiteRookRightCornerHasMoved==false && chessBoard[r][c+3].equals("R")) {
chessBoard[r][c] = " ";
chessBoard[r][c+1] = "A";
kingPositionC = 61;
if (kingSafe()) {
chessBoard[r][c+1] = " ";
chessBoard[r][c+2] = "A";
kingPositionC = 62;
if (kingSafe()) {
chessBoard[r][c+3] = " ";
chessBoard[r][c+1] = "R";
list = list + " " + " " + " " + "S" + "C";
}
}
chessBoard[r][c+1] = " ";
chessBoard[r][c+3] = "R";
chessBoard[r][c+2] = " ";
chessBoard[r][c] = "A";
kingPositionC = 60;
}
if (i==60 && whiteKingHasMoved==false && chessBoard[r][c-1].equals(" ") && chessBoard[r][c-2].equals(" ") && chessBoard[r][c-3].equals(" ") && whiteRookLeftCornerHasMoved==false && chessBoard[r][c-4].equals("R"))
{
oldPiece = " ";
chessBoard[r][c] = " ";
chessBoard[r][c-1] = "A";
kingPositionC = 59;
if (kingSafe()) {
chessBoard[r][c-1] = " ";
chessBoard[r][c-2] = "A";
kingPositionC = 58;
if (kingSafe()) {
chessBoard[r][c-4] = " ";
chessBoard[r][c-1] = "R";
list = list + " " + " " + " " + "L" + "C";
}
}
chessBoard[r][c-1] = " ";
chessBoard[r][c-4] = "R";
chessBoard[r][c-2] = " ";
chessBoard[r][c] = "A";
kingPositionC = 60;
}
//need to add castling later
return list;
}
public static int rating() {
return 0;
}
public static boolean kingSafe() {
//bishop/queen
int temp=1;
for (int i=-1; i<=1; i+=2) {
for (int j=-1; j<=1; j+=2) {
try {
while(" ".equals(chessBoard[kingPositionC/8+temp*i][kingPositionC%8+temp*j])) {temp++;}
if ("b".equals(chessBoard[kingPositionC/8+temp*i][kingPositionC%8+temp*j]) ||
"q".equals(chessBoard[kingPositionC/8+temp*i][kingPositionC%8+temp*j])) {
return false;
}
} catch (Exception e) {}
temp=1;
}
}
//rook/queen
for (int i=-1; i<=1; i+=2) {
try {
while(" ".equals(chessBoard[kingPositionC/8][kingPositionC%8+temp*i])) {temp++;}
if ("r".equals(chessBoard[kingPositionC/8][kingPositionC%8+temp*i]) ||
"q".equals(chessBoard[kingPositionC/8][kingPositionC%8+temp*i])) {
return false;
}
} catch (Exception e) {}
temp=1;
try {
while(" ".equals(chessBoard[kingPositionC/8+temp*i][kingPositionC%8])) {temp++;}
if ("r".equals(chessBoard[kingPositionC/8+temp*i][kingPositionC%8]) ||
"q".equals(chessBoard[kingPositionC/8+temp*i][kingPositionC%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[kingPositionC/8+i][kingPositionC%8+j*2])) {
return false;
}
} catch (Exception e) {}
try {
if ("k".equals(chessBoard[kingPositionC/8+i*2][kingPositionC%8+j])) {
return false;
}
} catch (Exception e) {}
}
}
//pawn
if (kingPositionC>=16) {
try {
if ("p".equals(chessBoard[kingPositionC/8-1][kingPositionC%8-1])) {
return false;
}
} catch (Exception e) {}
try {
if ("p".equals(chessBoard[kingPositionC/8-1][kingPositionC%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[kingPositionC/8+i][kingPositionC%8+j])) {
return false;
}
} catch (Exception e) {}
}
}
}
}
return true;
}
}
/* To do: en passent auskommentieren (5 stelliges Format und Endung auf 'E')
*
* Rochade: 5 Stelliges Format erstellen und Endung auf 'C'
50 Züge regel implementieren
Remis durch 3 malige Stellungswiederholung implementieren
TO DO: BUGS ELIMINIEREN
*
*/
Hi ich weiß, dass das erstmal viel Code ist und auch ein wenig Spaghetticode, aber es ist nur eine Übungsaufgabe, also solange ich etwas dabei lerne ist alles im grünen Bereich.
Der Code ist der Anfang eines Schachprogramms. Bis jetzt ist alles bugfrei, aber jetzt habe ich einen bug in der makeMove() und undoMove()-Methode.
Ich glaube es hängt damit zusammen, dass ich meine If-Anweisungen nicht richtig verschachtelt habe, aber ich saß da schon 2 Stunden dran und verliere so langsam den Überblick.......
Also meine Idee war, dass wenn ich mit weiß einen Turm schlage schwarz das Rochaderecht verliert, abhängig davon welchen Turm ich genau schlage.
Wär nett wenn mir jemand helfen könnte den bug rauszukriegen oder generell Tipps zu if-Verschachtelungen.
lg