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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [CONNECTK/] [connectk-2.0/] [src/] [ai/] [montec.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
#define MONTE_N 10
28
#define MONTE_NUM_RUNS 1000
29
 
30
// sequences.c
31
AIMoves *move_utilities(const Board *b);
32
 
33
AIMoves *empty_cells(Board *b)
34
/* returns an array of the empty locations on board b */
35
{
36
        int i, j;
37
        AIMoves *empties = aimoves_new();
38
        AIMove move;
39
 
40
        for (i = 0; i < board_size; i++) {
41
                for (j = 0; j < board_size; j++) {
42
                        if (piece_at(b, i, j) == PIECE_NONE) {
43
                                move.x = i;
44
                                move.y = j;
45
                                move.weight = 0.f;
46
                                aimoves_add(empties, &move);
47
                        }
48
                }
49
        }
50
        return empties;
51
}
52
 
53
int mc_run(Board *b)
54
/* plays a random game on board b, returns 1 if the current player wins
55
and 0 otherwise */
56
{
57
        Board *new_board = board_new();
58
        board_copy(b, new_board);
59
 
60
        AIMove move;
61
        AIMoves *empties;
62
        empties = empty_cells(new_board);
63
        int tries = 0;
64
        int i;
65
 
66
        while ( TRUE ) {
67
 
68
                /* if the board filled up, start over */
69
                if (empties->len == 0) {
70
                        board_copy(b, new_board);
71
                        empties = empty_cells(new_board);
72
                        tries++;
73
                        if (tries == 10) {
74
                                g_debug("bailing");
75
                                board_free(new_board);
76
                                aimoves_free(empties);
77
                                return 0;
78
                        }
79
                }
80
 
81
                i = g_random_int_range(0, empties->len);
82
                move = empties->data[i];
83
                aimoves_remove_index_fast(empties, i);
84
 
85
                place_piece(new_board, move.x, move.y);
86
 
87
                if (check_win(new_board, move.x, move.y)) {
88
                        if (new_board->turn == board->turn) {
89
                                board_free(new_board);
90
                                aimoves_free(empties);
91
                                return 1;
92
                        }
93
                        else {
94
                                board_free(new_board);
95
                                aimoves_free(empties);
96
                                return 0;
97
                        }
98
                }
99
 
100
                new_board->moves_left--;
101
                if (new_board->moves_left == 0) {
102
                        new_board->turn = other_player(new_board->turn);
103
                        new_board->moves_left = place_p;
104
                }
105
        }
106
}
107
 
108
AIMoves *ai_monte_carlo(const Board *b)
109
/* chooses the best move based on which one wins the most random games */
110
{
111
        int i, k, wins, len;
112
 
113
        Board *new_board = board_new();
114
 
115
        AIMove move;
116
        AIMoves *moves = move_utilities(b);
117
        moves->utility = 0;
118
        aimoves_crop(moves, MONTE_N);
119
 
120
        len = moves->len;
121
 
122
        for (i = 0; i < len; i++) {
123
 
124
                move = moves->data[i];
125
 
126
                board_copy(b, new_board);
127
                place_piece(new_board, move.x, move.y);
128
 
129
                if (check_win(new_board, move.x, move.y)) {
130
                        move.weight = MONTE_NUM_RUNS;
131
                        moves->data[i] = move;
132
                        moves->utility += MONTE_NUM_RUNS;
133
                } else {
134
                        /* run the monte carlo trials */
135
                        wins = 0;
136
                        for (k = 0; k < MONTE_NUM_RUNS; k++) {
137
                                wins += mc_run(new_board);
138
                        }
139
                        move.weight = wins;
140
                        moves->data[i] = move;
141
                        moves->utility += wins;
142
                }
143
        }
144
 
145
        board_free(new_board);
146
        return moves;
147
}

powered by: WebSVN 2.1.0

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