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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [CONNECTK/] [connectk-2.0/] [src/] [ai/] [mesh.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 sumanta.ch
 
2
/*
3
 
4
connectk -- a program to play the connect-k family of games
5
Copyright (C) 2007 Jeff Deitch
6
 
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License
9
as published by the Free Software Foundation; either version 2
10
of the License, or (at your option) any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 
21
*/
22
 
23
#include "config.h"
24
#include <glib.h>
25
#include "../shared.h"
26
 
27
int cur_player_weight = 50;
28
int opp_player_weight = 500;
29
int mesh_array[19][19];
30
 
31
int get_mesh_value(int x, int y) {
32
        if (x < 0 || y < 0 || x >= board_size || y >= board_size) {
33
                /* out of bounds */
34
                return 0;
35
        } else {
36
                return mesh_array[x][y];
37
        }
38
}
39
 
40
int stone_weight(int x, int y) {
41
        if (piece_at(board, x, y) == board->turn) {
42
                return cur_player_weight;
43
        } else if (piece_at(board, x, y) == other_player(board->turn)) {
44
                return opp_player_weight;
45
        } else {
46
                return 0;
47
        }
48
}
49
 
50
int surrounding_weight(int x, int y) {
51
 
52
        int weight = 0;
53
        int n = 4;  // number of tiles in each of the 8 radials that contribute to the weight.
54
 
55
        int dx, dy, ddx, ddy, i;
56
        int ddxs[] = {1, 0, 1,  1};
57
        int ddys[] = {0, 1, 1, -1};
58
 
59
        for (i = 0; i < 4; i++) {
60
 
61
                ddx = ddxs[i];
62
                ddy = ddys[i];
63
 
64
                for (dx = -(ddx * n), dy = -(ddy * n); dx <= n && dy <= n; dx += ddx, dy += ddy) {
65
                        if (dx == 0 && dy == 0) {
66
                                continue;
67
                        } else {
68
                                weight += get_mesh_value(x + dx, y + dy);
69
                        }
70
                }
71
        }
72
 
73
        return weight / (8 * n);
74
}
75
 
76
AIMoves *ai_mesh(const Board *b)
77
/* imagine the board is elastic and that each stone has a weight.  The current players stones have
78
a different weight than the opposite players stones.  Placing a stone on the board creates a depression,
79
and the more stones in an area, the deeper the depression.  This ai chooses the lowest, unplayed tile for
80
the next move.  The idea is to create clumps of stones.  */
81
{
82
        AIMove move;
83
        AIMoves *moves = aimoves_new();
84
 
85
        int i, x, y;
86
        int iterations = 10;
87
        int max_weight = 0;
88
 
89
        /* set all values to 0 */
90
        for (x = 0; x < board_size; x++) {
91
                for (y = 0; y < board_size; y++) {
92
                        mesh_array[x][y] = 0;
93
                }
94
        }
95
 
96
        /* iteratively find the depth of each tile */
97
        for (i = 0; i < iterations; i++) {
98
                for (x = 0; x < board_size; x++) {
99
                        for (y = 0; y < board_size; y++) {
100
                                mesh_array[x][y] = stone_weight(x, y) + surrounding_weight(x, y);
101
                        }
102
                }
103
        }
104
 
105
        /* find the max weight (i.e. the lowest spot on the board) */
106
        for (x = 0; x < board_size; x++) {
107
                for (y = 0; y < board_size; y++) {
108
                        if (piece_at(b, x, y) == PIECE_NONE) {
109
                                move.weight = mesh_array[x][y];
110
                                if (move.weight > max_weight)
111
                                        max_weight = move.weight;
112
                                move.x = x;
113
                                move.y = y;
114
                                aimoves_add(moves, &move);
115
                        }
116
                }
117
        }
118
 
119
        /* if the board is empty, play in the middle */
120
        if (max_weight == 0) {
121
                move.x = board_size / 2;
122
                move.y = board_size / 2;
123
                move.weight = 1;
124
                aimoves_add(moves, &move);
125
        }
126
 
127
        moves->utility = max_weight;
128
 
129
        /* return the array */
130
        return moves;
131
}

powered by: WebSVN 2.1.0

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