Exemplo n.º 1
0
 public Ship(int height, int width, ShipType type)
 {
     this._height             = height;
     this._width              = width;
     this._type               = type;
     this._initialCoordinates = new CoOrdinates('0', '0');
     this._acquiredCoordinate = new List <CoOrdinates>();
 }
Exemplo n.º 2
0
 public virtual IShip GetShip(CoOrdinates coordinates)
 {
     // Get ship for the passsed coordinates
     //
     return(this._ships.Find(item =>
     {
         return item.AcquiredCoordinates.Exists(item1 => item1.X == coordinates.X && item1.Y == coordinates.Y);
     }));
 }
Exemplo n.º 3
0
        public virtual void AddShip(IBattleArea battleArea, int height, int width, CoOrdinates initialCoordinates, ShipType type)
        {
            IShip ship = BattleShipGameFactory.Instance.GetShip();

            ship.Height = height;
            ship.Width  = width;

            battleArea.AddShip(ship, initialCoordinates);
        }
Exemplo n.º 4
0
        public virtual bool LaunchMissile(IPlayer targetPlayer)
        {
            CoOrdinates coordinates = this._missile.Dequeue();

            this._lastTarget = coordinates;

            bool hit = targetPlayer.BattleArea.Damage(coordinates);

            return(hit);
        }
Exemplo n.º 5
0
        public virtual List <CoOrdinates> AcquireCoordinates(CoOrdinates initialCoordinate)
        {
            List <CoOrdinates> acquired = new List <CoOrdinates>();

            // Occupy the coordinates for the ship.
            //
            acquired = this.AcquireCoordinatesInternal(this._height, this._width, this._type, initialCoordinate);
            this._acquiredCoordinate = acquired;
            this._initialCoordinates = initialCoordinate;

            return(acquired);
        }
Exemplo n.º 6
0
        public virtual bool Damage(CoOrdinates coordinates)
        {
            IShip ship = this.GetShip(coordinates);

            if (ship == null)
            {
                return(false);
            }

            bool hit = ship.Damage(coordinates);

            // Update the coordinates
            //
            return(hit);
        }
Exemplo n.º 7
0
        public virtual bool UpdateCoordinates(CoOrdinates coordinate)
        {
            // Update the battle area co-ordinates
            //
            var index = this._acquiredCoordinates.FindIndex(item => item.X == coordinate.X && item.Y == coordinate.Y);

            if (index < 0)
            {
                return(false);
            }
            else
            {
                this._acquiredCoordinates[index] = coordinate;
                return(true);
            }
        }
Exemplo n.º 8
0
        public virtual IShip AddShip(IShip ship, CoOrdinates initialCoordinate)
        {
            // Add ship
            //
            ship.InitialCoordinate = initialCoordinate;
            List <CoOrdinates> acquireCoordinates = ship.AcquireCoordinates(initialCoordinate);

            this._acquiredCoordinates.AddRange(acquireCoordinates);

            this._ships.Add(ship);

            // Set coordinate mediator for the ship
            //
            this._coordinateMediator.Ship = ship;
            ship.RegisterCoordinateMediator(this._coordinateMediator);

            return(ship);
        }
Exemplo n.º 9
0
        private List <CoOrdinates> AcquireCoordinatesInternal(int height, int width, ShipType type, CoOrdinates initialCoordinate)
        {
            // Acquire the coordinates for the ship.
            //
            List <CoOrdinates> acquired = new List <CoOrdinates>();

            for (int i = 0; i < height; i++)
            {
                char y = (char)(initialCoordinate.Y + i);
                for (int j = 0; j < width; j++)
                {
                    char        x      = (char)(initialCoordinate.X + j);
                    CoOrdinates coords = new CoOrdinates(x, y);
                    coords.Value = (int)type * 1;

                    acquired.Add(coords);
                }
            }

            return(acquired);
        }
