1 /* read_5.c read board from icds style 5 */
2
3 /*
4 * xcheckers: a point and click checkerboard
5 * (C): 1999, 2000, 2003 Peter Chiocchetti <girbal@tacheles.de>
6 */
7
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <time.h>
14 #include <X11/Xlib.h>
15 #include "xcheckers.h"
16 #include "read.h"
17 #include "board.h"
18 #include "child.h"
19 #include "status.h"
20 #include "events.h"
21
22 #define TOP 0
23 #define BOT 1
24
25 int currPlayer;
26
27 /* strip leading whitespace */
28 static void
trimLeft(char * s)29 trimLeft(char *s)
30 {
31 char *t;
32
33 t = s;
34 if (s) {
35 while (*s != '\0' && isspace (*s))
36 s++;
37 while (*s != '\0')
38 *t++ = *s++;
39 *t = '\0';
40 }
41 }
42
43 /* read in a board state from ICDS */
44 void
readPos5(char * line)45 readPos5(char *line)
46 {
47 /* parses argument looking for a board (style 5) */
48
49 int x, y, k;
50 int iclock;
51 char templine[32];
52
53 /*
54 * ICDS board style 5 description:
55 *
56 * offset string no
57 * 0 <5> 3 Style Info
58 * 3 001 3 Game Number
59 * 6 p 1 Match style (p = polish 10x10)
60 * 7 B 1 Point of view (B = black, W = white)
61 * 8 B 1 Player to move (B = black, W = white)
62 * 9 ... 17 Black Player Name
63 * 26 ... 17 White Player Name
64 * 43 ... .. Board itself
65 */
66
67 if (strneq(line, "Game", 4)) {
68 /*
69 * this is a board style 1, change to style 5 better check
70 * for something not an english word ? e.g. " a b
71 * c d e f g h"
72 */
73 message(strdup("set style 5\n"));
74 message(strdup("refresh\n"));
75 return;
76 }
77 if ((strneq(line, "icds% It is not your move.", 26))
78 || (strneq(line, "icds% Game clock is paused,", 27))
79 || (strneq(line, "icds% Illegal move.", 19))
80 || (strneq(line, "icds% Ambigous move.", 20))
81 || (strneq(line, "icds% It isn't your turn,", 25))) {
82 /* we made an illegal move and messed up the board */
83 message(strdup("refresh\n"));
84 return;
85 }
86 if (strneq(line, "<5>", 3)) {
87 draglock(1);
88
89 /* Board Size, Rotation */
90 /* Board Direction */
91 /* Player to move */
92 switch (line[6]) {
93 case 'p': //polish, international
94 changeBoard(INTERNL);
95 swapBoard(line[7] == 'B' ? NORTH : SOUTH);
96 currPlayer = (line[8] == 'B'
97 ? (direction == NORTH ? BOT : TOP)
98 : (direction == NORTH ? TOP : BOT));
99 break;
100 case 'i':
101 changeBoard(ITALIAN);
102 swapBoard(line[7] == 'B' ? NORTH : SOUTH);
103 currPlayer = (line[8] == 'W'
104 ? (direction == NORTH ? BOT : TOP)
105 : (direction == NORTH ? TOP : BOT));
106 break;
107 case 'm':
108 changeBoard(MAFIERZ);
109 swapBoard(line[7] == 'B' ? NORTH : SOUTH);
110 currPlayer = (line[8] == 'B'
111 ? (direction == NORTH ? BOT : TOP)
112 : (direction == NORTH ? TOP : BOT));
113 break;
114 case 'r':
115 changeBoard(RUSSIAN);
116 swapBoard(line[7] == 'B' ? NORTH : SOUTH);
117 currPlayer = (line[8] == 'W'
118 ? (direction == NORTH ? BOT : TOP)
119 : (direction == NORTH ? TOP : BOT));
120 break;
121 default:
122 changeBoard(ENGLISH);
123 swapBoard(line[7] == 'B' ? NORTH : SOUTH);
124 currPlayer = (line[8] == 'B'
125 ? (direction == NORTH ? BOT : TOP)
126 : (direction == NORTH ? TOP : BOT));
127 }
128
129 /* Status Line */
130 /* Black Player Name */
131 strncpy(templine, &line[9], 17);
132 templine[17] = '\0';
133 trimLeft(templine);
134 strcpy(&statusline[direction == NORTH ? 1 : 0][0], templine);
135 strcat(&statusline[direction == NORTH ? 1 : 0][0],
136 line[8] == 'B' ? "*" : "");
137
138 /* White Player Name */
139 strncpy(templine, &line[26], 17);
140 templine[17] = '\0';
141 trimLeft(templine);
142 strcpy(&statusline[direction == SOUTH ? 1 : 0][0], templine);
143 strcat(&statusline[direction == SOUTH ? 1 : 0][0],
144 line[8] == 'W' ? "*" : "");
145
146 k = 43;
147 // xcheckers grid has the origin top left
148 if (direction == NORTH) {
149 for (y = cells - 1; y >= 0; y--) {
150 for (x = 0; x < cells; x++) {
151 switch (line[k]) {
152 case ' ':
153 board[x][y].colour = o;
154 break;
155 case '-':
156 board[x][y].colour = n;
157 break;
158 case 'b':
159 board[x][y].colour = b;
160 break;
161 case 'w':
162 board[x][y].colour = w;
163 break;
164 case 'B':
165 board[x][y].colour = B;
166 break;
167 case 'W':
168 board[x][y].colour = W;
169 break;
170 default:
171 break;
172 }
173 k++;
174 }
175 }
176 } else
177 /* direction == SOUTH */
178 /* flip the board */
179 {
180 for (y = 0; y < cells; y++) {
181 for (x = cells - 1; x >= 0; x--) {
182 switch (line[k]) {
183 case ' ':
184 board[x][y].colour = o;
185 break;
186 case '-':
187 board[x][y].colour = n;
188 break;
189 case 'b':
190 board[x][y].colour = b;
191 break;
192 case 'w':
193 board[x][y].colour = w;
194 break;
195 case 'B':
196 board[x][y].colour = B;
197 break;
198 case 'W':
199 board[x][y].colour = W;
200 break;
201 default:
202 break;
203 }
204 k++;
205 }
206 }
207 }
208 /* Clock Line */
209 /* Black Clock */
210 strncpy(templine, &line[cells == 8 ? 107 : 143], 3);
211 templine[3] = '\0';
212 sscanf(templine, "%d", &iclock);
213 if (iclock < 0)
214 sprintf(&clockline[direction == NORTH ? 1 : 0][0], "-%d : %02d",
215 abs((int) (iclock / 60)), abs((int) (iclock % 60)));
216 else
217 sprintf(&clockline[direction == NORTH ? 1 : 0][0], " %d : %02d",
218 abs((int) (iclock / 60)), abs((int) (iclock % 60)));
219 if (line[8] == 'B') {
220 currTime = time(NULL);
221 currClock = iclock;
222 }
223 /* White Clock */
224 strncpy(templine, &line[cells == 8 ? 111 : 147], 3);
225 templine[3] = '\0';
226 sscanf(templine, "%d", &iclock);
227 if (iclock < 0)
228 sprintf(&clockline[direction == SOUTH ? 1 : 0][0], "-%d : %02d",
229 abs((int) (iclock / 60)), abs((int) (iclock % 60)));
230 else
231 sprintf(&clockline[direction == SOUTH ? 1 : 0][0], " %d : %02d",
232 abs((int) (iclock / 60)), abs((int) (iclock % 60)));
233 if (line[8] == 'W') {
234 currTime = time(NULL);
235 currClock = iclock;
236 }
237 draglock(0);
238 freshenBoard();
239 showStatus();
240 }
241 }
242