1

I am making an advanced chess engine for fun. I would like to use bit boards to monitor the positions of my pieces, however, the thing isn't working. I have written the method to convert a string of binary digits into the numerical representation so it can be stored in a long. I have verified that that part of the program works. My problem is with the to array method, which returns an array missing a rook instead of a filled array.

This is the result I expect when I call the method:

[ r, n, b, q, k, b, n, r]
[ p, p, p, p, p, p, p, p]
[  ,  ,  ,  ,  ,  ,  ,  ]
[  ,  ,  ,  ,  ,  ,  ,  ]
[  ,  ,  ,  ,  ,  ,  ,  ]
[  ,  ,  ,  ,  ,  ,  ,  ]
[ P, P, P, P, P, P, P, P]
[ R, N, B, Q, K, B, N, R]

This is what I get:

[r, n, b, q, k, b, n, r]
[p, p, p, p, p, p, p, p]
[ ,  ,  ,  ,  ,  ,  ,  ]
[ ,  ,  ,  ,  ,  ,  ,  ]
[ ,  ,  ,  ,  ,  ,  ,  ]
[ ,  ,  ,  ,  ,  ,  ,  ]
[P, P, P, P, P, P, P, P]
[ , N, B, Q, K, B, N, R] 

Here is the source code of the chess class:

package chess2;
import com.google.common.base.*;
import java.util.*;

public class Chess {
    private static long WR = 0L;
    private static long WN = 0L;
    private static long WB = 0L;
    private static long WQ = 0L;
    private static long WK = 0L;
    private static long WP = 0L;
    private static long BR = 0L;
    private static long BN = 0L;
    private static long BB = 0L;
    private static long BQ = 0L;
    private static long BK = 0L;
    private static long BP = 0L;
    private static double time;
    private static Scanner read;
    //private static long EP = 0L;

    public Chess(int id) {
        String wR = "1000000100000000000000000000000000000000000000000000000000000000";
        String wN = "0100001000000000000000000000000000000000000000000000000000000000";
        String wB = "0010010000000000000000000000000000000000000000000000000000000000";
        String wQ = "0000100000000000000000000000000000000000000000000000000000000000";
        String wK = "0001000000000000000000000000000000000000000000000000000000000000";
        String wP = "0000000011111111000000000000000000000000000000000000000000000000";
        String bR = "0000000000000000000000000000000000000000000000000000000010000001";
        String bN = "0000000000000000000000000000000000000000000000000000000001000010";
        String bB = "0000000000000000000000000000000000000000000000000000000000100100";
        String bQ = "0000000000000000000000000000000000000000000000000000000000001000";
        String bK = "0000000000000000000000000000000000000000000000000000000000010000";
        String bP = "0000000000000000000000000000000000000000000000001111111100000000";
        String time1 = stringToLong(wR, wN, wB, wQ, wK, wP, bR, bN, bB, bK, bQ, bP);
        read = new Scanner(time1);
        time = read.nextDouble();
    }

    public static String stringToLong(String wR2, String wN2, String wB2,
            String wQ2, String wK2, String wP2, String bR2, String bN2,
            String bB2, String bK2, String bQ2, String bP2) {
        Stopwatch timer = Stopwatch.createUnstarted();
        timer.start();
        WR = stringToLong(wR2);
        WN = stringToLong(wN2);
        WB = stringToLong(wB2);
        WQ = stringToLong(wQ2);
        WK = stringToLong(wK2);
        WP = stringToLong(wP2);
        BR = stringToLong(bR2);
        BN = stringToLong(bN2);
        BB = stringToLong(bB2);
        BQ = stringToLong(bQ2);
        BK = stringToLong(bK2);
        BP = stringToLong(bP2);
        String time = timer.stop().toString();
        return time;
    }

    public static long stringToLong(String Binary) {
        if (Binary.charAt(0) =='0') { //not going to be a negative number
            return Long.parseLong(Binary, 2);
        } 
        else { //is negative
            return Long.parseLong("1" + Binary.substring(2), 2) * 2;
        }
    }

    public double getTime() {
        return time;
    }

    public String toString() {
        return ("" + WR + " \n" + WN + " \n" + WB + " \n" + WQ + " \n" + WK + " \n" + WP + " \n"+
                   + BR + " \n" + BN + " \n" + BB + " \n" + BQ + " \n" + BK + " \n" + BP + " \n");
    }

    public String[][] toArray() {
        String chessBoard[][] = new String[8][8];
        for (int i = 0; i < 64; i++) {
            chessBoard[i / 8][i % 8] = " ";
        }
        for (int i=0;i<64;i++) {
            if (((WP >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="P";
            }
            else if (((WN >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="N";
            }
            else if (((WB >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="B";
            }
            else if (((WR >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="R";
            }
            else if (((WQ >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="Q";
            }
            else if (((WK >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="K";
            }
            else if (((BP >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="p";
            }
            else if (((BN >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="n";
            }
            else if (((BB >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="b";
            }
            else if (((BR >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="r";
            }
            else if (((BQ >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="q";
            }
            else if (((BK >> i) & 1) == 1) {
                chessBoard[i/8][i%8]="k";
            }
        }
        return chessBoard;
    }
}

Any and all help is greatly appreciated!

EDIT*

Sorry, I put the wrong code!

2
  • Don't declare chessBoard as a local variable? Commented Jun 4, 2015 at 3:03
  • @Gosu ,How else should I do it? The chessBoard in toArray() is completely different from the chessBoard in initiateStandardChess(). Commented Jun 4, 2015 at 3:11

1 Answer 1

1

Don't pass in all your long variables like WP, WN, WB etc to your arrayToBitboards function, because if you do then the values you set inside the function will just be the parameter variables and not the member variables in the class.

public static void arrayToBitboards(String[][] chessBoard) {
    ...
}

call:

arrayToBitboards(chessBoard);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.