rename files
[mancala] / src / mancala.c
1 /*  
2  *  Mancala Common Source Module -- mancala.c
3  *  Kevin Riggle
4  *  http://cmancala.sourceforge.net
5  *  $Source: /cvsroot/cmancala/mancala/src/Attic/mancala.c,v $
6  *  $Revision: 1.4.2.1 $
7  *  $Date: 2003/12/29 05:49:52 $
8  *
9  */
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include "mancala.h"
14
15 /* Set up the game board. */
16 void gameInit(int *aiBoard, int *humanBoard) {
17
18   int i;
19
20   *aiBoard = *humanBoard = 0;
21
22   for (i=1; i<=BOARD_MAX; i++)
23     *(aiBoard + i) = *(humanBoard + i) = INITIAL_STONES;
24
25 }
26
27 /* Has the game been won by someone? */
28 int gameWon(int *aiBoard, int *humanBoard) {
29
30   int aiTotal, humanTotal, i;
31
32   aiTotal = humanTotal = 0;
33
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);
38   }
39
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;
47     }    
48     return 1;
49   }
50   else
51     return 0;
52
53 }
54
55 /* Make a move, and return the position of the last stone! */
56 int move(int *activeBoard, int *passiveBoard, int move) {
57
58   int *currentPosition, stones;
59
60   currentPosition = activeBoard + move;
61
62   /* Pick up the stones. */
63   stones = *(activeBoard + move);
64   *(activeBoard + move) = 0;
65
66   /* Loop for each stone. */
67   for (; stones>0; stones--) { 
68
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;
74     else
75       currentPosition--;
76
77     /* Drop a stone. */
78     (*currentPosition)++;
79
80   }
81
82   /* If the last stone lands on an empty hole... */
83   if (*currentPosition == 1 && activeBoard < currentPosition && 
84       currentPosition <= (activeBoard + BOARD_MAX)) {
85
86     /* ...calculate the position of the opposite hole... */
87     int oppHole = (BOARD_MAX + 1) - (currentPosition - activeBoard);
88
89     /* ...and make the capture. */
90     *activeBoard = *activeBoard + *(passiveBoard + oppHole);
91     printf("Captured %d stones.\n", *(passiveBoard + oppHole));
92     *(passiveBoard + oppHole) = 0;
93   }
94
95   return currentPosition - activeBoard;
96    
97 }
98
99 int rand_btw(int min, int max) {
100
101         int range;
102
103         range = (max - min) + 1;
104
105         return ((rand() % range) + min);
106
107 }
108   
109 /*  End mancala.c  */