Exemple #1
0
    public override void findPossibleMoves() //can move along file or rank, as long as it is not blocked
    {
        PossibleMoves.Clear();
        Coordinate   pTmp = new Coordinate(Position);
        string       color;
        Action <int> checkFile = (direction) =>
        {
            pTmp = new Coordinate(Position);
            for (; pTmp.changeFile(direction);)
            {
                color = board.CheckColor(pTmp);
                if (color == "none") //no piece, continue normally
                {
                    PossibleMoves.Add(pTmp);
                }
                else if (color != Color) //opposite piece, add its position and stop
                {
                    PossibleMoves.Add(pTmp);
                    break;
                }
                else //same color piece, don't add and stop
                {
                    break;
                }
                pTmp = new Coordinate(pTmp);
            }
            pTmp = new Coordinate(Position); //new coordinate object, resets position as well
        };
        Action <int> checkRank = (direction) =>
        {
            pTmp = new Coordinate(Position);
            for (; pTmp.changeRank(direction);)
            {
                color = board.CheckColor(pTmp);
                if (color == Color)
                {
                    break;
                }
                else if (color == "none")
                {
                    PossibleMoves.Add(pTmp);
                    pTmp = new Coordinate(pTmp);
                }
                else //opposite piece, add its position and stop
                { //bug occurs somewhere here, goes one position further than it should, or perhaps even past
                    PossibleMoves.Add(pTmp);
                    Console.WriteLine("Ocuured attempting position " + pTmp);
                    break;
                }
            }
            pTmp = new Coordinate(Position); //new coordinate object, resets position as well
        };

        checkFile(1);  //check right
        checkFile(-1); //check left
        checkRank(1);  //check up
        checkRank(-1); //check down
    }
