2 * Recursive AI Algorithm -- ai-recurse.c
4 * http://cmancala.sourceforge.net
5 * $Source: /cvsroot/cmancala/mancala/src/Attic/ai-recurse.c,v $
6 * $Revision: 1.22.2.1 $
7 * $Date: 2003/12/29 05:49:52 $
17 /* How far should I go? */
20 /* keep the actual mechanics of the recursive algorithm internal */
21 static int aiRecurse (int *aiBoard, int *humanBoard, int depth, FILE *log);
23 /* new stub api function using init routine */
25 int aiMove(int *aiBoard, int *humanBoard) {
27 return aiInit(aiBoard, humanBoard, "recurse.log", aiRecurse);
31 /* actual recursive function, accept no imitations */
32 int aiRecurse (int *aiBoard, int *humanBoard, int depth, FILE *log) {
34 int aiBoardTemp[BOARD_MAX+1], humanBoardTemp[BOARD_MAX+1];
35 int i, j, bestmove, pts, endpos;
37 bestmove = pts = endpos = 0;
39 /*printf("AI at depth %d\n", depth);*/
40 fprintf(log, "AI at depth %d\n", depth);
42 /* Keep us from recursing infinitely */
43 if (depth > MAX_DEPTH)
46 /* iterate through each board position */
47 for (i=1; i<=BOARD_MAX; i++) {
49 /* copy boards to temp. location */
50 for(j=0; j<=BOARD_MAX; j++) {
51 aiBoardTemp[j] = aiBoard[j];
52 humanBoardTemp[j] = humanBoard[j];
55 /* test the current move */
56 endpos = move(aiBoardTemp, humanBoardTemp, i);
58 /* if we get another turn, recurse */
60 aiRecurse(aiBoardTemp, humanBoardTemp,
64 fprintf(log, "Moving from %d at depth %d\n", i, depth);
65 fprintf(log, "Ends at %d and nets %d points.\n", endpos,
66 (aiBoardTemp[0] - aiBoard[0]));
68 /* if this move is better, record it */
69 if ((aiBoardTemp[0] - aiBoard[0]) > pts) {
70 pts = (aiBoardTemp[0] - aiBoard[0]);
75 /* if no move is best and some still exist, pick one at random */
76 if ((bestmove == 0) && (!gameWon(aiBoard, humanBoard)))
77 while (aiBoard[(bestmove
78 = rand_btw(1,BOARD_MAX+1))] == 0)
81 /* make the final move and return */
82 move(aiBoard, humanBoard, bestmove);
88 /* End ai-recurse.c */