public static void PrintPiece(IPiece piece, ConsoleColor backgroundColor, int top, int left)
        {
            if (piece == null)
            {
                PrintEmptySquare(backgroundColor, top, left);
                return;
            }

            if (!patterns.ContainsKey(piece.GetType().Name))
            {
                return;
            }

            var piecePattern = patterns[piece.GetType().Name];


            for (int i = 0; i < piecePattern.GetLength(0); i++)
            {
                for (int j = 0; j < piecePattern.GetLength(1); j++)
                {
                    Console.SetCursorPosition(left + j, top + i);
                    if (piecePattern[i, j])
                    {
                        Console.BackgroundColor = piece.Color.ToConsoleColor();
                    }
                    else
                    {
                        Console.BackgroundColor = backgroundColor;
                    }
                    Console.Write(" ");
                }
            }
        }
Example #2
0
        // Changed Pawn by IPiece so this method can be reused for other pieces
        public void Add(IPiece piece, int xCoordinate, int yCoordinate)
        {
            switch (piece.GetType().Name)
            {
            case nameof(Pawn):
                AddPawn(piece as Pawn, xCoordinate, yCoordinate);
                break;

            // Other pieces added here
            // With a private method for each
            default:
                throw new Exception("Unknown " + piece.GetType().Name + " piece");
            }
        }
        private void validate(RankFile pieceCurrentPosition, RankFile pieceDesiredDestination)
        {
            BasicGame game = Player.Game;

            Chess.Game.Board board = game.Board;
            IPiece           piece = board[pieceCurrentPosition].Piece.Object;

            if (piece == null)
            {
                throw new InvalidMoveException($"There is no piece at the position {pieceCurrentPosition.ToString()}");
            }

            if (piece.CanMoveTo(board[pieceDesiredDestination]) == false)
            {
                throw new InvalidMoveException($"{piece.GetType().Name} at {pieceCurrentPosition.ToString()} cannot move to {pieceDesiredDestination.ToString()}");
            }

            if (Player.Pieces.Contains(piece) == false)
            {
                throw new InvalidMoveException($"{piece.GetType().Name} at {pieceCurrentPosition.ToString()} does not belong to {Player.Name}");
            }
        }
Example #4
0
        private View.Pieces.BasePiece CreatePiece(IPiece piece)
        {
            var pieceObject = Instantiate(mPiecePrefab, transform, true);

            pieceObject.transform.localScale    = new Vector3(1, 1, 1);
            pieceObject.transform.localRotation = Quaternion.identity;

            var pieceType = _modelMapping[piece.GetType()];
            var pieceView = (View.Pieces.BasePiece)pieceObject.AddComponent(pieceType);

            pieceView.viewManager = this;
            pieceView.Setup(piece.GetColor(), ColorSchema.PieceColors[piece.GetColor()]);

            return(pieceView);
        }
Example #5
0
        public MoveValidationOutput IsMoveValid(EPlayer activePlayer, IPiece piece, Cell src, Cell dst, Board board)
        {
            if (piece.Player != activePlayer)
            {
                return new MoveValidationOutput
                       {
                           Valid          = false,
                           InvalidMessage = "piece not players"
                       }
            }
            ;
            var rules = MovementRules[piece.GetType()];        // e.g., piece.GetRule().IsMoveValid(piece, src, dst, board) or

            return(rules.IsMoveValid(piece, src, dst, board)); // straightforward piece.IsMoveValid(src, dst, board)
        }
    }
Example #6
0
        /// <summary>Finds the Piece on this board that matches the given argument. There are three requirements for a match:
        ///first that the two pieces are at the same position on their respective boards (and they may belong to different boards),
        ///second is that they are of the same type, the third that they are the same color. Note that this function does
        /// not verify that they are the same object (and indeed the very purpose of this function is such that in most
        /// cases the argument and the return value should point to different objects entirely)</summary>
        ///
        /// <param name="piece">The piece to match</param>
        public IPiece FindMatchingPiece(IPiece piece)
        {
            Square square = FindMatchingSquare(piece.Square);

            if (square.Piece.HasValue)
            {
                IPiece potentialMatchingPiece = square.Piece.Object;

                if ((potentialMatchingPiece.GetType().Name == piece.GetType().Name) && (potentialMatchingPiece.Color == piece.Color))
                {
                    return(potentialMatchingPiece);
                }
            }

            throw new ArgumentException("Matching piece not found on this board");
        }