Exemple #2
0
    public override void findPossibleMoves() //move forward one, direction dependent on color. can also capture diagonally
    {
        PossibleMoves.Clear();
        int direction = 0;

        if (Color == "white")
        {
            direction = 1;
        }
        else if (Color == "black")
        {
            direction = -1;
        }
        Coordinate mPosition = new Coordinate(Position); //creates a duplicate of pawn's position

        mPosition.changeRank(direction);                 //to check one in front of the pawn

        string front = board.CheckColor(mPosition);

        if (front == "none") //has opposite colored piece or no piece
        {
            PossibleMoves.Add(mPosition);
            if (hasMoved == false) //can move two spaces up if it has not yet moved and there is no piece already in front of it
            {
                mPosition = new Coordinate(mPosition);
                mPosition.changeRank(direction);
                if (board.CheckColor(mPosition) == "none")
                {
                    PossibleMoves.Add(mPosition);
                }
            }
        }
        mPosition = new Coordinate(Position);


        mPosition.changeRank(direction);
        if (mPosition.changeFile(1))        //checks in front and to the right of the pawn
        {
            if (isOppositeColor(mPosition)) //must have an opposite colored piece
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        mPosition.changeRank(direction);
        if (mPosition.changeFile(-1)) //checks in front and to the left of the pawn
        {
            if (isOppositeColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
    }
Exemple #3
0
        public override void Sorcery(ref Cell[,] gameBoard)
        {
            Logic.DetermineUsableCells(CellTypes.Player2, CellTypes.Player1);
            GetPossibleMoves(gameBoard, ref PossibleMoves);
            Cell selected = GetRandomCell();

            if (selected != null)
            {
                gameBoard[selected.Y, selected.X].Type = CellTypes.Selected;
            }
            PossibleMoves.Clear();
        }
    /// <summary>
    /// Shows possible moves that can be done from given Field.
    /// </summary>
    /// <param name="InField">The Field for which we want to show possible moves.</param>
    public void ShowPossibleMoves(FieldController InField)
    {
        Logger.Log(this, "Trying to show possible moves from field {0}.", GetIndexOf(InField));
        if (InField)
        {
            PawnController topPawn = InField.GetPawn();
            if (topPawn && CanPawnBeMoved(topPawn))
            {
                // Clearing possible moves if needed
                if (PossibleMoves != null)
                {
                    PossibleMoves.Clear();
                }

                PossibleMoves = new PossibleMoves(this, InField, Dices);
            }
        }
    }
Exemple #5
0
        public override void ClearMoves(List <IMove> illegalMoves = null)
        {
            if (illegalMoves == null)
            {
                PossibleMoves.Clear();
                CastleMoves.Clear();
                return;
            }

            foreach (var move in illegalMoves)
            {
                if (move.GetMoveMeta().Contains("Castle"))
                {
                    CastleMoves.Remove((Castle)move);
                }

                else
                {
                    PossibleMoves.Remove(move);
                }
            }
        }
Exemple #6
0
    public override void findPossibleMoves() //bishop can move along any diagonal as long as it is not blocked
    {
        PossibleMoves.Clear();
        Coordinate        mPosition = new Coordinate(Position);
        string            color;
        Action <int, int> checkDiagonal = (x, y) =>
        {
            for (; mPosition.changeDiagonal(x, y);)
            {
                color = board.CheckColor(mPosition);
                if (color == "none")
                {
                    //if (!IntoCheck(mPosition))
                    PossibleMoves.Add(mPosition);
                    mPosition = new Coordinate(mPosition);
                }

                else if (color != Color)
                {
                    //if (!IntoCheck(mPosition))
                    PossibleMoves.Add(mPosition);
                    break;
                }
                else
                {
                    break;
                }
            }
            mPosition = new Coordinate(Position);
        };

        checkDiagonal(1, 1);   //up right
        checkDiagonal(1, -1);  //up left
        checkDiagonal(-1, 1);  //down right
        checkDiagonal(-1, -1); //down left
    }
Exemple #7
0
        public override void Update(GameManager gameManager, int deltaTime)
        {
            var realPoint = gameManager.Input.PointerPosition.ToVector2() + gameManager.Camera.Offset;

            Coordinate = GetCoordinate(realPoint);

            if (gameManager.Input.IsLeftButtonPressed)
            {
                if (!Hexes.ContainsKey(Coordinate))
                {
                    return;
                }

                var pawn = Hexes[Coordinate].Pawn;
                if (pawn == null)
                {
                    if (_selectedPawn != null)
                    {
                        _selectedPawn.Selected = false;
                        _selectedPawn          = null;
                        PossibleMoves.Clear();
                    }

                    return;
                }

                pawn.Selected = true;
                _selectedPawn = pawn;

                var moveQueue = new Queue <Move>();

                var openingMoves = GetNeighbours(Coordinate);
                foreach (var openingMove in openingMoves)
                {
                    var hex = Hexes[openingMove];
                    if (!hex.Passable)
                    {
                        continue;
                    }

                    moveQueue.Enqueue(new Move(hex));
                }

                var possibleMoves      = new List <Coordinate>();
                var visitedCoordinates = new HashSet <Coordinate>();

                while (moveQueue.Count > 0)
                {
                    var move = moveQueue.Dequeue();
                    if (visitedCoordinates.Contains(move.Hex.Coordinate))
                    {
                        continue;
                    }

                    visitedCoordinates.Add(move.Hex.Coordinate);

                    if (move.TotalMoveCost > pawn.MoveSpeed)
                    {
                        continue;
                    }

                    possibleMoves.Add(move.Hex.Coordinate);

                    var newMoves = GetNeighbours(move.Hex.Coordinate);
                    foreach (var newMove in newMoves)
                    {
                        if (visitedCoordinates.Contains(newMove))
                        {
                            continue;
                        }

                        var hex = Hexes[newMove];
                        if (!hex.Passable)
                        {
                            continue;
                        }

                        moveQueue.Enqueue(new Move(move, hex));
                    }
                }

                PossibleMoves = possibleMoves;
            }

            if (gameManager.Input.IsRightButtonPressed && _selectedPawn != null)
            {
                if (PossibleMoves.Contains(Coordinate))
                {
                    _selectedPawn.Hex.Pawn = null;
                    _selectedPawn.Hex      = Hexes[Coordinate];
                    _selectedPawn.Hex.Pawn = _selectedPawn;
                    _selectedPawn.Selected = false;
                    PossibleMoves.Clear();
                }
            }
        }
Exemple #8
0
 private void DisablePlacement()
 {
     PossibleMoves.Clear();
     PotentialPlacedWall.Clear();
     allPossibleWalls = null;
 }
Exemple #9
0
    public override void findPossibleMoves() //can move in L shapes, can also move over pieces
    {
        PossibleMoves.Clear();
        Coordinate mPosition = new Coordinate(Position);

        if (mPosition.changeDiagonal(2, 1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(1, 2))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-2, 1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-1, 2))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(2, -1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(1, -2))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-2, -1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-1, -2))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
    }
