using System.Collections; using System.Collections.Generic; using UnityEngine; public class MainGame2 : MonoBehaviour { // Start is called before the first frame update public string map = ".....\n" + "..XF.\n" + "..XX.\n" + "S....\n" + "4 5"; int rows, cols; string[] table; GameObject[,] objects; List placesToCheck=new List(); float lastTime = 0; bool isCorrectPlace(int[] place) { if (place[0] < 0) { return false; } if (place[1] < 0) { return false; } if (place[0] >= rows) { return false; } if (place[1] >= cols) { return false; } return table[place[0]][place[1]] == '.' && objects[place[0],place[1]].GetComponent().material.color==Color.white; } List freeMoves(int row, int col) { List answers = new List(); answers.Add(new int[] { row - 1, col }); answers.Add(new int[] { row + 1, col }); answers.Add(new int[] { row, col - 1 }); answers.Add(new int[] { row, col + 1 }); List answers2 = new List(); foreach (int[] answer in answers) { if (isCorrectPlace(answer)) { answers2.Add(answer); } } return answers2; } void Start() { table = map.Split(new char[] { '\n' }); string lastline = table[table.Length - 1]; string[] numbers = lastline.Split(new char[] { ' ' }); rows = int.Parse(numbers[0]); cols = int.Parse(numbers[1]); int startrow = 0, startcolumn = 0, finishrow = 0, finishcolumn = 0; objects = new GameObject[rows, cols]; for (int rownr = 0; rownr < rows; rownr++) { for (int colnr = 0; colnr < cols; colnr++) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.localScale = Vector3.one * 0.5f; cube.transform.position = new Vector3(colnr, -rownr, 0); objects[rownr, colnr] = cube; if (table[rownr][colnr] == '.') { cube.transform.localScale = Vector3.one * 0.3f; } if (table[rownr][colnr] == 'S') { cube.GetComponent().material.color = Color.green; startrow = rownr; startcolumn = colnr; } if (table[rownr][colnr] == 'F') { cube.GetComponent().material.color = Color.red; finishrow = rownr; finishcolumn = colnr; } //Make Start and Finish different color } } placesToCheck.Add(new int[] { startrow, startcolumn }); } // Update is called once per frame void Update() { if (Time.time >= lastTime + 1) { // List newPlacesToCheck = new List(); List newFreeMoves = new List(); foreach (int[] place in placesToCheck) { foreach (int[] move in freeMoves(place[0], place[1])) { newFreeMoves.Add(move); objects[move[0], move[1]].GetComponent().material.color = Color.yellow; } } placesToCheck = newFreeMoves; lastTime = Time.time; } } }