Exemplo n.º 10
0
        public virtual bool Damage(CoOrdinates coordinate)
        {
            // Damage the passed co-ordinates of the ship
            //
            var index  = this._acquiredCoordinate.FindIndex(item => item.X == coordinate.X && item.Y == coordinate.Y);
            var result = this._acquiredCoordinate[index];

            if (result.Value > 0)
            {
                result.Value--;

                // Update the coordinates
                //
                this.UpdateCoordinates(result);

                // Set damage status of the ship
                //
                this.SetDamageStatus();

                return(true);
            }

            return(false);
        }
Exemplo n.º 11
0
        public virtual bool UpdateCoordinates(CoOrdinates coordinate)
        {
            // Update the coordinates
            //
            var index = this._acquiredCoordinate.FindIndex(item => item.X == coordinate.X && item.Y == coordinate.Y);

            if (index < 0)
            {
                return(false);
            }

            this._acquiredCoordinate[index] = coordinate;

            // Update battle area coordinates as well usin he coordinate mediator.
            //
            if (this._coordinateMediator != null)
            {
                return(this._coordinateMediator.UpdateCoordinate((ICoordinate)this._coordinateMediator.BattleArea, coordinate));
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 12
0
 public virtual bool AddMissile(CoOrdinates coordinates)
 {
     this._missile.Enqueue(coordinates);
     return(true);
 }
Exemplo n.º 13
0
 private bool ValidateShipData(IBattleArea battleArea, int height, int width, CoOrdinates coordinate)
 {
     return((height >= 1 && height <= (int)(battleArea.Height - 'A')) && (width >= 1 && width <= (int)(battleArea.Width - '0')) &&
            (coordinate.X >= '1' && coordinate.X <= battleArea.Width) && (coordinate.Y >= 'A' && coordinate.Y <= battleArea.Height));
 }
Exemplo n.º 14
0
        private bool FetchShipData(IGame game, string[] battleShipDataArray, out int height, out int width, out ShipType shipType, out CoOrdinates ship1Coords, out CoOrdinates ship2Coords)
        {
            string type = battleShipDataArray[0];

            width  = int.Parse(battleShipDataArray[1]);
            height = int.Parse(battleShipDataArray[2]);
            string pos1 = battleShipDataArray[3];
            char   y1   = pos1[0];
            char   x1   = pos1[1];

            string pos2 = battleShipDataArray[4];
            char   y2   = pos2[0];
            char   x2   = pos2[1];

            shipType    = ShipType.TypeP;
            ship1Coords = null;
            ship2Coords = null;

            bool validShipData = this.ValidateShipType(type);

            if (!validShipData)
            {
                System.Console.WriteLine("Ship type is not valid.");
                return(false);
            }

            validShipData = this.ValidateShipPosLength(pos1);
            if (!validShipData)
            {
                System.Console.WriteLine("Ship1 position is not valid.");
                return(false);
            }

            validShipData = this.ValidateShipData(game.BattleArea1, height, width, new CoOrdinates(x1, y1));

            if (!validShipData)
            {
                System.Console.WriteLine("Ship1 data is not valid.");
                return(false);
            }

            validShipData = this.ValidateShipData(game.BattleArea2, height, width, new CoOrdinates(x2, y2));
            if (!validShipData)
            {
                System.Console.WriteLine("Ship2 data is not valid.");
                return(false);
            }

            validShipData = this.ValidateShipPosLength(pos2);
            if (!validShipData)
            {
                System.Console.WriteLine("Ship2 position is not valid.");
                return(false);
            }

            ship1Coords = new CoOrdinates(x1, y1);
            ship2Coords = new CoOrdinates(x2, y2);
            shipType    = type == "P" ? ShipType.TypeP : ShipType.TypeQ;

            return(true);
        }
Exemplo n.º 15
0
 public override bool UpdateCoordinate(ICoordinate entity, CoOrdinates coordinate)
 {
     return(entity.UpdateCoordinates(coordinate));
 }
Exemplo n.º 16
0
 public abstract bool UpdateCoordinate(ICoordinate entity, CoOrdinates coordinate);