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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [CONNECTK/] [connectk-2.0/] [src/] [ai/] [ai.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 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
        { "serial", "Serial Port", ai_serial },
45
};
46
 
47
static gboolean is_adjacent(const Board *b, BCOORD x, BCOORD y, int dist)
48
{
49
        int dx, dy, count;
50
        PIECE p;
51
 
52
        if (!piece_empty(piece_at(b, x, y)))
53
                return FALSE;
54
        for (dy = -1; dy < 2; dy++)
55
                for (dx = -1; dx < 2; dx++) {
56
                        if (!dx && !dy)
57
                                continue;
58
                        count = count_pieces(b, x, y, PIECE_NONE, dx, dy, &p);
59
                        if (count - 1 < dist && p != PIECE_NONE)
60
                                return TRUE;
61
                }
62
        return FALSE;
63
}
64
 
65
AIMoves *enum_adjacent(const Board *b, int dist)
66
{
67
        AIMoves *moves;
68
        AIMove move;
69
 
70
        move.weight = AIW_NONE;
71
        moves = aimoves_new();
72
        for (move.y = 0; move.y < board_size; move.y++)
73
                for (move.x = 0; move.x < board_size; move.x++)
74
                        if (is_adjacent(b, move.x, move.y, dist))
75
                                aimoves_append(moves, &move);
76
        aimoves_shuffle(moves);
77
        return moves;
78
}
79
 
80
AIMoves *ai_marks(const Board *b, PIECE min)
81
{
82
        AIMoves *moves = aimoves_new();
83
        AIMove move;
84
        PIECE p;
85
 
86
        for (move.y = 0; move.y < board_size; move.y++)
87
                for (move.x = 0; move.x < board_size; move.x++)
88
                        if ((p = piece_at(b, move.x, move.y)) >= min) {
89
                                move.weight = p - PIECE_THREAT0;
90
                                aimoves_set(moves, &move);
91
                        }
92
        return moves;
93
}
94
 
95
AIMoves *ai_random(const Board *b)
96
/* Returns a list of all empty tiles */
97
{
98
        AIMove move;
99
        AIMoves *moves;
100
 
101
        moves = aimoves_new();
102
        for (move.y = 0; move.y < board_size; move.y++)
103
                for (move.x = 0; move.x < board_size; move.x++)
104
                        if (piece_empty(piece_at(b, move.x, move.y))) {
105
                                move.weight =
106
                                           g_random_int_range(AIW_MIN, AIW_MAX);
107
                                aimoves_append(moves, &move);
108
                        }
109
        return moves;
110
}
111
 
112
AIMoves *ai_adjacent(const Board *b)
113
{
114
        AIMove move;
115
        AIMoves *moves;
116
 
117
        /* Get all open tiles adjacent to any piece */
118
        moves = enum_adjacent(b, 1);
119
        printf("ai adjacen move %d %d\n", moves->data[1].x, moves->data[1].y);
120
        if (moves->len)
121
                return moves;
122
 
123
        /* Play in the middle if there are no open adjacent tiles */
124
        move.x = board_size / 2;
125
        move.y = board_size / 2;
126
        move.weight = AIW_NONE;
127
        aimoves_append(moves, &move);
128
        return moves;
129
}
130
 
131
const char *player_to_string(PLAYER p)
132
{
133
        return ais[p].l_desc;
134
}
135
 
136
int number_of_ais(void)
137
{
138
        return sizeof (ais) / sizeof (*ais);
139
}
140
 
141
AI *ai(int n)
142
{
143
        if (n >= 0 && n < number_of_ais())
144
                return ais + n;
145
        return NULL;
146
}
147
 

powered by: WebSVN 2.1.0

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