2016-05-07 10 views
2

Ich initialisiere Bitboards für die Schachprogrammierung. Wenn ich jedoch die schwarzen Bitboards initialisiere, ist die binäre Ausgabe aus irgendeinem Grund nicht korrekt. Bitboard: Hexadezimal-Binär-Konvertierung

Im folgenden ist der Code:

#include <stdint.h> 
#include <inttypes.h> 
#include <sstream> 
#include <bitset> 
#include <string> 

uint64_t wpawn, wknight, wbishop, wrook, wqueen, wking, bpawn, bknight, bbishop, brook, bqueen, bking; 

using namespace std; 

void printBoard(uint64_t board)//print binary form of hexadecimal 
{ 
    printf("\n"); 
    stringstream ss; 
    ss<<board; 
    unsigned x; 
    ss>>x; 
    bitset<64>b(x); 
    string tmp = b.to_string(); 
    int count =0; 
    for (int i =0; i < 8; i ++) 
    { 
     printf("\n"); 
     for (int j=0;j <8;j++) 
     { 
      printf("%c ",tmp[count]); 
      count ++; 
     } 
    } 
} 

void printAll()//print all boards 
{ 
    printf("\nwhite\n"); 
    printBoard(wpawn); 
    printBoard(wknight); 
    printBoard(wbishop); 
    printBoard(wrook); 
    printBoard(wqueen); 
    printBoard(wking); 

    printf("\nblack\n"); 
    printBoard(bpawn); 
    printBoard(bknight); 
    printBoard(bbishop); 
    printBoard(brook); 
    printBoard(bqueen); 
    printBoard(bking); 
} 

int main(int argc, char *argv[]) 
{ 
    wpawn = 0x000000000000FF00;//initialize boards 
    wknight=0x0000000000000042; 
    wbishop=0x0000000000000024; 
    wrook = 0x0000000000000081; 
    wqueen =0x0000000000000010; 
    wking = 0x0000000000000008; 

    bpawn = 0x00FF000000000000; 
    bknight=0x4200000000000000; 
    bbishop=0x2400000000000000; 
    brook = 0x8100000000000000; 
    bqueen =0x1000000000000000; 
    bking = 0x0800000000000000; 
    printBoard(wpawn);//white pawn 
    printBoard(bpawn);//black pawn 
} 

Der Ausgang ist wie folgt:

white 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 


Black 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 

Beim Aufruf printAll(), die alle der weißen Bitboards Ausdruck in Ordnung, aber die schwarzen Bitboards alle Ausdrucke wie es der bpaw mit der ersten Hälfte alles gemacht hat. Hat jemand einen Rat, warum dies geschieht?

+1

Auch finde ich keinen Grund, warum Sie "Material" mit 'stringstream' tun, warum nicht nur' bitset <64> b (board) '? – PcAF

Antwort

1

Änderung unsigned x;

zu uint64_t x;