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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #include <stdio.h>
#include <stdlib.h>
#define COLUMNS 7
#define LINES 6
#define DONE 3
#define PLAYER_1 1
#define FULL 0
#define CLEAR system ("clear")
typedef int PLATEAU[LINES+1][COLUMNS+1];
int save;
void InitPlat(PLATEAU pl)
//Initialisation du plateau
{
int lcpt,
ccpt;
for (lcpt=1; lcpt < (LINES+1); lcpt++)
pl[lcpt][0] = 0;//lcpt;
for (ccpt=1; ccpt < (COLUMNS+1); ccpt++)
pl[0][ccpt] = ccpt;
for (lcpt=1; lcpt < (LINES+1); lcpt++)
for (ccpt=1; ccpt < (COLUMNS+1); ccpt++)
pl[lcpt][ccpt] = 0;
pl[0][0] = 0;
}
void AffichPlat (PLATEAU pl)
//affiche le plateau de jeu
{
int lcpt,
ccpt;
for (lcpt=0; lcpt < (LINES+1); lcpt++){
for (ccpt=0; ccpt < (COLUMNS+1); ccpt++)
printf(" %d ",pl[lcpt][ccpt]);
printf("\n\n\n");
}
}
int StrToInt (const char* str)
//conversion d'une chaine de caractères en un entier
{
int toc = 0;
sscanf(str,"%d",&toc);
if (toc == EOF) return (-1);
else
return toc;
}
int LirePos (void)
{
char tmp[16];
int ptmp;
fgets (tmp, sizeof(tmp), stdin);
ptmp = StrToInt (tmp);
if (ptmp == EOF) return 0;
return ptmp;
}
int slip (PLATEAU pl, int col, int play, int lin)
{
if (lin < (LINES+1)){
if ((pl[lin][col]==0)&&(pl[(lin+1)][col]!=0)){
pl[lin][col] = play;
save = lin;
return DONE;
}
else
slip (pl, col, play, (lin+1));
}
else{
return FULL;
}
}
int CheckIfWinner (PLATEAU pl, int col, int lin)
{
int count = 1,
cpt,cpt2;
for (cpt=lin; cpt<LINES; cpt++)
if (pl[cpt][col] == pl[lin][col]) count++;
else break;
if (count == 4) return count;
count = 1;
for (cpt=(col+1); cpt<COLUMNS; cpt++)
if (pl[lin][cpt] == pl[lin][col]) count++;
else break;
if (count == 4) return count;
else{
for (cpt=(col-1); cpt>0; cpt--)
if (pl[lin][cpt] == pl[lin][col]) count++;
else break;
if (count == 4) return count;
}
for ((cpt=(col+1)),(cpt2=(lin+1));(cpt<COLUMNS)&&(cpt2<LINES); cpt++,cpt2++)
if (pl[cpt2][cpt] == pl[lin][col]) count++;
else break;
if (count == 4) return count;
else{
for ((cpt=(col-1)),(cpt2=(lin-1));(cpt>0)&&(cpt2<0); cpt++,cpt2++)
if (pl[cpt2][cpt] == pl[lin][col]) count++;
else break;
if (count == 4) return count;
}
for ((cpt=(col+1)),(cpt2=(lin-1));(cpt<COLUMNS)&&(cpt2>0); cpt++,cpt2--)
if (pl[cpt2][cpt] == pl[lin][col]) count++;
else break;
if (count == 4) return count;
else{
for ((cpt=(col-1)),(cpt2=(lin+1));(cpt>0)&&(cpt2<LINES); cpt--,cpt2++)
if (pl[cpt2][cpt] == pl[lin][col]) count++;
else break;
if (count == 4) return count;
}
}
int main(void)
{
PLATEAU plat;
int pl, pc,
flag = 0,
pflag = 0,
i;
InitPlat(plat);
AffichPlat (plat);
do{
flag = 0;
do{
pflag = 0;
do{
printf ("Player>");
pc = LirePos();
if ((pc < 1)||(pc > COLUMNS)) printf("Limit: 1 -> 7\n");
}while ((pc < 1)||(pc > COLUMNS));
pl = 0;
i = slip (plat, pc, PLAYER_1, pl);
pflag = 1;
CLEAR;
AffichPlat (plat);
if ((i=CheckIfWinner (plat, pc, save))==4) printf("winner\n");
}while (pflag == 0);
do{
pflag = 0;
do{
printf ("COM>");
pc = LirePos();
if ((pc < 1)||(pc > COLUMNS)) printf("Limit: 1 -> 7\n");
}while ((pc < 1)||(pc > COLUMNS));
pl = 0;
i = slip (plat, pc, 2, pl);
pflag = 1;
CLEAR;
AffichPlat (plat);
if ((i=CheckIfWinner (plat, pc, save))==4) printf("winner\n");
}while (pflag == 0);
}while (flag == 0);
return 0;
} |
Partager