src/game.c

changeset 10
1347e4dabac0
parent 9
4e4f156bba58
child 11
08d7a6e3ec31
equal deleted inserted replaced
9:4e4f156bba58 10:1347e4dabac0
27 * 27 *
28 */ 28 */
29 29
30 #include "game.h" 30 #include "game.h"
31 #include "input.h" 31 #include "input.h"
32 #include "rules/rules.h"
32 #include <ncurses.h> 33 #include <ncurses.h>
33 #include <string.h> 34 #include <string.h>
34 35
35 static const uint8_t boardx = 10, boardy = 10; 36 static const uint8_t boardx = 10, boardy = 10;
36 37
89 } 90 }
90 } 91 }
91 } 92 }
92 93
93 static _Bool validate_move(Board board, uint8_t mycolor, Move *move) { 94 static _Bool validate_move(Board board, uint8_t mycolor, Move *move) {
94 _Bool result = TRUE; 95 _Bool result;
95 96
96 /* does piece exist */ 97 /* does piece exist */
97 result &= board[move->fromrow][move->fromfile] == move->piece; 98 result = board[move->fromrow][move->fromfile] == move->piece;
98 99
99 /* does move comply to rules */ 100 switch (move->piece & PIECE_MASK) {
100 // TODO: make it so 101 case PAWN:
101 102 result = result && pawn_chkrules(board, move);
102 /* is piece blocked */ 103 result = result && !pawn_isblocked(board, move);
103 // TODO: make it so 104 break;
105 case ROOK:
106 result = result && rook_chkrules(board, move);
107 result = result && !rook_isblocked(board, move);
108 break;
109 case KNIGHT:
110 result = result && knight_chkrules(board, move);
111 result = result && !knight_isblocked(board, move);
112 break;
113 case BISHOP:
114 result = result && bishop_chkrules(board, move);
115 result = result && !bishop_isblocked(board, move);
116 break;
117 case QUEEN:
118 result = result && queen_chkrules(board, move);
119 result = result && !queen_isblocked(board, move);
120 break;
121 case KING:
122 result = result && king_chkrules(board, move);
123 result = result && !king_isblocked(board, move);
124 break;
125 default:
126 result = FALSE;
127 }
104 128
105 /* is piece pinned */ 129 /* is piece pinned */
106 // TODO: make it so 130 // TODO: make it so
107 131
108 return result; 132 return result;
120 } 144 }
121 145
122 if (len == 2) { 146 if (len == 2) {
123 /* pawn move (e.g. "e4") */ 147 /* pawn move (e.g. "e4") */
124 if (isfile(mstr[0]) && isrow(mstr[1])) { 148 if (isfile(mstr[0]) && isrow(mstr[1])) {
125 move->piece = PAWN; 149 move->piece = PAWN|mycolor;
126 move->fromfile = move->tofile = fileidx(mstr[0]); 150 move->tofile = fileidx(mstr[0]);
127 move->torow = rowidx(mstr[1]); 151 move->torow = rowidx(mstr[1]);
128 move->fromrow = rowidx(mstr[1]) + (mycolor == WHITE ? -1 : 1); 152 if (!pawn_getlocation(board, move)) {
129 if (move->fromrow > 6) {
130 move->piece = 0; 153 move->piece = 0;
131 } else {
132 /* advanced first move */
133 if (move->fromrow == (mycolor == WHITE ? 2 : 5) &&
134 board[move->fromrow][move->fromfile] != (mycolor|PAWN)) {
135
136 move->fromrow += (mycolor == WHITE ? -1 : 1);
137 if (move->fromrow > 6) {
138 move->piece = 0;
139 }
140 }
141 } 154 }
142 } 155 }
143 } else if (len == 3) { 156 } else if (len == 3) {
144 if (strcmp(mstr, "O-O") == 0) { 157 if (strcmp(mstr, "O-O") == 0) {
145 /* king side castling */ 158 /* king side castling */
146 move->piece = KING; 159 move->piece = KING|mycolor;
147 move->fromfile = fileidx('e'); 160 move->fromfile = fileidx('e');
148 move->tofile = fileidx('g'); 161 move->tofile = fileidx('g');
149 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 162 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
150 } else { 163 } else {
151 /* unambiguous move (e.g. "Nf3") */ 164 /* unambiguous move (e.g. "Nf3") */
157 /* unambiguous capture (e.g. "Nxf3", "dxe5") */ 170 /* unambiguous capture (e.g. "Nxf3", "dxe5") */
158 171
159 } else if (len == 5) { 172 } else if (len == 5) {
160 if (strcmp(mstr, "O-O-O") == 0) { 173 if (strcmp(mstr, "O-O-O") == 0) {
161 /* queen side castling "O-O-O" */ 174 /* queen side castling "O-O-O" */
162 move->piece = KING; 175 move->piece = KING|mycolor;
163 move->fromfile = fileidx('e'); 176 move->fromfile = fileidx('e');
164 move->tofile = fileidx('c'); 177 move->tofile = fileidx('c');
165 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 178 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
166 } else { 179 } else {
167 /* ambiguous capture (e.g. "Ndxf3") */ 180 /* ambiguous capture (e.g. "Ndxf3") */
172 } 185 }
173 } else if (len == 6) { 186 } else if (len == 6) {
174 /* long notation capture (e.g. "Nc5xf3") */ 187 /* long notation capture (e.g. "Nc5xf3") */
175 } 188 }
176 189
177 if (move->piece) { 190 return move->piece != 0;
178 move->piece |= mycolor;
179 return TRUE;
180 } else {
181 return FALSE;
182 }
183 } 191 }
184 192
185 static int sendmove(Board board, uint8_t mycolor, int opponent) { 193 static int sendmove(Board board, uint8_t mycolor, int opponent) {
186 const size_t buflen = 8; 194 const size_t buflen = 8;
187 char movestr[buflen]; 195 char movestr[buflen];

mercurial