summaryrefslogtreecommitdiff
path: root/src/search/bishop_moves.c
blob: 207535e1980e1e9422bc6b2b29dce78463af3db2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "search.h"

void __forloop_bishop_moves_gen(
  moves_t* moves,
  bitboard_t friendly_type,
  bitboard_t friendly_pieces,
  bitboard_t enemy_pieces
) {
  while (friendly_type) {
    int from = __builtin_ctzll(friendly_type);
    friendly_type &= friendly_type - 1;

    int og_rank = from / 8; // range: [0, 7]
    int og_file = from % 8; // range: [0, 7]
    int rank = og_rank + 1, file = og_file + 1, index = rank * 8 + file;


    while ((rank < 8 && file < 8) && !occupied_by(friendly_pieces, index)) {
      add_move(moves, from, index);
      if (occupied_by(enemy_pieces, index)) {
        break;
      }
      index = ++rank * 8 + ++file;
    }

    rank = og_rank - 1, file = og_file - 1, index = rank * 8 + file;
    while ((rank >= 0 && file >= 0) && !occupied_by(friendly_pieces, index)) {
      add_move(moves, from, index);
      if (occupied_by(enemy_pieces, index)) {
        break;
      }
      index = --rank * 8 + --file;
    }

    rank = og_rank - 1, file = og_file + 1, index = rank * 8 + file;
    while ((rank >= 0 && file < 8) && !occupied_by(friendly_pieces, index)) {
      add_move(moves, from, index);
      if (occupied_by(enemy_pieces, index)) {
        break;
      }
      index = --rank * 8 + ++file;
    }
    
    rank = og_rank + 1, file = og_file - 1, index = rank * 8 + file;
    while ((rank < 8 && file >= 0) && !occupied_by(friendly_pieces, index)) {
      add_move(moves, from, index);
      if (occupied_by(enemy_pieces, index)) {
        break;
      }
      index = ++rank * 8 + --file;
    }
  }
}

void get_bishop_moves(moves_t* moves, position_t position) {
  assert_valid_position(position);

  bitboard_t friendly_bishops;
  bitboard_t friendly_pieces;
  bitboard_t enemy_pieces;
  if (position.turn == WHITE_TURN) {
    friendly_pieces = whites(position);
    enemy_pieces = blacks(position);
    friendly_bishops = position.bitboards[WHITE_BISHOP];
  } else {
    friendly_pieces = blacks(position);
    enemy_pieces = whites(position);
    friendly_bishops = position.bitboards[BLACK_BISHOP];
  }
  
  __forloop_bishop_moves_gen(
    moves,
    friendly_bishops,
    friendly_pieces,
    enemy_pieces
  );
}