Beispiel #1
0
        public MapGenerator(dynamic state)
        {
            this.Width  = state.PlayerMap.MapWidth;
            this.Height = state.PlayerMap.MapHeight;


            CurrentMap.Cells = new List <MapSpace>();
            for (int i = 0; i < Width * Height; i++)
            {
                MapSpace nextSpace = new MapSpace((int)(state.OpponentMap.Cells[i].X), (int)(state.OpponentMap.Cells[i].Y));
                if (state.OpponentMap.Cells[i].Damaged == true)
                {
                    nextSpace.State = SpaceState.Hit;
                }
                else if (state.OpponentMap.Cells[i].Missed == true)
                {
                    nextSpace.State = SpaceState.Miss;
                }
                else
                {
                    nextSpace.State = SpaceState.Open;
                }
                CurrentMap.Cells.Add(nextSpace);
            }
        }
Beispiel #2
0
        public MapGenerator(int mapWidth, int mapHeight, int maxShots)
        {
            Height = mapHeight;
            Width  = mapWidth;

            // 1. Randomly Place ships
            var shipsToPlace = new List <Ship>
            {
                Ship.Battleship,
                Ship.Carrier,
                Ship.Cruiser,
                Ship.Destroyer,
                Ship.Submarine
            };

            List <ShipPlacement> MyShips = new List <ShipPlacement>();

            // for now randomly place ships
            foreach (Ship s in shipsToPlace)
            {
                bool   gotPosB = false;
                Random rnd     = new Random();

                while (gotPosB == false)
                {
                    int x = rnd.Next(0, mapWidth);
                    int y = rnd.Next(0, mapHeight);

                    var       v      = Enum.GetValues(typeof(Direction));
                    Direction newDir = (Direction)v.GetValue(rnd.Next(v.Length));

                    ShipPlacement newShip = new ShipPlacement(new Point(x, y), s, newDir);
                    if ((newShip.IsCollidedB(MyShips) == false) && (newShip.IsOffMapB(mapWidth, mapHeight) == false))
                    {
                        MyShips.Add(newShip);
                        gotPosB = true;
                    }
                }
            }
            // assign list of ships
            ShipList = MyShips;


            // 2. Randomly choose number of shots
            Random rand     = new Random();
            int    numShots = 0;

            if (maxShots != 0)
            {
                numShots = rand.Next(1, maxShots + 1);
                Shots    = numShots;
            }

            // 3. Choose random co-ords to shoot at
            // create list
            CurrentMap.Cells = new List <MapSpace>();
            for (int i = 0; i < mapWidth * mapHeight; i++)
            {
                MapSpace nextSpace = new MapSpace(i % mapWidth, (int)(i / mapHeight));
                nextSpace.State = SpaceState.Open;
                CurrentMap.Cells.Add(nextSpace);
            }
            while (numShots > 0)
            {
                bool foundOpenB = false;

                while (foundOpenB == false)
                {
                    int shotX = rand.Next(0, mapWidth);
                    int shotY = rand.Next(0, mapHeight);

                    if (CurrentMap.GetSpace(shotX, shotY).State == SpaceState.Open)
                    {
                        foundOpenB = true;

                        bool isHitB = false;
                        foreach (ShipPlacement s in ShipList)
                        {
                            if (s.IsCollidedB(CurrentMap.GetSpace(shotX, shotY).Coords))
                            {
                                isHitB = true;
                                break;
                            }
                        }

                        if (isHitB == true)
                        {
                            Hits++;
                            CurrentMap.GetSpace(shotX, shotY).State = SpaceState.Hit;
                        }
                        else
                        {
                            Misses++;
                            CurrentMap.GetSpace(shotX, shotY).State = SpaceState.Miss;
                        }
                    }
                }
                numShots--;
            }
        }