OpenCores
URL https://opencores.org/ocsvn/connect-6/connect-6/trunk

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [BUILD_SCC/] [synth_src/] [search.cpp] - Blame information for rev 10

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 sumanta.ch
 
2
/*
3
 
4
connectk -- UMN CSci 5512W project
5
 
6
*/
7
 
8 10 sumanta.ch
//#include "config.h"
9
//#include <string.h>
10
//#include <glib.h>
11
#include "./shared.h"
12
#include "pico.h"
13
//#include "../connectk.h"
14 8 sumanta.ch
 
15
/* Variables required to check for cache hits */
16 10 sumanta.ch
//static int cache_id = -1, cache_depth = -1, cache_branch = -1;
17
//static SEARCH cache_search = -1;
18
//static AIMoves *cache_moves = NULL;
19
//static AIWEIGHT cache_best;
20
//static Player *cache_player;
21
//static AIFunc cache_func;
22
//static BCOORD cache_size;
23 8 sumanta.ch
 
24 10 sumanta.ch
int ai_stop=0;
25
static AIWEIGHT df_search(Board *b, AIMoves *moves,unsigned int *index, Player *player,
26 8 sumanta.ch
                          int depth, int cache_index,
27
                          PIECE searched, AIWEIGHT alpha, AIWEIGHT beta)
28
/* Depth is in _moves_ */
29
{
30
        int i, j;
31
 
32
        /* Halt and depth abort */
33
        if (ai_stop || depth < 1)
34
                return moves->utility;
35
 
36
        /* Alpha-beta sanity check */
37
        if (alpha >= beta) {
38 10 sumanta.ch
                //g_warning("DFS alpha-beta failed sanity check");
39 8 sumanta.ch
                return moves->utility;
40
        }
41
 
42
        /* Search only the top moves beyond the minimum */
43 10 sumanta.ch
        //aimoves_sort(moves);
44 8 sumanta.ch
        if (moves->len > player->branch) {
45
                for (i = player->branch; i < moves->len; i++)
46
                        if (moves->data[i].weight != moves->data[0].weight)
47
                                break;
48
                moves->len = i;
49
        }
50
 
51
        /* No moves left -- its a draw */
52
        if (moves->len < 1)
53
                return AIW_DRAW;
54
 
55
        /* Search each move available in depth first order */
56
        for (i = 0; i < moves->len; i++) {
57 10 sumanta.ch
                static Board b_next;
58 8 sumanta.ch
                AIMove *aim = moves->data + i;
59 10 sumanta.ch
                AIMoves moves_next;
60 8 sumanta.ch
 
61
                /* Did we get a bad move? */
62
                if (!piece_empty(piece_at(b, aim->x, aim->y))) {
63 10 sumanta.ch
                        //g_warning("DFS utility function suggested a bad move "
64
                                  //"(%s)", bcoords_to_string(aim->x, aim->y));
65 8 sumanta.ch
                        continue;
66
                }
67
 
68
                /* Already searched here? */
69
                if (piece_at(b, aim->x, aim->y) == searched)
70
                        continue;
71
                place_piece_type(b, aim->x, aim->y, searched);
72
 
73 10 sumanta.ch
                //b_next = board_new();
74
                board_copy(b, &b_next);
75
                place_piece(&b_next, aim->x, aim->y);
76 8 sumanta.ch
 
77
                /* Did we win? */
78 10 sumanta.ch
                if (check_win(&b_next, aim->x, aim->y))
79 8 sumanta.ch
                        aim->weight = AIW_WIN;
80
 
81
                /* Otherwise, search deeper */
82
                else  {
83
                        int next_ci = cache_index + 1;
84
                        AIWEIGHT next_alpha = alpha, next_beta = beta;
85 10 sumanta.ch
                        //AIFunc func;
86 8 sumanta.ch
 
87 10 sumanta.ch
                        b_next.moves_left--;
88 8 sumanta.ch
 
89
                        /* Player has changed */
90 10 sumanta.ch
                        if (b_next.moves_left <= 0) {
91
                                b_next.moves_left = place_p;
92
                                b_next.turn = other_player(b->turn);
93 8 sumanta.ch
                                next_ci += place_p;
94
                                searched++;
95
                                next_alpha = -beta;
96
                                next_beta = -alpha;
97
                        }
98
 
99 10 sumanta.ch
                        //func = ai(player->ai)->func;
100
                        //if (!func) {
101
                        //        g_warning("DFS player has no AI function");
102
                        //        return moves->utility;
103
                        //}
104
                        //moves_next = func(b_next);
105
                        ai_threats(&b_next,&moves_next,index);
106
 
107
                        aim->weight = df_search(&b_next, &moves_next, index,player,
108 8 sumanta.ch
                                                depth - 1, next_ci, searched,
109
                                                next_alpha, next_beta);
110 10 sumanta.ch
                        //aimoves_free(moves_next);
111
                        if (b_next.turn != b->turn)
112 8 sumanta.ch
                                aim->weight = -aim->weight;
113
                }
114
 
115
                /* Debug search */
116 10 sumanta.ch
                //if (opt_debug_dfsc) {
117
                //        for(j = MAX_DEPTH - depth; j > 0; j--)
118
                //                //g_print("-");
119
                //        //g_print("> d=%d, %s, u=%d, a=%d, b=%d %s\n",
120
                //        //        depth, bcoords_to_string(aim->x, aim->y),
121
                //        //        aim->weight, alpha, beta,
122
                //        //        piece_to_string(b->turn));
123
                //}
124 8 sumanta.ch
 
125 10 sumanta.ch
                //board_free(b_next);
126 8 sumanta.ch
                if (aim->weight > alpha) {
127
                        alpha = aim->weight;
128 10 sumanta.ch
                        //cache_set(cache_index, aim);
129 8 sumanta.ch
 
130
                        /* Victory abort */
131
                        if (alpha >= AIW_WIN)
132
                                return AIW_WIN;
133
 
134
                        /* Alpha-beta pruning */
135
                        if (alpha >= beta)
136
                                return alpha;
137
                }
138
        }
139
 
140
        return alpha;
141
}
142
 
