5 Info: game example for RICE
19 /* global variables */
21 int stopGame
= 0, autoGame
= 0;
22 int myMark
, yourMark
, emptyMark
= ' ';
23 int xTable
[] = {0, 36, 40, 44}, yTable
[] = {0, 10, 12, 14};
24 int fieldTable
[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
28 /* compiler dependent functions, for different compilers: adjust code */
30 #if defined(_MSC_VER) || defined(_QC)
31 # define __MICROSOFTC__
35 #if defined(__MICROSOFTC__)
37 /* this part is coded for use with Microsoft C(++) 7.00 compiler */
42 /* hiding and showing cursor */
44 #define hideCursor() _displaycursor(_GCURSOROFF)
45 #define showCursor() _displaycursor(_GCURSORON)
48 /* goto position on screen */
50 #define gotoPos(x, y) _settextposition(y, x)
53 /* goto position and put character */
55 #define putChar(x, y, c) gotoPos(x, y); _putch(c)
58 /* write mark to screen */
60 void putMark(int x
, int y
, int c
)
62 putChar(xTable
[x
], yTable
[y
], c
);
63 fieldTable
[x
- 1][y
- 1] = c
;
69 #define clearScreen() _clearscreen(_GCLEARSCREEN)
72 #elif defined(__BORLANDC__)
75 /* this part is coded for use with Borland C(++) 3.1 compiler */
80 /* hiding and showing cursor */
82 #define hideCursor() _setcursortype(_NOCURSOR)
83 #define showCursor() _setcursortype(_NORMALCURSOR)
86 /* goto position on screen */
88 #define gotoPos(x, y) gotoxy(x, y)
91 /* goto position and put character */
93 #define putChar(x, y, c) gotoPos(x, y); putchar(c)
96 /* write mark to screen */
98 void putMark(int x
, int y
, int c
)
100 unsigned char buf
[2];
101 gettext(xTable
[x
], yTable
[y
], xTable
[x
], yTable
[y
], buf
);
102 fieldTable
[x
- 1][y
- 1] = buf
[0] = c
;
103 puttext(xTable
[x
], yTable
[y
], xTable
[x
], yTable
[y
], buf
);
109 #define clearScreen() clrscr()
112 /* increase stack length */
114 extern unsigned _stklen
= 10000U;
116 #elif defined(__GNUC__) & defined(__MSDOS__)
119 /* this part is coded for use with Gnu C++ (DJGPP 2.4.1) compiler */
121 #include <conio.h> /* dummy header file including gppconio.h */
124 /* hiding and showing cursor */
126 #define hideCursor() _setcursortype(_NOCURSOR)
127 #define showCursor() _setcursortype(_NORMALCURSOR)
130 /* goto position on screen */
132 #define gotoPos(x, y) gotoxy(x, y)
135 /* goto position and put character */
137 #define putChar(x, y, c) gotoPos(x, y); putch(c)
140 /* write mark to screen */
142 void putMark(int x
, int y
, int c
)
144 unsigned char buf
[2];
145 gettext(xTable
[x
], yTable
[y
], xTable
[x
], yTable
[y
], buf
);
146 fieldTable
[x
- 1][y
- 1] = buf
[0] = c
;
147 puttext(xTable
[x
], yTable
[y
], xTable
[x
], yTable
[y
], buf
);
153 #define clearScreen() clrscr()
157 # error Compiler should be Microsoft C/C++ 7.0 or Borland C++ 3.1
163 void printMsg(char *, ...);
166 /* get mark from field */
168 int getMark(int x
, int y
)
170 return fieldTable
[x
- 1][y
- 1];
176 void SwapMark(int *m1
, int *m2
)
188 void doError(char *msg
)
193 printMsg("\x7Error: %s\n", msg
);
196 void redirError(int kb
, int type
, char *msg
)
202 /* show message from kb */
204 void showInfo(char *msg
)
206 if(autoGame
) /* just play, don't talk */
215 void redirInfo(int kb
, int argc
, char *argv
[], float *grade
)
221 /* show field on screen */
230 putChar(x
- 7, y
- 2, 'a');
231 putChar(x
- 7, y
, 'b');
232 putChar(x
- 7, y
+ 2, 'c');
234 putChar(x
- 4, y
- 3, '1');
235 putChar(x
, y
- 3, '2');
236 putChar(x
+ 4, y
- 3, '3');
238 putChar(x
- 2, y
- 2, 179);
239 putChar(x
- 2, y
- 1, 179);
240 putChar(x
- 2, y
, 179);
241 putChar(x
- 2, y
+ 1, 179);
242 putChar(x
- 2, y
+ 2, 179);
243 putChar(x
+ 2, y
- 2, 179);
244 putChar(x
+ 2, y
- 1, 179);
245 putChar(x
+ 2, y
, 179);
246 putChar(x
+ 2, y
+ 1, 179);
247 putChar(x
+ 2, y
+ 2, 179);
249 putChar(x
- 5, y
- 1, 196);
250 putChar(x
- 4, y
- 1, 196);
251 putChar(x
- 3, y
- 1, 196);
252 putChar(x
- 3, y
+ 1, 196);
253 putChar(x
- 4, y
+ 1, 196);
254 putChar(x
- 5, y
+ 1, 196);
255 putChar(x
- 1, y
- 1, 196);
256 putChar(x
, y
- 1, 196);
257 putChar(x
+ 1, y
+ 1, 196);
258 putChar(x
- 1, y
+ 1, 196);
259 putChar(x
, y
+ 1, 196);
260 putChar(x
+ 1, y
- 1, 196);
261 putChar(x
+ 5, y
- 1, 196);
262 putChar(x
+ 4, y
- 1, 196);
263 putChar(x
+ 3, y
- 1, 196);
264 putChar(x
+ 3, y
+ 1, 196);
265 putChar(x
+ 4, y
+ 1, 196);
266 putChar(x
+ 5, y
+ 1, 196);
268 putChar(x
- 2, y
- 1, 197);
269 putChar(x
- 2, y
+ 1, 197);
270 putChar(x
+ 2, y
- 1, 197);
271 putChar(x
+ 2, y
+ 1, 197);
277 int getPos(int argc
, char *argv
[], int *x
, int *y
)
281 doError("incomplete position");
284 *x
= argv
[1][1] - '1' + 1;
285 *y
= argv
[1][0] - 'a' + 1;
286 if(*x
< 1 || 3 < *x
|| *y
< 1 || 3 < *y
)
288 doError("incorrect position");
295 /* print a message */
297 void printMsg(char *msg
, ...)
301 if(!msg
) /* clear line: dirty, but working */
305 va_start(arguments
, msg
);
306 vprintf(msg
, arguments
);
315 void getYourMark(void)
322 printMsg("Put your mark (o/x): ");
325 } while(yourMark
!= 'o' && yourMark
!= 'x');
327 myMark
= (yourMark
== 'o') ? 'x' : 'o';
342 printMsg("Your move [a..c][1..3]: ");
349 } while(x
< 1 || x
> 3 || y
< 1 || y
> 3);
350 } while(getMark(x
, y
) != emptyMark
);
352 putMark(x
, y
, yourMark
);
359 /* linker function */
361 RICE_Linker(theLinkerFunction
)
363 RICE_Link("put me on",
366 if(getPos(rice_argc
, rice_argv
, &x
, &y
)) return;
367 putMark(x
, y
, myMark
);
374 if(getPos(rice_argc
, rice_argv
, &x
, &y
)) return;
375 *rice_grade
= (getMark(x
, y
) == myMark
) ? 1.0 : 0.0;
378 RICE_Link("you are on",
381 if(getPos(rice_argc
, rice_argv
, &x
, &y
)) return;
382 *rice_grade
= (getMark(x
, y
) == yourMark
) ? 1.0 : 0.0;
385 RICE_Link("is empty",
388 if(getPos(rice_argc
, rice_argv
, &x
, &y
)) return;
389 *rice_grade
= (getMark(x
, y
) == emptyMark
) ? 1.0 : 0.0;
392 RICE_Link("put me somewhere",
393 putMark(1 + rand()%3, 1 + rand()%3, myMark
);
397 RICE_Link("stop the game", stopGame
= 1);
400 RICE_LINKER theLinker
= theLinkerFunction
;
405 #define GAME_KB "game.kb"
407 int main(int argc
, char *argv
[])
409 int theES
, theOtherES
= 0;
415 autoGame
= (argc
> 1);
417 /* put field on screen */
422 /* create and set up knowledge base */
423 theES
= rice_CreateES();
424 rice_SetInteractive(theES
, 0);
425 rice_RedirectReport(theES
, redirError
);
426 rice_RedirectInform(theES
, redirInfo
);
427 rice_UseLinker(theES
, 1, &theLinker
);
428 if(rice_CompileKB(theES
, GAME_KB
))
434 if(autoGame
) /* if autonomous game */
436 theOtherES
= rice_CopyES(0, theES
);
437 if(argc
> 1 && rice_CompileKB(theOtherES
, (argc
> 1) ? argv
[1] : GAME_KB
))
453 /* throw dice for starting */
454 if(rand() > RAND_MAX
/2) /* other is first */
457 SwapMark(&yourMark
, &myMark
);
458 rice_InferKB(theOtherES
);
459 SwapMark(&yourMark
, &myMark
);
465 while(!stopGame
&& nrOfMoves
< 9)
470 if(!stopGame
&& nrOfMoves
< 9)
473 SwapMark(&yourMark
, &myMark
);
474 rice_ClearKB(theOtherES
);
475 rice_InferKB(theOtherES
);
476 SwapMark(&yourMark
, &myMark
);
482 /* goto bottom of screen */
485 /* clean up expert systems */
486 rice_DeleteES(theES
);
487 if(theOtherES
) rice_DeleteES(theOtherES
);