HI,
Trying to write a program to randomly generate a path across a grid, but for some reason the int playerCol resets it’s value back to 0 for some unknown reason.
Both playerCol and startCol are intiated at 0. Then startCol is randomized between 0 - 9 (10 columns) at line 50. At line 52 playerCol is given the same value as startCol, let us just 8 for example.
Then is goes through a series of 4 methods to determine if the next square in that direction is available. Problem is West keep coming up false, even with it should be true. Through a series of Debug statements I find that playerCol holds it’s value (8) after North(), after South() and after East(), line 61 print 8, but the next line calls the West() method and the first line in West(), playeCol reads 0. startCol holds it’s value(8), but not playerCol.
This has been confirmed by default.log and values as shown in the inspector.
Any ideas?
Thanks.
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 bool nortest = false;
private bool soutest = false;
private bool westest = false;
private bool eastest = false;
private int mapCode = 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()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
mazeArray[i, j] = 0;
}
}
return;
}
void GeneratePath()
{
startCol = Random.Range(0, 10);
mazeArray[startRow, startCol] = mapCode = 1;
int playerCol = startCol;
int test = 0; // used to limit while loop
while ( test < 1 ) //(tempRow < 7)
{
North();
Debug.Log(playerCol.ToString());
South();
Debug.Log(playerCol.ToString());
East();
Debug.Log(playerCol.ToString());
West();
//SelectDir();
test++;
}
//Debug.Log("start row " + startRow.ToString());
//Debug.Log("start col " + startCol.ToString());
//Debug.Log("north = " + nortest);
//Debug.Log("south = " + soutest);
//Debug.Log("east = " + eastest);
//Debug.Log("west = " + westest);
//Debug.Log(playerRow.ToString());
//Debug.Log(playerCol.ToString());
}
void North()
{
if (playerRow + 1 == 7)
{
nortest = false;
return; // make finish block
}
else if (mazeArray[playerRow + 1, playerCol] != 0)
{
nortest = false;
return;
}
else dirction.Add("north");
nortest = true;
return;
}
void South()
{
if (playerRow - 1 < 0)
{
soutest = false;
return;
}
else if (mazeArray[playerRow - 1, playerCol] != 0)
{
soutest = false;
return;
}
else dirction.Add("south");
soutest = true;
return;
}
void East()
{
if (playerCol + 1 > 9)
{
eastest = false;
return;
}
else if (mazeArray[playerRow, playerCol + 1] != 0)
{
eastest = false;
return;
}
else dirction.Add("east");
eastest = true;
return;
}
void West()
{
Debug.Log(playerCol.ToString());
Debug.Log(startCol.ToString());
if (playerCol - 1 < 0)
{
westest = false;
Debug.Log("if one");
Debug.Log(playerCol.ToString());
return;
}
else if (mazeArray[playerRow, playerCol - 1] != 0)
{
westest = false;
return;
}
else dirction.Add("west");
westest = true;
return;
}
void SelectDir()
{
int select = 0;
string selection = "";
if (dirction.Count != 0)
{
select = Random.Range(0, dirction.Count + 1);
Debug.Log("direction count = " + dirction.Count);
Debug.Log("selection = " + selection.ToString());
selection = dirction[select];
Debug.Log(selection);
}
else selection = "blocked";
switch (selection)
{
case "blocked":
break; // TODO - add back up method
case "north":
playerRow = playerRow + 1;
break;
case "south":
playerRow = playerRow - 1;
break;
case "east":
playerCol = playerCol + 1;
break;
case "west":
playerCol = playerCol - 1;
break;
default:
break;
}
return;
}
}