Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
CommandForce3
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Hex { int height_index; // location in map grid int width_index; // location in map grid TerrainType terrain; // sea (default 0) or land (1) Hex[] myNeighbors = new Hex[6]; enum HexNeighbor {TOP_RIGHT, RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT}; public Hex(int height_index, int width_index) { this.height_index = height_index; this.width_index = width_index; // set all the neighbor tiles, 0 is the top-right tile, go clockwise (6 neighbors total) // check for TOP RIGHT neighbor if (height_index > 0) // not first row { if (height_index / 2) // odd row { if (width_index != mapWidth - 1) // not the last hex in the row myNeighbors[HexNeighbor.TOP_RIGHT] = map_hexes[height_index - 1, width_index + 1]; } else // even row myNeighbors[HexNeighbor.TOP_RIGHT] = map_hexes[height_index - 1, width_index]; } // check for RIGHT neighbor if (width_index != mapWidth - 1) // not the last hex in the row myNeighbors[HexNeighbor.RIGHT] = map_hexes[height_index, width_index + 1]; // check for BOTTOM RIGHT neighbor if (height_index < mapHeight - 1) // not last row { if (width_index / 2) // odd row { if (width_index != mapWidth - 1) // not the last hex in the row myNeighbors[HexNeighbor.BOTTOM_RIGHT] = map_hexes[height_index + 1], width_index + 1]; } else // even row myNeighbors[HexNeighbor.BOTTOM_RIGHT] = map_hexes[height_index + 1, width_index]; } // check for BOTTOM LEFT neighbor if (height_index < mapHeight - 1) // not last row { if (width_index / 2) // odd row myNeighbors[HexNeighbor.BOTTOM_LEFT] = map_hexes[height_index + 1, width_index]; else // even row if (width_index != 0) // not the first hex in the row myNeighbors[HexNeighbor.BOTTOM_LEFT = map_hexes[height_index + 1, width_index - 1]; // Check for LEFT neighbor if (width_index != 0) // not the first hex in the row myNeighbors[HexNeighbor.LEFT] = map_hexes[height_index, width_index - 1]; // Check for TOP LEFT neighbor if (height_index > 0) // not first row { if (width_index / 2) // odd row { myNeighbors[HexNeighbor.TOP_LEFT] = map_hexes[height_index - 1, width_index]; } else // even row if (width_index != 0) // not the first hex in the row myNeighbors[HexNeighbor.TOP_LEFT] = map_hexes[height_index - 1, width_index - 1]; } // all neighbors hexes should be set now, if an edge has no neighbor, it should remain null } } public static void Main(string[] args) { //Your code goes here enum TerrainType {SEA, LAND}; // generate map int mapHeight = 20; int mapWidth = 30; Hex[ , ] map_hexes = new Hex[mapHeight, mapWidth]; // create all the tiles //Go across map, left to right, top to bottom public int INTERNAL_CHANCE = 95; // change this in testing until get good results int gradient = [get value from user, from 1 to 10]; // 5 is average, 10 more continental for (int height_index = 0; height_index<mapHeight; height_index++) { for (int width_index = 0; width_index<mapWidth; width_index++) { Hex hex = new Hex(height_index, width_index); hex.terrain = TerrainType.LAND; map_hexes[height_index][width_index] = hex; // Depending on gradient, have random chance of each hex being land. //For low gradient=lower chance, high gradient=higher chance. int chance = Random.Range(1, 100) * gradient; if (chance >= INTERNAL_CHANCE) { //Change hex from SEA to LAND hex.terrain = TerrainType.LAND; } } } // After seeding island hexes throughout map grid, go to each land hex and generate // neighboring land hexes based on random chance for each neighbor. As long as at // least one neighbor generated (don't make a neighbor land if it is already land), // keep going but chance to be land hex drops every time until chance reaches 0. // Do this for each seeded land hex. List<Hex> neighborHexes = new List<Hex>(); // list of neighbors to go thru for (int height_index = 0; height_index<mapWidth; height_index++) { for (int width_index 0; width_index<mapHeight; width_index++) { if (hex.terrain == TerrainType.LAND) { // get all neighbors hexes here and add them to neighborHexes neighborHexes.clear(); AddNeighbors(neighborHexes, map[height_index, width_index]); foreach (Hex neighbor_hex in neighborHexes) { // skip if already land if (Random.Range(1,2) == 1) { neighbor_hex.terrain = TerrainType.Land; } } } } } private void AddNeighbors(List hexes, Hex hex) { foreach (Hex n_hex in hex.myNeighbors) { hexes.Add(n_hex); } } } }
run
|
edit
|
history
|
help
0
EFDWSDCS
Simple Regex Example
do while
Math 10.95 fracOp (mult div only)
Chest Interaction Unity
Calculate X and Y using Lambda Expressions
card 2
Singleton Example
Reverse Array (!Array.Reverse())
12