tuto Reversi : checker qu'un pion est bien entourée

LouLou Membre
octobre 2014 modifié dans API UIKit #1

Salut,


dans un jeu du "Reversi" (le tuto, le code se trouve en lien en bas de page), si on entoure une pièce blanche avec des pièces noires, la pièce blanche devient noire. Dans ce tuto, il me semble que l'on peut placer une pièce blanche sur une ligne à  côté d'une autre pièce blanche disons, et que si une pièce noire se trouve à  "une" case, même sans l'entourer, la pièce noire devient blanche. Alors qu'il faudrait vérifier que 2 pièces blanches entourent bien la pièce noire... 


 


En gros, le code fait ça : on va checker les 8 cases entourant la pièce qu'on possède, mais pas si une pièce se trouve à  "2 cases" ce qui voudrait dire qu'elle est bien entourée :


 


Le typedef pour checker les 8 cases autour de la pièce :



typedef void (^BoardNavigationFunction)(NSInteger*, NSInteger*);

BoardNavigationFunction BoardNavigationFunctionRight = ^(NSInteger* c, NSInteger* r) {
(*c)++;
};

BoardNavigationFunction BoardNavigationFunctionLeft = ^(NSInteger* c, NSInteger* r) {
(*c)--;
};

BoardNavigationFunction BoardNavigationFunctionUp = ^(NSInteger* c, NSInteger* r) {
(*r)--;
};

BoardNavigationFunction BoardNavigationFunctionDown = ^(NSInteger* c, NSInteger* r) {
(*r)++;
};

BoardNavigationFunction BoardNavigationFunctionRightUp = ^(NSInteger* c, NSInteger* r) {
(*c)++;
(*r)--;
}; etc...........

la fonction qui appelle flip... :



- (void)makeMoveToColumn:(NSInteger)column andRow:(NSInteger)row
{
// place the playing piece at the given location
[self setCellState:self.nextMove forColumn:column andRow:row];

// check the 8 play directions and flip pieces
for(int i=0; i<8; i++)
{
[self flipOponnentsCountersForColumn:column
andRow:row
withNavigationFunction:_boardNavigationFunctions[i]
toState:self.nextMove];
}

_nextMove = [self invertState:_nextMove];

}


la fonction flip :



- (void) flipOponnentsCountersForColumn:(int) column andRow:(int)row withNavigationFunction:(BoardNavigationFunction) navigationFunction toState:(BoardCellState) state
{
// are any pieces surrounded in this direction?
if (![self moveSurroundsCountersForColumn:column
andRow:row
withNavigationFunction:navigationFunction
toState:state])
return;

BoardCellState opponentsState = [self invertState:state];
BoardCellState currentCellState;

// flip counters until the edge of the boards is reached, or
// a piece of the current state is reached
do
{
// advance to the next cell
navigationFunction(&column, &row);
currentCellState = [super cellStateAtColumn:column andRow:row];
[self setCellState:state forColumn:column andRow:row];
}
while(column>=0 && column<=7 &&
row>=0 && row<=7 &&
currentCellState == opponentsState);

}

J'ai sûrement du zapper un truc, mais je ne vois pas où...


Connectez-vous ou Inscrivez-vous pour répondre.