public virtual bool CheckForMate(ChessSide side, byte king_index) { IChessItemModel kingModel = new NullChessItemModel(); for (int i = 0; i < FiguresPlacement.GetLength(0); i++) { for (int j = 0; j < FiguresPlacement.GetLength(1); j++) { var cm_ij = FiguresPlacement[i, j]; if (!cm_ij.IsNullObject && cm_ij.Type == king_index && cm_ij.Side == side) { kingModel = cm_ij; break; } } } if (kingModel.IsNullObject) { throw new Exception(side.ToString() + " king is missing!"); } foreach (var cm_ij in FiguresPlacement) { if (!cm_ij.IsNullObject && cm_ij.Side == side) { List <bool> isJumpTemp; List <bool> killPossibilityList; foreach (var poss_move in cm_ij.GetPossibleMoves(FiguresPlacement, out isJumpTemp, out killPossibilityList)) { var prev_pos = cm_ij.Pos; var temp_chess_item = MoveChessItemPredictForward(cm_ij.Pos, poss_move); bool isCheck = CheckForCheck(side); MoveChessItemPredictBackward(temp_chess_item, prev_pos, cm_ij.Pos); if (!isCheck && poss_move != prev_pos) { return(false); } } } } return(true); }
protected bool CheckForCheck(ChessSide side, byte king_index) { IChessItemModel cm = new NullChessItemModel(); for (int i = 0; i < FiguresPlacement.GetLength(0); i++) { for (int j = 0; j < FiguresPlacement.GetLength(1); j++) { var cm_ij = FiguresPlacement[i, j]; if (!cm_ij.IsNullObject && cm_ij.Type == king_index && cm_ij.Side == side) { cm = cm_ij; break; } } } if (cm.IsNullObject) { return(false); } foreach (var cm_ij in FiguresPlacement) { if (!cm_ij.IsNullObject && cm_ij != cm && cm_ij.Side != side) { List <bool> isJumpTemp; List <bool> killPossibilityList; var poss_moves = cm_ij.GetPossibleMoves(FiguresPlacement, out isJumpTemp, out killPossibilityList); int k = -1; foreach (var poss_move in poss_moves) { k++; if (poss_move == cm.Pos && killPossibilityList[k]) { return(true); } } } } return(false); }