Example #1
0
        /// <summary>
        /// Player wants to play a move
        /// </summary>
        /// <param name="sender">Player</param>
        /// <param name="Move">Move to play</param>
        private void Player_MakesMove(object sender, int Move)
        {
            if (GamePosition.Status != Position.GameResult.InProcess)
            {
                MessageBox.Show("Error. Game is finished. No moves allowed");
            }

            //Engine tried to move but couldn't
            if (Move == Engine.NO_MOVES)
            {
                MessageBox.Show(PerfectChess.Move.Details(Move));
                return;
            }

            //Check for legality
            if (!GamePosition.LegalMoves().Contains(Move))
            {
                MessageBox.Show(PerfectChess.Move.Details(Move));
                return;
            }

            //Make the move
            GamePosition.Make(Move);
            if (PlayerWaiting is HumanPlayer)
            {
                BoardView.FinishMove(Move);
            }
            else
            {
                BoardView.PerformComputerMove(Move);
            }

            //Add visual move effects
            ApplyMoveEffects();
            if (GamePosition.Status != Position.GameResult.InProcess)
            {
                return;
            }
            //if (GamePosition.GameFinished) return;

            //Tell the other guy he can move
            PlayerToMove.YourMove(GamePosition.DeepCopy());
        }
Example #2
0
 /// <summary>
 /// Сделать ход в позиции (возвращает - возможен ли он)
 /// </summary>
 public static bool TryMoved(MoveCoord move)
 {
     if (GamePosition.Board[move.xStart, move.yStart].Figure.Type == TypeFigur.king)
     {
         if (move.yStart == 4)
         {
             if ((move.xStart == 0 && GamePosition.IsWhiteMove) || (move.xStart == 7 && GamePosition.IsWhiteMove == false))
             {
                 if (move.yEnd == 2 || move.yEnd == 6)
                 {
                     move.IsCastling = true;
                 }
             }
         }
     }
     if (GamePosition.Board[move.xStart, move.yStart].Figure.CheckMove(GamePosition, move))
     {
         Positions.Add((Position)GamePosition.DeepCopy());
         MoveCoord mc = new MoveCoord();
         mc.xStart      = move.xStart;
         mc.yStart      = move.yStart;
         mc.yEnd        = move.yEnd;
         mc.xEnd        = move.xEnd;
         mc.StartFigure = move.StartFigure;
         Moves.Add(mc);
         int coll = 0;
         GamePosition.MoveChess(move);
         if (move.EndFigure == new NotFigur() && !move.IsEnPassant)
         {
             MovesWithoutEating++;
         }
         else
         {
             MovesWithoutEating = 0;
         }
         foreach (Position p in Positions)
         {
             if (p.Equals(GamePosition))
             {
                 coll++;
             }
         }
         if (GamePosition.IsWhiteMove)
         {
             TimeBlack.Stop();
             TimeWhite.Start();
             int?i = CheckPosition(GamePosition);
             if (i < 0)
             {
                 BlackWin = true;
                 StopGame();
             }
             else if (i == 0)
             {
                 StopGame();
             }
             else if (coll > 2)
             {
                 MessageBox.Show("Троекратное повторение ходов. Ничья!");
                 MovesWithoutEating = 50;
                 StopGame();
             }
             else if (MovesWithoutEating > 49)
             {
                 MessageBox.Show("50 ходов без взятий. Ничья!");
                 StopGame();
             }
             return(true);
         }
         else
         {
             TimeWhite.Stop();
             TimeBlack.Start();
             int?i = CheckPosition(GamePosition);
             if (i > 0)
             {
                 WhiteWin = true;
                 StopGame();
             }
             if (i == 0)
             {
                 StopGame();
             }
             if (coll > 2 || MovesWithoutEating > 49)
             {
                 StopGame();
             }
             return(true);
         }
     }
     move = new MoveCoord();
     return(false);
 }