/// <summary> /// Uses recursion to find a path from each white cell to another. If no path can be found, returns false. /// </summary> /// <param name="cur"></param> /// <returns>True if all white cells are connected vertically/horizontally and false otherwise.</returns> private bool Connected(PuzzleCell cur) { cur.Visited = true; if (_PuzzleGrid[cur.Row, cur.Column].Row != 0 && _PuzzleGrid[cur.Row - 1, cur.Column].White == true && _PuzzleGrid[cur.Row - 1, cur.Column].Visited == false) { Connected(_PuzzleGrid[cur.Row - 1, cur.Column]); } if (_PuzzleGrid[cur.Row, cur.Column].Column != _PuzzleGrid.GetLength(1) - 1 && _PuzzleGrid[cur.Row, cur.Column + 1].White == true && _PuzzleGrid[cur.Row, cur.Column + 1].Visited == false) { Connected(_PuzzleGrid[cur.Row, cur.Column + 1]); } if (_PuzzleGrid[cur.Row, cur.Column].Row != _PuzzleGrid.GetLength(0) - 1 && _PuzzleGrid[cur.Row + 1, cur.Column].White == true && _PuzzleGrid[cur.Row + 1, cur.Column].Visited == false) { Connected(_PuzzleGrid[cur.Row + 1, cur.Column]); } if (_PuzzleGrid[cur.Row, cur.Column].Column != 0 && _PuzzleGrid[cur.Row, cur.Column - 1].White == true && _PuzzleGrid[cur.Row, cur.Column - 1].Visited == false) { Connected(_PuzzleGrid[cur.Row, cur.Column - 1]); } if (CheckVisited()) { return(true); } return(false); }
/// <summary> /// Method to be called upon clicking a label. If the label is white, sets background to black and forecolor to white. /// If the label is black, sets background to white and forecolor to black. Updates PuzzleCell properties. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ux_PuzzleCell_Click(object sender, EventArgs e) { PuzzleCell clicked = sender as PuzzleCell; if (clicked.BackColor == Color.White) { clicked.BackColor = Color.Black; clicked.ForeColor = Color.White; clicked.White = false; } else { clicked.BackColor = Color.White; clicked.ForeColor = Color.Black; clicked.White = true; } }
/// <summary> /// Reads in a puzzle file and error checks for a valid input file. /// </summary> private void ReadPuzzleFile() { if (ux_OpenFileDialog.ShowDialog() == DialogResult.OK) { if (_PuzzleGrid == null) { int[,] tempPuzzleGrid = new int[5, 5]; using (StreamReader inFile = new StreamReader(ux_OpenFileDialog.FileName)) { int rowIndex = 0; while (!inFile.EndOfStream) { string[] nums = inFile.ReadLine().Split(' '); if (nums.Length != 5) { throw new Exception(); } for (int i = 0; i < nums.Length; i++) { if (Convert.ToInt32(nums[i]) < 1 || Convert.ToInt32(nums[i]) > 5) { throw new Exception(); } } for (int i = 0; i < nums.Length; i++) { tempPuzzleGrid[rowIndex, i] = Convert.ToInt32(nums[i]); } rowIndex++; } } int xLocation = 130; int yLocation = 80; _PuzzleGrid = new PuzzleCell[5, 5]; for (int r = 0; r < tempPuzzleGrid.GetLength(0); r++) { for (int c = 0; c < tempPuzzleGrid.GetLength(1); c++) { PuzzleCell tempPC = new PuzzleCell(tempPuzzleGrid[r, c], r, c); _PuzzleGrid[r, c] = tempPC; tempPC.Text = tempPC.Number.ToString(); tempPC.Location = new Point(xLocation, yLocation); tempPC.Size = new Size(30, 30); tempPC.BackColor = Color.White; Controls.Add(tempPC); tempPC.Click += new EventHandler(ux_PuzzleCell_Click); xLocation += 35; } xLocation = 130; yLocation += 35; } } else { for (int r = 0; r < _PuzzleGrid.GetLength(0); r++) { for (int c = 0; c < _PuzzleGrid.GetLength(1); c++) { Controls.Remove(_PuzzleGrid[r, c]); } } int[,] tempPuzzleGrid = new int[5, 5]; using (StreamReader inFile = new StreamReader(ux_OpenFileDialog.FileName)) { int rowIndex = 0; while (!inFile.EndOfStream) { string[] nums = inFile.ReadLine().Split(' '); if (nums.Length != 5) { throw new Exception(); } for (int i = 0; i < nums.Length; i++) { if (Convert.ToInt32(nums[i]) < 1 || Convert.ToInt32(nums[i]) > 5) { throw new Exception(); } } for (int i = 0; i < nums.Length; i++) { tempPuzzleGrid[rowIndex, i] = Convert.ToInt32(nums[i]); } rowIndex++; } } int xLocation = 130; int yLocation = 80; _PuzzleGrid = new PuzzleCell[5, 5]; for (int r = 0; r < tempPuzzleGrid.GetLength(0); r++) { for (int c = 0; c < tempPuzzleGrid.GetLength(1); c++) { PuzzleCell tempPC = new PuzzleCell(tempPuzzleGrid[r, c], r, c); _PuzzleGrid[r, c] = tempPC; tempPC.Text = tempPC.Number.ToString(); tempPC.Location = new Point(xLocation, yLocation); tempPC.Size = new Size(30, 30); tempPC.BackColor = Color.White; Controls.Add(tempPC); tempPC.Click += new EventHandler(ux_PuzzleCell_Click); xLocation += 35; } xLocation = 130; yLocation += 35; } } } }