Exemple #1
0
        private List <ChessBoardPosition> GetValidMovePositions(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition)
        {
            var validPositions = new List <ChessBoardPosition>();

            ChessBoardPosition upLeftPosition  = chessBoard.CalculatePosition(currentPosition, 2, 1);
            ChessBoardPosition upRightPosition = chessBoard.CalculatePosition(currentPosition, 2, -1);

            ChessBoardPosition downLeftPosition  = chessBoard.CalculatePosition(currentPosition, -2, 1);
            ChessBoardPosition downRightPosition = chessBoard.CalculatePosition(currentPosition, -2, -1);

            ChessBoardPosition leftUpPosition   = chessBoard.CalculatePosition(currentPosition, 1, 2);
            ChessBoardPosition leftDownPosition = chessBoard.CalculatePosition(currentPosition, -1, 2);

            ChessBoardPosition rightUpPosition   = chessBoard.CalculatePosition(currentPosition, 1, -2);
            ChessBoardPosition rightDownPosition = chessBoard.CalculatePosition(currentPosition, -1, -2);

            validPositions.Add(upLeftPosition);
            validPositions.Add(upRightPosition);
            validPositions.Add(downLeftPosition);
            validPositions.Add(downRightPosition);
            validPositions.Add(leftUpPosition);
            validPositions.Add(leftDownPosition);
            validPositions.Add(rightUpPosition);
            validPositions.Add(rightDownPosition);

            return(validPositions);
        }
Exemple #2
0
        private List <ChessBoardPosition> GetValidMovePositions(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition)
        {
            var validPositions = new List <ChessBoardPosition>();

            for (int radius = 1; radius < MovementPoint + 1; radius++)
            {
                ChessBoardPosition upLeftPosition    = chessBoard.CalculatePosition(currentPosition, radius, radius);
                ChessBoardPosition upRight           = chessBoard.CalculatePosition(currentPosition, radius, -radius);
                ChessBoardPosition downLeftPosition  = chessBoard.CalculatePosition(currentPosition, -radius, radius);
                ChessBoardPosition downRightPosition = chessBoard.CalculatePosition(currentPosition, -radius, -radius);

                validPositions.Add(upLeftPosition);
                validPositions.Add(upRight);
                validPositions.Add(downLeftPosition);
                validPositions.Add(downRightPosition);

                ChessBoardPosition upPosition    = chessBoard.CalculatePosition(currentPosition, radius, 0);
                ChessBoardPosition downPosition  = chessBoard.CalculatePosition(currentPosition, -radius, 0);
                ChessBoardPosition leftPosition  = chessBoard.CalculatePosition(currentPosition, 0, radius);
                ChessBoardPosition rightPosition = chessBoard.CalculatePosition(currentPosition, 0, -radius);

                validPositions.Add(upPosition);
                validPositions.Add(downPosition);
                validPositions.Add(leftPosition);
                validPositions.Add(rightPosition);
            }

            return(validPositions);
        }
Exemple #3
0
        private List <ChessBoardPosition> GetValidMovePositions(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition)
        {
            var validPositions = new List <ChessBoardPosition>();

            ChessBoardPosition upPosition    = chessBoard.CalculatePosition(currentPosition, MovementPoint, 0);
            ChessBoardPosition downPosition  = chessBoard.CalculatePosition(currentPosition, -MovementPoint, 0);
            ChessBoardPosition leftPosition  = chessBoard.CalculatePosition(currentPosition, 0, MovementPoint);
            ChessBoardPosition rightPosition = chessBoard.CalculatePosition(currentPosition, 0, -MovementPoint);

            validPositions.Add(upPosition);
            validPositions.Add(downPosition);
            validPositions.Add(leftPosition);
            validPositions.Add(rightPosition);

            return(validPositions);
        }
