예제 #1
0
        /// <summary>
        /// Permet la surbrillance d'une tuile.
        /// </summary>
        /// <param name="coordFrom">D'ou provient le mouvement.</param>
        public void tryMove(int[] coordFrom)
        {
            //if (m_board[p_x,p_y].CurrentPiece != null)
            bool isMove = m_board.isMoving(coordFrom, m_turn);

            //If the player is trying to select a piece
            if (!isMove)
            {
                bool isValidTile = m_board.trySelectTile(coordFrom, m_turn);
                if (isValidTile)
                {
                    m_board.markPossible(coordFrom, coordFrom);
                    if (this.m_turn == 'W')
                    {
                        this.m_move = new Move(m_board.SelectedTile);
                    }
                    else
                    {
                        this.m_move = new Move(m_board.SelectedTile);
                    }
                }
            }
            //If the player is trying to move
            else
            {
                m_move.End = m_board[coordFrom[0], coordFrom[1]];
                bool destValid = this.m_board.isDestinationValid(m_move.Start, m_move.End, this.m_turn);
                if (destValid)
                {
                    bool validMovement = this.m_move.isValidMovement();
                    if (validMovement)
                    {
                        bool isCollisionning = this.m_board.isCollisionning(m_move.getCoordFrom(), m_move.getCoordTo());
                        if (!isCollisionning)
                        {
                            //If the board is in a check state
                            if (!askBoardCheck())
                            {
                                move(m_move.getCoordFrom(), m_move.getCoordTo());
                                switchTurns();
                                if (m_board.detectCheck(m_turn))
                                {
                                    m_gameGUI.writeEvent(getCurrentPlayerName() + ": Déplacement invalide vers X(" + (m_move.getCoordTo()[0] + 1) + "), Y(" + (m_move.getCoordTo()[1] + 1) + "), car vous êtes en échec!");
                                }
                            }
                            else
                            {
                                if (askBoardCheckPat())
                                {
                                    Console.WriteLine(m_turn + "checkPat Detected !");
                                }
                            }


                            this.m_board.isCheckState = m_board.detectCheck(this.m_turn);


                            if (askBoardCheckMat())
                            {
                                switchTurns();
                                m_gameGUI.writeEvent(getCurrentPlayerName() + ": a gagné!");
                                m_gameGUI.Refresh();
                                refreshBoard();

                                if (Turn)
                                {
                                    endGame(m_pWhite, m_pBlack);
                                }
                                else
                                {
                                    endGame(m_pBlack, m_pWhite);
                                }

                                Thread.Sleep(3000);
                                m_gameGUI.Close();
                            }
                            refreshBoard();
                        }
                    }
                }
            }
            refreshBoard();
        }
예제 #2
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);
        }