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
|
/* $Id: random_board.c,v 1.3 2002/04/19 04:12:18 riq Exp $ */
/*
* Archivo que contiene los algoritmos para generar
* una tabla aleatoria valida
*/
/* 'Users use same prandom in localhost' fix by Radovan Garabik <garabik@atlas03.dnp.fmph.uniba.sk> */
#include <stdlib.h>
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "protocol.h"
#include "cliente.h"
#include "random_board.h"
#include "pantalla.h"
void
generar( void )
{
gint x,y,d,t,i,j;
if( usuario.play==PLAY || usuario.play==TURN ) {
/* permito que un PERDIO modifique su board */
textfill(0,_("You can't modify your board while playing."));
return ;
} else if( usuario.play==BOARD ) {
textfill(0,_("You've already sent your board to the server. You can't modify it."));
return ;
} else {
usuario.random = g_settings_get_uint(settings, "random");
srandom( usuario.random );
usuario.random = random();
g_settings_set_uint(settings, "random", usuario.random);
for(i=0;i<10;i++) {
/* clean tabla */
for(j=0;j<10;j++)
usuario.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 ); /* Tama#o */
buscar_poscicion(x,y,d,t);
}
gtk_widget_queue_draw(drawing_left);
}
}
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(x-1+i,y) && vacio( x-1+i,y-1) && vacio( x-1+i,y+1)) )
return FALSE;
}
return TRUE;
}
else{
/* Vertical */
for( i=0; i<=t+1; i++ ) {
if(! (vacio( x, y-1+i ) && vacio( x-1, y-1+i ) && vacio( 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;
}
/* Incrementa Y en uno y asi sucesivamente */
/* Digamos que recorre todo - Nada de Backtracking o algo mejor */
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( gint x, gint y)
{
if(x<1 || x>10 || y<1 || y>10)
return TRUE;
if( usuario.mitabla[x-1][y-1]==NOBARCO || usuario.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++)
usuario.mitabla[x-2+i][y-1]=BARCO;
}
else {
/* Vertical */
for(i=1;i<=t;i++)
usuario.mitabla[x-1][y-2+i]=BARCO;
}
}
|