Exemple #4
0
        private List <ChessBoardPosition> GetValidAttackPositions(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition)
        {
            var validPositions = new List <ChessBoardPosition>();

            ChessBoardPosition upPosition    = chessBoard.CalculatePosition(currentPosition, 1, 0, true);
            ChessBoardPosition downPosition  = chessBoard.CalculatePosition(currentPosition, -1, 0, true);
            ChessBoardPosition leftPosition  = chessBoard.CalculatePosition(currentPosition, 0, 1, true);
            ChessBoardPosition rightPosition = chessBoard.CalculatePosition(currentPosition, 0, -1, true);

            validPositions.Add(upPosition);
            validPositions.Add(downPosition);
            validPositions.Add(leftPosition);
            validPositions.Add(rightPosition);

            return(validPositions);
        }
Exemple #5
0
        private List <ChessBoardPosition> GetValidAttackPositions(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition)
        {
            var validPositions = new List <ChessBoardPosition>();

            for (int diagonalCorner = 1; diagonalCorner < chessBoard.Rows; diagonalCorner++)
            {
                ChessBoardPosition upLeftPosition    = chessBoard.CalculatePosition(currentPosition, diagonalCorner, diagonalCorner, true);
                ChessBoardPosition upRight           = chessBoard.CalculatePosition(currentPosition, diagonalCorner, -diagonalCorner, true);
                ChessBoardPosition downLeftPosition  = chessBoard.CalculatePosition(currentPosition, -diagonalCorner, diagonalCorner, true);
                ChessBoardPosition downRightPosition = chessBoard.CalculatePosition(currentPosition, -diagonalCorner, -diagonalCorner, true);

                validPositions.Add(upLeftPosition);
                validPositions.Add(upRight);
                validPositions.Add(downLeftPosition);
                validPositions.Add(downRightPosition);
            }

            return(validPositions);
        }
Exemple #6
0
        private List <ChessBoardPosition> GetValidAttackPositions(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition)
        {
            var validPositions = new List <ChessBoardPosition>();

            for (int orthogonal = 1; orthogonal < chessBoard.Rows; orthogonal++)
            {
                ChessBoardPosition upPosition    = chessBoard.CalculatePosition(currentPosition, orthogonal, 0, true);
                ChessBoardPosition downPosition  = chessBoard.CalculatePosition(currentPosition, -orthogonal, 0, true);
                ChessBoardPosition leftPosition  = chessBoard.CalculatePosition(currentPosition, 0, orthogonal, true);
                ChessBoardPosition rightPosition = chessBoard.CalculatePosition(currentPosition, 0, -orthogonal, true);

                validPositions.Add(upPosition);
                validPositions.Add(downPosition);
                validPositions.Add(leftPosition);
                validPositions.Add(rightPosition);
            }

            return(validPositions);
        }
        public override void Attack(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition, ChessBoardPosition targetPosition)
        {
            var validAttackPosition = new List <ChessBoardPosition>();

            ChessBoardPosition upLeftPosition    = chessBoard.CalculatePosition(currentPosition, 1, 1, true);
            ChessBoardPosition upRight           = chessBoard.CalculatePosition(currentPosition, 1, -1, true);
            ChessBoardPosition downLeftPosition  = chessBoard.CalculatePosition(currentPosition, -1, 1, true);
            ChessBoardPosition downRightPosition = chessBoard.CalculatePosition(currentPosition, -1, -1, true);
            ChessBoardPosition upPosition        = chessBoard.CalculatePosition(currentPosition, 1, 0, true);
            ChessBoardPosition downPosition      = chessBoard.CalculatePosition(currentPosition, -1, 0, true);
            ChessBoardPosition leftPosition      = chessBoard.CalculatePosition(currentPosition, 0, 1, true);
            ChessBoardPosition rightPosition     = chessBoard.CalculatePosition(currentPosition, 0, -1, true);

            validAttackPosition.Add(upLeftPosition);
            validAttackPosition.Add(upRight);
            validAttackPosition.Add(downLeftPosition);
            validAttackPosition.Add(downRightPosition);
            validAttackPosition.Add(upPosition);
            validAttackPosition.Add(downPosition);
            validAttackPosition.Add(leftPosition);
            validAttackPosition.Add(rightPosition);

            validAttackPosition.RemoveAll(p => p == null);

            foreach (ChessBoardPosition position in validAttackPosition)
            {
                ChessBoardCell target = chessBoard.GetCell(position);
                if (target.IsOccupied &&
                    target.Entity != null &&
                    target.Entity.ControlType == EntityControlType.Human)
                {
                    target.Entity.HitPoints = target.Entity.HitPoints - AttackPower;

                    Console.WriteLine($"Hit on enemy {target.Entity.Name} for {AttackPower} damage!");
                    if (target.Entity.HitPoints < 1)
                    {
                        Console.WriteLine($"Enemy {target.Entity.Name} is destroyed!");
                        chessBoard.EmptyCell(position);
                    }

                    Console.ReadLine();
                }
            }
        }
