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
|
public int FlipColor(int x, int y, int player)
{
int num_flipped = 0;
int positionsToFlip[][] = null;
int next_player = X_CELL;
if (player == X_CELL)
{
next_player = O_CELL;
}
//Si il s'agit d'une position jouable
if (IsLegalMove(x, y, player))
{
int factor = 2;
int tmp_position = -2;
//On commence par chercher le nombre de directions possibles pour faire des transformations
for (int i = 0 ; i < directions.length ; i++)
{
int abs = x + directions[i][0];
int ord = y + directions[i][1];
if (grid[abs][ord] == next_player)
{
Vector xHistory = new Vector();
Vector yHistory = new Vector();
while (tmp_position != EMPTY_CELL || tmp_position != SIDE_CELL && tmp_position == next_player)
{
int tmp_abs = abs * factor;
int tmp_ord = ord * factor;
//On memorise la position de l'adversaire
xHistory.add(tmp_abs);
yHistory.add(tmp_ord);
tmp_position = grid[tmp_abs][tmp_ord];
//On memorise la position du pion a l'extremite
if (tmp_position == player)
{
xHistory.add(tmp_abs);
yHistory.add(tmp_ord);
positionsToFlip = new int[xHistory.size()][2];
for (int k = 0 ; k < positionsToFlip.length ; k++)
{
positionsToFlip[k][0] = Integer.valueOf(xHistory.get(k).toString());
positionsToFlip[k][1] = Integer.valueOf(yHistory.get(k).toString());
}
}
factor++;
}
}
}
for (int i = 0 ; i < positionsToFlip.length ; i++)
{
int abs = positionsToFlip[i][0];
int ord = positionsToFlip[i][1];
grid[abs][ord] = player;
num_flipped++;
}
}
return num_flipped;
} |
Partager