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 $
9 * Copyright (C) 2003, 2004 Kevin Riggle
10 * Copyright (C) 2009 Reto Zingg
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
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.
29 /* Set up the game board. */
30 void gameInit(int *aiBoard, int *humanBoard) {
34 *aiBoard = *humanBoard = 0;
36 for (i=1; i<=BOARD_MAX; i++)
37 *(aiBoard + i) = *(humanBoard + i) = INITIAL_STONES;
41 /* Has the game been won by someone? */
42 int gameWon(int *aiBoard, int *humanBoard) {
44 int aiTotal, humanTotal, i;
46 aiTotal = humanTotal = 0;
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);
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;
69 /* Make a move, and return the position of the last stone! */
70 int move(int *activeBoard, int *passiveBoard, int move) {
72 int *currentPosition, stones;
74 currentPosition = activeBoard + move;
76 stones = *(activeBoard + move);
78 /* Pick up the stones. */
79 *(activeBoard + move) = 0;
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;
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;
105 return currentPosition - activeBoard;
108 printf("move from empty hole...\n");
109 /* return 0 so human can make a move again */
115 int rand_btw(int min, int max) {
119 range = (max - min) + 1;
121 return ((rand() % range) + min);