Example #7
0
            /// <summary>
            ///    Creates a new piece by copying the piece passed
            /// </summary>
            /// <param name="piece">
            ///     The piece to copy
            /// </param>
            /// <returns>A copy of piece</returns>
            /// <exception cref="TypeInitializationException"></exception>
            public static Graphical.Piece Create(IPiece piece)
            {
                switch (piece)
                {
                case IPawn pawn:
                {
                    return(new Pawn(pawn));
                }

                case IKnight knight:
                {
                    return(new Knight(knight));
                }

                case IBishop bishop:
                {
                    return(new Bishop(bishop));
                }

                case IRook rook:
                {
                    return(new Rook(rook));
                }

                case IQueen queen:
                {
                    return(new Queen(queen));
                }

                case IKing king:
                {
                    return(new King(king));
                }

                default:
                {
                    throw new TypeInitializationException(piece.GetType().Name, new Exception("Unknown subclass of Piece"));
                }
                }
            }
Example #8
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            //Check mouse
            MouseState mouseState = Mouse.GetState();

            if (mouseState.LeftButton == ButtonState.Pressed && playing)
            {
                foreach (IPiece piece in pieces)
                {
                    //Check if no piece is selected, the piece is on the right side, and the piece was clicked on
                    if (!isPieceSelected && piece.Side == turn && !piece.Dead && piece.CollidesWithPiece(mouseState.Position))
                    {
                        piece.Select();
                        previousPiece    = piece;
                        previousPosition = previousPiece.Position;
                        isPieceSelected  = true;
                    }
                    else if (piece.Selected)
                    {
                        //Get move location
                        Vector2 moveLocation = new Vector2((mouseState.Position.X / 64) * 64, (mouseState.Position.Y / 64) * 64);

                        bool movedPiece = false;

                        IPiece collidingPiece = null;
                        //Check if piece is attacking another piece
                        foreach (IPiece otherPiece in pieces)
                        {
                            if (otherPiece.CollidesWithPiece(moveLocation.ToPoint()) && !otherPiece.Dead)
                            {
                                collidingPiece = otherPiece;
                                break;
                            }
                        }

                        if (collidingPiece == null)
                        {
                            killedPiece = null;
                            pieceKilled = false;
                            piece.Move(moveLocation);
                            movedPiece = true;
                        }
                        else if (collidingPiece.Side != turn)
                        {
                            piece.Attack();
                            piece.Move(moveLocation);
                            killedPiece = collidingPiece;
                            pieceKilled = true;
                            collidingPiece.Kill();
                            movedPiece = true;

                            //Check for end game condition
                            if (collidingPiece.GetType() == typeof(King))
                            {
                                playing     = false;
                                winningTeam = turn;
                            }
                        }

                        if (movedPiece)
                        {
                            isPieceSelected = false;
                            if (turn == "white")
                            {
                                previousTurn = "white";
                                turn         = "black";
                            }
                            else
                            {
                                previousTurn = "black";
                                turn         = "white";
                            }
                        }
                    }
                }
            }
            else if (mouseState.RightButton == ButtonState.Pressed && playing)
            {
                isPieceSelected = false;
            }

            //Check if the next song needs to be played
            if (MediaPlayer.State != MediaState.Playing)
            {
                MediaPlayer.Play(songs[songiterator]);
                songiterator++;
                if (songiterator > 4)
                {
                    songiterator = 0;
                }
            }

            // Check for board reset
            if (Keyboard.GetState().IsKeyDown(Keys.R))
            {
                ResetBoard();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Back) && playing)
            {
                previousPiece.Move(previousPosition);
                turn = previousTurn;
                if (pieceKilled)
                {
                    killedPiece.setState(AnimationState.Idle0);
                }
                pieceKilled = false;
            }

            //Update pieces
            foreach (IPiece piece in pieces)
            {
                piece.Update(gameTime);
            }

            base.Update(gameTime);
        }
