Exemplo n.º 1
0
        public Point ShootToNearestEnd(Point previous, Model world)
        {
            var prevCell = world.playerField[previous.X, previous.Y];

            for (var i = -1; i < 2; i++)
            {
                if (Converter.GetDirection(prevCell) == ShipDirection.Right)
                {
                    var newCell = world.playerField[previous.X, previous.Y + i];
                    if (Ship.IsShip(newCell) && !Ship.IsMiddlePart(newCell))
                    {
                        ProcessPoint(previous.X, previous.Y + i, world);
                        return(new Point(previous.X, previous.Y + i));
                    }
                }
                else
                {
                    var newCell = world.playerField[previous.X + i, previous.Y];
                    if (Ship.IsShip(newCell) && !Ship.IsMiddlePart(newCell))
                    {
                        ProcessPoint(previous.X + i, previous.Y, world);
                        return(new Point(previous.X + i, previous.Y));
                    }
                }
            }
            throw new ArgumentException();
        }
Exemplo n.º 2
0
 void ProcessPoint(int x, int y, Model world)
 {
     if (world.playerField[x, y] == CellStates.Water)
     {
         avalibleTargets.Remove(new Point(x, y));
         world.playerField[x, y] = CellStates.Empty;
         canShootAgain           = false;
     }
     else
     {
         for (var dx = -1; dx <= 1; dx++)
         {
             for (var dy = -1; dy <= 1; dy++)
             {
                 if (x + dx < 0 || y + dy < 0 || x + dx > 9 || y + dy > 9)
                 {
                     continue;
                 }
                 //if (Ship.IsShip(world.playerField[x + dx, y + dy])) continue;
                 if (world.playerField[x, y] == CellStates.ShipBackRight && dx == 0 && dy == 1)
                 {
                     continue;
                 }
                 else if (world.playerField[x, y] == CellStates.ShipMiddleRight && dx == 0 && dy != 0)
                 {
                     continue;
                 }
                 else if (world.playerField[x, y] == CellStates.ShipHeadRight && dx == 0 && dy == -1)
                 {
                     continue;
                 }
                 else if (world.playerField[x, y] == CellStates.ShipBackUp && dx == -1 && dy == 0)
                 {
                     continue;
                 }
                 else if (world.playerField[x, y] == CellStates.ShipMiddleUp && dx != 0 && dy == 0)
                 {
                     continue;
                 }
                 else if (world.playerField[x, y] == CellStates.ShipHeadUp && dx == 1 && dy == 0)
                 {
                     continue;
                 }
                 avalibleTargets.Remove(new Point(x + dx, y + dy));
             }
         }
         world.playerField[x, y] = Converter.GetDeadState(world.playerField[x, y]);
         knownDirection          = Converter.GetDirection(world.playerField[x, y]);
         if (Ship.IsDead(world.playerField, new Point(x, y)))
         {
             world.playerShipCount--;
         }
         canShootAgain = true;
     }
 }
Exemplo n.º 3
0
        public static bool IsDead(CellStates[,] field, Point point)
        {
            var cell = field[point.X, point.Y];

            if (cell == CellStates.DeadSmallShip)
            {
                return(true);
            }
            if (cell == CellStates.SmallShip)
            {
                return(false);
            }
            if (cell == CellStates.Water || cell == CellStates.Empty)
            {
                throw new ArgumentException();
            }
            var direction = Converter.GetDirection(cell);

            if (direction == ShipDirection.Right)
            {
                while (field[point.X, point.Y] != CellStates.ShipBackRight && field[point.X, point.Y] != CellStates.ShipBackRightDead)
                {
                    point = new Point(point.X, point.Y - 1);
                }
                while (field[point.X, point.Y] != CellStates.ShipHeadRight && field[point.X, point.Y] != CellStates.ShipHeadRightDead)
                {
                    cell = field[point.X, point.Y];
                    if (cell != CellStates.ShipBackRightDead && cell != CellStates.ShipHeadRightDead && cell != CellStates.ShipMiddleRightDead)
                    {
                        return(false);
                    }
                    point = new Point(point.X, point.Y + 1);
                }
                if (field[point.X, point.Y] != CellStates.ShipHeadRightDead)
                {
                    return(false);
                }
                return(true);
            }
            else
            {
                while (field[point.X, point.Y] != CellStates.ShipBackUp && field[point.X, point.Y] != CellStates.ShipBackUpDead)
                {
                    point = new Point(point.X + 1, point.Y);
                }
                while (field[point.X, point.Y] != CellStates.ShipHeadUp && field[point.X, point.Y] != CellStates.ShipHeadUpDead)
                {
                    cell = field[point.X, point.Y];
                    if (cell != CellStates.ShipBackUpDead && cell != CellStates.ShipHeadUpDead && cell != CellStates.ShipMiddleUpDead)
                    {
                        return(false);
                    }
                    point = new Point(point.X - 1, point.Y);
                }
                if (field[point.X, point.Y] != CellStates.ShipHeadUpDead)
                {
                    return(false);
                }
                return(true);
            }
        }