src/chess/rules.c

changeset 78
ceb9197b3c6d
parent 69
c8f2c280cff7
equal deleted inserted replaced
77:808a7324b467 78:ceb9197b3c6d
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 * 27 *
28 */ 28 */
29 29
30 #include "rules.h" 30 #include "rules.h"
31 #include "chess.h" 31 #include "pawn.h"
32 #include "rook.h"
33 #include "knight.h"
34 #include "bishop.h"
35 #include "queen.h"
36 #include "king.h"
37
32 #include <string.h> 38 #include <string.h>
33 #include <stdlib.h> 39 #include <stdlib.h>
34 #include <sys/time.h> 40 #include <sys/time.h>
35 41
36 static GameState gamestate_copy_sim(GameState *gamestate) { 42 static GameState gamestate_copy_sim(GameState *gamestate) {
41 *lastmovecopy = *(simulation.lastmove); 47 *lastmovecopy = *(simulation.lastmove);
42 simulation.movelist = simulation.lastmove = lastmovecopy; 48 simulation.movelist = simulation.lastmove = lastmovecopy;
43 } 49 }
44 50
45 return simulation; 51 return simulation;
46 }
47
48 void gamestate_init(GameState *gamestate) {
49 memset(gamestate, 0, sizeof(GameState));
50
51 Board initboard = {
52 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
53 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
54 {0, 0, 0, 0, 0, 0, 0, 0},
55 {0, 0, 0, 0, 0, 0, 0, 0},
56 {0, 0, 0, 0, 0, 0, 0, 0},
57 {0, 0, 0, 0, 0, 0, 0, 0},
58 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
59 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
60 };
61 memcpy(gamestate->board, initboard, sizeof(Board));
62 }
63
64 void gamestate_cleanup(GameState *gamestate) {
65 MoveList *elem;
66 elem = gamestate->movelist;
67 while (elem) {
68 MoveList *cur = elem;
69 elem = elem->next;
70 free(cur);
71 };
72 } 52 }
73 53
74 /* MUST be called BETWEEN validating AND applying a move to work correctly */ 54 /* MUST be called BETWEEN validating AND applying a move to work correctly */
75 static void format_move(GameState *gamestate, Move *move) { 55 static void format_move(GameState *gamestate, Move *move) {
76 char *string = &(move->string[0]); 56 char *string = &(move->string[0]);

mercurial