Data de publicació: Dec 04, 2011 5:44:38 PM
Connect Four (also known as Captain's Mistress, Four Up, Plot Four, Find Four,Fourplay, Four in a Row, and Four in a Line) is a two-player game in which the players first choose a color and then take turns dropping their colored discs from the top into a seven-column, six-row vertically-suspended grid. The pieces fall straight down, occupying the next available space within the column. The object of the game is to connect four of one's own discs of the same color next to each other vertically, horizontally, or diagonally before one's opponent can do so. There are many variations on the board size, the most commonly used being 6×7, followed by 8×7, 9×7, and 10×7.
The game was first sold under the famous Connect Four trademark by Milton Bradley in February 1974.[wikipedia]
4enRatlla.h
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define MAX 8
#define JUG_1 1
#define SJUG_1 '*'
#define JUG_2 2
#define SJUG_2 'O'
#define SBUIT '-'
#define RATLLA 4
#define _HRZT 1
#define _VERT 2
#define _DIAA 3
#define _DIAB 4
#define t_bool int
void inicialitzar();
char simbol_jugador(int jugador);
char simbol_buit();
t_bool jugar(char simbol,int *i,int *j);
t_bool ubicar(char simbol,int *fila,int columna);
t_bool evaluar_enratlla(int i,int j,int NR);
int num_simbols(int i,int j,int orientacio);
void seg_v(int *i,int *j);
void ant_v(int *i,int *j);
void seg_h(int *i,int *j);
void ant_h(int *i,int *j);
void seg_dA(int *i,int *j);
void ant_dA(int *i,int *j);
void seg_dB(int *i,int *j);
void ant_dB(int *i,int *j);
void canvia_jugador(int *jugador);
int X_obtener_columna();
void X_imprimir();
void X_torn(int jugador);
void X_msg_guanyador(int jugador);
void test(char *txt);
char tab[MAX][MAX];
t_bool fi_partida,jugada_valida;
4enRatlla.c
#include "4enRatlla.h"
int main(){
t_bool fi_partida,jugada_valida;
int jugador,i,j;
fi_partida=FALSE;
jugada_valida=FALSE;
jugador=JUG_1;
inicialitzar();
while (!fi_partida){
do{
X_torn(jugador);
jugada_valida=jugar(simbol_jugador(jugador),&i,&j);
}while (!jugada_valida);
X_imprimir();
if (evaluar_enratlla(i,j,RATLLA)){
X_msg_guanyador(jugador);
fi_partida=TRUE;
}
if (!fi_partida) canvia_jugador(&jugador);
};
}
char simbol_buit(){
return SBUIT;
}
void inicialitzar(){
int i,j;
for (i=0;i<MAX;i++)
for (j=0;j<MAX;j++)
tab[i][j]=simbol_buit();
}
char simbol_jugador(int jugador){
switch(jugador){
case JUG_1: return SJUG_1;
case JUG_2: return SJUG_2;
}
}
t_bool jugar(char simbol,int *i,int *j){
*j=X_obtener_columna();
return ubicar(simbol,i,*j);
}
t_bool ubicar(char simbol,int *fila,int columna){
*fila=0;
while((tab[*fila][columna])!=simbol_buit() && ((*fila)<MAX)) (*fila)++;
if ((*fila)<MAX) tab[*fila][columna]=simbol;
return ((*fila)<MAX);
}
t_bool evaluar_enratlla(int i,int j,int NR){
int nsimbols=0;
nsimbols=num_simbols(i,j,_HRZT);
if (nsimbols>=NR) return TRUE;
nsimbols=num_simbols(i,j,_VERT);
if (nsimbols>=NR) return TRUE;
nsimbols=num_simbols(i,j,_DIAA);
if (nsimbols>=NR) return TRUE;
nsimbols=num_simbols(i,j,_DIAB);
if (nsimbols>=NR) return TRUE;
return FALSE;
}
void seg_v(int *i,int *j){(*i)++;}
void ant_v(int *i,int *j){(*i)--;}
void seg_h(int *i,int *j){(*j)++;}
void ant_h(int *i,int *j){(*j)--;}
void seg_dA(int *i,int *j){(*i)++;(*j)++;}
void ant_dA(int *i,int *j){(*i)--;(*j)--;}
void seg_dB(int *i,int *j){(*i)++;(*j)--;}
void ant_dB(int *i,int *j){(*i)--;(*j)++;}
int num_simbols(int i,int j,int orientacio){
void (*seg)(int *,int *);
void (*ant)(int *,int *);
char simbol;
int n_simbols=1;
int f,c;
simbol=tab[i][j];
switch(orientacio){
case _HRZT:seg=&seg_h;
ant=&ant_h;
break;
case _VERT:seg=&seg_v;
ant=&ant_v;
break;
case _DIAA:seg=&seg_dA;
ant=&ant_dA;
break;
case _DIAB:seg=&seg_dB;
ant=&ant_dB;
break;
default: exit(-1);
}
f=i;c=j;
seg(&f,&c);
while ((f>=0)&&(f<MAX)&&(c>=0)&&(c<MAX)&&(tab[f][c]==simbol)){seg(&f,&c);n_simbols++;}
f=i;c=j;
ant(&f,&c);
while ((f>=0)&&(f<MAX)&&(c>=0)&&(c<MAX)&&(tab[f][c]==simbol)){ant(&f,&c);n_simbols++;}
return n_simbols;
}
void canvia_jugador(int *jugador){
switch(*jugador){
case JUG_1:(*jugador)=JUG_2;
break;
case JUG_2:(*jugador)=JUG_1;
break;
}
}
//Funcions X
int X_obtener_columna(){
int columna;
do{
printf("\nColumna? ");
scanf("%d",&columna);
}while((columna<0) || (columna>=MAX));
return(columna);
}
void X_imprimir(){
int i,j;
for (i=MAX-1;i>=0;i--){
for (j=0;j<MAX;j++)
printf("%3c",tab[i][j]);
printf("\n");
}
for (i=0;i<MAX;i++)
printf("%3d",i);
printf("\n");
}
void X_torn(int jugador){
switch(jugador){
case JUG_1:printf("%s (%c)","Torn de jugador 1",SJUG_1);
break;
case JUG_2:printf("%s (%c)","Torn de jugador 2",SJUG_2);
break;
}
}
void X_msg_guanyador(int jugador){
printf("\n*****************************************\n");
printf("\n ENHORABONA!! HAS GUANYAT jugador:%d\n",jugador);
printf("\n*****************************************\n");
}
void test(char *txt){
printf("%s",txt);getchar();
}
Output
Torn de jugador 1 (*)
Columna? 1
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- * - - - - - -
0 1 2 3 4 5 6 7
Torn de jugador 2 (O)
Columna? 2
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- * O - - - - -
0 1 2 3 4 5 6 7
Torn de jugador 1 (*)
Columna? 1
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- * - - - - - -
- * O - - - - -
0 1 2 3 4 5 6 7
Torn de jugador 2 (O)
Columna? 2
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- * O - - - - -
- * O - - - - -
0 1 2 3 4 5 6 7
Torn de jugador 1 (*)
Columna? 1
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- * - - - - - -
- * O - - - - -
- * O - - - - -
0 1 2 3 4 5 6 7
Torn de jugador 2 (O)
Columna? 2
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- * O - - - - -
- * O - - - - -
- * O - - - - -
0 1 2 3 4 5 6 7
Torn de jugador 1 (*)
Columna? 1
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- * - - - - - -
- * O - - - - -
- * O - - - - -
- * O - - - - -
0 1 2 3 4 5 6 7
*****************************************
ENHORABONA!! HAS GUANYAT jugador:1
*****************************************
[ikasten@ikasten 4enratlla]$