I am trying to build a chess engine as a long time project. Currently I am working on a method to flipp the board (like turning it around but changing colors as well). Uppercase letters represent white pieces, lowercaser letters black ones. But it seems like java is overwriting my temp variable even though I am not assigning any value to it, after the initial initialisation. As seen in the System.out.println: "r" <- first output; "R" <- second output
I am quite new to JAVA and think that the problem is caused when assigning the value of a static variable to a temporary variable. In my eyes should the rest of the code work fine.
public class chess{
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"}};
}
public static void flipBoard() {
String temp[][]=chessBoard;
System.out.println(temp[0][0]);
for(int i=0;i<64;i++){
int r=i/8, c=i%8;
chessBoard[r][c]=temp[7-r][7-c];
}
System.out.println(temp[0][0]);
}
I am expecting:
chessBoard[][]={
{"R","K","B","A","Q","B","K","R"},
{"P"," ","P","P","P","P","P","P"},
{" ","P"," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{"p","p","p","p","p","p","p","p"},
{"r","k","b","a","q","b","k","r"}};
But I am getting:
chessBoard[][]={
{"R","K","B","A","Q","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"}};
As you can see, all pieces are white now. I am really losing my mind about that and any help is greatly appreciated!