private static void BoardTest1() { Debug.WriteLine( "Testing Construction and Initialization..." ); SudokuEngine.Board board = new SudokuEngine.Board(); Debug.Assert( board.Rows == 9, "FAILURE: member initialization" ); Debug.Assert( board.Columns == 9, "FAILURE: member initialization" ); Debug.Assert( board.SubRows == 3, "FAILURE: member initialization" ); Debug.Assert( board.SubColumns == 3, "FAILURE: member initialization" ); for ( int row = 0; row < board.Rows; ++row ) { for ( int col = 0; col < board.Columns; ++col ) { // Exercise the cell at [ row, col ]. If it hasn't been // initialized, an exception will be thrown. try { board[ row, col ].Locked = true; } catch { Debug.Assert( false, "FAILURE: board initialization [ " + row + ", " + col + " ]" ); } } } }
private static void BoardTest2() { // Constrution with bad values test. try { SudokuEngine.Board board = new SudokuEngine.Board( 1, 2, 2 ); } catch ( ArgumentOutOfRangeException ) { return; } catch { Debug.Assert( false, "FAILURE: sub-dimension > dimension unhandled exception" ); return; } Debug.Assert( false, "FAILURE: sub-dimension > dimension not caught" ); }
private static void BoardTest3() { // Board dimensions not a multiple of block dimensions test. try { SudokuEngine.Board board = new SudokuEngine.Board( 9, 2, 2 ); } catch ( ArgumentOutOfRangeException ) { return; } catch { Debug.Assert( false, "FAILURE: dimension % sub-dimension unhandled exception" ); return; } Debug.Assert( false, "FAILURE: dimension % sub-dimension not caught" ); }
private static void BoardTest7() { Debug.WriteLine( "Testing generate interface..." ); // There's no way to exhaustively test this interface in a repeatable // way since the SudokuEngine uses random number generation. The best // we can do is exercise the interface a statistically-relevant number // of times and hope that any bugs show themselves in the sampling. for ( int i = 0; i < 10; ++i ) { SudokuEngine.Board board = new SudokuEngine.Board(); board.generate(); Debug.WriteLine( "Generated board:" ); Debug.WriteLine( board ); board.solve(); Debug.WriteLine( "Solved board:" ); Debug.WriteLine( board ); verifyBoard( board ); } }
private static void BoardTest5() { Debug.WriteLine( "Testing operators..." ); SudokuEngine.Board board1 = new SudokuEngine.Board(); SudokuEngine.Board board2 = new SudokuEngine.Board(); Debug.Assert( board1 == board2, "FAILURE: operator ==" ); Debug.Assert( !( board1 == board2 ) == ( board1 != board2 ), "FAILURE: operator ==" ); }
private static void BoardTest4() { Debug.WriteLine( "Testing Accessors..." ); int dim = 6; int subRows = 2; int subColumns = 2; SudokuEngine.Board board = new SudokuEngine.Board( dim, subRows, subColumns ); Debug.Assert( board.Rows == dim, "FAILURE: Rows accessor" ); Debug.Assert( board.Columns == dim, "FAILURE: Columns accessor" ); Debug.Assert( board.SubRows == subRows, "FAILURE: SubRows accessor" ); Debug.Assert( board.SubColumns == subColumns, "FAILURE: SubColumns accessor" ); }
public SudokuForm() { InitializeComponent(); gameBoard = new SudokuEngine.Board(); symbol = 0; radioButton1.Checked = true; radioButton_CheckedChanged( radioButton1, new EventArgs() ); // Organize the cells (labels) in a more useful way. // I probably wouldn't have to do this if I built the board // manually. Maybe next time. #region Cell Array Initialization cells = new Label[ 9, 9 ]; cells[ 0, 0 ] = cell0_0; cells[ 0, 1 ] = cell0_1; cells[ 0, 2 ] = cell0_2; cells[ 0, 3 ] = cell0_3; cells[ 0, 4 ] = cell0_4; cells[ 0, 5 ] = cell0_5; cells[ 0, 6 ] = cell0_6; cells[ 0, 7 ] = cell0_7; cells[ 0, 8 ] = cell0_8; cells[ 1, 0 ] = cell1_0; cells[ 1, 1 ] = cell1_1; cells[ 1, 2 ] = cell1_2; cells[ 1, 3 ] = cell1_3; cells[ 1, 4 ] = cell1_4; cells[ 1, 5 ] = cell1_5; cells[ 1, 6 ] = cell1_6; cells[ 1, 7 ] = cell1_7; cells[ 1, 8 ] = cell1_8; cells[ 2, 0 ] = cell2_0; cells[ 2, 1 ] = cell2_1; cells[ 2, 2 ] = cell2_2; cells[ 2, 3 ] = cell2_3; cells[ 2, 4 ] = cell2_4; cells[ 2, 5 ] = cell2_5; cells[ 2, 6 ] = cell2_6; cells[ 2, 7 ] = cell2_7; cells[ 2, 8 ] = cell2_8; cells[ 3, 0 ] = cell3_0; cells[ 3, 1 ] = cell3_1; cells[ 3, 2 ] = cell3_2; cells[ 3, 3 ] = cell3_3; cells[ 3, 4 ] = cell3_4; cells[ 3, 5 ] = cell3_5; cells[ 3, 6 ] = cell3_6; cells[ 3, 7 ] = cell3_7; cells[ 3, 8 ] = cell3_8; cells[ 4, 0 ] = cell4_0; cells[ 4, 1 ] = cell4_1; cells[ 4, 2 ] = cell4_2; cells[ 4, 3 ] = cell4_3; cells[ 4, 4 ] = cell4_4; cells[ 4, 5 ] = cell4_5; cells[ 4, 6 ] = cell4_6; cells[ 4, 7 ] = cell4_7; cells[ 4, 8 ] = cell4_8; cells[ 5, 0 ] = cell5_0; cells[ 5, 1 ] = cell5_1; cells[ 5, 2 ] = cell5_2; cells[ 5, 3 ] = cell5_3; cells[ 5, 4 ] = cell5_4; cells[ 5, 5 ] = cell5_5; cells[ 5, 6 ] = cell5_6; cells[ 5, 7 ] = cell5_7; cells[ 5, 8 ] = cell5_8; cells[ 6, 0 ] = cell6_0; cells[ 6, 1 ] = cell6_1; cells[ 6, 2 ] = cell6_2; cells[ 6, 3 ] = cell6_3; cells[ 6, 4 ] = cell6_4; cells[ 6, 5 ] = cell6_5; cells[ 6, 6 ] = cell6_6; cells[ 6, 7 ] = cell6_7; cells[ 6, 8 ] = cell6_8; cells[ 7, 0 ] = cell7_0; cells[ 7, 1 ] = cell7_1; cells[ 7, 2 ] = cell7_2; cells[ 7, 3 ] = cell7_3; cells[ 7, 4 ] = cell7_4; cells[ 7, 5 ] = cell7_5; cells[ 7, 6 ] = cell7_6; cells[ 7, 7 ] = cell7_7; cells[ 7, 8 ] = cell7_8; cells[ 8, 0 ] = cell8_0; cells[ 8, 1 ] = cell8_1; cells[ 8, 2 ] = cell8_2; cells[ 8, 3 ] = cell8_3; cells[ 8, 4 ] = cell8_4; cells[ 8, 5 ] = cell8_5; cells[ 8, 6 ] = cell8_6; cells[ 8, 7 ] = cell8_7; cells[ 8, 8 ] = cell8_8; #endregion activeCellRow = activeCellColumn = 0; activeColor = Color.LightGray; inactiveColor = cells[ activeCellRow, activeCellColumn ].BackColor; cells[ activeCellRow, activeCellColumn ].BackColor = activeColor; // Generate a new game right off the bat. newToolStripMenuItem_Click( newToolStripMenuItem, new EventArgs() ); }
/// <summary> /// Show the solved board for the current puzzle. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void solveToolStripMenuItem_Click( object sender, EventArgs e ) { this.Cursor = Cursors.WaitCursor; // Copy the solution. gameBoard = solutionBoard.deepCopy(); // Display the solution. for ( int row = 0; row < gameBoard.Rows; ++row ) { for ( int col = 0; col < gameBoard.Columns; ++col ) { updateCellDisplay( row, col ); } } this.Cursor = Cursors.Default; }
/// <summary> /// Make a new board. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void newToolStripMenuItem_Click( object sender, EventArgs e ) { this.Cursor = Cursors.WaitCursor; // Gneerate a new board. gameBoard.generate(); solutionBoard = gameBoard.deepCopy(); solutionBoard.solve(); // Display the new board. for ( int row = 0; row < gameBoard.Rows; ++row ) { for ( int col = 0; col < gameBoard.Columns; ++col ) { if ( gameBoard[ row, col ].Entry != 0 ) gameBoard[ row, col ].Locked = true; else gameBoard[ row, col ].Locked = false; updateCellDisplay( row, col ); } } this.Cursor = Cursors.Default; }