Пример #1
0
        /// <summary>
        /// Demande au plateau de vérifiré la possibilité d'un échec et mat.
        /// </summary>
        /// <returns>Vrai si un des rois est en échec et mat.</returns>
        public bool askBoardCheckMat()
        {
            //trying move on temp board
            Board tempBoard = new Board(m_board.ToString());

            for (int tileX = 0; tileX < tempBoard.Width; tileX++)
            {
                for (int tileY = 0; tileY < tempBoard.Width; tileY++)
                {
                    if (tempBoard[tileX, tileY].isOccupied() && tempBoard[tileX, tileY].getPieceColor() == m_turn)
                    {
                        for (int x = -1; x <= 1; x++)
                        {
                            for (int y = -1; y <= 1; y++)
                            {
                                if (x != 0 || y != 0)
                                {
                                    int[] direction = new int[] { x, y };
                                    int[] start     = new int[] { tempBoard[tileX, tileY].X, tempBoard[tileX, tileY].Y };
                                    int[] end       = new int[] { start[0], start[1] };
                                    Move  tempMove  = new Move(tempBoard[start[0], start[1]]);

                                    bool isOutOfIndex = true;
                                    bool isPossible   = false;
                                    do
                                    {
                                        isOutOfIndex = !((end[0] + direction[0] > 0 && end[0] + direction[0] < 8) && (end[1] + direction[1] > 0 && end[1] + direction[1] < 8));
                                        if (!isOutOfIndex)
                                        {
                                            end[0]      += direction[0];
                                            end[1]      += direction[1];
                                            tempMove.End = tempBoard[end[0], end[1]];
                                            isPossible   = (tempMove.isValidMovement() && !tempBoard.isCollisionning(tempMove.getCoordFrom(), tempMove.getCoordTo()) && tempBoard[tempMove.getCoordTo()[0], tempMove.getCoordTo()[1]].getPieceColor() != m_turn);
                                            if (isPossible)
                                            {
                                                tempBoard.movePiece(start, end);

                                                //Does the move puts us in check position on the temp board
                                                if (!tempBoard.detectCheck(m_turn))
                                                {
                                                    return(false);
                                                }
                                                tempBoard.movePiece(end, start);
                                            }
                                        }
                                    } while (!isOutOfIndex && isPossible);
                                }
                            }
                        }
                    }
                }
            }
            return(true);
        }