src/chess/rules.c

changeset 50
41017d0a72c5
parent 49
02c509a44e98
child 51
84f2e380a434
equal deleted inserted replaced
49:02c509a44e98 50:41017d0a72c5
40 *lastmovecopy = *(simulation.lastmove); 40 *lastmovecopy = *(simulation.lastmove);
41 simulation.movelist = simulation.lastmove = lastmovecopy; 41 simulation.movelist = simulation.lastmove = lastmovecopy;
42 } 42 }
43 43
44 return simulation; 44 return simulation;
45 }
46
47 void gamestate_init(GameState *gamestate) {
48 memset(gamestate, 0, sizeof(GameState));
49
50 Board initboard = {
51 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
52 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
53 {0, 0, 0, 0, 0, 0, 0, 0},
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 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
58 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
59 };
60 memcpy(gamestate->board, initboard, sizeof(Board));
45 } 61 }
46 62
47 void gamestate_cleanup(GameState *gamestate) { 63 void gamestate_cleanup(GameState *gamestate) {
48 MoveList *elem; 64 MoveList *elem;
49 elem = gamestate->movelist; 65 elem = gamestate->movelist;
585 } else { 601 } else {
586 return INVALID_POSITION; 602 return INVALID_POSITION;
587 } 603 }
588 } 604 }
589 605
590 int eval_move(GameState *gamestate, char *mstr, Move *move) { 606 int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color) {
591 memset(move, 0, sizeof(Move)); 607 memset(move, 0, sizeof(Move));
592 move->fromfile = POS_UNSPECIFIED; 608 move->fromfile = POS_UNSPECIFIED;
593 move->fromrow = POS_UNSPECIFIED; 609 move->fromrow = POS_UNSPECIFIED;
594 610
595 size_t len = strlen(mstr); 611 size_t len = strlen(mstr);
610 if (len > 3 && mstr[len-2] == '=') { 626 if (len > 3 && mstr[len-2] == '=') {
611 move->promotion = getpiece(mstr[len-1]); 627 move->promotion = getpiece(mstr[len-1]);
612 if (!move->promotion) { 628 if (!move->promotion) {
613 return INVALID_MOVE_SYNTAX; 629 return INVALID_MOVE_SYNTAX;
614 } else { 630 } else {
615 move->promotion |= gamestate->mycolor; 631 move->promotion |= color;
616 len -= 2; 632 len -= 2;
617 mstr[len] = 0; 633 mstr[len] = 0;
618 } 634 }
619 } 635 }
620 636
627 if (strcmp(mstr, "O-O") == 0) { 643 if (strcmp(mstr, "O-O") == 0) {
628 /* king side castling */ 644 /* king side castling */
629 move->piece = KING; 645 move->piece = KING;
630 move->fromfile = fileidx('e'); 646 move->fromfile = fileidx('e');
631 move->tofile = fileidx('g'); 647 move->tofile = fileidx('g');
632 move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; 648 move->fromrow = move->torow = color == WHITE ? 0 : 7;
633 } else { 649 } else {
634 /* move (e.g. "Nf3") */ 650 /* move (e.g. "Nf3") */
635 move->piece = getpiece(mstr[0]); 651 move->piece = getpiece(mstr[0]);
636 move->tofile = fileidx(mstr[1]); 652 move->tofile = fileidx(mstr[1]);
637 move->torow = rowidx(mstr[2]); 653 move->torow = rowidx(mstr[2]);
662 if (strcmp(mstr, "O-O-O") == 0) { 678 if (strcmp(mstr, "O-O-O") == 0) {
663 /* queen side castling "O-O-O" */ 679 /* queen side castling "O-O-O" */
664 move->piece = KING; 680 move->piece = KING;
665 move->fromfile = fileidx('e'); 681 move->fromfile = fileidx('e');
666 move->tofile = fileidx('c'); 682 move->tofile = fileidx('c');
667 move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; 683 move->fromrow = move->torow = color == WHITE ? 0 : 7;
668 } else { 684 } else {
669 move->piece = getpiece(mstr[0]); 685 move->piece = getpiece(mstr[0]);
670 if (mstr[2] == 'x') { 686 if (mstr[2] == 'x') {
671 move->capture = 1; 687 move->capture = 1;
672 if (move->piece) { 688 if (move->piece) {
699 } 715 }
700 716
701 717
702 if (move->piece) { 718 if (move->piece) {
703 if (move->piece == PAWN 719 if (move->piece == PAWN
704 && move->torow == (gamestate->mycolor==WHITE?7:0) 720 && move->torow == (color==WHITE?7:0)
705 && !move->promotion) { 721 && !move->promotion) {
706 return NEED_PROMOTION; 722 return NEED_PROMOTION;
707 } 723 }
708 724
709 move->piece |= gamestate->mycolor; 725 move->piece |= color;
710 if (move->fromfile == POS_UNSPECIFIED 726 if (move->fromfile == POS_UNSPECIFIED
711 || move->fromrow == POS_UNSPECIFIED) { 727 || move->fromrow == POS_UNSPECIFIED) {
712 return getlocation(gamestate, move); 728 return getlocation(gamestate, move);
713 } else { 729 } else {
714 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; 730 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;

mercurial