Example #9
0
        public override void HandleLeftMouseButtonUp()
        {
            IPlayer     thisPlayer        = model.ThisPlayer;
            IPiece      pieceBeingDragged = thisPlayer.PieceBeingDragged;
            IPlayerHand playerHand        = model.CurrentGameBox.CurrentGame.GetPlayerHand(thisPlayer.Guid);

            if (pieceBeingDragged != null && playerHand.Count > 0 && playerHand.Pieces[0].Stack == pieceBeingDragged.Stack)
            {
                ICursorLocation cursorLocation = thisPlayer.CursorLocation;
                // over the hand
                if (cursorLocation is IHandCursorLocation)
                {
                    IHandCursorLocation location = (IHandCursorLocation)cursorLocation;
                    int insertionIndex           = location.Index;
                    int currentIndex             = pieceBeingDragged.IndexInStackFromBottomToTop;
                    // assumption: the stack will remain unchanged in the meantime
                    if (insertionIndex != currentIndex && insertionIndex != currentIndex + 1 &&
                        !model.AnimationManager.IsBeingAnimated(pieceBeingDragged.Stack))
                    {
                        networkClient.Send(new RearrangePlayerHandMessage(model.StateChangeSequenceNumber, currentIndex, insertionIndex));
                    }
                    else
                    {
                        networkClient.Send(new DragDropAbortedMessage());
                    }
                    // over the board
                }
                else if (cursorLocation is IBoardCursorLocation)
                {
                    IBoardCursorLocation location = (IBoardCursorLocation)cursorLocation;
                    IPiece pieceAtMousePosition   = location.Piece;
                    // over an unattached stack
                    if (model.CurrentGameBox.CurrentGame.StackingEnabled && pieceAtMousePosition != null && !pieceAtMousePosition.Stack.AttachedToCounterSection && pieceAtMousePosition.GetType() == pieceBeingDragged.GetType() && !(pieceBeingDragged is ITerrainClone))
                    {
                        // assumption: both stacks will remain unchanged in the meantime
                        if (pieceAtMousePosition.Stack != pieceBeingDragged.Stack &&
                            !model.AnimationManager.IsBeingAnimated(pieceBeingDragged.Stack) &&
                            !model.AnimationManager.IsBeingAnimated(pieceAtMousePosition.Stack))
                        {
                            networkClient.Send(new DragDropPieceOnTopOfOtherStackMessage(model.StateChangeSequenceNumber, pieceBeingDragged.Id, pieceAtMousePosition.Stack.Id));
                        }
                        else
                        {
                            networkClient.Send(new DragDropAbortedMessage());
                        }
                        // over an empty space
                    }
                    else
                    {
                        // assumption: the stack will remain unchanged in the meantime
                        if (!model.AnimationManager.IsBeingAnimated(pieceBeingDragged.Stack))
                        {
                            PointF newPiecePosition = new PointF(
                                location.ModelPosition.X - thisPlayer.DragAndDropAnchor.X,
                                location.ModelPosition.Y - thisPlayer.DragAndDropAnchor.Y);
                            if (pieceBeingDragged is ITerrainClone)
                            {
                                networkClient.Send(new DragDropTerrainMessage(model.StateChangeSequenceNumber, -1, pieceBeingDragged.IndexInStackFromBottomToTop, newPiecePosition));
                            }
                            else
                            {
                                networkClient.Send(new DragDropPieceMessage(model.StateChangeSequenceNumber, pieceBeingDragged.Id, newPiecePosition));
                            }
                        }
                        else
                        {
                            networkClient.Send(new DragDropAbortedMessage());
                        }
                    }
                    // over the stack inspector
                }
                else if (cursorLocation is IStackInspectorCursorLocation)
                {
                    IStackInspectorCursorLocation location = (IStackInspectorCursorLocation)cursorLocation;
                    // assumption: boths stacks will remain unchanged in the meantime
                    if (model.CurrentSelection != null && model.CurrentSelection.Stack != pieceBeingDragged.Stack && model.CurrentSelection.Stack.Pieces[0].GetType() == pieceBeingDragged.GetType() &&
                        !model.AnimationManager.IsBeingAnimated(model.CurrentSelection.Stack) &&
                        !model.AnimationManager.IsBeingAnimated(pieceBeingDragged.Stack))
                    {
                        networkClient.Send(new DragDropPieceIntoOtherStackMessage(model.StateChangeSequenceNumber, pieceBeingDragged.Id, location.Index));
                    }
                    else
                    {
                        networkClient.Send(new DragDropAbortedMessage());
                    }
                    // over a counter sheet tab
                    // assumption: the stack will remain unchanged in the meantime
                }
                else if (cursorLocation is ITabsCursorLocation && ((ITabsCursorLocation)cursorLocation).Tab is ICounterSheet &&
                         !model.AnimationManager.IsBeingAnimated(pieceBeingDragged.Stack))
                {
                    if (pieceBeingDragged is ITerrainClone)
                    {
                        networkClient.Send(new RemoveTerrainMessage(model.StateChangeSequenceNumber, -1, pieceBeingDragged.IndexInStackFromBottomToTop));
                    }
                    else
                    {
                        networkClient.Send(new UnpunchPieceMessage(model.StateChangeSequenceNumber, pieceBeingDragged.Id));
                    }
                }
                else
                {
                    networkClient.Send(new DragDropAbortedMessage());
                }
            }
            thisPlayer.PieceBeingDragged = null;
            controller.State             = controller.IdleState;
        }
