Esempio n. 1
0
    private void Transport(Vector2Int origin, Vector2Int destination)
    {
        SetAt(destination, GetAt(origin));
        SetAt(origin, TileState.None);

        OnPieceMoved?.Invoke(this, origin, destination);
    }
Esempio n. 2
0
        protected virtual void Move()
        {
            var prevCell    = _currentCell;
            var targetPiece = targetCell.CurrentPiece;

            targetCell.KillPiece();
            targetCell.RemovePiece();
            _currentCell.RemovePiece();

            _currentCell = targetCell;
            _currentCell.SetPiece(this);
            gameObject.SetActive(true);

            transform.position = _currentCell.transform.position;

            targetCell  = null;
            isFirstMove = false;

            LastMove = new Move(prevCell.BoardPosition, _currentCell.BoardPosition, this);


            // Send message about movement
            OnPieceMoved?.Invoke(this);

            // GameManager.Instance.GameData.AddHalfMove(new Move(currentCell.BoardPosition, currentCell.CurrentPiece));

            Debug.Log(
                AnnotationEngine.Instance.ToSan(_currentCell, prevCell.BoardPosition, targetPiece, TeamColor,
                                                CastleStatus.NONE));
        }
Esempio n. 3
0
        private void AttemptMove(ChessTableSquare from, ChessTableSquare to, bool ignoreRules)
        {
            if (from.RepresentedPiece.Position.Equals(to.RepresentedPiece.Position))
            {
                throw new IllegalMoveException(to.RepresentedPiece, "You can't move on the same space");
            }
            PiecePosition toPosition   = new PiecePosition(to.RepresentedPiece.Position);
            PiecePosition fromPosition = new PiecePosition(from.RepresentedPiece.Position);

            System.Diagnostics.Debug.WriteLine(squares[(int)to.RepresentedPiece.Position.Column - 1, to.RepresentedPiece.Position.Row - 1].RepresentedPiece.ToString() + "\n" + squares[(int)from.RepresentedPiece.Position.Column - 1, from.RepresentedPiece.Position.Row - 1].RepresentedPiece.ToString());

            //If there is another piece there not owned by player1
            if (!ignoreRules && to.RepresentedPiece.Owner.Equals(Player1.Owner))
            {
                MessageBox.Show("You can not move over your own pieces");
                return;
            }

            //If it's players turn
            if (false)
            {
                throw new IllegalMoveException(to.RepresentedPiece, "It's not your turn");
            }

            //If the move is even permitted, move
            if (!from.RepresentedPiece.Move(to.RepresentedPiece.Position, squares))
            {
                throw new IllegalMoveException(from.RepresentedPiece, String.Format("Can not move from {0} to {1}", from.RepresentedPiece.Position.ToString(), to.RepresentedPiece.Position.ToString()));
            }


            //If it got to this point notify player2 of what happened
            // TODO: "from" and "to" are the same when are being accesed outside
            // NOTE: OnPieceMoved is called when forcibly moved by opponent
            OnPieceMoved?.Invoke(this, new PieceMovedEventArgs(from.RepresentedPiece, to.RepresentedPiece, fromPosition, toPosition));
            //If player2 has pieces there, take them out of the board,in this case, just raise event
            if (to.RepresentedPiece.Owner != from.RepresentedPiece.Owner)
            {
                OnPieceLost?.Invoke(this, new PieceLostEventArgs(from.RepresentedPiece, to.RepresentedPiece, fromPosition, toPosition));
            }
            if (to.RepresentedPiece.Type == PieceTypes.King)
            {
                OnKingLost?.Invoke(this, new PieceLostEventArgs(to.RepresentedPiece));
            }
            //If move was succesful then move graphically
            if (to.RepresentedPiece is DummyPiece)
            {
                to.RepresentedPiece.Dispose();
            }
            squares[(int)toPosition.Column - 1, toPosition.Row - 1].RepresentedPiece = from.RepresentedPiece;

            from.Selected      = false;
            userHasSelected    = false;
            lastSelectedSquare = null;

            squares[(int)fromPosition.Column - 1, fromPosition.Row - 1].RepresentedPiece = new DummyPiece(fromPosition);
        }
Esempio n. 4
0
        private void Chessboard_MouseUp(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Default;
            if (DragDropOperation.DraggedPiece != null && DragDropOperation.DraggedPiece.Kind != ChessPieceKind.None)
            {
                if (e.X > DigitAreaWidth && e.X < this.Width && e.Y < Height - LetterAreaHeight && e.Y > 0)
                {
                    //  Drops a piece on the board
                    var destinationSquare = (BoardDirection == BoardDirection.BlackOnTop ?
                                             new ChessSquare((ChessFile)((e.X - DigitAreaWidth) / SquareWidth), (ChessRank)(7 - (e.Y / SquareHeight))) :
                                             new ChessSquare((ChessFile)(7 - (e.X - DigitAreaWidth) / SquareWidth), (ChessRank)(e.Y / SquareHeight)));
                    var moveValidationResult = ChessEngine.GetMoveValidity(DragDropOperation.Origin, destinationSquare);
                    if (moveValidationResult.IsValid)
                    {
                        var promotionCancelled = false;
                        if (moveValidationResult.MoveKind.HasFlag(ChessMoveType.Promotion))
                        {
                            var pieceChooser = new FrmPromotion(ChessEngine.Turn);
                            promotionCancelled = pieceChooser.ShowDialog() != DialogResult.OK;
                            moveValidationResult.PromotedTo = pieceChooser.ChoosePiece;
                        }
                        if (!promotionCancelled)
                        {
                            moveValidationResult.ToSAN = ChessEngine.MoveToSAN(moveValidationResult);   //  Update the SAN after promotion
                            MovePiece(moveValidationResult);

                            OnPieceMoved?.Invoke(this, moveValidationResult);
                            if (ChessEngine.IsCheckmate)
                            {
                                OnCheckmate?.Invoke(this, new EventArgs());
                            }
                            else if (ChessEngine.IsCheck)
                            {
                                OnCheck?.Invoke(this, new EventArgs());
                            }
                            else if (ChessEngine.IsDraw)
                            {
                                OnDraw?.Invoke(this, new EventArgs());
                            }
                        }
                        else
                        {
                            Invalidate();
                        }
                    }
                    else
                    {
                        Invalidate();
                    }
                }
                else
                {
                    //  Drops a piece outside of the board
                    ChessEngine.RemovePieceAt(DragDropOperation.Origin);
                    Invalidate();
                    OnPieceRemoved?.Invoke(this, DragDropOperation.Origin, DragDropOperation.DraggedPiece, e.Location);
                }
            }
            DragDropOperation = new DragOperation(null, null);
            OnSquareUnselected?.Invoke(this, new EventArgs());
        }