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
|
/* $Id: check_board.c,v 1.1.1.1 2000/02/13 00:56:46 riq Exp $ */
/*
*
* Algoritmo que chequea la validez de una tabla
* un poco mas descente que el anterior :)
*
*/
/*
* Points to next pos.return TRUE . FALSE in case there are no more ships
* this rutine uses global x,y
*/
/* exported: algoritmo() */
#include <stdlib.h>
#include <glib.h>
#include <config.h>
#include "check_board.h"
#include "protocol.h"
#include "server.h"
extern struct st_datos usuario;
static gint
vacio( gint x, gint y, gint jugador )
{
if(x>=10 || x<0 || y>=10 || y<0)
return TRUE;
if( usuario.table[jugador].p[x][y]==NOBARCO)
return TRUE;
else
return FALSE;
}
static void
siguiente_pos( gint *x, gint *y, gint jugador )
{
if( vacio( *x, *y, jugador ) )
(*x)++;
else {
while( (*x)<10 && !vacio(*x,*y,jugador) )
(*x)++;
}
if( (*x) >=10 ) {
(*x)=0;
(*y)++;
}
}
static gint
tamano_barco( gint x, gint y, gint jugador)
{
gint b;
b=0;
if(!vacio(x+1,y,jugador)) { /* Barco horizontal */
while(!vacio(x,y,jugador)) {
b++;
x++;
}
return b;
}
if(!vacio(x,y+1,jugador)) { /* Barco Vertical */
while(!vacio(x,y,jugador)) {
b++;
y++;
}
return b;
}
return 1; /* Barco de una unidad */
}
static gint
valid_pos( gint x, gint y, gint jugador )
{
if( (!vacio(x,y,jugador)) && (!vacio(x+1,y+1,jugador)) )
return FALSE;
if( (!vacio(x+1,y,jugador)) && (!vacio(x,y+1,jugador)) )
return FALSE;
return TRUE;
}
/* ALGORITMO_REC */
static gint
algoritmo_rec( gint *x, gint *y, int jugador, char *barcos)
{
if(*y >= 10)
return TRUE;
if( (!valid_pos( *x, *y, jugador ) ) )
return FALSE;
if( (!vacio( *x, *y, jugador ) ) && (vacio( *x, (*y)-1, jugador)) )
barcos[ tamano_barco(*x, *y, jugador)]++;
siguiente_pos( x, y, jugador );
return algoritmo_rec( x, y, jugador, barcos );
}
/* return TRUE if table is OK .else return FALSE */
gint
algoritmo(gint num_jug)
{
gint x,y,i;
char barcos[11];
for(i=0;i<11;i++)
barcos[i]=0;
x=0;y=0;
if(!(algoritmo_rec(&x,&y,num_jug,barcos) ))
return FALSE; // Por Colision
if( (barcos[1]==4) &&
(barcos[2]==3) &&
(barcos[3]==2) &&
(barcos[4]==1) &&
(barcos[5]==0) &&
(barcos[6]==0) &&
(barcos[7]==0) &&
(barcos[8]==0) &&
(barcos[9]==0) &&
(barcos[10]==0)
)
return TRUE;
else
return FALSE;
}
|