2 * Mancala Common Source Module -- mancala.c
4 * http://cmancala.sourceforge.net
5 * $Source: /cvsroot/cmancala/mancala/src/Attic/mancala.c,v $
7 * $Date: 2003/12/29 05:49:52 $
15 /* Set up the game board. */
16 void gameInit(int *aiBoard, int *humanBoard) {
20 *aiBoard = *humanBoard = 0;
22 for (i=1; i<=BOARD_MAX; i++)
23 *(aiBoard + i) = *(humanBoard + i) = INITIAL_STONES;
27 /* Has the game been won by someone? */
28 int gameWon(int *aiBoard, int *humanBoard) {
30 int aiTotal, humanTotal, i;
32 aiTotal = humanTotal = 0;
34 /* Sum the stones on each side... */
35 for (i=1; i<=BOARD_MAX; i++) {
36 aiTotal = aiTotal + *(aiBoard + i);
37 humanTotal = humanTotal + *(humanBoard + i);
40 /* If one side has none, return accordingly. */
41 if (aiTotal == 0 || humanTotal == 0) {
42 /* Calculate the final score. */
43 for (i=1; i<=BOARD_MAX; i++) {
44 *aiBoard = *aiBoard + *(aiBoard + i);
45 *humanBoard = *humanBoard + *(humanBoard + i);
46 *(aiBoard + i) = *(humanBoard + i) = 0;
55 /* Make a move, and return the position of the last stone! */
56 int move(int *activeBoard, int *passiveBoard, int move) {
58 int *currentPosition, stones;
60 currentPosition = activeBoard + move;
62 /* Pick up the stones. */
63 stones = *(activeBoard + move);
64 *(activeBoard + move) = 0;
66 /* Loop for each stone. */
67 for (; stones>0; stones--) {
69 /* Move to the next board location. */
70 if (currentPosition == activeBoard)
71 currentPosition = passiveBoard + BOARD_MAX;
72 else if (currentPosition == passiveBoard + 1)
73 currentPosition = activeBoard + BOARD_MAX;
82 /* If the last stone lands on an empty hole... */
83 if (*currentPosition == 1 && activeBoard < currentPosition &&
84 currentPosition <= (activeBoard + BOARD_MAX)) {
86 /* ...calculate the position of the opposite hole... */
87 int oppHole = (BOARD_MAX + 1) - (currentPosition - activeBoard);
89 /* ...and make the capture. */
90 *activeBoard = *activeBoard + *(passiveBoard + oppHole);
91 printf("Captured %d stones.\n", *(passiveBoard + oppHole));
92 *(passiveBoard + oppHole) = 0;
95 return currentPosition - activeBoard;
99 int rand_btw(int min, int max) {
103 range = (max - min) + 1;
105 return ((rand() % range) + min);