Islands challenge, skeloton code given.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Islands challenge, skeloton code given.

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    1

    Islands challenge, skeloton code given.

    Okay, so here's the challenge and i have created a basic starter skeleton if you wish to use it:
    You are given a Boolean two dimensional array,which can either contain true(land), or false(water). Now either using my starter or create your own you have to randomly generate "natural" looking islands.


    Generate Islands Class
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace RandomLand
    {
        public class Land
        {
    
            int continents;
          
            public struct Piece           // Each Element in the generated land array, will contain a Piece.
            {           
                public int ContinentID;
                public bool Type;            // determine whether land or water
                public int expansionNumber;  // helps regulate the amount of times it will be needed to expand, might not be 
            }                                // Might not be needed when random numbers comes in.
    
             Piece[,] generatedLand = new Piece[50, 50];
    
            public Piece[,] GenerateLand()
            {
                
                Random random = new Random();
                
                continents = random.Next(2, 6);
                for (int i = 0; i < continents; i++)
                {                                                           // Generates Continents
                    int X = random.Next(0, 50);
                    int Y = random.Next(0, 50);
                    generatedLand[X, Y].Type = true;
                    generatedLand[X, Y].ContinentID = i;
                    generatedLand[X, Y].expansionNumber = 0;
                    
                }                                 
                generatedLand = ExpandContinents(continents);
                return generatedLand;
            }
    
            public Piece[,] ExpandContinents(int continents)                 // This is the unfinished function that expands the islands(continents)
            {
                Random random = new Random();
                for (int continent = 0; continent < continents; continent++)                         // Does each continent seperatelly
                {                
                    
                    int numberOfExpantions = random.Next(1, 10);
                   
                    for (int Expanding = 0; Expanding < numberOfExpantions; Expanding++)       // Will have a random number of expansions
                    {
                        for (int y = 0; y < 50; y++)
                        {
                            for (int x = 0; x < 50; x++)
                            {
                                if (generatedLand[x, y].Type == true && generatedLand[x, y].expansionNumber < numberOfExpantions && generatedLand[x, y].ContinentID == continent)             // BeginExpansion
                                {
    
    
                                                                                         // This will be the bit where you generate continets, so far all it does is create Diamond shaped islands
                                  //  int newRandom = random.Next(1, 10);                                  
                                    if (!(x + 1 >= 50))
                                    {
                                        generatedLand[x + 1, y].ContinentID = continent;
                                        generatedLand[x + 1, y].Type = true;
                                        generatedLand[x + 1, y].expansionNumber = generatedLand[x, y].expansionNumber; 
                                        generatedLand[x + 1, y].expansionNumber++;
                                        
                                    }
                                    if (!(x - 1 < 0))
                                    {
                                        generatedLand[x - 1, y].ContinentID = continent;
                                        generatedLand[x - 1, y].Type = true;
                                        generatedLand[x - 1, y].expansionNumber = generatedLand[x, y].expansionNumber;
                                        generatedLand[x - 1, y].expansionNumber++;
                                       
                                    }
                                    if (!(y + 1 >= 50))
                                    {
                                        generatedLand[x, y + 1].ContinentID = continent;
                                        generatedLand[x, y + 1].Type = true;
                                        generatedLand[x, y + 1].expansionNumber = generatedLand[x, y].expansionNumber;
                                        generatedLand[x, y + 1].expansionNumber++;
                                       
                                    }
                                    if (!(y - 1 < 0))
                                    {
                                        generatedLand[x, y - 1].ContinentID = continent;
                                        generatedLand[x, y - 1].Type = true;
                                        generatedLand[x, y - 1].expansionNumber = generatedLand[x, y].expansionNumber;
                                        generatedLand[x, y - 1].expansionNumber++;
                                      
                                    }
                                    
                                }                                                       
                            }                      
                        }
                    }
                }
                
                return generatedLand;
    
            }
        }
    
        
    }
    The Console Program
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using RandomLand;
    namespace LandTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                Land land = new Land();
                Land.Piece[,] generatedLand = new Land.Piece[50, 50];
                generatedLand = land.GenerateLand();
    
                for (int x = 0; x < 50; x++)
                {
                    for (int y = 0; y < 50; y++)
                    {
                        if (generatedLand[x, y].Type == true)
                        {
                            Console.Write("X");
                        } else {
                            Console.Write(" ");
                        }
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
        }
    }
    i have added a few comments, just to get you to understand what i have done. If you do not like what i have done feel free to start from scratch.



    Good luck

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Islands challenge, skeloton code given.

    your post doesnt seem to contain a question..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center