Exemple #8
0
        public override void Move(ChessBoard.ChessBoard chessBoard, ChessBoardPosition oldPosition, ChessBoardPosition newPosition)
        {
            List <PlayerPiece>   playerPieces = chessBoard.GetPlayerPiecesBasedOnControlType(EntityControlType.Human);
            CommandUnitMoveState moveState    = CommandUnitMoveState.StayStill;

            var possibleAttacks = new List <KeyValuePair <int, List <Point> > >();

            foreach (PlayerPiece playerPiece in playerPieces)
            {
                bool[,] chessBoardGrid = chessBoard.GetPathfinderGrid();
                chessBoardGrid[oldPosition.CurrentRow, oldPosition.CurrentColumn] = true;
                chessBoardGrid[playerPiece.CurrentPosition.CurrentRow, playerPiece.CurrentPosition.CurrentColumn] = true;

                Point startLocation = new Point(oldPosition.CurrentRow, oldPosition.CurrentColumn);
                Point endLocation   = new Point(playerPiece.CurrentPosition.CurrentRow, playerPiece.CurrentPosition.CurrentColumn);

                SearchParameters sp         = new SearchParameters(startLocation, endLocation, chessBoardGrid);
                PathFinder       pathfinder = new PathFinder(sp);

                List <Point> nodes = pathfinder.FindPath();
                possibleAttacks.Add(new KeyValuePair <int, List <Point> >(nodes.Count, nodes));
            }

            var paths = new List <int>();

            foreach (KeyValuePair <int, List <Point> > pair in possibleAttacks)
            {
                paths.Add(pair.Key);
            }

            paths.Sort();
            KeyValuePair <int, List <Point> > nearestEnemy = new KeyValuePair <int, List <Point> >(0, null);

            if (paths.Count > 0)
            {
                nearestEnemy = possibleAttacks.Find(m => m.Key == paths[0]);
            }

            if (paths.Count > 0 && nearestEnemy.Key != 0)
            {
                if (nearestEnemy.Key == 1 || nearestEnemy.Key == 2)
                {
                    if (nearestEnemy.Value[nearestEnemy.Value.Count - 1].X >= oldPosition.CurrentRow)
                    {
                        moveState = CommandUnitMoveState.MoveLeft;
                    }
                    else if ((nearestEnemy.Value[nearestEnemy.Value.Count - 1].X <= oldPosition.CurrentRow))
                    {
                        moveState = CommandUnitMoveState.MoveRight;
                    }
                }
                else
                {
                    bool safeUpLeft = false;
                    for (int radius = 0; radius < chessBoard.Columns; radius++)
                    {
                        ChessBoardPosition upLeftPosition = chessBoard.CalculatePosition(oldPosition, radius, radius, true);
                        if (upLeftPosition != null)
                        {
                            ChessBoardCell cell = chessBoard.GetCell(upLeftPosition);
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Human)
                            {
                                safeUpLeft = false;
                            }
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Ai)
                            {
                                safeUpLeft = true;
                            }
                        }
                    }

                    bool safeUpRight = false;
                    for (int radius = 0; radius < chessBoard.Columns; radius++)
                    {
                        ChessBoardPosition upRightPosition = chessBoard.CalculatePosition(oldPosition, radius, -radius, true);
                        if (upRightPosition != null)
                        {
                            ChessBoardCell cell = chessBoard.GetCell(upRightPosition);
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Human)
                            {
                                safeUpRight = false;
                            }
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Ai)
                            {
                                safeUpRight = true;
                            }
                        }
                    }

                    bool safeDownLeft = false;
                    for (int radius = 0; radius < chessBoard.Columns; radius++)
                    {
                        ChessBoardPosition downLeftPosition = chessBoard.CalculatePosition(oldPosition, -radius, radius, true);
                        if (downLeftPosition != null)
                        {
                            ChessBoardCell cell = chessBoard.GetCell(downLeftPosition);
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Human)
                            {
                                safeDownLeft = false;
                            }
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Ai)
                            {
                                safeDownLeft = true;
                            }
                        }
                    }

                    bool safeDownRight = false;
                    for (int radius = 0; radius < chessBoard.Columns; radius++)
                    {
                        ChessBoardPosition downRightPosition = chessBoard.CalculatePosition(oldPosition, -radius, -radius, true);
                        if (downRightPosition != null)
                        {
                            ChessBoardCell cell = chessBoard.GetCell(downRightPosition);
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Human)
                            {
                                safeDownRight = false;
                            }
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Ai)
                            {
                                safeDownRight = true;
                            }
                        }
                    }

                    bool safeUp = false;
                    for (int radius = 0; radius < chessBoard.Columns; radius++)
                    {
                        ChessBoardPosition upPosition = chessBoard.CalculatePosition(oldPosition, radius, 0, true);
                        if (upPosition != null)
                        {
                            ChessBoardCell cell = chessBoard.GetCell(upPosition);
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Human)
                            {
                                safeUp = false;
                            }
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Ai)
                            {
                                safeUp = true;
                            }
                        }
                    }

                    bool safeDown = false;
                    for (int radius = 0; radius < chessBoard.Columns; radius++)
                    {
                        ChessBoardPosition downPosition = chessBoard.CalculatePosition(oldPosition, -radius, 0, true);
                        if (downPosition != null)
                        {
                            ChessBoardCell cell = chessBoard.GetCell(downPosition);
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Human)
                            {
                                safeDown = false;
                            }
                            if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Ai)
                            {
                                safeDown = true;
                            }
                        }
                    }

                    if (safeUpLeft && safeUpRight && safeDownLeft && safeDownRight)
                    {
                        moveState = CommandUnitMoveState.StayStill;
                    }

                    if (!safeUpLeft || !safeDownLeft || !safeUp)
                    {
                        moveState = CommandUnitMoveState.MoveRight;
                    }

                    if (!safeUpRight || !safeDownRight || !safeDown)
                    {
                        moveState = CommandUnitMoveState.MoveLeft;
                    }
                }
            }

            if (moveState == CommandUnitMoveState.MoveLeft)
            {
                newPosition = new ChessBoardPosition
                {
                    CurrentColumn = oldPosition.CurrentColumn - 1,
                    CurrentRow    = oldPosition.CurrentRow,
                };
            }
            else if (moveState == CommandUnitMoveState.MoveRight)
            {
                newPosition = new ChessBoardPosition
                {
                    CurrentColumn = oldPosition.CurrentColumn + 1,
                    CurrentRow    = oldPosition.CurrentRow,
                };
            }
            else if (moveState == CommandUnitMoveState.StayStill)
            {
                return;
            }

            if (newPosition.CurrentColumn >= 0 && newPosition.CurrentRow >= 0)
            {
                ChessBoardCell newCell = chessBoard.GetCell(newPosition);
                if (newCell.IsOccupied)
                {
                    return;
                }
                else
                {
                    ChessBoardCell cell = chessBoard.GetCell(oldPosition);
                    chessBoard.SetCell(newPosition, cell);

                    chessBoard.EmptyCell(oldPosition);
                }
            }
        }