143 10 sumanta.ch
int  search(const Board *b, AIMove *move, Player *player)
144 8 sumanta.ch
{
145 10 sumanta.ch
        AIMoves moves;
146
        Board copy;
147
        unsigned int index[361]={0};
148
        //AIFunc move_func = ai(player->ai)->func;
149 8 sumanta.ch
 
150
        /* Player is not configured to search */
151 10 sumanta.ch
        //if (player->search == SEARCH_NONE)
152
        //        return;
153 8 sumanta.ch
 
154
        /* Moves list does not need to be searched */
155 10 sumanta.ch
        //if (moves->len <= b->moves_left) {
156
        ////        if (opt_debug_dfsc)
157
        ////                g_debug("DFS no choice abort");
158
        //        return;
159
        //}
160 8 sumanta.ch
 
161 10 sumanta.ch
        ///* Board size changed, cache is invalidated */
162
        //if (board_size != cache_size)
163
        //        cache_moves = NULL;
164
        //cache_size = board_size;
165 8 sumanta.ch
 
166 10 sumanta.ch
        ///* Cache hit, last or same board */
167
        //if (player->cache && cache_moves && cache_moves->len &&
168
        //    cache_search == player->search &&
169
        //    cache_depth == player->depth &&
170
        //    cache_player == player &&
171
        //    cache_func == move_func &&
172
        //    cache_branch == player->branch) {
173
        //        if (b->parent && cache_id == b->parent->ac.id) {
174
        //                aimoves_remove(cache_moves, b->parent->move_x,
175
        //                               b->parent->move_y);
176
        //                cache_id = b->ac.id;
177
        //        }
178
        //        if (cache_id == b->ac.id && cache_moves->len) {
179
        //                if (cache_moves->len) {
180
        //                        aimoves_copy(cache_moves, moves);
181
        //                        if (opt_debug_dfsc)
182
        //                                g_debug("DFS cache HIT");
183
        //                        return;
184
        //                }
185
        //                aimoves_free(cache_moves);
186
        //                cache_moves = NULL;
187
        //        }
188
        //}
189 8 sumanta.ch
 
190
        /* Cache miss */
191 10 sumanta.ch
        //if (opt_debug_dfsc)
192
        //        g_debug("DFS cache MISS");
193
        //cache_id = b->ac.id;
194
        //if (!cache_moves)
195
        //        cache_moves = aimoves_new();
196
        //cache_moves->len = 0;
197
        //cache_best = AIW_MIN;
198
        //copy = board_new();
199
        board_copy(b, &copy);
200
        ai_threats(&copy,&moves,&index[0]);
201
 
202
        //if (player->search == SEARCH_DFS) {
203
                df_search(&copy, &moves, &index[0],player, player->depth, 0,
204 8 sumanta.ch
                          PIECE_SEARCHED, AIW_LOSE, AIW_WIN);
205 10 sumanta.ch
        int ret_val;
206
        ret_val=aimoves_choose(&moves, move,&index[0]);
207
        if (!ret_val)
208
                return 0;
209
        else return 1;
210
          //      if (cache_moves->len)
211
          //              aimoves_copy(cache_moves, moves);
212
        //} else {
213
        //        board_free(copy);
214
        //        g_warning("Unsupported search type %d", player->search);
215
        //        return;
216
        //}
217
        //board_free(copy);
218 8 sumanta.ch
 
219 10 sumanta.ch
        ///* Debug DFS search */
220
        //if (opt_debug_dfsc)
221
        //        dfs_cache_dump();
222 8 sumanta.ch
 
223 10 sumanta.ch
        ///* Save params so we can check if we have a hit later */
224
        //cache_player = player;
225
        //cache_search = player->search;
226
        //cache_depth = player->depth;
227
        //cache_branch = player->branch;
228
        //cache_func = move_func;
229 8 sumanta.ch
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.