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

Subversion Repositories connect-6

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 sumanta.ch
/*
2
   connect6.cpp
3
   June 9, 2011
4
   This file contains the game AI
5
   By Kevin Nam
6
 
7
 */
8
 
9
//#include <time.h>
10
//#include <stdlib.h>
11
//
12
//#include "util.h"
13
//#include "connect6.h"
14
#include "./shared.h"
15
 
16
// Subtract this many points for moves at the edges.
17
#define EDGEPENALTY 5
18
 
19
 
20
/*  The cost function simply counts all of the consecutive stones of same colour in
21
    every direction from the spot for which the points is being calculated.
22
 
23
Ex:
24
 
25
.DDLL
26
.DLDD
27
DXDDD
28
...D.
29
 
30
Above, X is the spot being calculated.
31
The points would be 2 (above) + 2(topright) + 3(right) + 1 (left) = 8.
32
It treats opponent's stones and own stones with equal weighting.
33
 
34
Return 0 if the spot y,x is already taken, else return the calculated value
35
 
36
 */
37
void move_to_ascii(int x,int y, char *move){
38
    if (y >= 10){
39
        move[0] = '1';
40
        y -= 10;
41
    } else {
42
        move[0] = '0';
43
    }
44
    if      (y == 0) move[1] = '0';
45
    else if (y == 1) move[1] = '1';
46
    else if (y == 2) move[1] = '2';
47
    else if (y == 3) move[1] = '3';
48
    else if (y == 4) move[1] = '4';
49
    else if (y == 5) move[1] = '5';
50
    else if (y == 6) move[1] = '6';
51
    else if (y == 7) move[1] = '7';
52
    else if (y == 8) move[1] = '8';
53
    else if (y == 9) move[1] = '9';
54
 
55
    // Do same for x.
56
    if (x >= 10){
57
        move[2] = '1';
58
        x -= 10;
59
    } else {
60
        move[2] = '0';
61
    }
62
    if      (x == 0) move[3] = '0';
63
    else if (x == 1) move[3] = '1';
64
    else if (x == 2) move[3] = '2';
65
    else if (x == 3) move[3] = '3';
66
    else if (x == 4) move[3] = '4';
67
    else if (x == 5) move[3] = '5';
68
    else if (x == 6) move[3] = '6';
69
    else if (x == 7) move[3] = '7';
70
    else if (x == 8) move[3] = '8';
71
    else if (x == 9) move[3] = '9';
72
 
73
}
74
 
75
static int char_to_int(short x){
76
        if(x>=48)
77
                return x-48;
78
        else
79
                return 0;
80
}
81
 
82
 
83
/*
84
   The AI Function that calls the cost function for every spot on the board.
85
   It returns the location with the highest points. In the event of a tie, randomly decide.
86
   Input the board and the colour being played.
87
   Puts the move (in ASCII chars) inside move[4]. This is from [1 ... 19]
88
   Puts the move (in integers) in moveY and moveX. This is from [0 ... 18]
89
 */
90
int connect6ai_synth(int firstmove,char movein[8], char colour, char moveout[8]){
91
        #pragma bitsize firstmove 17
92
        short x,y,highx = 0;
93
        Board *myboard ;
94
        AIMoves *moves;
95
        //-------------------------------------------------------------------------
96
        if((firstmove >= 1)){
97
                //update the board
98
                y = char_to_int(movein[0])*10 + char_to_int(movein[1]) - 1;
99
                x = char_to_int(movein[2])*10 + char_to_int(movein[3]) - 1;
100
                if(colour==68){//'D')
101
                        //myboard[y][x] = (char)2;//76;//'L';
102
        place_piece_type(myboard,x,y,PIECE_WHITE);
103
        myboard->turn=PIECE_BLACK;
104
                }else{
105
                        //myboard[y][x] = (char)1;//68;//'D';
106
        place_piece_type(myboard,x,y,PIECE_BLACK);
107
        myboard->turn=PIECE_WHITE;
108
                }
109
        }
110
        if((firstmove >=3)){
111
                //update the board
112
                y = char_to_int(movein[4])*10 + char_to_int(movein[5]) - 1;
113
                x = char_to_int(movein[6])*10 + char_to_int(movein[7]) - 1;
114
                if(colour==68){//'D')
115
                        //myboard[y][x] = (char)2;//76;//'L';
116
        place_piece_type(myboard,x,y,PIECE_WHITE);
117
        myboard->turn=PIECE_BLACK;
118
                }else{
119
                        //myboard[y][x] = (char)1;//68;//'D';
120
        place_piece_type(myboard,x,y,PIECE_BLACK);
121
        myboard->turn=PIECE_WHITE;
122
                }
123
        }
124
        moves=ai_threats(myboard);
125
        // Modify the myboard based on current move.
126
        place_piece_type(myboard,moves->data[1].x,moves->data[1].y,myboard->turn);
127
        /// Convert the int coordinates to corresponding ASCII chars
128
 
129
        move_to_ascii(moves->data[1].x+1,moves->data[1].y+1,moveout);
130
 
131
 
132
        //-------------------------------------------------------------------------
133
        if(firstmove>=1){
134
        moves=ai_threats(myboard);
135
        // Modify the myboard based on current move.
136
        place_piece_type(myboard,moves->data[1].x,moves->data[1].y,myboard->turn);
137
 
138
                /// Convert the int coordinates to corresponding ASCII chars
139
        move_to_ascii(moves->data[1].x+1,moves->data[1].y+1,&moveout[4]);
140
        }
141
        return 0;
142
}
143
 
144
 
145
 

powered by: WebSVN 2.1.0

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