gen.c (3013B)
1 #include <stdbool.h> 2 3 #include "bb.h" 4 #include "pos.h" 5 6 /* Todo for writing a move generator: 7 * 8 * Create a method for storing a move 9 * Get the attacks for the pieces 10 * Iterate through each piece and create each move based on their attacks 11 * For pawns, cry 12 * For castling, cry 13 * 14 * Move structure 15 * want a move to be relatively simple to store as we will be making a list of 16 * moves 17 * needed information: 18 * from square 19 * to square 20 * promotion piece 21 * helpful to have information: 22 * whether the move is en passant or castling 23 * maybe if it is castling what type of castling 24 * 25 * from is 6 bits 26 * to is 6 bits 27 * movetype is 2 bits 28 * promotion piece is 2 bits OR castling type is 2 bits! 29 * thats 16 bits total 30 * 00 promotion/castling bits 31 * 00 move type 32 * 000000 to square 33 * 000000 from square 34 */ 35 36 unsigned short 37 storemove(char from, char to, char movetype, char extra) { 38 return from + (to << 6) + (movetype << 12) + (extra << 14); 39 } 40 41 /* When generating moves, we want to make it so that we request for a move, and 42 * the generator gives the next move that needs to be generated. This means 43 * that there is no time wasted generating moves that wont be used, and there 44 * is also no time wasted storing the moves in a list */ 45 46 void 47 gen_pseudo(pos *p) { 48 char from, to; 49 bb our_pieces[6], att, occ, us; 50 51 occ = p->sides[WHITE] | p->sides[BLACK]; 52 us = p->sides[p->turn]; 53 54 for (char pc = PAWN; pc <= KING; pc++) { 55 our_pieces[pc] = p->pieces[pc] & us; 56 } 57 58 while ((from = poplsb(&our_pieces[KNIGHT]))) { 59 att = knight_att(from); 60 while ((to = poplsb(&att))) { 61 /* add to move list somehow */ 62 storemove(from, to, 0, 0); 63 } 64 } 65 66 while ((from = poplsb(&our_pieces[KING]))) { 67 att = king_att(from); 68 while ((to = poplsb(&att))) { 69 /* add to move list somehow */ 70 storemove(from, to, 0, 0); 71 } 72 } 73 74 while ((from = poplsb(&our_pieces[BISHOP]))) { 75 att = bishop_att(from, occ); 76 while ((to = poplsb(&att))) { 77 /* add to move list somehow */ 78 storemove(from, to, 0, 0); 79 } 80 } 81 82 while ((from = poplsb(&our_pieces[ROOK]))) { 83 att = rook_att(from, occ); 84 while ((to = poplsb(&att))) { 85 /* add to move list somehow */ 86 storemove(from, to, 0, 0); 87 } 88 } 89 90 while ((from = poplsb(&our_pieces[QUEEN]))) { 91 att = queen_att(from, occ); 92 while ((to = poplsb(&att))) { 93 /* add to move list somehow */ 94 storemove(from, to, 0, 0); 95 } 96 } 97 98 /* Generate castle */ 99 100 /* Generate pawn moves */ 101 102 bb push1, push2, leftatt, rightatt; 103 104 push1 = our_pieces[PAWN] << 8 & ~occ; 105 push2 = (push1 & 0xFF0000) << 8 & ~occ; 106 leftatt = (our_pieces[PAWN] & 0x7F7F7F7F7F7F7F7F) << 9 & ~us; 107 rightatt = (our_pieces[PAWN] & 0xFEFEFEFEFEFEFEFE) << 7 & ~us; 108 109 /* Account for promotion too! */ 110 while ((to = poplsb(&push1))) { 111 storemove(to-8, to, 0, 0); 112 } 113 while ((to = poplsb(&push2))) { 114 storemove(to-16, to, 0, 0); 115 } 116 while ((to = poplsb(&leftatt))) { 117 storemove(to-9, to, 0, 0); 118 } 119 while ((to = poplsb(&rightatt))) { 120 storemove(to-7, to, 0, 0); 121 } 122 123 /* En passant */ 124 125 }