Exemple #10
0
    public override void findPossibleMoves() //combines the moves of a rook and bishop, along files, ranks, and diagonals
    {
        PossibleMoves.Clear();
        Coordinate        mPosition = new Coordinate(Position);
        string            color;
        Action <int, int> checkDiagonal = (x, y) =>
        {
            for (; mPosition.changeDiagonal(x, y);)
            {
                color = board.CheckColor(mPosition);
                if (color == "none")
                {
                    PossibleMoves.Add(mPosition);
                    mPosition = new Coordinate(mPosition);
                }

                else if (color != Color)
                {
                    // if (!IntoCheck(mPosition))
                    PossibleMoves.Add(mPosition);
                    break;
                }
                else
                {
                    break;
                }
            }
            mPosition = new Coordinate(Position);
        };
        Action <int> checkFile = (direction) =>
        {
            mPosition = new Coordinate(Position);
            for (; mPosition.changeFile(direction);)
            {
                color = board.CheckColor(mPosition);
                if (color == "none") //no piece, continue normally
                {
                    PossibleMoves.Add(mPosition);
                }
                else if (color != Color) //opposite piece, add its position and stop
                {
                    PossibleMoves.Add(mPosition);
                    break;
                }
                else //same color piece, don't add and stop
                {
                    break;
                }
                mPosition = new Coordinate(mPosition);
            }
            mPosition = new Coordinate(Position); //new coordinate object, resets position as well
        };
        Action <int> checkRank = (direction) =>
        {
            mPosition = new Coordinate(Position);
            for (; mPosition.changeRank(direction);)
            {
                color = board.CheckColor(mPosition);
                if (color == Color)
                {
                    break;
                }
                else if (color == "none")
                {
                    PossibleMoves.Add(mPosition);
                    mPosition = new Coordinate(mPosition);
                }
                else //opposite piece, add its position and stop
                { //bug occurs somewhere here, goes one position further than it should, or perhaps even past
                    PossibleMoves.Add(mPosition);
                    break;
                }
            }
            mPosition = new Coordinate(Position); //new coordinate object, resets position as well
        };

        checkFile(1);          //check right
        checkFile(-1);         //check left
        checkRank(1);          //check up
        checkRank(-1);         //check down

        checkDiagonal(1, 1);   //up right
        checkDiagonal(1, -1);  //up left
        checkDiagonal(-1, 1);  //down right
        checkDiagonal(-1, -1); //down left
    }
Exemple #11
0
    public override void findPossibleMoves() //king can move in any direction by one square
    {
        PossibleMoves.Clear();
        //check up
        Coordinate mPosition = new Coordinate(Position);

        if (mPosition.changeRank(1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check down
        mPosition = new Coordinate(Position);
        if (mPosition.changeRank(-1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check right
        mPosition = new Coordinate(Position);
        if (mPosition.changeFile(1))
        {
            if (board.CheckColor(mPosition) == "none")
            {
                PossibleMoves.Add(mPosition);
            }
        }

        //check left
        mPosition = new Coordinate(Position);
        if (mPosition.changeFile(-1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check up right
        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(1, 1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check up left
        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(1, -1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check down right
        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-1, 1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check down left
        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-1, -1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
    }
Exemple #12
0
        public void UpdatePossibleMoves()
        {
            PossibleMoves.Clear();

            switch (PieceType)
            {
            case PieceType.Pawn:
                CheckAndSetPossibleMoves(MoveDirection.Up, 2, 0, true);
                break;

            case PieceType.Knight:
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Right, 1, 2, false);
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Left, 1, 2, false);
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Left, 2, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Right, 2, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Right, 1, 2, false);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Left, 1, 2, false);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Left, 2, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Right, 2, 1, false);
                break;

            case PieceType.Bishop:
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Left, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Right, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Right, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Left, 8, 8, true);
                break;

            case PieceType.Rook:
                CheckAndSetPossibleMoves(MoveDirection.Down, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Up, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Left, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Right, 8, 8, true);
                break;

            case PieceType.Queen:
                CheckAndSetPossibleMoves(MoveDirection.Down, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Up, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Left, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Right, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Left, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Right, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Right, 8, 8, true);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Left, 8, 8, true);
                break;

            case PieceType.King:
                CheckAndSetPossibleMoves(MoveDirection.Down, 1, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Up, 1, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Left, 1, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Right, 1, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Left, 1, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Up | MoveDirection.Right, 1, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Right, 1, 1, false);
                CheckAndSetPossibleMoves(MoveDirection.Down | MoveDirection.Left, 1, 1, false);
                break;

            default:
                break;
            }
        }