//* JUEGO CONECTA 4*//
/*Librerias utilizadas*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LENGTH_JUGADOR 40
#define LENGTH_CEDULA 8
#define MAX_PARTIDAS 20
#define DIM 16 //Constante utilizada para definir la maxima dimension del tablero
/* Estructuras */
struct jugador
{
char nom_jug[ LENGTH_JUGADOR + 1 ];
char ced_jug[ LENGTH_CEDULA + 1 ];
};
struct partida
{
// Para almacenar los datos del jugador1
struct jugador jugador1;
// Para almacenar los datos del jugador2
struct jugador jugador2;
// Almacena 1 ( ganador jugador 1 ) o 2 ( ganador jugador 2 ).
int ganador;
};
struct manager_partidas
{
// Almacena todas las partidas jugadas
struct partida partidas[ MAX_PARTIDAS ];
// Indica la partida actual
int num_partida;
};
/*PROCEDIMIENTOS Y FUNCIONES*/
void pausa( );
int mostrar_menu( int );
int calcula_numero_jugadores( struct partida* );
void pedir_datos_jugador( struct jugador* );
void mostrar_resultados( struct manager_partidas* );
void inicializar_tablero(char tablero[][DIM],int);
int juegan_O_(char tabla[][DIM],int,int);
int juegan_X_(char tablero[][DIM],int,int);
int cuatro_en_linea(char tablero[][DIM],int);
int limpiar(char nom_jug[]);
int main()
{
char tablero[ DIM ][ DIM ];
int numero_jugadores = 0;
int opcion = -1;
// Se inicializa la estructura de las partidas
struct manager_partidas manager;
memset( &manager, 0, sizeof( manager ) );
while ( opcion != 5 )
{
// Puntero a la partida actual... para evitar líneas de código
struct partida* partida_actual = &manager.partidas[ manager.num_partida ];
// Se comprueba el número de jugadores en la partida actual
numero_jugadores = calcula_numero_jugadores( partida_actual );
opcion = mostrar_menu( numero_jugadores );
switch ( opcion )
{
case 1:
pedir_datos_jugador( &partida_actual->jugador1 );
break;
case 2:
pedir_datos_jugador( &partida_actual->jugador2 );
break;
case 3:
// Se simula una partida, para ello se elige un ganador
// y se incrementa el contador de la partida
partida_actual->ganador = ( manager.num_partida % 2 );
++manager.num_partida;
// Jugar
break;
case 4:
// Se pasa una referencia para no copiar toda la estructura
mostrar_resultados( &manager );
break;
case 5:
break;
}
}
}
void pausa( )
{
printf( "\nPulse una tecla para continuar..." );
fseek( stdin, 0, SEEK_END);
getchar( );
}
int mostrar_menu( int numero_jugadores )
{
int ok = 0;
int opcion = -1;
while ( !ok )
{
// Limpiamos el buffer, por si acaso
fseek( stdin, 0, SEEK_END);
system( "cls" );
printf( "\n\n\n JUEGO CUATRO EN LINEA\n\n\n\n\n" );
printf( " 1) Ingresar datos del primer jugador\n\n" );
printf( " 2) Ingresar datos del segundo jugador\n\n" );
printf( " 3) Jugar\n\n" );
printf( " 4) Listar todos los juegos realizados\n\n" );
printf( " 5) Salir\n\n\n" );
if ( opcion == 3 )
{
printf( " Error: Antes de jugar debe ingresar los datos de los dos jugadores. \n\n\n" );
printf( " REINGRESE SU OPCION: " );
}
else if ( opcion != -1 )
{
printf( " Opcion incorrecta \n\n\n" );
printf( " REINGRESE SU OPCION: " );
}
else
{
printf( "INGRESE SU OPCION: " );
}
scanf( "%d", &opcion );
if ( opcion == 3 )
ok = ( numero_jugadores == 2 );
else
ok = ( opcion > 0 && opcion < 6 );
}
return opcion;
}
int calcula_numero_jugadores( struct partida* partida )
{
int jugadores = 0;
if ( partida->jugador1.nom_jug[ 0 ] != '\0' )
++jugadores;
if ( partida->jugador2.nom_jug[ 0 ] != '\0' )
++jugadores;
return jugadores;
}
void pedir_datos_jugador( struct jugador* jugador )
{
int ok = 0;
while ( !ok )
{
// Se limpia el buffer de entrada.
fseek( stdin, 0, SEEK_END);
printf( "\n Ingrese su Nombre: " );
scanf("%40[^\n]", jugador->nom_jug );
ok = ( jugador->nom_jug[ 0 ] != '\0' );
if ( !ok )
printf("\n ERROR" );
}
ok = 0;
while ( !ok )
{
// Se limpia el buffer de entrada.
fseek( stdin, 0, SEEK_END);
printf( "\n Ingrese su cedula: " );
scanf("%40[^\n]", jugador->ced_jug );
ok = ( jugador->ced_jug[ 0 ] != '\0' );
if ( !ok )
printf("\n ERROR" );
}
pausa( );
}
void mostrar_resultados( struct manager_partidas* manager )
{
if ( manager->num_partida == 0 )
{
printf ( "No se han jugador partidas." );
}
else
{
int i;
for ( i=0; i<manager->num_partida; ++i )
{
printf( "Partida %d\n", i+1 );
printf( "\n");
printf( "Jugador 1: %s\n", manager->partidas[ i ].jugador1.nom_jug );
printf( "Cedula: %s\n", manager->partidas[ i ].jugador1.ced_jug );
printf( "Jugador 2: %s\n", manager->partidas[ i ].jugador2.nom_jug );
printf( "Cedula: %s\n", manager->partidas[ i ].jugador2.ced_jug );
printf( "Ganador jugador %d\n\n", manager->partidas[ i ].ganador );
}
}
pausa( );
}