private void CellDidChange(object sender, DataGridViewCellEventArgs e) { DataGridViewTextBoxCell cell = (DataGridViewTextBoxCell)DGGameBoardView.CurrentCell; _board.SetUpTile(cell.RowIndex, cell.ColumnIndex, Convert.ToInt32(cell.Value), false); bool rowSolved = _board.CheckRow(cell.RowIndex); bool colSolved = _board.CheckCol(cell.ColumnIndex); List <bool> listOfAllRegionStatus = new List <bool> { }; int rowsInRegion = 0; switch (_board.Size) { case 9: rowsInRegion = 3; break; case 6: rowsInRegion = 2; break; default: break; } for (int r = 0; r < (_board.Size); r += rowsInRegion) { for (int c = 0; c < (_board.Size); c += 3) { bool regionSolved = _board.CheckRegion(r, c, rowsInRegion); if (regionSolved) { listOfAllRegionStatus.Add(true); } } } bool regionsComplete = false; if (listOfAllRegionStatus.Count == _board.Size) { regionsComplete = true; } if (rowSolved && colSolved && regionsComplete) { SolvedLabel.Text = "SOLVED"; } }
private void OpenSudokuFile() { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Text |*.txt"; DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { FileStream fs = (FileStream)dialog.OpenFile(); StreamReader reader = new StreamReader(fs); string boardSize = reader.ReadLine(); int size; switch (boardSize) { case "4x4": size = 4; break; case "6x6": size = 6; break; case "9x9": size = 9; break; default: size = 0; break; } _board = new GameBoard(size); while (!reader.EndOfStream) { //Read the locked tiles. string tileLine = reader.ReadLine(); string[] tileDetails = tileLine.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); _board.SetUpTile(Convert.ToInt32(tileDetails[0]), Convert.ToInt32(tileDetails[1]), Convert.ToInt32(tileDetails[2]), true); } reader.Close(); BindGameBoardToUI(); } }
//Form Methods private bool OpenSudokuFile() { //Read a .txt file containing the sudoku puzzle. OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Text |*.txt"; DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { FileStream fs = (FileStream)dialog.OpenFile(); StreamReader reader = new StreamReader(fs); string boardSize = reader.ReadLine(); int size; switch (boardSize) { case "4x4": size = 4; break; case "6x6": size = 6; break; case "9x9": size = 9; break; default: //Crazy size. We can't handle that. size = 0; StatusLabel.Text = String.Format("Error, can't make a {0} board.", boardSize); StatusLabel.ForeColor = Color.Red; StatusLabel.Visible = true; return(false); } _board = new GameBoard(size); while (!reader.EndOfStream) { //Read the 'locked' tiles from the file. string tileLine = reader.ReadLine(); string[] tileDetails = tileLine.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); try { _board.SetUpTile(Convert.ToInt32(tileDetails[0]), Convert.ToInt32(tileDetails[1]), Convert.ToInt32(tileDetails[2]), true); } catch { //The file was not correct. StatusLabel.Text = "Error: The File Could Not Be Read"; StatusLabel.Visible = true; StatusLabel.ForeColor = Color.Red; reader.Close(); return(false); } } reader.Close(); } else { //User cancelled finding a file, bad user. return(false); } return(true); }
private void CellDidChange(object sender, DataGridViewCellEventArgs e) { //User entered something into a cell, figure out if it is valid, and check if they solved any regions, rows, columns, or the whole thing. DataGridViewTextBoxCell cell = (DataGridViewTextBoxCell)DGGameBoardView.CurrentCell; try { _board.SetUpTile(cell.RowIndex, cell.ColumnIndex, Convert.ToInt32(cell.Value), false); } catch { //User entered something other than a number. Sudoku != Crossword. StatusLabel.Text = "Error: Please enter a number from 1 - 9"; StatusLabel.ForeColor = Color.Red; StatusLabel.Visible = true; cell.Value = null; } bool rowSolved = _board.CheckRow(cell.RowIndex); if (rowSolved) { for (int i = 0; i < _board.Size; i++) { cell.OwningRow.Cells[i].Style.BackColor = Color.LightGreen; } } bool colSolved = _board.CheckCol(cell.ColumnIndex); if (colSolved) { for (int i = 0; i < _board.Size; i++) { cell.OwningColumn.DefaultCellStyle.BackColor = Color.LightGreen; } } List <bool> listOfAllRegionStatus = new List <bool> { }; for (int r = 0; r < (_board.Size); r += _board.RowsInRegion) { for (int c = 0; c < (_board.Size); c += _board.ColsInRegion) { bool regionSolved = _board.CheckRegion(r, c); if (regionSolved) { listOfAllRegionStatus.Add(true); } } } bool regionsComplete = false; if (listOfAllRegionStatus.Count == _board.Size) { regionsComplete = true; } if (rowSolved && colSolved && regionsComplete) { //User did a sudoku. StatusLabel.Text = "Puzzle Solved!"; StatusLabel.ForeColor = Color.Black; StatusLabel.Visible = true; } }