using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections; public partial class _Default : System.Web.UI.Page { private com.harbormist.XudokuService xudokuService; private int[][] problem; private int[][] solution; private bool[][] readOnly; protected void refreshSolveButton() { this.solveButton.Enabled = problem != null; } private void fillProblemGrid(int size) { for (int i = 0; i < size; i++) { TableRow bigRow = new TableRow(); bigRow.BorderStyle = BorderStyle.Solid; bigRow.BorderWidth = 2; for (int j = 0; j < size; j++) { TableCell bigCell = new TableCell(); Table smallTable = new Table(); smallTable.GridLines = GridLines.Both; smallTable.BorderStyle = BorderStyle.Solid; smallTable.BorderWidth = 1; for (int k = 0; k < size; k++) { TableRow smallRow = new TableRow(); for (int m = 0; m < size; m++) { TableCell smallCell = new TableCell(); int row = (size * i) + k; int column = (size * j) + m; smallCell.Text = problem[row][column] == 0 ? " " : problem[row][column].ToString(); smallCell.Height = 25; smallCell.Width = 25; smallCell.HorizontalAlign = HorizontalAlign.Center; smallCell.VerticalAlign = VerticalAlign.Middle; smallRow.Cells.Add(smallCell); } smallTable.Rows.Add(smallRow); } bigCell.Controls.Add(smallTable); bigRow.Cells.Add(bigCell); } problemGrid.Rows.Add(bigRow); } } private void fillSolutionGrid(int size) { for (int i = 0; i < size; i++) { TableRow bigRow = new TableRow(); bigRow.BorderStyle = BorderStyle.Solid; bigRow.BorderWidth = 2; for (int j = 0; j < size; j++) { TableCell bigCell = new TableCell(); Table smallTable = new Table(); smallTable.GridLines = GridLines.Both; smallTable.BorderStyle = BorderStyle.Solid; smallTable.BorderWidth = 1; for (int k = 0; k < size; k++) { TableRow smallRow = new TableRow(); for (int m = 0; m < size; m++) { TableCell smallCell = new TableCell(); int row = (size * i) + k; int column = (size * j) + m; smallCell.Text = solution[row][column].ToString(); smallCell.Height = 25; smallCell.Width = 25; smallCell.HorizontalAlign = HorizontalAlign.Center; smallCell.VerticalAlign = VerticalAlign.Middle; smallCell.Font.Bold = readOnly[row][column]; smallRow.Cells.Add(smallCell); } smallTable.Rows.Add(smallRow); } bigCell.Controls.Add(smallTable); bigRow.Cells.Add(bigCell); } solutionGrid.Rows.Add(bigRow); } } private void fixQuestion() { readOnly = new bool[problem.Length][]; for (int i = 0; i < problem.Length; i++) { readOnly[i] = new bool[problem[i].Length]; for (int j = 0; j < problem.Length; j++) { readOnly[i][j] = problem[i][j] != 0; } } Session["ReadOnly"] = readOnly; } protected void Page_Load(object sender, EventArgs e) { xudokuService = new com.harbormist.XudokuService(); refreshSolveButton(); } protected void createButton_Click(object sender, EventArgs e) { Session["SizeIndex"] = sizeBox.SelectedIndex; int size = Int32.Parse(sizeBox.SelectedValue); try { problem = xudokuService.generateXudoku(size); Session["Problem"] = problem; fixQuestion(); } catch (Exception ex) { this.statusLabel.Text = ex.Message + "\n" + ex.StackTrace; return; } fillProblemGrid(size); refreshSolveButton(); sizeBox.Enabled = false; } protected void solveButton_Click(object sender, EventArgs e) { sizeBox.SelectedIndex = (int)Session["SizeIndex"]; int size = Int32.Parse(sizeBox.SelectedValue); problem = (int[][])Session["Problem"]; readOnly = (bool[][])Session["ReadOnly"]; try { solution = xudokuService.solveXudoku(problem); Session["Solution"] = solution; } catch (Exception ex) { this.statusLabel.Text = ex.Message + "\n" + ex.StackTrace; return; } fillProblemGrid(size); fillSolutionGrid(size); } protected void clearButton_Click(object sender, EventArgs e) { this.problem = null; this.solution = null; this.readOnly = null; this.problemGrid.Rows.Clear(); this.solutionGrid.Rows.Clear(); Session.Clear(); refreshSolveButton(); sizeBox.Enabled = true; } protected void sizeBox_SelectedIndexChanged(object sender, EventArgs e) { clearButton_Click(sender, e); Session["SizeIndex"] = sizeBox.SelectedIndex; } }