3
* Copyright (C) 2005, 2008 Frederic Mazzarol
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, see <http://www.gnu.org/licenses/>.
19
#include "awele_utils.h"
22
#include "gcompris/gcompris.h"
28
* Fonction d'evaluation d'un plateau
29
* La fonction d'evaluation va evaluer la difference du nombre de graines capturees (Facteur preponderant),\n
30
* la difference de la mobilite des deux joueurs, la difference des cases menacantes,\n
31
* et la difference du nombre de graine active de chaque joueur.\n
32
* @param AWALE *aw Pointeur sur la structure AWALE a evaluer
33
* @return Une note d'evaluation du plateau.
35
gint eval (GNode *node){
36
AWALE *aw = node->data;
38
if (aw->CapturedBeans[COMPUTER] > 24)
41
if (aw->CapturedBeans[HUMAN] > 24)
44
return (aw->CapturedBeans[COMPUTER] - aw->CapturedBeans[HUMAN]);
48
* Evaluation function for level 1-2
49
* this function returns always 0. The play is random,
50
* because tree building is randomised.
53
gint eval_to_null (GNode *node){
58
gint eval_to_best_capture (GNode *node){
59
AWALE *aw = node->data;
61
return (aw->CapturedBeans[COMPUTER]);
65
* firstChild. create all the childs and return first one
67
GNode *firstChild(GNode *node)
69
AWALE *aw = node->data;
72
gint eval_node = eval(node);
75
/* Case node is winning one */
76
if ((eval_node == 25) || (eval_node == -25))
80
rand_play = g_random_int_range(1, 5);
82
for (i = 0 ; i < 6; i++)
84
tmpaw = moveAwale((rand_play + i)%6 + ((aw->player == HUMAN )? 6 : 0), aw);
86
tmpnode = g_node_new(tmpaw);
87
g_node_insert (node, -1, tmpnode);
91
return g_node_first_child(node);
95
GNode *nextSibling(GNode *node)
97
return g_node_next_sibling(node);
101
gboolean free_awale(GNode *node,
109
* Fonction de jeu de la machine
110
* Cette Fonction est appelee pour faire jouer l'ordinateur, \n
111
* la racine de l'arbre est cree, puis passe en argument a la fonction AlphaBeta\n
112
* La profondeur augmente au fur et mesure de la partie quand le nombre de graines diminue.\n
113
* @param aw Un pointeur sur le plateau a partir duquel reflechir
114
* @return Le meilleur coup calcule par la machine
115
* le player est celui qui a joué le dernier coup.
118
short int think( AWALE *static_awale, short int level){
120
AWALE *aw = g_malloc(sizeof(AWALE));
121
memcpy (aw, static_awale, sizeof(AWALE));
123
GNode *t = g_node_new(aw) ;
127
EvalFunction use_eval = NULL;
132
use_eval = (EvalFunction)&eval_to_null;
133
g_warning("search depth 1, evaluation null");
137
use_eval = (EvalFunction)&eval_to_best_capture;
138
g_warning("search depth 1, evaluation best capture");
143
use_eval = (EvalFunction)&eval;
144
g_warning("search depth %d, evaluation best difference", maxprof);
149
use_eval = (EvalFunction)&eval;
150
g_warning("search depth %d, evaluation best difference", maxprof);
155
use_eval = (EvalFunction)&eval;
156
g_warning("search depth %d, evaluation best difference", maxprof);
160
use_eval = (EvalFunction)&eval;
161
g_warning("search depth %d, evaluation best difference", maxprof);
165
use_eval = (EvalFunction)&eval;
166
g_warning("search depth %d, evaluation best difference", maxprof);
170
value = gc_alphabeta( TRUE,
174
(FirstChildGameFunction) firstChild,
175
(NextSiblingGameFunction) nextSibling,
181
g_warning("Leaf node, game is over");
184
GNode *tmpNode = g_node_nth_child (t, best);
186
AWALE *tmpaw = tmpNode->data;
188
g_warning("THINK best : %d, play: %d", value, tmpaw->last_play);
190
best = tmpaw->last_play;
197
(GNodeTraverseFunc) free_awale,