public void Change(Laby laby) { if (laby.Width == widthNum && laby.Height == heightNum) { Laby.SetStartPoint(laby.GetStart().CellCor); Laby.SetEndPoint(laby.GetEnd().CellCor); for (int x = 0; x < laby.Width; x++) { for (int y = 0; y < laby.Height; y++) { Laby.SetCell(x, y, laby.GetCell(x, y)); } } } else { widthNum = laby.Width; heightNum = laby.Height; Laby = new Laby(laby); Laby = new Laby(widthNum, heightNum); Laby.SetStartPoint(laby.GetStart().CellCor); Laby.SetEndPoint(laby.GetEnd().CellCor); for (int x = 0; x < laby.Width; x++) { for (int y = 0; y < laby.Height; y++) { Laby.SetCell(x, y, laby.GetCell(x, y)); } } } }
private void labyPicture_Click(object sender, EventArgs e)//设置起止点与障碍 { if (materialTab.SelectedTab == showPage) { return; } MouseEventArgs mouseLoc = (MouseEventArgs)e; int cellWidth = labyPicture.Width / CurrentLaby.Width; int cellHeight = labyPicture.Height / CurrentLaby.Height; int sideNum, cellLength, StartX, StartY; if (CurrentLaby.Width > CurrentLaby.Height) { sideNum = CurrentLaby.Width;//以长宽中分段较多的作为依据,保证显示格点为方形 cellLength = cellWidth; StartX = 0; StartY = (labyPicture.Height - cellLength * CurrentLaby.Height) / 2 - 1; } else { sideNum = CurrentLaby.Height;//以长宽中分段较多的作为依据 cellLength = cellHeight; StartY = 0; StartX = (labyPicture.Width - cellLength * CurrentLaby.Width) / 2 - 1; } int x = (mouseLoc.X - StartX) / cellLength; int y = (mouseLoc.Y - StartY) / cellLength; if (mouseLoc.X < StartX || mouseLoc.Y < StartY || x >= CurrentLaby.Width || y >= CurrentLaby.Height) { return; } else { if (setBarrierButton.Checked) { Cell TempCell = CurrentLaby.GetCell(x, y); if (TempCell.CellType == Type.Empty) { CurrentLaby.SetCell(x, y, Type.Barrier); myDraw.Change(CurrentLaby); myDraw.DrawLaby(); } else if (TempCell.CellType == Type.Barrier) { CurrentLaby.SetCell(x, y, Type.Empty); Random rand = new Random(); int weight = rand.Next(1, 4); Cell cell = new Cell { CellCor = new Cor(x, y), CellType = Type.Empty, CellWeight = weight }; CurrentLaby.SetCell(x, y, cell); myDraw.Change(CurrentLaby); myDraw.DrawLaby(); } else { return; //不能把起止点改为障碍 } } if (setStartButton.Checked) { Cell TempCell = CurrentLaby.GetCell(x, y); if (TempCell.CellType == Type.End)//不能把起止点设为重合 { return; } CurrentLaby.SetStartPoint(new Cor(x, y)); myDraw.Change(CurrentLaby); myDraw.DrawLaby(); } if (setEndButton.Checked) { Cell TempCell = CurrentLaby.GetCell(x, y); if (TempCell.CellType == Type.Start) { return; } CurrentLaby.SetEndPoint(new Cor(x, y)); myDraw.Change(CurrentLaby); myDraw.DrawLaby(); } } }