Java Bit Print printBitboard(long bitboard)

Here you can find the source of printBitboard(long bitboard)

Description

Prints a bitboard by sending the result of bitboardToString to System.out.

License

Open Source License

Parameter

Parameter Description
bitboard the bitboard to print.

Declaration

public static void printBitboard(long bitboard) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   ww  w.j  a  v  a2  s . co  m*/
     * Prints a bitboard by sending the result of <code>bitboardToString</code>
     * to <code>System.out</code>.
     * 
     * @param bitboard
     *            the bitboard to print.
     */
    public static void printBitboard(long bitboard) {
        System.out.println(bitboardToString(bitboard));
    }

    /**
     * Converts a <i>bitboard</i>, not a board, to a string. The string will
     * look identical to the result of Board.toString, except that pieces are
     * instead shown as 'X'.
     * 
     * @param bitboard
     *            the bitboard to represent as a string.
     * @return a string representation of the bitboard.
     */
    public static String bitboardToString(long bitboard) {
        String asBinary = Long.toBinaryString(bitboard);

        while (asBinary.length() != 64)
            asBinary = "0" + asBinary;

        String s = "     a   b   c   d   e   f   g   h\n";
        s += "   +---+---+---+---+---+---+---+---+\n 8 | ";

        for (int up = 7; up >= 0; up--) {
            for (int out = 0; out < 8; out++) {
                if (asBinary.charAt(63 - (up * 8 + out)) == '1')
                    s += "X | ";
                else
                    s += "  | ";
            }
            s += (up + 1) + "\n   +---+---+---+---+---+---+---+---+";
            if (up != 0)
                s += "\n " + up + " | ";
        }

        s += "\n     a   b   c   d   e   f   g   h\n";
        return s;
    }
}

Related

  1. printBitBinary(byte[] bytes)
  2. printBitFormat(int number)
  3. printBitLong(long A)
  4. printBits(byte[] bytes)
  5. printBits(int meta)