File: robot_random_board.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 (162 lines) | stat: -rw-r--r-- 2,939 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
/*	$Id: robot_random_board.c,v 1.2 2001/04/04 15:37:28 riq Exp $	*/
/* 
 * Archivo que contiene los algoritmos para generar
 * una tabla aleatoria valida
 */

#include <stdlib.h>
#include <config.h>
#include <glib.h>

#include "robot_cliente.h"
#include "robot_random_board.h"
#include "protocol.h"

void generar( void )
{
	GSettings *settings;
	gint x,y,d,t,i,j;

	settings = g_settings_new("net.sf.batnav.gbnrobot");
	cliente.random = g_settings_get_uint(settings, "random");
	srandom( cliente.random );
	cliente.random = random();		
	g_settings_set_uint(settings, "random", cliente.random);
	g_object_unref(settings);

	for(i=0;i<10;i++) { /* clean tabla */
		for(j=0;j<10;j++)
			cliente.mitabla[i][j]=NOBARCO;
	}

	for(i=1;i<=10;i++) {	
		x = 1+(int) (10.0*rand()/(RAND_MAX+1.0));
		y = 1+(int) (10.0*rand()/(RAND_MAX+1.0));
		d = (x + y) & 1;		/* Direccion */
		t = tamano( i );		/* Tamao */

		buscar_poscicion(x,y,d,t);
	}

}

gint posible( gint x, gint y, gint d, gint t)
{
	gint i;

	/* Por tamano, entra ? */	
	if( !entra( x,y,d,t) )
		return FALSE;
	
	/* Por colision, vale ? */
	if(d==0) {				/* Horizontal */ 
		for( i=0; i<=t+1; i++ ) {
			if(!( vacio_board(x-1+i,y) && vacio_board( x-1+i,y-1) && vacio_board( x-1+i,y+1))	)
				return FALSE;
		}
		return TRUE;
	} else {					/* Vertical */
		for( i=0; i<=t+1; i++ ) {
			if(! (vacio_board( x, y-1+i ) && vacio_board( x-1, y-1+i ) && vacio_board( x+1, y-1+i ))	)
				return FALSE;
		}
		return TRUE;
	}
}

/*
 * Algoritmo recursivo
 */
void 
buscar_poscicion( gint x, gint y, gint d, gint t)
{
	/* Pregunta si es posible en la otra direccion */
	if( posible( x,y, ((d^1) & 1),t )) {
		poner_barco( x,y,  ((d^1) &1), t);
		return;
	}

	/* FIXME: Usar algo mas potable (backtracking talvez ?) */
	/* Incrementa Y en uno y asi sucesivamente */
	/* Digamos que recorre todo */	
	y++;
	if(y>10) {
		y=1;
		x++;
		if(x>10)
			x=1;
	}
	if( posible( x,y,d,t) ) {
		poner_barco( x,y,d,t);
		return ;
	} else
		return buscar_poscicion( x,y,d,t);
}


gint tamano( gint i )
{
	gint a;

	switch(i) {	
	case 1:
	case 3:
	case 7:
	case 8:
		a = 1;
		break;
	case 2:
	case 9:
	case 5:
		a = 2;
		break;
	case 4:
	case 10:
		a = 3;
		break;
	case 6:
		a = 4;
		break;
	}
	return a;
}

/* Pregunta si un barco entra por tamano */
gint entra( gint x, gint y, gint d, gint t)
{
	if(d==0) { /* Horizontal */
		if(x+t-1<=10)
			return TRUE;
		else
			return FALSE;
	} else { /* Vertical */
		if(y+t-1<=10)
			return TRUE;
		else
			return FALSE;
	}
}

gint vacio_board( gint x, gint y)
{
	if(x<1 || x>10 || y<1 || y>10)
		return TRUE;

	if( cliente.mitabla[x-1][y-1]==NOBARCO || cliente.mitabla[x-1][y-1]==AGUA )
		return TRUE;
	else
		return FALSE;
}

void poner_barco( gint x, gint y, gint d, gint t)
{
	gint i;

	if(d==0) {				/* Horizontal */
		for(i=1;i<=t;i++)
			cliente.mitabla[x-2+i][y-1]=BARCO;
	} else {				/* Vertical */
		for(i=1;i<=t;i++)
			cliente.mitabla[x-1][y-2+i]=BARCO;
	}
}