/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here graphics.PreferredBackBufferWidth = 1024; graphics.PreferredBackBufferHeight = 768; graphics.ApplyChanges(); grid = new GridCell[graphics.PreferredBackBufferWidth / gridSize, graphics.PreferredBackBufferHeight / gridSize]; for (int i = 0; i < graphics.PreferredBackBufferWidth / gridSize; i++) { for (int j = 0; j < graphics.PreferredBackBufferHeight / gridSize; j++) { grid[i, j] = new GridCell(this, new Vector2(i * gridSize, j * gridSize), new Vector2(gridSize, gridSize)); Components.Add(grid[i, j]); } } myCat = new HalfDeadCat(this); Components.Add(myCat); StartRandomSearch(); IsMouseVisible = true; base.Initialize(); }
public Graph(GridCell[,] gridCell, int width, int height, HalfDeadCat cat) { myCat = cat; this.gridCell = gridCell; this.width = width; this.height = height; nodeGrid = new Node[width, height]; searchType = SearchType.BREADTH; //Initializes all of the nodes for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (!gridCell[i, j].IsBlocked) gridCell[i, j].ChangeColor = Color.CornflowerBlue; nodeGrid[i, j] = new Node(gridCell[i, j], i, j); } } //Sets neighboring nodes for each node for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { //if node has four neighbors if (i != 0 && i != width - 1 && j != 0 && j != height - 1) { nodeGrid[i, j].Neighbors = new List<Edge> {new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } //if node has two neighbors else if ((i == 0 || i == width - 1) && (j == 0 || j == height - 1)) { if (i == 0 && j == 0) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1) }; } else if (i == 0 && j == height - 1) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1) }; } else if (i == width - 1 && j == 0) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } else if (i == width - 1 && j == height - 1) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } } //if node has three neighbors else { if (i == 0) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1) }; } else if (i == width - 1) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } else if (j == 0) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } else if (j == height - 1) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } } } } }
public Graph(GridCell[,] gridCell, int width, int height, bool isDijkstra, HalfDeadCat cat) { myCat = cat; this.gridCell = gridCell; this.width = width; this.height = height; nodeGrid = new Node[width, height]; if (isDijkstra) { searchType = SearchType.DIJKSTRA; } else { searchType = SearchType.ASTAR; } //Initializes all of the nodes for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (!gridCell[i, j].IsBlocked) gridCell[i, j].ChangeColor = Color.CornflowerBlue; nodeGrid[i, j] = new Node(gridCell[i, j], i, j); } } //Sets neighboring nodes for each node for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { //if node has 8 neighbors if (i != 0 && i != width - 1 && j != 0 && j != height - 1) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j - 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j - 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j + 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j + 1], (float)Math.Sqrt(2)),}; } //if node has 3 neighbors else if ((i == 0 || i == width - 1) && (j == 0 || j == height - 1)) { if (i == 0 && j == 0) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j + 1], (float)Math.Sqrt(2)) }; } else if (i == 0 && j == height - 1) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j - 1], (float)Math.Sqrt(2)) }; } else if (i == width - 1 && j == 0) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j + 1], (float)Math.Sqrt(2)) }; } else if (i == width - 1 && j == height - 1) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j - 1], (float)Math.Sqrt(2)) }; } } //if node has 5 neighbors else { if (i == 0) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j + 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j - 1], (float)Math.Sqrt(2))}; } else if (i == width - 1) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j - 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j + 1], (float)Math.Sqrt(2))}; } else if (j == 0) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j + 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j + 1], (float)Math.Sqrt(2))}; } else if (j == height - 1) { nodeGrid[i, j].Neighbors = new List<Edge> { new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j - 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j - 1], (float)Math.Sqrt(2))}; } } } } }
public Graph(GridCell[,] gridCell, int width, int height, HalfDeadCat cat) { myCat = cat; this.gridCell = gridCell; this.width = width; this.height = height; nodeGrid = new Node[width, height]; searchType = SearchType.BREADTH; //Initializes all of the nodes for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (!gridCell[i, j].IsBlocked) { gridCell[i, j].ChangeColor = Color.CornflowerBlue; } nodeGrid[i, j] = new Node(gridCell[i, j], i, j); } } //Sets neighboring nodes for each node for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { //if node has four neighbors if (i != 0 && i != width - 1 && j != 0 && j != height - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } //if node has two neighbors else if ((i == 0 || i == width - 1) && (j == 0 || j == height - 1)) { if (i == 0 && j == 0) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1) }; } else if (i == 0 && j == height - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1) }; } else if (i == width - 1 && j == 0) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } else if (i == width - 1 && j == height - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } } //if node has three neighbors else { if (i == 0) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1) }; } else if (i == width - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } else if (j == 0) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } else if (j == height - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1) }; } } } } }
public Graph(GridCell[,] gridCell, int width, int height, bool isDijkstra, HalfDeadCat cat) { myCat = cat; this.gridCell = gridCell; this.width = width; this.height = height; nodeGrid = new Node[width, height]; if (isDijkstra) { searchType = SearchType.DIJKSTRA; } else { searchType = SearchType.ASTAR; } //Initializes all of the nodes for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (!gridCell[i, j].IsBlocked) { gridCell[i, j].ChangeColor = Color.CornflowerBlue; } nodeGrid[i, j] = new Node(gridCell[i, j], i, j); } } //Sets neighboring nodes for each node for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { //if node has 8 neighbors if (i != 0 && i != width - 1 && j != 0 && j != height - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j - 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j - 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j + 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j + 1], (float)Math.Sqrt(2)), }; } //if node has 3 neighbors else if ((i == 0 || i == width - 1) && (j == 0 || j == height - 1)) { if (i == 0 && j == 0) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j + 1], (float)Math.Sqrt(2)) }; } else if (i == 0 && j == height - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j - 1], (float)Math.Sqrt(2)) }; } else if (i == width - 1 && j == 0) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j + 1], (float)Math.Sqrt(2)) }; } else if (i == width - 1 && j == height - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j - 1], (float)Math.Sqrt(2)) }; } } //if node has 5 neighbors else { if (i == 0) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j + 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j - 1], (float)Math.Sqrt(2)) }; } else if (i == width - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j - 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j + 1], (float)Math.Sqrt(2)) }; } else if (j == 0) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i, j + 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j + 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j + 1], (float)Math.Sqrt(2)) }; } else if (j == height - 1) { nodeGrid[i, j].Neighbors = new List <Edge> { new Edge(nodeGrid[i, j], nodeGrid[i + 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i, j - 1], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j], 1), new Edge(nodeGrid[i, j], nodeGrid[i - 1, j - 1], (float)Math.Sqrt(2)), new Edge(nodeGrid[i, j], nodeGrid[i + 1, j - 1], (float)Math.Sqrt(2)) }; } } } } }