Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / openchess / src / rules.c
blobd894af62219212c7e332437065dd88874126bed8
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
20 #include "client.h"
21 #include "proto.h"
22 #include "game.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
28 #define OK 0
29 #define FAIL -1
31 int rules_verify (game_t *game, int zx, int zy, int cx, int cy)
33 int i;
34 int ok;
36 char f = game->board[(int)zy][(int)zx];
38 if (game->player == 'w') {
39 if (f > 'a' && f < 'z')
40 return 1;
41 } else if (game->player == 'b')
42 if (f > 'A' && f < 'Z')
43 return 1;
45 switch (f) {
46 case 'p': // cerny pesec
48 ok = 1;
49 if ((game->board[cy][cx] == '.') && (abs (cy-zy)== 1))
50 ok --;
51 // pokud je cilove policko volne a pohyb je o 1 pole tak je to OK
52 if ((game->board[cy][cx] == '.') && (game->board[cy+1][cx] == '.') && (zy == 6) && (abs (cy-zy) == 2))
53 ok --; // pocatecni pozice
55 // pokud je cilove a predcilove policko volne a pohyb je o 2 pole tak je to OK
56 if ((abs (cx-zx)== 1) && ((zy-cy)== 1) && (game->board[cy][cx] < 'Z') && (game->board[cy][cx] > '.'))
57 ok --; // jiny sloupec -> bere figuru, pouze souperovu
60 break;
62 case 'P': // BILY pesec
63 ok = 1;
64 if ((game->board[cy][cx] == '.') && ((cy - zy)== 1))
65 ok --;
66 // pokud je cilove policko volne a pohyb je o 1 pole tak je to OK
68 if ((game->board[cy][cx] == '.') &&
69 (game->board[cy-1][cx] == '.') &&
70 (zy == 1) && // pocatecni pozice
71 (abs (cy - zy) == 2))
72 ok --;
73 // pokud je cilove a predcilove policko volne a pohyb je o 2 pole tak je to OK
75 if ((abs (cx - zx) == 1) && ((cy - zy) == 1) &&
76 (game->board[cy][cx] > 'Z'))
77 ok --;
78 // jiny sloupec -> bere figuru, pouze souperovu
80 break;
82 case 'K': // kral
83 case 'k':
84 ok = 2;
86 if (abs (cx - zx) <= 1)
87 ok --; // pohyb pouze o jedno pole v libovolnem smeru
89 if (abs (cy - zy) <= 1)
90 ok --;
92 break;
93 case 'D': // dama
94 case 'd':
95 ok = 1;
97 if ((cx == zx) || (cy == zy)) { // pohyb pouze horizontalne nebo vertikalne
98 for (i = zx; i < cx; i ++)
99 if (game->board[zy][i] != '.')
100 ok ++; // f stoji v ceste
102 for (i = zx; i > cx; i --)
103 if (game->board[zy][i] != '.')
104 ok ++; // f stoji v ceste
106 for (i = zy; i<cy; i ++)
107 if (game->board[i][zx] != '.')
108 ok ++; // f stoji v ceste
110 for (i = zy; i>cy; i --)
111 if (game->board[i][zx] != '.')
112 ok ++; // f stoji v ceste
114 ok -= 2;
115 } else {
116 for (i = 0; i < (cx - zx); i ++) {
117 if (cy > zy) {
118 if (game->board[zy+i][zx+i] != '.')
119 ok ++;
120 } else
121 if (game->board[zy-i][zx+i] != '.')
122 ok ++;
125 for (i = 0; i > (cx - zx); i --) {
126 if (cy < zy) {
127 if (game->board[zy+i][zx+i] != '.')
128 ok ++;
129 } else {
130 if (game->board[zy-i][zx+i] != '.')
131 ok ++; // f stoji v ceste
135 ok -= 2;
138 break;
139 case 'S': // strelec
140 case 's':
142 ok = 1;
144 for (i = 0; i < (cx - zx); i ++) {
145 if (cy > zy) {
146 if (game->board[zy+i][zx+i] != '.')
147 ok ++;
148 } else
149 if (game->board[zy-i][zx+i] != '.')
150 ok ++;
154 for (i = 0; i > (cx - zx); i --) {
155 if (cy < zy) {
156 if (game->board[zy+i][zx+i] != '.')
157 ok ++;
158 } else {
159 if (game->board[zy-i][zx+i] != '.')
160 ok ++; // f stoji v ceste
166 ok -= 2;
168 break;
169 case 'J': // jezdec / kun
170 case 'j':
171 ok = 1;
172 if (((abs (cx - zx) == 2) && (abs (cy - zy) == 1))
173 || ((abs (cy - zy) == 2) && (abs (cx - zx) == 1)))
174 ok --; // pohyb pouze do "L"
176 break;
177 case 'V': // vez
178 case 'v':
179 ok = 1;
180 if ((cx == zx) || (cy == zy))
181 ok --; // pohyb pouze horizontalne nebo vertikalne
183 for (i = zx; i < cx; i ++)
184 if (game->board[zy][i] != '.')
185 ok ++; // f stoji v ceste
187 for (i = zx; i > cx; i --)
188 if (game->board[zy][i] != '.') ok ++; // f stoji v ceste
190 for (i = zy; i < cy; i ++)
191 if (game->board[i][zx] != '.')
192 ok ++; // f stoji v ceste
194 for (i = zy; i > cy; i --)
195 if (game->board[i][zx] != '.')
196 ok ++; // f stoji v ceste
198 ok -= 1; // 1x se testuje pozice veze samotne
199 break;
201 default: return FAIL;
204 if (!ok)
205 return OK;
207 return FAIL;