using System.Collections; using System.Collections.Generic; using Unity.VisualScripting.Antlr3.Runtime; using UnityEngine; public class MainGame : 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; 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]] == '.'; } 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; GameObject[,] 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 } } foreach (int[] move in freeMoves(startrow, startcolumn)) { objects[move[0], move[1]].GetComponent().material.color = Color.yellow; } // objects[0, 1].GetComponent().material.color = Color.yellow; //Make all cubes horizontally or vertically connected to start yellow } // Update is called once per frame void Update() { } }