Example #1
0
 public bool isBoat(RowCol pos) =>
 pos.Row > -1 && pos.Row <RowCol.MaxRow &&
                          pos.Col> -1 && pos.Col < RowCol.MaxCol && (
     ((ShipType)Board.GetValue(pos.Row, pos.Col)) == ShipType.Battleship ||
     ((ShipType)Board.GetValue(pos.Row, pos.Col)) == ShipType.Cruiser ||
     ((ShipType)Board.GetValue(pos.Row, pos.Col)) == ShipType.Carrier ||
     ((ShipType)Board.GetValue(pos.Row, pos.Col)) == ShipType.Destroyer ||
     ((ShipType)Board.GetValue(pos.Row, pos.Col)) == ShipType.Submarine);
Example #2
0
        public bool Place(IShip ship)
        {
            int size     = ship.Size;
            var startPos = new RowCol(seed.NextRow(), seed.NextColumn());
            var endPos   = new RowCol((size + startPos.Row) - 1, startPos.Col);

            bool canBePlacedHere()
            {
                if (boatFitsInSpace(startPos, endPos))
                {
                    ship.Position = new Position {
                        Start    = startPos,
                        End      = endPos,
                        Occupies = AllocateGridPositions(startPos, endPos)
                    };
                    foreach (var space in ship.Position.Occupies)
                    {
                        Board.SetValue(ship.ShipType, space.Row, space.Col);
                    }
                    return(true);
                }
                return(false);
            }

            if (canBePlacedHere())
            {
                return(true);
            }

            endPos = new RowCol(startPos.Row, (startPos.Col + size) - 1);
            if (canBePlacedHere())
            {
                return(true);
            }

            endPos = new RowCol(startPos.Row, (startPos.Col - size) + 1);
            if (canBePlacedHere())
            {
                return(true);
            }

            endPos = new RowCol((startPos.Row - size) + 1, startPos.Col);
            if (canBePlacedHere())
            {
                return(true);
            }

            return(false);
        }
Example #3
0
        public ShipType TakeTurn(BattleshipBoard enemy)
        {
            var random = new Random();
            int row    = random.Next(10);
            int col    = random.Next(10);
            var target = new RowCol(row, col);

            if (lastGo.Hit == ShipType.Hit)
            {
                var temp = lastGo.LastRowCol.FindAdjacentEmptyCell(enemy);
                if (temp.IsValid())
                {
                    target = temp;
                }
            }
            lastGo.Hit = ShipType.Miss;

            while (!enemy.findPossibleTarget(target))
            {
                row    = random.Next(10);
                col    = random.Next(10);
                target = new RowCol(row, col);
            }


            lastGo.Hit        = enemy.FireMissile(target);
            lastGo.LastRowCol = target;

            switch (lastGo.Hit)
            {
            case ShipType.Battleship:
            case ShipType.Carrier:
            case ShipType.Cruiser:
            case ShipType.Destroyer:
            case ShipType.Submarine:
                Console.WriteLine($"You hit my {lastGo.Hit.ToString()}");
                lastGo.Hit = ShipType.Hit;
                break;

            case ShipType.Water:
                Console.WriteLine($"Miss!!!");
                lastGo.Hit = ShipType.Miss;
                break;
            }


            return(lastGo.Hit);
        }
Example #4
0
        public bool boatFitsInSpace(RowCol startPos, RowCol endPos)
        {
            var  positions = AllocateGridPositions(startPos, endPos);
            bool valid     = true;

            foreach (var position in positions)
            {
                if (!isWater(position))
                {
                    valid = false;
                    break;
                }
            }

            return(valid);
        }
Example #5
0
        public ShipType FireMissile(RowCol pos)
        {
            Console.WriteLine($"Fire: {pos.Row}{pos.Col}");

            var square = (ShipType)Board.GetValue(pos.Row, pos.Col);

            if (isBoat(pos))
            {
                Board.SetValue(ShipType.Hit, pos.Row, pos.Col);
                Fleet.First(x => x.ShipType == square).Hits++;
            }
            else
            {
                Board.SetValue(ShipType.Miss, pos.Row, pos.Col);
            }

            return(square);
        }
Example #6
0
        public List <RowCol> AllocateGridPositions(RowCol start, RowCol end)
        {
            var positions = new List <RowCol>();
            var cols      = Enumerable.Range(Math.Min(start.Col, end.Col), Math.Abs(end.Col - start.Col) + 1).Select(x => x);

            var rows = Enumerable.Range(Math.Min(start.Row, end.Row), Math.Abs(end.Row - start.Row) + 1).Select(x => x);

            // set middle placements
            foreach (var col in cols)
            {
                foreach (var row in rows)
                {
                    positions.Add(new RowCol(row, col));
                }
            }

            return(positions);
        }
Example #7
0
 public bool findPossibleTarget(RowCol pos) =>
 ((ShipType)Board.GetValue(pos.Row, pos.Col)) != ShipType.Hit &&
 ((ShipType)Board.GetValue(pos.Row, pos.Col)) != ShipType.Miss;
Example #8
0
 public bool isWater(RowCol pos) =>
 pos.Row > -1 && pos.Row <RowCol.MaxRow &&
                          pos.Col> -1 && pos.Col < RowCol.MaxCol &&
 ((ShipType)Board.GetValue(pos.Row, pos.Col)) == ShipType.Water;