Example #10
0
        public override void UpdateCursor(System.Windows.Forms.Form mainForm, IView view)
        {
            IPiece pieceBeingDragged = model.ThisPlayer.PieceBeingDragged;

            if (pieceBeingDragged == null)
            {
                controller.State = controller.IdleState;
                controller.IdleState.UpdateCursor(mainForm, view);
            }
            else
            {
                ICursorLocation cursorLocation = model.ThisPlayer.CursorLocation;
                if (cursorLocation is IHandCursorLocation)
                {
                    mainForm.Cursor = view.FingerCursor;
                }
                else if (cursorLocation is IBoardCursorLocation)
                {
                    IBoardCursorLocation location = (IBoardCursorLocation)cursorLocation;
                    mainForm.Cursor = (!model.CurrentGameBox.CurrentGame.StackingEnabled || location.Piece == null || location.Piece.Stack.AttachedToCounterSection || location.Piece.GetType() != pieceBeingDragged.GetType() || pieceBeingDragged is ITerrainClone ? view.FingerCursor : view.FingerAddCursor);
                }
                else if (cursorLocation is IStackInspectorCursorLocation)
                {
                    mainForm.Cursor = (model.CurrentSelection == null || model.CurrentSelection.Stack.Pieces[0].GetType() != pieceBeingDragged.GetType() ?
                                       System.Windows.Forms.Cursors.No : view.FingerAddCursor);
                }
                else if (cursorLocation is ITabsCursorLocation)
                {
                    ITabsCursorLocation location = (ITabsCursorLocation)cursorLocation;
                    mainForm.Cursor = (location.Tab is ICounterSheet ? view.FingerRemoveCursor : view.FingerCursor);
                }
                else
                {
                    mainForm.Cursor = System.Windows.Forms.Cursors.No;
                }
            }
        }
Example #11
0
 public bool PieceIsEnemy(IPiece piece)
 {
     return(piece.GetType() != CurrentPlayer.Pieces[0].GetType());
 }