You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

283 lines
5.0 KiB

#include "gamestate.h"
#include <stdio.h>
int getHoleIdFromCoordinates(const int x, const int y)
{
switch (y)
{
case -4:
return (x == 0) ? 0 : -1;
case -3:
return (x >= 0 && x <= 1) ? x+1 : -1;
case -2:
return (x >= 0 && x <= 2) ? x+3 : -1;
case -1:
return (x >= 0 && x <= 3) ? x+6 : -1;
case 0:
return (x >= -4 && x <= 8) ? x+14 : -1;
case 1:
return (x >= -3 && x <= 8) ? x+26 : -1;
case 2:
return (x >= -2 && x <= 8) ? x+37 : -1;
case 3:
return (x >= -1 && x <= 8) ? x+47 : -1;
case 4:
return (x >= 0 && x <= 8) ? x+56 : -1;
case 5:
return (x >= 0 && x <= 9) ? x+65 : -1;
case 6:
return (x >= 0 && x <= 10) ? x+75 : -1;
case 7:
return (x >= 0 && x <= 11) ? x+86 : -1;
case 8:
return (x >= 0 && x <= 12) ? x+98 : -1;
case 9:
return (x >= 5 && x <= 8) ? x+106 : -1;
case 10:
return (x >= 6 && x <= 8) ? x+109 : -1;
case 11:
return (x >= 7 && x <= 8) ? x+111 : -1;
case 12:
return (x == 8) ? 120 : -1;
default:
return -1;
}
}
void getHoleCoordinatesFromId(int id, int* x, int* y)
{
int lineLength[] = {1,2,3,4,
13,12,11,10,9,10,11,12,13,
4,3,2,2};
(*y) = 0;
while (id >= lineLength[(*y)])
{
id -= lineLength[*y];
(*y) ++;
}
if ((*y) < 0 || (*y) > 16)
return;
(*y) -= 4;
switch (*y)
{
case -4:
*x = 0;
break;
case -3:
*x = id;
break;
case -2:
*x = id;
break;
case -1:
*x = id;
break;
case 0:
*x = id-4;
break;
case 1:
*x = id-3;
break;
case 2:
*x = id-2;
break;
case 3:
*x = id-1;
break;
case 4:
*x = id;
break;
case 5:
*x = id;
break;
case 6:
*x = id;
break;
case 7:
*x = id;
break;
case 8:
*x = id;
break;
case 9:
*x = id+5;
break;
case 10:
*x = id+6;
break;
case 11:
*x = id+7;
break;
case 12:
*x = 8;
break;
}
}
void findPlayerPieces(const SGameState* state, const EPlayer color, int pieces[10])
{
int nb = 0;
int i=0;
while (nb < 10 && i < 121)
{
if (state->board[i] == color)
{
pieces[nb] = i;
nb++;
}
i++;
}
}
int getNeighbour(const int hole, Direction dir)
{
int x,y;
getHoleCoordinatesFromId(hole, &x, &y);
switch (dir)
{
case EAST:
x += 1;
break;
case SOUTH_EAST:
x += 1;
y += 1;
break;
case SOUTH_WEST:
y += 1;
break;
case WEST:
x -= 1;
break;
case NORTH_WEST:
x -= 1;
y -= 1;
break;
case NORTH_EAST:
y -= 1;
break;
}
return getHoleIdFromCoordinates(x, y);
}
void getNeighbours(const int hole, int* neighbours)
{
int x,y;
getHoleCoordinatesFromId(hole, &x, &y);
neighbours[0] = getHoleIdFromCoordinates(x+1,y);
neighbours[1] = getHoleIdFromCoordinates(x+1,y+1);
neighbours[2] = getHoleIdFromCoordinates(x,y+1);
neighbours[3] = getHoleIdFromCoordinates(x-1,y);
neighbours[4] = getHoleIdFromCoordinates(x-1,y-1);
neighbours[5] = getHoleIdFromCoordinates(x,y-1);
}
void getNeighbours2(const int hole, int* neighbours)
{
int x,y;
getHoleCoordinatesFromId(hole, &x, &y);
neighbours[0] = getHoleIdFromCoordinates(x+2,y);
neighbours[1] = getHoleIdFromCoordinates(x+2,y+2);
neighbours[2] = getHoleIdFromCoordinates(x,y+2);
neighbours[3] = getHoleIdFromCoordinates(x-2,y);
neighbours[4] = getHoleIdFromCoordinates(x-2,y-2);
neighbours[5] = getHoleIdFromCoordinates(x,y-2);
}
void findReachableHoles(const SGameState* state, const int start, int* holes)
{
int n1[6], n2[6];
getNeighbours(start, n1);
getNeighbours2(start, n2);
for (int i=0; i<6; i++)
{
if (n1[i] != -1 && state->board[n1[i]] == none)
holes[i] = n1[i];
else if (n2[i] != -1 && state->board[n2[i]] == none)
holes[i] = n2[i];
else
holes[i] = -1;
}
}
int isStarBranch(const int hole)
{
int lineMax[17] = {0,2,5,9,22,34,45,55,64,74,85,97,110,114,117,119,120};
int TGaucheHaut[10]={0};
int TGaucheBas [10]={0};
int TDroiteHaut[10]={0};
int TDroiteBas [10]={0};
int i=0;
int j=0;
int k=0;
if((hole <=9 && hole>=0) )
return 1;
if((hole <=120 && hole>=111))
return 4;
k=0;
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
TDroiteHaut[k++]=lineMax[4+i]-j;
}
}
for(i=0;i<10;i++)
if( TDroiteHaut[i]==hole)
return 2;
k=0;
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
TDroiteBas[k++]=lineMax[12-i]-j;
}
}
for(i=0;i<10;i++)
if( TDroiteBas [i]==hole)
return 3;
k=0;
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
TGaucheBas[k++]=lineMax[11-i]+1+j;
}
}
for(i=0;i<10;i++)
if( TGaucheBas[i]==hole)
return 5;
k=0;
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
TGaucheHaut[k++]=lineMax[3+i]+1+j;
}
}
for(i=0;i<10;i++)
if( TGaucheHaut[i]==hole)
return 6;
return 0;
}