Ejemplo n.º 1
0
        public override void Attack(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition, ChessBoardPosition targetPosition)
        {
            var validPositions = GetValidAttackPositions(chessBoard, currentPosition);

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

            if (targetPosition == null)
            {
                throw new Exception("The specified cell is outside the chess board field.");
            }

            if (validPositions.Any(p => p.CurrentRow == targetPosition.CurrentRow &&
                                   p.CurrentColumn == targetPosition.CurrentColumn))
            {
                ChessBoardCell cell = chessBoard.GetCell(targetPosition);
                if (cell.IsOccupied && cell.Entity != null)
                {
                    cell.Entity.HitPoints = cell.Entity.HitPoints - AttackPower;

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

                    Console.ReadLine();
                }
            }
            else
            {
                throw new Exception("Invalid attack position. Specified cell is not in the attack range.");
            }
        }
Ejemplo n.º 2
0
        public override void Move(ChessBoard.ChessBoard chessBoard, ChessBoardPosition oldPosition, ChessBoardPosition newPosition)
        {
            List <PlayerPiece> playerPieces = chessBoard.GetPlayerPiecesBasedOnControlType(EntityControlType.Human);

            var validMoves = 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();
                validMoves.Add(new KeyValuePair <int, List <Point> >(nodes.Count, nodes));
            }

            var paths = new List <int>();

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

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

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

            if (paths.Count > 0 && nextPosition.Key != 0)
            {
                newPosition = new ChessBoardPosition
                {
                    CurrentColumn = nextPosition.Value[0].Y,
                    CurrentRow    = nextPosition.Value[0].X,
                };

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

                    chessBoard.EmptyCell(oldPosition);
                }
            }
        }
Ejemplo n.º 3
0
        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();
                }
            }
        }
Ejemplo n.º 4
0
        public override void Attack(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition, ChessBoardPosition targetPosition)
        {
            var validPositions = GetValidAttackPositions(chessBoard, currentPosition);

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

            var validTargets = new List <ChessBoardPosition>();

            foreach (ChessBoardPosition validPosition in validPositions)
            {
                ChessBoardCell cell = chessBoard.GetCell(validPosition);

                if (cell.IsOccupied && cell.Entity.ControlType == EntityControlType.Human)
                {
                    validTargets.Add(validPosition);
                }
            }

            if (validTargets.Count > 0)
            {
                validTargets.Shuffle();

                ChessBoardCell target = chessBoard.GetCell(validTargets[0]);
                if (target.IsOccupied && target.Entity != null)
                {
                    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(validTargets[0]);
                    }

                    Console.ReadLine();
                }
            }
        }
Ejemplo n.º 5
0
        public override void Move(ChessBoard.ChessBoard chessBoard, ChessBoardPosition oldPosition, ChessBoardPosition newPosition)
        {
            var validPositions = GetValidMovePositions(chessBoard, oldPosition);

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

            if (newPosition == null)
            {
                throw new Exception("The specified cell is outside the chess board field.");
            }

            if (validPositions.Any(p => p.CurrentRow == newPosition.CurrentRow &&
                                   p.CurrentColumn == newPosition.CurrentColumn))
            {
                ChessBoardCell cell = chessBoard.GetCell(oldPosition);
                chessBoard.SetCell(newPosition, cell);

                chessBoard.EmptyCell(oldPosition);
            }
            else
            {
                throw new Exception("Invalid move. Specified cell is not a valid move.");
            }
        }
Ejemplo n.º 6
0
        public override void Attack(ChessBoard.ChessBoard chessBoard, ChessBoardPosition currentPosition, ChessBoardPosition targetPosition)
        {
            var validPositions = GetValidAttackPositions(chessBoard, currentPosition);

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

            foreach (ChessBoardPosition position in validPositions)
            {
                ChessBoardCell cell = chessBoard.GetCell(position);
                if (cell.IsOccupied && cell.Entity != null)
                {
                    cell.Entity.HitPoints = cell.Entity.HitPoints - AttackPower;

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

                    Console.ReadLine();
                }
            }
        }
Ejemplo n.º 7
0
        public override void Move(ChessBoard.ChessBoard chessBoard, ChessBoardPosition oldPosition, ChessBoardPosition newPosition)
        {
            if ((oldPosition.CurrentRow + 1) < 8)
            {
                newPosition = new ChessBoardPosition
                {
                    CurrentColumn = oldPosition.CurrentColumn,
                    CurrentRow    = oldPosition.CurrentRow + 1,
                };

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

                    chessBoard.EmptyCell(oldPosition);
                }
            }
        }
Ejemplo n.º 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);
                }
            }
        }