OpenCores

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [BUILD_SCC/] [synth_src/] [ai.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 sumanta.chaudhuri
 
2
/*
3
 
4
connectk -- a program to play the connect-k family of games
5
Copyright (C) 2007 Michael Levin
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 <string.h>
25
#include <math.h>
26
#include <glib.h>
27
#include "../shared.h"
28
#include "../connectk.h"
29
 
30
/*
31
 *      AIs
32
 */
33
 
34
static AI ais[] = {
35
        { "human", "Human", NULL },
36
        { "random", "Random", ai_random },
37
        { "adjacent", "Adjacent", ai_adjacent },
38
        { "threats", "Threats", ai_threats },
39
        /*{ "windows", "Windows", ai_windows },*/
40
        /*{ "priority", "Prioritized Threats", ai_priority },*/
41
        { "sequences", "Sequences", ai_sequences },
42
        { "mesh", "Mesh", ai_mesh },
43
        { "montecarlo", "Monte Carlo", ai_monte_carlo },
44
};
45
 
46
static gboolean is_adjacent(const Board *b, BCOORD x, BCOORD y, int dist)
47
{
48
        int dx, dy, count;
49
        PIECE p;
50
 
51
        if (!piece_empty(piece_at(b, x, y)))
52
                return FALSE;
53
        for (dy = -1; dy < 2; dy++)
54
                for (dx = -1; dx < 2; dx++) {
55
                        if (!dx && !dy)
56
                                continue;
57
                        count = count_pieces(b, x, y, PIECE_NONE, dx, dy, &p);
58
                        if (count - 1 < dist && p != PIECE_NONE)
59
                                return TRUE;
60
                }
61
        return FALSE;
62
}
63
 
64
AIMoves *enum_adjacent(const Board *b, int dist)
65
{
66
        AIMoves *moves;
67
        AIMove move;
68
 
69
        move.weight = AIW_NONE;
70
        moves = aimoves_new();
71
        for (move.y = 0; move.y < board_size; move.y++)
72
                for (move.x = 0; move.x < board_size; move.x++)
73
                        if (is_adjacent(b, move.x, move.y, dist))
74
                                aimoves_append(moves, &move);
75
        aimoves_shuffle(moves);
76
        return moves;
77
}
78
 
79
AIMoves *ai_marks(const Board *b, PIECE min)
80
{
81
        AIMoves *moves = aimoves_new();
82
        AIMove move;
83
        PIECE p;
84
 
85
        for (move.y = 0; move.y < board_size; move.y++)
86
                for (move.x = 0; move.x < board_size; move.x++)
87
                        if ((p = piece_at(b, move.x, move.y)) >= min) {
88
                                move.weight = p - PIECE_THREAT0;
89
                                aimoves_set(moves, &move);
90
                        }
91
        return moves;
92
}
93
 
94
AIMoves *ai_random(const Board *b)
95
/* Returns a list of all empty tiles */
96
{
97
        AIMove move;
98
        AIMoves *moves;
99
 
100
        moves = aimoves_new();
101
        for (move.y = 0; move.y < board_size; move.y++)
102
                for (move.x = 0; move.x < board_size; move.x++)
103
                        if (piece_empty(piece_at(b, move.x, move.y))) {
104
                                move.weight =
105
                                           g_random_int_range(AIW_MIN, AIW_MAX);
106
                                aimoves_append(moves, &move);
107
                        }
108
        return moves;
109
}
110
 
111
AIMoves *ai_adjacent(const Board *b)
112
{
113
        AIMove move;
114
        AIMoves *moves;
115
 
116
        /* Get all open tiles adjacent to any piece */
117
        moves = enum_adjacent(b, 1);
118
        if (moves->len)
119
                return moves;
120
 
121
        /* Play in the middle if there are no open adjacent tiles */
122
        move.x = board_size / 2;
123
        move.y = board_size / 2;
124
        move.weight = AIW_NONE;
125
        aimoves_append(moves, &move);
126
        return moves;
127
}
128
 
129
const char *player_to_string(PLAYER p)
130
{
131
        return ais[p].l_desc;
132
}
133
 
134
int number_of_ais(void)
135
{
136
        return sizeof (ais) / sizeof (*ais);
137
}
138
 
139
AI *ai(int n)
140
{
141
        if (n >= 0 && n < number_of_ais())
142
                return ais + n;
143
        return NULL;
144
}
145
 

powered by: WebSVN 2.1.0

© copyright 1999-2014 OpenCores.org, equivalent to ORSoC AB, all rights reserved. OpenCores®, registered trademark.