File: robot_ai.c

package info (click to toggle)
gbatnav 1.0.4cvs20051004-8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 4,780 kB
  • sloc: ansic: 14,122; sh: 11,667; makefile: 643; yacc: 288; xml: 42; sed: 16
file content (196 lines) | stat: -rw-r--r-- 3,306 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*	$Id: robot_ai.c,v 1.2 2001/04/04 15:37:28 riq Exp $	*/

#include <glib.h>
#include "robot_cliente.h"
#include "protocol.h"
#include "robot_ai.h"

static void pcell( int b, int x, int y, int estado )
{
	if(!(x<0 || x>9 || y<0 || y>9))
		cliente.boards[b][x][y]=estado;
}

static void inteliclient( int b)
{
	gint i,x,y;
   
	x=0;
	y=0;
   
	for(i=0;i<100;i++) {
		switch(cliente.boards[b][x][y]) {
		case HUNDIDO:
		case TOCADO:
			pcell(b,x-1,y-1,AGUA);
			pcell(b,x-1,y+1,AGUA);
			pcell(b,x+1,y-1,AGUA);							
			pcell(b,x+1,y+1,AGUA);
			break;
		case NOBARCO:
			if(x<9 && cliente.boards[b][x+1][y]==HUNDIDO)
				pcell(b,x,y,AGUA);
	     
			if(x>0 && cliente.boards[b][x-1][y]==HUNDIDO)
				pcell(b,x,y,AGUA);
     
			if(y<9 && cliente.boards[b][x][y+1]==HUNDIDO)
				pcell(b,x,y,AGUA);
     
			if(y>0 && cliente.boards[b][x][y-1]==HUNDIDO)
				pcell(b,x,y,AGUA);
			break;
		default:
			break;
		}
		x++;
		if(x==10) {
			x=0;
			y++;
		}
	}
}

static void buscar_next_free( int b, int i, int j, int *x, int *y )
{
	gint k;	

	for(k=0;k<100;k++) {
		if( cliente.boards[b][i][j]==NOBARCO ) {
			*x = i;
			*y = j;
			return;
		}
		i++;
		if(i==10) {
			i=0;
			j++;
			if(j==10)
				j=0;
		}

	}
	printf("gbnrobot %d: buscar_next_free %d: Aca no tengo que llegar\n",cliente.numjug,b);
	*x = 2;
	*y = 2;
}

static int buscar_tocado( int b, int *x, int *y)
{
	gint k;	
	gint i,j;

	i=0;j=0;
	for(k=0;k<100;k++) {
		if( cliente.boards[b][i][j]==TOCADO ) {
			*x = i;
			*y = j;
			return TRUE;
		}
		i++;
		if(i==10) {
			i=0;
			j++;
		}
	}
	return FALSE;
}


static int que_soy_board( int b, int x , int y )
{
	if( x<0 || x>9 || y<0 || y>9)
		return AGUA;	/* -1 */
	else
		return cliente.boards[b][x][y];
}

static int r_tocado_hit( int b, int *x, int *y, int i, int j )
{
	gint a;

	a=que_soy_board( b,*x,*y);
	if(a==NOBARCO)
		return TRUE;
	if(a==TOCADO) {
		*x=*x+i;
		*y=*y+j;
		return r_tocado_hit( b, x, y, i, j);
	} else
		return FALSE;
}

static void buscar_tocado_hit( int b, int *xx, int *yy )
{
	int x,y;

	
	x=*xx;
	y=*yy;
	
	if(!(que_soy_board(b,x,y-1)==TOCADO || que_soy_board(b,x,y+1)==TOCADO )) {
		if( r_tocado_hit( b, &x, &y, -1, 0 )) {
			*xx=x;
			*yy=y;
			return;
		}
		x=*xx;
		y=*yy;
		if( r_tocado_hit( b, &x, &y, +1, 0 )) {
			*xx=x;
			*yy=y;
			return;
		}
	}

	x=*xx;
	y=*yy;
	if( r_tocado_hit( b, &x, &y, 0, -1 )) {
		*xx=x;
		*yy=y;
		return;
	}
	x=*xx;
	y=*yy;
	if( r_tocado_hit( b, &x, &y, 0, +1 )) {
		*xx=x;
		*yy=y;
		return;
	}

}

void robot_ai( int *x, int*y)
{
	int xx,yy;

	switch( cliente.ai ) {

		case ROBOT_AI_VERYMORON:
			*x = (int) (10.0*rand()/(RAND_MAX+1.0));
			*y = (int) (10.0*rand()/(RAND_MAX+1.0));
			break;
		case ROBOT_AI_MORON:
			xx = (int) (10.0*rand()/(RAND_MAX+1.0));
			yy = (int) (10.0*rand()/(RAND_MAX+1.0));
			buscar_next_free( cliente.usrfrom, xx, yy, &xx, &yy );
			*x = xx;
			*y = yy;
			break;
		case ROBOT_AI_AVERAGE:
		case ROBOT_AI_HI:		/* TODO */
			inteliclient(cliente.usrfrom);
			if( buscar_tocado( cliente.usrfrom, &xx, &yy) ) {
				buscar_tocado_hit( cliente.usrfrom, &xx, &yy );
				*x = xx;
				*y = yy;
			} else {
				xx = (int) (10.0*rand()/(RAND_MAX+1.0));
				yy = (int) (10.0*rand()/(RAND_MAX+1.0));
				buscar_next_free( cliente.usrfrom, xx, yy, &xx, &yy );
				*x = xx;
				*y = yy;
			}
			break;
	}
}