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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [XILINX/] [BUILD_SCC_SRCH/] [synth_src/] [search.cpp] - Blame information for rev 17

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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