Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/boards/awele_alphaBeta.c
blob: 76cf733363f6bdedec02cde3aa7365416919f498 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * gcompris - awele.c
 *
 * Copyright (C) 2005 Frederic Mazzarol
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "awele_utils.h"
#include <string.h>
#include <stdlib.h>
#include "gcompris/gcompris.h"


static gint maxprof;

/**
* Fonction d'evaluation d'un plateau
* La fonction d'evaluation va evaluer la difference du nombre de graines capturees (Facteur preponderant),\n
* la difference de la mobilite des deux joueurs, la difference des cases menacantes,\n
* et la difference du nombre de graine active de chaque joueur.\n
* @param AWALE *aw Pointeur sur la structure AWALE a evaluer
* @return Une note d'evaluation du plateau.
*/
gint eval (GNode *node){
  AWALE *aw = node->data;

  if (aw->CapturedBeans[COMPUTER] > 24)
    return 25;

  if (aw->CapturedBeans[HUMAN] > 24)
    return -25;

  return (aw->CapturedBeans[COMPUTER] - aw->CapturedBeans[HUMAN]);
}

/*
 * Evaluation function for level 1-2
 * this function returns always 0. The play is random,
 * because tree building is randomised.
 *
 */
gint eval_to_null (GNode *node){
  return 0;
}


gint eval_to_best_capture (GNode *node){
  AWALE *aw = node->data;

  return (aw->CapturedBeans[COMPUTER]);
}

/*
 * firstChild. create all the childs and return first one
 */
GNode *firstChild(GNode *node)
{
  AWALE *aw = node->data;
  AWALE *tmpaw;
  GNode *tmpnode;
  gint eval_node = eval(node);
  gint rand_play;

  /* Case node is winning one */
  if ((eval_node == 25) || (eval_node == -25))
    return NULL;

  gint i;
  rand_play = g_random_int_range(1, 5);

  for (i = 0 ; i < 6; i++)
    {
      tmpaw = moveAwale((rand_play + i)%6 + ((aw->player == HUMAN )? 6 : 0), aw);
      if (tmpaw){
	tmpnode = g_node_new(tmpaw);
	g_node_insert (node, -1, tmpnode);
      }
    }

  return g_node_first_child(node);
}

/* next sibling */
GNode *nextSibling(GNode *node)
{
  return g_node_next_sibling(node);
}


gboolean free_awale(GNode *node,
		    gpointer data){
  g_free(data);
  return TRUE;
}


/**
* Fonction de jeu de la machine
* Cette Fonction est appelee pour faire jouer l'ordinateur, \n
* la racine de l'arbre est cree, puis passe en argument a la fonction AlphaBeta\n
* La profondeur augmente au fur et mesure de la partie quand le nombre de graines diminue.\n
* @param aw Un pointeur sur le plateau a partir duquel reflechir
* @return Le meilleur coup calcule par la machine
* le player est celui qui a joué le dernier coup.
*/

short int  think( AWALE *static_awale, short int level){

  AWALE *aw = g_malloc(sizeof(AWALE));
  memcpy (aw, static_awale, sizeof(AWALE));

  GNode *t = g_node_new(aw) ;

  int best = -1;
  int value = 0;
  EvalFunction use_eval = NULL;

  switch (level) {
  case 1:
    maxprof = 1;
    use_eval = (EvalFunction)&eval_to_null;
    g_warning("search depth 1, evaluation null");
    break;
  case 2:
    maxprof = 1;
    use_eval = (EvalFunction)&eval_to_best_capture;
    g_warning("search depth 1, evaluation best capture");
    break;
  case 3:
  case 4:
    maxprof = 2;
    use_eval = (EvalFunction)&eval;
    g_warning("search depth %d, evaluation best difference", maxprof);
    break;
  case 5:
  case 6:
    maxprof = 4;
    use_eval = (EvalFunction)&eval;
    g_warning("search depth %d, evaluation best difference", maxprof);
    break;
  case 7:
  case 8:
    maxprof = 6;
    use_eval = (EvalFunction)&eval;
    g_warning("search depth %d, evaluation best difference", maxprof);
    break;
  case 9:
    maxprof = 8;
    use_eval = (EvalFunction)&eval;
    g_warning("search depth %d, evaluation best difference", maxprof);
    break;
  default:
    maxprof = 8;
    use_eval = (EvalFunction)&eval;
    g_warning("search depth %d, evaluation best difference", maxprof);
    break;
  }

  value = gc_alphabeta( TRUE,
			t,
			use_eval,
			&best,
			(FirstChildGameFunction) firstChild,
			(NextSiblingGameFunction) nextSibling,
			-INFINI ,
			INFINI,
			maxprof) ;

  if (best < 0){
    g_warning("Leaf node, game is over");
    return -1;
  }
  GNode *tmpNode = g_node_nth_child (t, best);

  AWALE *tmpaw = tmpNode->data;

  g_warning("THINK best : %d, play: %d", value, tmpaw->last_play);

  best = tmpaw->last_play;

  /* free awales*/
  g_node_traverse (t,
		   G_IN_ORDER,
		   G_TRAVERSE_ALL,
		   -1,
		   (GNodeTraverseFunc) free_awale,
		   NULL);

  /* free tree */
  g_node_destroy(t);

  return (best);
}