Ah. After a long (due to a holiday on Thursday and Kindergarten being closed on Friday) I decided to start anew as the code was getting way to convoluted and I was getting led around by the nose by the coordinate system. One problem I was having was that the orientation of the prefab tiles (their textures) in the Inspector wasn’t matching what I was seeing at runtime (180° difference).
Well, now at least (so far) my script is doing what I want it to do (all"random sides are set to "A = Corridor, hence the tidiness):

I changed the previous file “Tile” to “TileCode”:
using UnityEngine;
using System.Collections;
public class TileCode : MonoBehaviour{
public string tileCode;
/* Codes connectors on each side of a tile prefab
* X = Wall
* A = Corridor
*
*/
}
eliminating the extra step of converting an array of integers into a string.
The main script now looks like this:
using UnityEngine;
using System.Collections;
public class MapMaker : MonoBehaviour {
public GameObject floorTileCorner;
public GameObject floorTileCorridor;
public GameObject floorTileDeadEnd;
public GameObject floorTileEntry;
public GameObject floorTileExit;
public GameObject floorTileTJunction;
public GameObject floorTileXJunction;
public GameObject tilePrefab;
public GameObject[] floorTilePrefabArray;
public int mapTilesX = 4;
public int mapTilesZ = 4;
public int xTiles;
public int zTiles;
public float positionX;
public float positionZ;
public string[,] tileCodes;
public string[,] assignedTileCodes; // x,z : contains the target code for each laid out tile
public Hashtable tileCodesHits = new Hashtable();
public int rndRotate;
public Tile[] tiles;
void Start () {
GenerateMap();
}
void GenerateMap () {
SetupPrefabArray();
assignedTileCodes = new string[mapTilesX, mapTilesZ];
SetupTileCodeArray();
for( zTiles = 0; zTiles < mapTilesZ; zTiles++){ // build up further rows
for( xTiles = 0; xTiles < mapTilesX; xTiles++){ // lay the first row left to right
SelectTile(); // select legal tile.
positionX = (float)(-0.5 + xTiles * 6);//setup the coordinates for each tile
//positionZ = -0.5f;
positionZ = (float)(-0.5 + zTiles * 6);
//GameObject go = Instantiate(tilePrefab, new Vector3( positionX, 0, positionZ ), Quaternion.Euler(0, rndRotate, 0)) as GameObject;
//Tile tile = go.GetComponent<Tile>(); // Get a reference to your Tile Component - TileCode now!
Instantiate(tilePrefab, new Vector3( positionX, 0, positionZ ), Quaternion.Euler(0, rndRotate, 0)); // go wasn't needed up to this point
} // end for xTiles
} // end for zTiles
} // end GenerateMap()
string RandomBorder() {
return "A"; // currently we just generate "Corridor"
}
void SelectTile () {
//Debug.Log(xTiles);
// tiles have a string code like XAAX (corner) at 0 rotation. At 90 rotation it would be XXAA
// we need a table of "which gameobject at what rotation is a legal choice here" -> is set up in setupTileCodeArray
// now we need to build our target string "1234", where 1 is North, 2 East etc.
string targetString = "";
// 1st element: North
if ( zTiles == (mapTilesZ - 1))
targetString += "X";
else targetString += RandomBorder();
// 2nd element: East
if ( xTiles == (mapTilesX -1))
targetString += "X";
else targetString += RandomBorder();
// 3rd element: South
if ( zTiles == 0)
targetString += "X";
else targetString += RandomBorder();
// 4th element: West
if ( xTiles == 0)
targetString += "X";
else {
string westWall = assignedTileCodes[xTiles -1, zTiles];
targetString += westWall.Substring( 1, 1);
Debug.Log(westWall);
}
assignedTileCodes[xTiles, zTiles] = targetString;
Debug.Log("TString: " + targetString);
// compare targetString to tileCodes array
for( int xx = 0; xx < floorTilePrefabArray.Length; xx++) {
for( int yy = 0; yy < 4; yy++) {
if ( targetString == tileCodes[xx,yy] ){
print("We have a hit: " + xx.ToString() + " " + yy.ToString());
tileCodesHits[xx] = yy; // Hashtable does not need type declaration
print("Hashtable: " + tileCodesHits.Count);
} // end if
} // end for yy
} // end for xx
// Hashtable tile CodesHits contains all suitable results
int rndTile = Random.Range(0, tileCodesHits.Count);
int r = 0;
foreach (DictionaryEntry entry in tileCodesHits)
{
Debug.Log("Key: "+entry.Key+" Value: "+entry.Value);
if ( r == rndTile ) {
tilePrefab = floorTilePrefabArray[(int)entry.Key] ;
rndRotate = (int)entry.Value * 90;
break;
}
r++;
}
tileCodesHits.Clear(); // need to clear Hashtable for next tile
} // end SelectTile()
void SetupPrefabArray () {
floorTilePrefabArray = new GameObject[5] { floorTileCorner, floorTileCorridor, floorTileDeadEnd, floorTileTJunction, floorTileXJunction };
} // end method setupPrefabArray
void SetupTileCodeArray () { // multi-array: the first position contains the position of the prefab in floorTilePrefabArray, the second the rotation (* 90°)
tileCodes = new string[floorTilePrefabArray.Length, 4]; // 4 is the number of sides and hence possible rotations
for(int i = 0; i < floorTilePrefabArray.Length; i++) {
// iterate all through all possible rotations for the tile and store the codes as strings
TileCode ft = floorTilePrefabArray[i].GetComponent<TileCode>();
tileCodes[i,0] = ft.tileCode;
tileCodes[i,1] = ft.tileCode.Substring( 3, 1)+ft.tileCode.Substring( 0, 3);
tileCodes[i,2] = ft.tileCode.Substring( 2, 2)+ft.tileCode.Substring( 0, 2);
tileCodes[i,3] = ft.tileCode.Substring( 1, 3)+ft.tileCode.Substring( 0, 1);
} // end for i
} // end method setupTileCodeArray
} // end class MapMaker
Next step: Randomize and add special rooms.