0

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!

Maxi Kießler
  • 23
  • 1
  • 5
  • 1
    You are making temp reference your static chessboard. As such, they are essentially the same thing. You need to make a new chessboard and populate. String temp[][]=chessBoard;does NOT copy the values to a new object, it just creates a reference to the original chessBoard. – AHungerArtist Feb 04 '19 at 18:37
  • This link might give a deeper understanding: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – AHungerArtist Feb 04 '19 at 18:39
  • or you realy exchange the elements with a swapVariable. – kai Feb 04 '19 at 19:03

2 Answers2

2

String temp[][]=chessBoard; //assigns the chessBoard variable's reference to the temp.

To make a deep copy of the array, Try this

String[][] temp = new String[chessBoard.length][chessBoard[0].length];
Vamsidhar
  • 822
  • 11
  • 24
0

Why not:

public static void flipBoard() {
    for(int r=0;r<4;r++){
       String[] temp = chessBoard[r];          
       chessBoard[r] = chessBoard[7-r];
       chessBoard[7-r] = temp;
    }
}

Or you may want to reverse it along the diagonale:

public static void flipBoard() {
    for(int r=0;r<4;r++){
       for(int c=0;c<8;c++){
          String temp= chessBoard[r][c];        
          chessBoard[r][c] = chessBoard[7-r][7-c];
          chessBoard[7-r][7-c] = temp;
       }
    }
}
kai
  • 894
  • 1
  • 7
  • 15