Réponse acceptée !
Have fun...
/****************************************************************************************
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*=======================================================================================
*
*/
#define CX 7
#define CY 6
#define CONNECT 4
#define PLAYER1 1
#define PLAYER2 2
#define HUMAN 1
#define CPU 2
/*=======================================================================================
*
*/
#define OTHER_PLAYER(player) ((player == PLAYER1) ? PLAYER2 : PLAYER1)
/*=======================================================================================
*
*/
typedef struct GRID_TAG
{
int matrix[CX][CY];
int columns[CX];
int count;
}GRID;
/*=======================================================================================
*
*/
typedef int (*PFN_IA)(const GRID*, int);
/*=======================================================================================
*
*/
int get_winner(const GRID* grid);
int can_play(const GRID* grid, int x);
void play(GRID* grid, int x, int player);
int ia1(const GRID* grid, int player);
int ia2(const GRID* grid, int player);
int ia3(const GRID* grid, int player);
int get_player(int player);
int get_choice(const GRID* grid, int player, int type);
void display_grid(const GRID* grid);
/****************************************************************************************
*
*/
int get_winner(const GRID* grid)
{
int x = 0, y = 0, cx = 0, cy = 0, xn = 0, yn = 0;
int i = 0, j = 0, score = 0, winner = 0;
int dep[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};
int ok = 0, player = 0;
for(i = 0; i < 4; i++)
{
cx = dep[i][0];
cy = dep[i][1];
for(x = 0; winner == 0 && x < CX; x++)
{
for(y = 0; winner == 0 && y < CY; y++)
{
ok = 1;
score = 0;
player = grid->matrix[x][y];
for(j = 0; ok && player != 0 && j < CONNECT; j++)
{
xn = x+cx*j;
yn = y+cy*j;
ok = (xn >= 0 && xn < CX && yn >= 0 && yn < CY);
ok = ok && (grid->matrix[xn][yn] == player);
if(ok)
score += grid->matrix[xn][yn];
}
if(ok && player != 0 && score == CONNECT*player)
winner = player;
}
}
}
return winner;
}
/****************************************************************************************
*
*/
int can_play(const GRID* grid, int x)
{
return (grid->columns[x] < CY);
}
/****************************************************************************************
*
*/
void play(GRID* grid, int x, int player)
{
grid->matrix[x][grid->columns[x]++] = player;
grid->count++;
}
/****************************************************************************************
*
*/
int is_full(const GRID* grid)
{
return (grid->count == CX*CY);
}
/****************************************************************************************
*
*/
int ia1(const GRID* grid, int player)
{
GRID testgrid = {0};
int x = 0, choice = -1;
for(x = 0; choice == -1 && x < CX; x++)
{
memcpy(&testgrid, grid, sizeof(GRID));
if(can_play(&testgrid, x))
{
play(&testgrid, x, player);
if(get_winner(&testgrid) == player)
choice = x;
}
}
return choice;
}
/****************************************************************************************
*
*/
int ia2(const GRID* grid, int player)
{
GRID testgrid = {0};
int xother = 0, choice = -1;
for(xother = 0; choice == -1 && xother < CX; xother++)
{
memcpy(&testgrid, grid, sizeof(GRID));
if(can_play(&testgrid, xother))
{
play(&testgrid, xother, OTHER_PLAYER(player));
if(get_winner(&testgrid) == OTHER_PLAYER(player))
choice = xother;
}
}
return choice;
}
/****************************************************************************************
*
*/
int ia3(const GRID* grid, int player)
{
GRID testgrid = {0};
GRID testgrid2 = {0};
int x = 0, xother = 0, choice = -1;
for(x = 0; choice == -1 && x < CX; x++)
{
memcpy(&testgrid, grid, sizeof(GRID));
if(can_play(&testgrid, x))
{
play(&testgrid, x, player);
choice = x;
for(xother = 0; choice == x && xother < CX; xother++)
{
memcpy(&testgrid2, &testgrid, sizeof(GRID));
if(can_play(&testgrid2, xother))
{
play(&testgrid2, xother, OTHER_PLAYER(player));
if(ia1(&testgrid2, player) != player)
choice = -1;
}
}
}
}
return choice;
}
/****************************************************************************************
*
*/
int get_player(int player)
{
int type = 0;
do
{
printf("Player %d (%d : Human, %d : CPU) : ", player, HUMAN, CPU);
scanf("%d", &type);
}while(type != HUMAN && type != CPU);
return type;
}
/****************************************************************************************
*
*/
int get_choice(const GRID* grid, int player, int type)
{
int choice = 0, i = 0;
PFN_IA ia[3] = {&ia1, &ia2, &ia3};
if(type == HUMAN)
{
do
{
printf("Player %d (%c) : ", player, (player == PLAYER1) ? 'X' : 'O');
scanf("%d", &choice);
choice--;
}while(choice < 0 || choice >= CX || !can_play(grid, choice));
}
else
{
choice = -1;
for(i = 0; choice == -1 && i < 3; i++)
choice = (*ia[i])(grid, player);
if(choice == -1)
{
do
{
choice = rand()%CX;
}while(!can_play(grid, choice));
}
}
return choice;
}
/****************************************************************************************
*
*/
void display_grid(const GRID* grid)
{
int x = 0, y = 0;
printf("\n");
for(x = 0; x < CX; x++)
printf(" %2d ", x+1);
printf("\n");
for(y = CY-1; y >= 0; y--)
{
for(x = 0; x < CX; x++)
printf("----");
printf("-\n");
for(x = 0; x < CX; x++)
{
switch(grid->matrix[x][y])
{
case PLAYER1 : printf("| X "); break;
case PLAYER2 : printf("| O "); break;
default : printf("| "); break;
}
}
printf("|\n");
}
for(x = 0; x < CX; x++)
printf("----");
printf("-\n");
}
/****************************************************************************************
*
*/
int main()
{
int players[2] = {0, 0};
GRID grid = {0};
int stop = 0, currentplayer = 0, currenttype = 0, winner = 0, choice = 0;
char c = 0;
srand(time(NULL));
do
{
printf("\n\nPuissance %d\n\n", CONNECT);
players[0] = get_player(PLAYER1);
players[1] = get_player(PLAYER2);
memset(&grid, 0, sizeof(GRID));
currentplayer = (rand()%2 == 0) ? PLAYER1 : PLAYER2;
do
{
display_grid(&grid);
currenttype = ((currentplayer == PLAYER1) ? players[0] : players[1]);
choice = get_choice(&grid, currentplayer, currenttype);
play(&grid, choice, currentplayer);
winner = get_winner(&grid);
currentplayer = OTHER_PLAYER(currentplayer);
}while(winner == 0 && !is_full(&grid));
display_grid(&grid);
if(winner == 0)
printf("Nobody won\n");
else
printf("Player %d won\n", winner);
printf("Continue (Y/N) ? ");
fflush(stdin);
scanf("%c", &c);
stop = (c != 'Y' && c != 'y');
}while(!stop);
return 0;
}