in fremantle additional software shall be in /home/opt/ where there is more space
[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  *  Copyright (C) 2003, 2004 Kevin Riggle
10  *  Copyright (C) 2009 Reto Zingg
11  *
12  *  This program is free software; you can redistribute it and/or modify it
13  *  under the terms of the GNU General Public License as published by the
14  *  Free Software Foundation; either version 2, or (at your option) any
15  *  later version.
16  *
17  *  This program is distributed in the hope that it will be useful, but
18  *  WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  General Public License for more details, a copy of which may be found in
21  *  the file COPYING provided in the main directory of this release.
22  *
23  */
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include "mancala.h"
28
29 /* Set up the game board. */
30 void gameInit(int *aiBoard, int *humanBoard) {
31
32   int i;
33
34   *aiBoard = *humanBoard = 0;
35
36   for (i=1; i<=BOARD_MAX; i++)
37     *(aiBoard + i) = *(humanBoard + i) = INITIAL_STONES;
38
39 }
40
41 /* Has the game been won by someone? */
42 int gameWon(int *aiBoard, int *humanBoard) {
43
44   int aiTotal, humanTotal, i;
45
46   aiTotal = humanTotal = 0;
47
48   /* Sum the stones on each side... */
49   for (i=1; i<=BOARD_MAX; i++) {
50     aiTotal = aiTotal + *(aiBoard + i);
51     humanTotal = humanTotal + *(humanBoard + i);
52   }
53
54   /* If one side has none, return accordingly. */
55   if (aiTotal == 0 || humanTotal == 0) {
56     /* Calculate the final score. */
57     for (i=1; i<=BOARD_MAX; i++) {
58       *aiBoard = *aiBoard + *(aiBoard + i);
59       *humanBoard = *humanBoard + *(humanBoard + i);
60       *(aiBoard + i) = *(humanBoard + i) = 0;
61     }    
62     return 1;
63   }
64   else
65     return 0;
66
67 }
68
69 /* Make a move, and return the position of the last stone! */
70 int move(int *activeBoard, int *passiveBoard, int move) {
71
72   int *currentPosition, stones;
73
74   currentPosition = activeBoard + move;
75
76   stones = *(activeBoard + move);
77   if (stones > 0){
78       /* Pick up the stones. */
79       *(activeBoard + move) = 0;
80
81       /* Loop for each stone. */
82       for (; stones>0; stones--) {
83               /* Move to the next board location. */
84               if (currentPosition == activeBoard)
85                       currentPosition = passiveBoard + BOARD_MAX;
86               else if (currentPosition == passiveBoard + 1)
87                       currentPosition = activeBoard + BOARD_MAX;
88               else
89                       currentPosition--;
90               /* Drop a stone. */
91               (*currentPosition)++;
92       }
93       
94       /* If the last stone lands on an empty hole... */
95       if (*currentPosition == 1 && activeBoard < currentPosition && 
96               currentPosition <= (activeBoard + BOARD_MAX)) {
97               /* ...calculate the position of the opposite hole... */
98               int oppHole = (BOARD_MAX + 1) - (currentPosition - activeBoard);
99               /* ...and make the capture. */
100               *activeBoard = *activeBoard + *(passiveBoard + oppHole);
101               printf("Captured %d stones.\n", *(passiveBoard + oppHole));
102               *(passiveBoard + oppHole) = 0;
103       }
104       
105       return currentPosition - activeBoard;
106   }
107   else{
108       printf("move from empty hole...\n");
109       /* return 0 so human can make a move again */
110       return 0;
111   }
112    
113 }
114
115 int rand_btw(int min, int max) {
116
117         int range;
118
119         range = (max - min) + 1;
120
121         return ((rand() % range) + min);
122
123 }
124   
125 /*  End mancala.c  */