Hi everyone,
I’ve completed the random path generator I was trying to do and was just hoping to get a critique or two. I know it’s pretty much all basic commands, but that’s about where my knowledge currently is.
The code will generate a path across a 10 wide x 8 tall grid. The code will back itself up out of a dead end situation and when it reaches row 8 it will end the path. So far after about 20 test runs it has not failed and path length has ranged between 15 - 54 moves to complete.
Any suggestions on how I could improved the code would be greatly appreciated. Especially in the BackUp method where I used a ton of IF Else If statements to sort 4 int’s by value.
As always, thanks in advance. (And yes I am posting because I’m a bit proud I was able to do it, but really am looking for critique)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public int[,] mazeArray;
public int startRow = 0;
public int startCol = 0;
public int playerRow = 0;
public int playerCol = 0;
private int mapCode = 0;
private int finalCode = 0;
private List<string> dirction = new List<string>();
void Start ()
{
mazeArray = new int[8, 10]; // rows, columns
dirction.Clear(); // clear list
ResetArray(); // set all elements to 0
GeneratePath(); // generate random path
}
void Update ()
{
}
void ResetArray() // Set all elements to 0 which = available space
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
mazeArray[i, j] = 0;
}
}
finalCode = 0; // final code = last space in path
return;
}
void GeneratePath()
{
startCol = Random.Range(0, 10); // pick starting coloumn, row is 0
mazeArray[startRow, startCol] = mapCode = 1; // assign 1 to start square
playerCol = startCol;
while ( finalCode == 0 )
{
South(); // determine if directions are available
North();
East();
West();
SelectDir(); // random selection of available directions
}
Debug.Log(startRow);
Debug.Log(startCol);
Debug.Log(playerRow);
Debug.Log(playerCol);
Debug.Log(finalCode);
return;
}
void North()
{
if (playerRow + 1 == 8) // Array out of range
{
return;
}
else if (mazeArray[playerRow + 1, playerCol] != 0) // space used
{
return;
}
else dirction.Add("north"); // add north to list
return;
}
void South()
{
if (playerRow - 1 < 0)
{
return;
}
else if (mazeArray[playerRow - 1, playerCol] != 0)
{
return;
}
else dirction.Add("south");
return;
}
void East()
{
if (playerCol + 1 > 9)
{
return;
}
else if (mazeArray[playerRow, playerCol + 1] != 0)
{
return;
}
else dirction.Add("east");
return;
}
void West()
{
if (playerCol - 1 < 0)
{
return;
}
else if (mazeArray[playerRow, playerCol - 1] != 0)
{
return;
}
else dirction.Add("west");
return;
}
void SelectDir()
{
int select = 0;
string selection = "";
if (dirction.Count != 0) // select random direction from list
{
select = Random.Range(0, dirction.Count);
selection = dirction[select];
}
else selection = "blocked"; // no moves available
switch (selection)
{
case "blocked":
BackUp(); // back up to last avail square with move options
break;
case "north":
playerRow = playerRow + 1;
mapCode = mapCode + 1;
mazeArray[playerRow, playerCol] = mapCode;
if (playerRow == 7)
{
finalCode = mapCode; // set final square if row 7
}
dirction.Clear();
break;
case "south":
playerRow = playerRow - 1;
mapCode = mapCode + 1;
mazeArray[playerRow, playerCol] = mapCode;
dirction.Clear();
break;
case "east":
playerCol = playerCol + 1;
mapCode = mapCode + 1;
mazeArray[playerRow, playerCol] = mapCode;
dirction.Clear();
break;
case "west":
playerCol = playerCol - 1;
mapCode = mapCode + 1;
mazeArray[playerRow, playerCol] = mapCode;
dirction.Clear();
break;
default:
break;
}
return;
}
void BackUp()
{
mazeArray[playerRow, playerCol] = -1; // set element to indicate no movement
mapCode = mapCode - 1; // set to previous map code
dirction.Clear();
int tempN = 0;
int tempS = 0;
int tempE = 0;
int tempW = 0;
// start logic to determine last space before dead end.
// gets mapCode for surroundind square, assign to temp int.
if (playerRow < 7)
{
tempN = mazeArray[playerRow + 1, playerCol];
}
if (playerRow > 0)
{
tempS = mazeArray[playerRow - 1, playerCol];
}
if (playerCol < 9)
{
tempE = mazeArray[playerRow, playerCol + 1];
}
if (playerCol > 0)
{
tempW = mazeArray[playerRow, playerCol - 1];
}
// determine highest value of temp int's. Highest value is the
// square previous to dead end. Set playerRow or or playerCol accordingly.
if (tempN > tempS)
{
if (tempN > tempE)
{
if (tempN > tempW)
{
playerRow = playerRow + 1;
return;
}
else
{
playerCol = playerCol - 1;
return;
}
}
else if (tempE > tempW)
{
playerCol = playerCol + 1;
return;
}
else
{
playerCol = playerCol - 1;
return;
}
}
else if (tempS > tempE)
{
if (tempS > tempW)
{
playerRow = playerRow - 1;
return;
}
else
{
playerCol = playerCol - 1;
return;
}
}
else if (tempE > tempW)
{
playerCol = playerCol + 1;
return;
}
else
{
playerCol = playerCol - 1;
return;
}
}
}