/// <summary> /// Paint function to draw the grid /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void panGrid_Paint(object sender, PaintEventArgs e) { Graphics gr = e.Graphics; for (int r = 1; r < mFieldGrid.Rows; r++) { gr.DrawLine(SystemPens.GrayText, 0, r * mSquareSizeY, panGrid.Width, r * mSquareSizeY); } for (int c = 1; c < mFieldGrid.Cols; c++) { gr.DrawLine(SystemPens.GrayText, c * mSquareSizeX, 0, c * mSquareSizeX, panGrid.Height); } for (int r = 0; r < mFieldGrid.Rows; r++) { for (int c = 0; c < mFieldGrid.Cols; c++) { if (mFieldGrid.GetCell(r, c) == FieldElement.Wall) { gr.FillRectangle(Brushes.Gray, c * mSquareSizeX, r * mSquareSizeY, mSquareSizeX, mSquareSizeY); } else if (mFieldGrid.GetCell(r, c) == FieldElement.Start) { gr.FillRectangle(Brushes.Green, c * mSquareSizeX, r * mSquareSizeY, mSquareSizeX, mSquareSizeY); } else if (mFieldGrid.GetCell(r, c) == FieldElement.Finish) { gr.FillRectangle(Brushes.Red, c * mSquareSizeX, r * mSquareSizeY, mSquareSizeX, mSquareSizeY); } } } if (mSolver != null) { if (mSolver.GetSortedSolutions().Count > 0) { foreach (FieldCoord coord in mSolver.GetSortedSolutions()[0]) { if (mFieldGrid.GetCell(coord.Row, coord.Col) == FieldElement.Empty) { gr.FillRectangle(Brushes.Yellow, coord.Col * mSquareSizeX, coord.Row * mSquareSizeY, mSquareSizeX, mSquareSizeY); } } } } }
/// <summary> /// Find the coordinates of a specific FieldElement element /// </summary> /// <param name="element">The element to be found</param> /// <returns>The coordinates of the element found of null if it is not found</returns> private FieldCoord FindFieldElement(FieldElement element) { for (int r = 0; r < mGrid.Rows; r++) { for (int c = 0; c < mGrid.Cols; c++) { if (mGrid.GetCell(r, c) == element) { return(new FieldCoord(r, c)); } } } return(null); }