initial load of http://downloads.sourceforge.net/project/cmancala/mancala-gui/mancala...
[mancala] / src / ai.c
1 /*  
2  *  AI Source Module -- ai.c
3  *  Kevin Riggle
4  *  http://cmancala.sourceforge.net
5  *  $Source: /cvsroot/cmancala/mancala/src/Attic/ai.c,v $
6  *  $Revision: 1.5.2.1 $
7  *  $Date: 2003/12/29 05:49:52 $
8  *
9  */
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include "ai.h"
14 #include "mancala.h"
15
16 /* Return the computer's move. */
17 int aiMove(int *aiBoard, int *humanBoard) {
18
19   int BestCapture, CapturePoints, BestMove, MoveSize, Test, i;
20   
21   BestCapture = CapturePoints = BestMove = MoveSize = Test = i = 0;
22
23   /* printf("HAL: I'm thinking, Dave...\n"); */
24   /* Loop through possible moves... */
25   for(i=1;i<=BOARD_MAX;i++) {
26     /* ...only if there is any move to make! */
27     if (aiBoard[i] != 0) {
28       /* Calculate position of final stone */
29       Test = i - (aiBoard[i] % ((2 * BOARD_MAX)+1));
30     
31       /* If this move ends on my home, take it. */
32       if (Test == 0)
33         return i;
34       /* If it doesn't but a capture does occur... */
35       else if ((Test>0) && (aiBoard[Test] == 0)) {
36         /* ...that's the best we've seen so far, store the info. */
37         if (humanBoard[(BOARD_MAX + 1) - Test] > CapturePoints) {
38           BestCapture = i;
39           CapturePoints = humanBoard[(BOARD_MAX + 1) - Test];
40         }
41       }
42       /* If neither happen, but this is larger than previous, record it. */
43       if (aiBoard[i] > MoveSize) {
44         BestMove = i;
45         MoveSize = aiBoard[i];
46       }
47     }
48   }
49
50   /* If we have a good capture, take it. */
51   if (BestCapture != 0)
52     return BestCapture;
53   
54   /* Otherwise, move the largest concentration of stones. */
55   return BestMove;
56
57 }
58
59 /*  End ai.c  */