7 Replies - 1115 Views - Last Post: 11 June 2010 - 01:18 PM Rate Topic: -----

#1 carlosv56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-April 10

connect4

Posted 10 June 2010 - 03:40 AM

This is the game I am doing.
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.


Is This A Good Question/Topic? 0
  • +

Replies To: connect4

#2 Skaggles   User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 255
  • View blog
  • Posts: 641
  • Joined: 01-March 09

Re: connect4

Posted 10 June 2010 - 03:47 AM

You're not telling us what exactly your problem is or the errors you are receiving. Please be a little more clearer in what you need help with.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: connect4

Posted 10 June 2010 - 04:00 AM

And, when posting code USE CODE TAGS!!!!

:code:
Was This Post Helpful? 0
  • +
  • -

#4 carlosv56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-April 10

Re: connect4

Posted 10 June 2010 - 04:33 AM

Yes. I have problem with the diagonals, when there is a space in between.
Also I need some ideas in how I could do the file to show the scores.
thanks
Was This Post Helpful? 0
  • +
  • -

#5 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: connect4

Posted 10 June 2010 - 06:55 AM

View Postcarlosv56, on 10 June 2010 - 08:33 PM, said:

Yes. I have problem with the diagonals, when there is a space in between.

And the problem you have with diagonals is what?
You need to be clear and explicit if you want good help.
Few people will be bothered to wade through all your code and run lots off unit testing to find out what is wrong.
It is your job to tell us what is wrong.



View Postcarlosv56, on 10 June 2010 - 08:33 PM, said:

Also I need some ideas in how I could do the file to show the scores.

Let's solve the mystery of the diagonals before we try to deal with whatever it is you mean by that sentence.
Was This Post Helpful? 0
  • +
  • -

#6 carlosv56   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-April 10

Re: connect4

Posted 10 June 2010 - 02:24 PM

Ok.
I will try to explain it. When you start the diagonal in column 1 or 6 no problem. The problem is when you have a free column in between.Ej:you strat in 5 and no one use column six.
Was This Post Helpful? 0
  • +
  • -

#7 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: connect4

Posted 10 June 2010 - 07:16 PM

View Postcarlosv56, on 11 June 2010 - 06:24 AM, said:

The problem is when you have a free column in between.Ej:you strat in 5 and no one use column six.


And this problem that occurs in that (completely unclear to me) set of circumstances is what?

You program does something other than what you wanted?
What does it do?
What did you want it to do?
What is the difference?
What have you done to try and fix this that did not work?

Or is the problem that wild beasts magically appear in your room and growl menacingly?
Or do all the lights in your house blink out the morse code for the Icelandic national anthem?
Or something else?

I urge you to read this:
http://www.joelonsof...0000000029.html
Was This Post Helpful? 0
  • +
  • -

#8 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: connect4

Posted 11 June 2010 - 01:18 PM

First, let's organize and fix up the easy one:
int horiz(int fila,int COLUMNAS) {
	int col=0;
	int cuento1 = 0;
	int cuento2 = 0;

	while (col < COLUMNAS) {
		int p = mat[fila][col];
		if (p == 1) {
			if(++cuento1 == 4) { return 1; }
			cuento2 = 0; // important, you forgot this
		} else if (p == 2) {
			if(++cuento2 == 4) { return 2; }
			cuento1 = 0;
		} else {
			cuento1 = 0;
			cuento2 = 0;
		}
		col++;
	}
}



Actually, if you give it the number of columns up front it can go down to zero:
int horiz(int fila, int col) {
	int cuento1 = 0;
	int cuento2 = 0;

	while (col>=0) {
		int p = mat[fila][col--];
		if (p == 1) {
			if(++cuento1 == 4) { return 1; }
			cuento2 = 0;
		} else if (p == 2) {
			if(++cuento2 == 4) { return 2; }
			cuento1 = 0;
		} else {
			cuento1 = 0;
			cuento2 = 0;
		}
	}
}



Once one is working properly, going diagonal doesn't seem a big deal:
int diag_derecha (int fila, int col) {
	int cuento1 = 0;
	int cuento2 = 0;

	while (fila>=0 && col>=0) {
		int p = mat[fila--][col--];
		if (p == 1) {
			if(++cuento1 == 4) { return 1; }
			cuento2 = 0;
		} else if (p == 2) {
			if(++cuento2 == 4) { return 2; }
			cuento1 = 0;
		} else {
			cuento1 = 0;
			cuento2 = 0;
		}
	}
	return 0;
}



With a little consideration, you could probably generalize this.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1