CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Islands challenge, skeloton code given.

1. Junior Member
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;

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 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();
}
}
}
}```
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. ## Re: Islands challenge, skeloton code given.

your post doesnt seem to contain a question..

#### Posting Permissions

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