public Node(NewPanel panel, int Y, int X) { Cat = Wall = Cheese = Agent = false; None = true; this.X = X; this.Y = Y; this.panel = panel; }
private void InitializeGrid() { hashtable.Clear(); int gridWidth = 600; int gridHeight = 600; int size = Int32.Parse(gridSize.SelectedItem.ToString()); nodes = new Node[size, size]; int height = gridWidth / size; int width = gridHeight / size; int thickness = 5; // node border thickness int count = 0; int y = 100; for (int i = 0; i < size; i++) { int x = 20; for (int j = 0; j < size; j++) { count++; NewPanel panel = new NewPanel(); panel.Location = new Point(x, y); panel.BackColor = Color.White; panel.Size = new Size(width, height); panel.Name = "panel" + count.ToString(); panel.PanelLabel = "0"; panel.BackgroundImageLayout = ImageLayout.Zoom; panel.MouseClick += Panel_MouseClick; panel.BorderThickness = thickness; this.Controls.Add(panel); Node node = new Node(panel, i, j); node.state = count; nodes[i, j] = node; hashtable.Add(count, node); x += (width - thickness); } y += (height - thickness); } }
private void Panel_MouseClick(object sender, MouseEventArgs e) { try { NewPanel panel = (NewPanel)sender; int number = Convert.ToInt32(Regex.Replace(panel.Name, "panel", string.Empty)); Node node = (Node)hashtable[number]; if (agentBtn.Checked) { if (node.Agent) { node.panel.BackgroundImage = null; node.Agent = false; node.None = true; agentExists = false; AgentNode = null; return; } else if (node.Wall) { throw new Exception("CANNOT PLACE AN AGENT ON THE WALL!"); } else if (node.Cheese) { throw new Exception("CANNOT PLACE AN AGENT ON THE CHEESE!"); } else if (agentExists) { AgentNode.panel.BackgroundImage = null; AgentNode.Agent = false; AgentNode.None = true; } node.panel.BackgroundImage = mouseImage; node.Agent = true; node.None = false; AgentNode = node; agentExists = true; } else if (cheeseBtn.Checked) { if (node.Cheese) { node.panel.BackgroundImage = null; node.Cheese = false; node.None = true; cheeseExists = false; CheeseNode = null; return; } else if (node.Wall) { throw new Exception("CANNOT PLACE A CHEESE ON THE WALL!"); } else if (node.Agent) { throw new Exception("CANNOT PLACE A CHEESE ON THE AGENT!"); } else if (cheeseExists) { CheeseNode.panel.BackgroundImage = null; CheeseNode.Cheese = false; CheeseNode.None = true; } node.panel.BackgroundImage = cheeseImage; node.Cheese = true; node.None = false; CheeseNode = node; cheeseExists = true; } else if (wallBtn.Checked) { if (node.Wall) { node.panel.BackgroundImage = null; node.Wall = false; node.None = true; } else if (node.Cheese) { throw new Exception("CANNOT PLACE A WALL ON THE CHEESE!"); } else if (node.Agent) { throw new Exception("CANNOT PLACE A WALL ON THE AGENT!"); } else { node.Wall = true; node.None = false; node.panel.BackgroundImage = wallImage; } } else if (catBtn.Checked) { if (node.Cat) { node.panel.BackgroundImage = null; node.Cat = false; node.None = true; } else if (node.Cheese) { throw new Exception("CANNOT PLACE A CAT ON THE CHEESE!"); } else if (node.Agent) { throw new Exception("CANNOT PLACE A CAT ON THE AGENT!"); } else if (node.Wall) { throw new Exception("CANNOT PLACE A CAT ON THE WALL!"); } else { node.Cat = true; node.None = false; node.panel.BackgroundImage = catImage; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } }