-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.c
More file actions
97 lines (66 loc) · 2.1 KB
/
Move.c
File metadata and controls
97 lines (66 loc) · 2.1 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "Move.h"
#include <stdio.h>
/* NOTE
* moves are actually smaller than a 64 bit (8 byte) pointer
* so it does not make sense to pass by reference, just pass
* the move itself when possible
*/
unsigned char moving_type(Move m) {
return (unsigned char) (m.moving_capturing >> 4) & 15;
}
unsigned char capturing_type(Move m) {
return (unsigned char) m.moving_capturing & 15;
}
void move_set(Move *move, int from, int to, int moving, int capturing) {
/*
printf("creating move from %d to %d, %d is moving, %d is captured\n",
from, to, moving, capturing);
*/
move->from = (char) from;
move->to = (char) to;
move->moving_capturing = (((char) moving) << 4) + ((char) capturing);
}
void apply_move(Board *board, Move move) {
int moving, capt, playing;
playing = to_play(board);
moving = moving_type(move);
capt = capturing_type(move);
/*
printf("applying move from %d to %d,\t%d is moving to %d\n",
move.from, move.to, moving, capt);
*/
/* TODO: handle castling, en pessant, etc */
/* remove the source piece */
place_piece(board, playing, NA, move.from);
/* remove piece being attacked (if any) */
if(capt != NA) {
place_piece(board, 1-playing, NA, move.to);
}
/* place the attacking piece on destination */
place_piece(board, playing, moving, move.to);
/* flip the piece to play */
if(to_play(board) == WHITE)
set_play(board, BLACK);
else set_play(board, WHITE);
/* increase the ply */
set_ply(board, ply(board)+1);
}
void undo_move(Board *board, Move move) {
int moving, capt, playing;
playing = to_play(board);
moving = moving_type(move);
capt = capturing_type(move);
/* TODO: handle castling, en pessant, etc */
/* remove the attacking piece from dest */
place_piece(board, 1-playing, NA, move.to);
/* place the attacking piece at source */
place_piece(board, 1-playing, moving, move.from);
/* replace the captured piece (if any) */
place_piece(board, playing, capt, move.to);
/* flip the piece to play */
if(to_play(board) == WHITE)
set_play(board, BLACK);
else set_play(board, WHITE);
/* decrease the ply */
set_ply(board, ply(board)-1);
}