I have to do a file with the score showing the player and the points they have (5 points if the player wins). This is getting me crazy. If someone could help me. Sorry that the code is in Spanish,Thanks in advance
//JUEGO CONECTA 4 ---------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <conio.h>
//#define puntaje 0
struct jug{
char nombre[20];
char dni [20];
char puntaje [3];
int color ;
};
struct jug jug;
struct jug jugador1;
struct jug jugador2;
/*struct jugador2{
char nombre[20];
char Dni [20];
int color;
}jug2; */
int mat[8][8]; // variable global
void menu(void)
{
gotoxy(1,40);
printf("\t\nMENU");
gotoxy(1,41);
printf("\t1.NUEVA PARTIDA");
gotoxy(1,42);
printf("\t2.NIVEL DE DIFICULTAD");
gotoxy(1,43);
printf("\t3.CLASIFICACION");
gotoxy(1,44);
printf("\t4.SALIR");
}
//---------------------------------------------------------------------------
void cuadrado (int x1, int y1,int x2, int y2) //FORMULA PARA UN CUADRADO//
{
char form [6]={218,191,217,192,196,179};
int i ;
gotoxy(x1,y1);
printf("%c",form[0]);
for (i = 0; i <x2-x1-1; i++) {
printf("%c",form[4]);
}
printf("%c",form[1]);
gotoxy (x1,y2);
printf("%c",form [3]);
for (i = 0; i < x2-x1-1; i++)
{
printf("%c",form[4]);
}
printf("%c",form[2]);
for (i = 0; i < y2-y1-1; i++)
{
gotoxy (x1,y1+i+1);
printf("%c",form[5]);
}
for (i = 0; i < y2-y1-1; i++)
{
gotoxy (x2,y1+i+1);
printf("%c",form[5]);
}
}
void tablero(int ancho, int largo) //FORMULA PARA HACER TABLERO.LLAMA A CUADRADO///
{
int i,j,X1=1,Y1=1, X2,Y2;
for (i = 0; i < ancho; i++)
{
for (j = 0; j < largo; j++)
cuadrado(X1+j*6,Y1+i*3,X1+(j+1)*6,Y1+(i+1)*3);
}
printf("\n\n\n\n\ \n");
}
/////////////////*************************************/////////////
void insedatos (void) /// insertar DATOS DE LOS JUGADORES///
{
int i,num;
FILE *fichero;
fichero=fopen("conecta4.txt","a");
if (fichero==NULL)
{
printf("error");
exit (1);
}
// for (i = 0; i < 2; i++)
// {
//gotoxy(1,42);
printf("PERFIL JUGADORES\n",i+1);
// gotoxy(1,43);
printf("\nNOMBRE_JUG1:");
gets(jugador1.nombre);
// while (getchar()!='\n');
//gotoxy(1,44);
printf("\nDNI_JUG1:");
gets (jugador1.dni);
// gotoxy(1,45);
do{
printf("\nCOLOR_JUG1: \n");
printf("rojo<-4; azul<-9; verde<-2\n");
scanf("%i",&jugador1.color);
if (jugador1.color !=4 && jugador1.color!=9 && jugador1.color!=2);
printf("ERROR.COLOR NO DISPONIBLE");
//getchar();
} while (jugador1.color!=4&&jugador1.color!=9&&jugador1.color!=2);
while (getchar()!='\n');
printf("\nNOMBRE_JUG2:");
gets(jugador2.nombre);
printf("\nDNI_JUG2:");
gets (jugador2.dni);
do{
printf("\nCOLOR_JUG2:\n ");
printf("rojo<-4; azul<-9; verde<-2\n");
scanf("%i",&jugador2.color);
if ((jugador2.color!=4&&jugador2.color!=9&&jugador2.color!=2)||(jugador1.color==jugador2.color));
printf("ERROR.COLOR NO DISPONIBLE");
//getch();
} while ((jugador2.color!=4&&jugador2.color!=9&&jugador2.color!=2)||(jugador1.color==jugador2.color));
while (getchar()!='\n');
fwrite((&jugador1),sizeof(jugador1),1,fichero);
fwrite((&jugador2),sizeof(jugador2),1,fichero);
if (fclose(fichero)!=0)
{
printf("error");
exit (1);
}
}
///////////////***************//////////////***************//////////////////
void inicializa (int nivel) //ME DA EL ANCHO TABLERO DE ACUERDO A NIVEL
{
int i,j;
int ancho;
int largo;
switch (nivel)
{
case 1:
//gotoxy (6,6);
tablero(6,6);
break ;
case 2:
tablero(6,8);
break;
case 3:
tablero(8,8);
break;
}
}
void comenzar (int nivel) //me da la matriz de acuerdo al nivel
{
int i,j;
int FILAS,COLUMNAS;
if (nivel==1)
{
FILAS=6;
COLUMNAS=6;
}
if (nivel==2)
{
FILAS=6;
COLUMNAS=8;
}
if (nivel==3)
{
FILAS=8;
COLUMNAS=8;
}
for(i=0;i<FILAS;i++)
for(j=0;j<COLUMNAS;j++)
mat[i][j]=0;
}
void relleno(int x1, int y1,int x2, int y2,int color,char forma [6])
{
int i, j;
//int col=9;
char pinta=219;
for (i = 1; i <x2-x1; i++)
for (j = 1; j < y2-y1; j++) {
{
gotoxy(x1+i,y1+j);
textcolor(color);
cprintf("%c",pinta);
}
}
}
///////////////////////**** JUGADA VALIDA *********///////////////////
//////**********CONDICIONES PARA QUE UNA JUGADA SEA VALIDA*******/////////////
int jugada_valida (int fila, int col)
{
int FILAS=8,COLUMNAS=8;
int jugada_valida; //Guarda un 1 ó un 0 para indicar si la jugada es
// correcta o no. 1-Jugada correcta. 0-Jugada errónea: Cuando
// la fila y/o columna no son válidas. Ej= La jugada se sale
// del tablero .
jugada_valida=1;
if (fila>=FILAS || fila<0 || col>=COLUMNAS || col<0)
jugada_valida=0;
if (jugada_valida==1)
{
if (mat[fila][col]== 1 || mat[fila][col]==2) //ocupado por uno de
// de los jugadores
jugada_valida=0;
}
if (jugada_valida==1)
{
if (fila!=FILAS-1 && mat[fila+1][col]==0)
jugada_valida=0;
}
return jugada_valida;
}
///////////////////////////////*******************/////////////////////////////
///////////***************HACE LA JUGADA ***************//////////
int Hace_jugada (int jug_emp, int nivel,int *col)
{
int fila,FILAS,COLUMNAS;
int jugador=jug_emp; // jugador. 1- Para jug1, 2- Para jug2.
int i;
int colores=6; //marca color cuando hay columna fuera de rango;
int ficha; //Para guardar la ficha del jugador
//int X1=COLUMNAS*8,Y1=FILAS*5, X2,Y2;
int sw=0;
int tinte; // tinte=color elejido por jugador;
int p1=1, p2=1; //p1 y p2: Posiciones XY del primer cuadrado.
char form [6]={218,191,217,192,196,179};
if (nivel==1)
{
FILAS=6;
COLUMNAS=6;
}
else if (nivel==2)
{
FILAS=6;
COLUMNAS=8;
}
else if (nivel==3)
{
FILAS=8;
COLUMNAS=8;
}
do{
do{
if (jugador==1)
{
gotoxy (13,42);
printf(" ");
gotoxy(13,42);
tinte=jugador1.color;
}
else
{
gotoxy (40,42);
printf(" ");
gotoxy(40,42);
tinte=jugador2.color;
}
gotoxy(51,6);
printf("INDIQUE COLUMNA:");
scanf("%d",col);
(*col)--;
if (*col<0||*col >=COLUMNAS)
{
gotoxy(51,7);
textcolor(11);
cprintf ("Fuera de rango");
//getchar ();
}
} while (*col<0||*col >=COLUMNAS);
gotoxy(51,8);
textcolor (colores);
cprintf("\nJUGADOR %i -> COLUMNA %i",jug_emp,*col);
// printf("\t%i -> %i",jugador,col);
// printf("\n\n\n\n\n\n\n\n\n\n\n ");
i=FILAS-1;
while ((sw==0)&&(i>=0))
{
if (mat[i][*col]==0)
sw=1;
else
i--;
}
} while(sw==0);
if (sw==1)
{
mat[i][*col]=jugador;
relleno(p1 +(*col)*6, p2+(i)*3, p1+(*col+1)*6,p2+(i+1)*3,tinte, form);
}
return i; //devol la fila
}
////////////////*********** COMPROBACIONES *************////////////////////
/************************************************************
ganador horizontal()
entradas:
f fila actuual que está disponible
c columna que jugador ha elejdo
salidas:
0 no ganador
Return ganador si hay 4 en una fila en direccion horizontal
************************************************************/
int horiz(int f,int COLUMNAS)
{
int i,j=0; //USAR for, para recorrer el tablero
int ganador; //0 no gana nadie, 1: gana jug 1, 2: gana jug2
int fila,col,FILAS;
//int COLUMNAS=COL;
int cuento1 = 0;
int cuento2= 0;
int nivel;
// ganador=0;
while (( j < COLUMNAS) && (cuento1<4)&&(cuento2<4))
{
if (mat[f][j] == 0)
{
cuento1=0;
cuento2=0;
}
if (mat[f][j] == 2)
cuento2++;
else
if (mat[f][j] == 1)
cuento1++;
j++;
}
if(cuento1 == 4)
return 1;
else
if(cuento2 == 4)
return 2;
else
return 0;
}
/************************************************************
ganador VERTICAL()
entradas:
f fila actuual que está disponible
c columna que jugador ha elejdo
salidas:
0 no ganador
Return ganador si hay 4 en una fila en direccion vertical
************************************************************/
int vert (int FILAS, int c)
{
int i=0,j; //USAR for, para recorrer el tablero
int ganador; //0 no gana nadie, 1: gana jug 1, 2: gana jug2
int jugador;
int fila,col,COLUMNAS;
//int FILAS=FIL;
int cuento1 = 0;
int cuento2= 0;
int nivel;
// ganador=0;
while (( i < FILAS) && (cuento1<4)&&(cuento2<4))
//for (i = 0; i < FILAS ; i++)
{
if (mat[i][c] == 0)
{
cuento1= 0;
cuento2=0;
// return 0;
}
if (mat[i][c] == 2)
cuento2++;
else if (mat[i][c]== 1)
cuento1++;
i++;
}
if(cuento1 == 4)
return 1;
else
if(cuento2 == 4)
return 2;
else
return 0;
}
/************************************************************
ganador diagonal derecho()
Entradas:
f fila que está disponible
c columna que jugador ha elejido
Salidas=
1 jugador1, 2 jugador2
0 no hay ganador
Return 1 o 2 si hay 4 en una fila en inclinación diagonal DERECHA correcta
************************************************************/
int diag_derecha (int f, int c, int FILAS, int COLUMNAS)
{
int comienzafil, comienzacol;
int j=0, i=0; //USAR for, para recorrer el tablero
int ganador; //0 no gana nadie, 1: gana jug 1, 2: gana jug2
int jugador;
int fila,col;
//int FILAS =FIL;
//nt COLUMNAS=COL ;
int cuento1 = 0;
int cuento2= 0;
int nivel;
//ganador=0;
//comienzacol = c;
//comienzafil = f;
//encuentro punto más bajo en diagonal
while((f > 0) && (c > 0))
{
f --;
c --;
}
j = c;
//busqueda hacia arriba por 4 en una fila
while ((i < FILAS)&& (j < COLUMNAS) && (cuento1<4)&&(cuento2<4))
//for(i = f; (i < FILAS) && (j < COLUMNAS); i++)
{
if (mat[i][j] ==0)
{
cuento1=0;
cuento2=0;
}
if (mat[i][j] == 2)
cuento2++;
else
if(mat[i][j] == 1)
cuento1++;
j++;
i++;
}
if(cuento1 == 4)
return 1;
else
if(cuento2 == 4)
return 2;
else
return 0;
}
/************************************************************
ganador diagonal izquierdo()
Entradas:
f fila que está disponible
c columna que jugador ha elejido
Salidas=
1 jugador1, 2 jugador2
0 no hay ganador
Return 1 o 2 si hay 4 en una fila en inclinación diagonal IZQ correcta
************************************************************/
int diag_izquierda (int f, int c, int FILAS, int COLUMNAS)
{
int comienzafil, comienzacol;
int j=0, i=0; //USAR for, para recorrer el tablero
int ganador; //0 no gana nadie, 1: gana jug 1, 2: gana jug2
int jugador;
int fila,col;
//int FILAS =FIL;
//int COLUMNAS=COL ;
int cuento1 = 0;
int cuento2= 0;
int nivel;
// ganador=0;
//Encontrar punto más bajo en diagonal
while((f > 0) && (c < COLUMNAS))
{
f--;
c++;
}
j = c;
//busqueda hacia arriba por 4 en una fila
while ((i < FILAS)&& (j <COLUMNAS) && (cuento1<4)&&(cuento2<4))
// for(i = f; (i < FILAS) && (j > 1); i++)
{
if (mat[i][j] ==0)
{
cuento1=0;
cuento2=0;
}
if (mat[i][j] == 2)
cuento2++;
else
if(mat[i][j] == 1)
cuento1++;
j--;
i++;
}
if(cuento1 == 4)
return 1;
else
if(cuento2 == 4)
return 2;
else
return 0;
}
int comprobar_fin(int f,int c,int nivel)
{
int i,j; //USAR for, para recorrer el tablero
// int ganador; //0 no gana nadie, 1: gana jug 1, 2: gana jug2
int jugador;
int fila,col,FILAS,COLUMNAS;
// int cuento1 = 0;
// int cuento2= 0;
//ganador=0;
if (nivel==1)
{
FILAS=6;
COLUMNAS=6;
}
else if (nivel==2)
{
FILAS=6;
COLUMNAS=8;
}
else if (nivel==3)
{
FILAS=8;
COLUMNAS=8;
}
jugador= horiz(f,COLUMNAS);
if (jugador!=0)
return jugador;
else
{
jugador= vert (FILAS, c );
if (jugador!=0)
return jugador;
else
{
jugador=diag_derecha (f,c,FILAS,COLUMNAS );
if (jugador!=0)
return jugador;
else
{
jugador=diag_izquierda (f,c,FILAS,COLUMNAS );
if (jugador!=0)
return jugador;
else
return 0 ;
}
}
}
}
/***************************************************************
Hay_Ganador()
Al comprobar_fin() retorna cuento1 (jugador) para jugador1 para
cuento2 (jugador) para jugador2;
0 no hay gandor
******************************************************************/
void Hay_ganador (int ganador)
{
if (ganador==1)
{
gotoxy(48,17);
textcolor (11);
//ganador=jugador1.nombre;
cprintf("%s ganador.Felicidades",jugador1.nombre);
}
else
{
if (ganador==2)
{
gotoxy(48,17);
textcolor (11);
//ganador=jugador2.nombre;
cprintf("%s ganador.Felicidades",jugador2.nombre);
// cprintf("Jugador2 ha ganado. Felicidades");
}
else
{
gotoxy(48,17);
textcolor (11);
cprintf("No hay ganador");
}
}
}
/***********************MOVIMINETO JUGADOR (mov_jugador ) *****************
Muestra quien es el jugador 1 y 2 después del inicio aleatorio, viene de
hace Jugada y cuenta hasta 7 para hacer las comprobaciones y ver si hay ganador
o no. Termina cuando hay ganador o todas las posibles jugadas de acuerdo al
nivel COLUMNAS * FILAS (36,48,64)
jug_emp: Jugador que empieza ****************************************/
void mov_jugador(int jug_emp, int nivel)
{
int cont_jugada=1;
int player=jug_emp;
int gana=0;
int FILAS,COLUMNAS,fila,columna;
if (nivel==1)
{
FILAS=6;
COLUMNAS=6;
}
else if (nivel==2)
{
FILAS=6;
COLUMNAS=8;
}
else if (nivel==3)
{
FILAS=8;
COLUMNAS=8;
}
do{
fila=Hace_jugada(player,nivel,&columna);
if (cont_jugada>=7)
{
gana=comprobar_fin(fila,columna, nivel);
}
if (player==1)
player=2;
else
player=1;
cont_jugada++;
} while ((gana==0)&& (cont_jugada<=FILAS*COLUMNAS));
// && comprob_fin==-1);
Hay_ganador (gana);
getch();
}
/////////////////////////////********************** /////////////////////////
///////////*******************//////////////////*********************
/********** VER RESULTADOS, PUNTAJE, CLASIFICACION JUGADORES *******/
void busca (void )
{
struct jug ; //****BUSCAR****
int i=0;
int cont=0;
int num;
FILE* fichero;
int sw=0;
fichero=fopen("conecta4.txt","r+");
if (fichero==NULL)
{
printf("Error no se puede abrir fichero");
exit(1);
}
do{
if( fread(&jug, sizeof(jug), 1, fichero))
{
if (strcmp(&jugador1.nombre, jug.nombre)==0)
{
num=atoi(jug.puntaje)+5;
itoa (num,jug.puntaje,10);
puts(jug.nombre);
puts(jug.puntaje);
fseek (fichero,sizeof(jug)*(cont-1),SEEK_SET);
fwrite (&jug, sizeof(jug), 1, fichero);
fseek (fichero,sizeof(jug)*(cont-1),SEEK_SET);
}
else
if (strcmp(&jugador2.nombre, jug.nombre)==0)
{
num=atoi(jug.puntaje)+5;
itoa (num,jug.puntaje,10);
puts(jug.nombre);
puts(jug.puntaje);
fseek (fichero,sizeof(jug)*(cont-1),SEEK_SET);
fwrite (&jug, sizeof(jug), 1, fichero);
fseek (fichero,sizeof(jug)*(cont-1),SEEK_SET);
}
}
} while (feof(fichero)==0);
if (fclose(fichero)!=0)
{
printf("Error al cerrar fichero");
exit(1);
}
}
// void clasificación (void)
///************************************************************************
//**************************************************************************
//**************************************************************************
void main (void)
{
int opcion,elect,option=1,op;
int exit=5,salir =4;
int fi, co;
int result, ganador;
int i,j, X2,Y2;
int jug; //Indica el jugador al que le toca jugar.
int jugador_emp;
FILE * fichero;
struct jug jugadores[2];
do{
clrscr();
menu ();
printf("\t\nOPCION\n");
scanf("%i",&opcion);
while (getchar()!='\n');
switch (opcion)
{
case 1:
gotoxy(1,46);
printf("DATOS JUGADORES\n");
insedatos ();
gotoxy(51,3);
textcolor (14);
cprintf("BIENVENIDO AL JUEGO CONECTA4");
gotoxy(51,4);
textcolor (14);
cprintf("COMIENZA PARTIDA");
inicializa (option);//Dibuja tablero
comenzar(option); // sirve para inicilizar matriz de juego
gotoxy(1,55);
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nque Jugador comienza (1 o 2)\n");
randomize();
jug= random (2)+1;
//Hace_jugada(jug, option);
mov_jugador (jug,option);
break ;
case 2:
gotoxy(1,41);
printf("1->Basico 2 -> Medio 3 -> Alto");
gotoxy(1,42);
printf("\t1.NIVEL1");
printf("\t2.NIVEL2");
printf("\t3.NIVEL3");
printf("\tEXIT 4\n");
do{
gotoxy(1,44);
printf("\t\nOPCION\n");
scanf("%i",&option);
// while (getchar()!='\n');
} while(!salir);
case 3:
busca();
break;
case 4:
break;
}
}while(opcion!=4);
system("pause");
}
This post has been edited by JackOfAllTrades: 10 June 2010 - 04:00 AM
Reason for edit:: Added code tags.

New Topic/Question
Reply



MultiQuote









|