src/chess/rook.c

changeset 47
d726e4b46c33
parent 23
824c9522ce66
child 55
54ea19938d57
equal deleted inserted replaced
46:4dcfb4c58b6d 47:d726e4b46c33
56 } 56 }
57 } 57 }
58 58
59 return 0; 59 return 0;
60 } 60 }
61
62 static int rook_getloc_fixedrow(GameState *gamestate, Move *move) {
63 uint8_t file = POS_UNSPECIFIED;
64 for (uint8_t f = 0 ; f < 8 ; f++) {
65 if (gamestate->board[move->fromrow][f] == move->piece) {
66 if (file == POS_UNSPECIFIED) {
67 file = f;
68 } else {
69 return AMBIGUOUS_MOVE;
70 }
71 }
72 }
73 if (file == POS_UNSPECIFIED) {
74 return INVALID_POSITION;
75 } else {
76 move->fromfile = file;
77 return VALID_MOVE_SYNTAX;
78 }
79 }
80
81 static int rook_getloc_fixedfile(GameState *gamestate, Move *move) {
82 uint8_t row = POS_UNSPECIFIED;
83 for (uint8_t r = 0 ; r < 8 ; r++) {
84 if (gamestate->board[r][move->fromfile] == move->piece) {
85 if (row == POS_UNSPECIFIED) {
86 row = r;
87 } else {
88 return AMBIGUOUS_MOVE;
89 }
90 }
91 }
92 if (row == POS_UNSPECIFIED) {
93 return INVALID_POSITION;
94 } else {
95 move->fromrow = row;
96 return VALID_MOVE_SYNTAX;
97 }
98 }
99
100 int rook_getlocation(GameState *gamestate, Move *move) {
101
102 if (move->fromfile != POS_UNSPECIFIED) {
103 if (move->fromfile == move->tofile) {
104 return rook_getloc_fixedfile(gamestate, move);
105 } else {
106 if (gamestate->board[move->torow][move->fromfile] == move->piece) {
107 move->fromrow = move->torow;
108 return VALID_MOVE_SYNTAX;
109 } else {
110 return INVALID_POSITION;
111 }
112 }
113 }
114
115 if (move->fromrow != POS_UNSPECIFIED) {
116 if (move->fromrow == move->torow) {
117 return rook_getloc_fixedrow(gamestate, move);
118 } else {
119 if (gamestate->board[move->fromrow][move->tofile] == move->piece) {
120 move->fromfile = move->tofile;
121 return VALID_MOVE_SYNTAX;
122 } else {
123 return INVALID_POSITION;
124 }
125 }
126 }
127
128 Move chkrowmove = *move, chkfilemove = *move;
129
130 chkrowmove.fromrow = move->torow;
131 int chkrow = rook_getloc_fixedrow(gamestate, &chkrowmove);
132
133 chkfilemove.fromfile = move->tofile;
134 int chkfile = rook_getloc_fixedfile(gamestate, &chkfilemove);
135
136 if ((chkrow == VALID_MOVE_SYNTAX && chkfile == VALID_MOVE_SYNTAX) ||
137 chkrow == AMBIGUOUS_MOVE || chkfile == AMBIGUOUS_MOVE) {
138 return AMBIGUOUS_MOVE;
139 }
140
141 if (chkrow == VALID_MOVE_SYNTAX) {
142 *move = chkrowmove;
143 return VALID_MOVE_SYNTAX;
144 }
145
146 if (chkfile == VALID_MOVE_SYNTAX) {
147 *move = chkfilemove;
148 return VALID_MOVE_SYNTAX;
149 }
150
151 return INVALID_POSITION;
152 }

mercurial