From 9268c19fd8416009f05442233c9dc23d11cc56ff Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Fri, 26 Oct 2018 00:43:54 -0500 Subject: [PATCH] format code --- About.cs | 206 +++++------- HowToPlay.cs | 195 ++++++------ SudokuGrid.cs | 810 ++++++++++++++++++------------------------------ SudokuReader.cs | 138 +++------ 4 files changed, 523 insertions(+), 826 deletions(-) diff --git a/About.cs b/About.cs index 2ac48ad..f139a46 100644 --- a/About.cs +++ b/About.cs @@ -4,13 +4,11 @@ using System.ComponentModel; using System.Windows.Forms; -namespace Sudoku -{ +namespace Sudoku { /// /// Summary description for About. /// - public class About : System.Windows.Forms.Form - { + public class About : System.Windows.Forms.Form { #region private stuff private System.Windows.Forms.Button btnOK; @@ -20,8 +18,7 @@ public class About : System.Windows.Forms.Form private System.ComponentModel.Container components = null; #endregion #region public About() - public About() - { + public About() { // // Required for Windows Form Designer support // @@ -35,16 +32,13 @@ public About() /// /// Clean up any resources being used. /// - protected override void Dispose( bool disposing ) - { - if( disposing ) - { - if(components != null) - { + protected override void Dispose(bool disposing) { + if (disposing) { + if (components != null) { components.Dispose(); } } - base.Dispose( disposing ); + base.Dispose(disposing); } #endregion #region Windows Form Designer generated code @@ -52,38 +46,37 @@ protected override void Dispose( bool disposing ) /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// - private void InitializeComponent() - { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(About)); - this.btnOK = new System.Windows.Forms.Button(); - this.SuspendLayout(); - // - // btnOK - // - this.btnOK.BackColor = System.Drawing.Color.Black; - this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; - this.btnOK.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.btnOK.ForeColor = System.Drawing.Color.LightGreen; - this.btnOK.Location = new System.Drawing.Point(43, 133); - this.btnOK.Name = "btnOK"; - this.btnOK.Size = new System.Drawing.Size(75, 23); - this.btnOK.TabIndex = 3; - this.btnOK.Text = "OK"; - this.btnOK.UseVisualStyleBackColor = false; - // - // About - // - this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); - this.BackColor = System.Drawing.Color.Gold; - this.ClientSize = new System.Drawing.Size(162, 162); - this.Controls.Add(this.btnOK); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; - this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); - this.Name = "About"; - this.ShowInTaskbar = false; - this.Text = "Sudoku v4.1"; - this.Paint += new System.Windows.Forms.PaintEventHandler(this.About_Paint); - this.ResumeLayout(false); + private void InitializeComponent() { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(About)); + this.btnOK = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // btnOK + // + this.btnOK.BackColor = System.Drawing.Color.Black; + this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; + this.btnOK.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnOK.ForeColor = System.Drawing.Color.LightGreen; + this.btnOK.Location = new System.Drawing.Point(43, 133); + this.btnOK.Name = "btnOK"; + this.btnOK.Size = new System.Drawing.Size(75, 23); + this.btnOK.TabIndex = 3; + this.btnOK.Text = "OK"; + this.btnOK.UseVisualStyleBackColor = false; + // + // About + // + this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); + this.BackColor = System.Drawing.Color.Gold; + this.ClientSize = new System.Drawing.Size(162, 162); + this.Controls.Add(this.btnOK); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; + this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.Name = "About"; + this.ShowInTaskbar = false; + this.Text = "Sudoku v4.1"; + this.Paint += new System.Windows.Forms.PaintEventHandler(this.About_Paint); + this.ResumeLayout(false); } #endregion @@ -91,26 +84,22 @@ private void InitializeComponent() int count = 0; int _left = 28; int _top = 35; - int[,] _grid = new int[9,9]; + int[,] _grid = new int[9, 9]; SudokuGrid grid = new SudokuGrid(); - int [,,] gridHints = new int[9,9,9]; + int[,,] gridHints = new int[9, 9, 9]; Pen _penThick = new Pen(Color.Black, 3); - Rectangle[,] _boardArray = new Rectangle[9,9]; + Rectangle[,] _boardArray = new Rectangle[9, 9]; Font _sudukoFont = new Font("Arial", 10, FontStyle.Regular); Font _hintsFont = new Font("Small Fonts", 4); - Rectangle _board = new Rectangle(0,0, 0, 0); + Rectangle _board = new Rectangle(0, 0, 0, 0); #endregion #region void DrawSudokuGrid(Graphics g) - void DrawSudokuGrid(Graphics g) - { - if (count == 0) - { + void DrawSudokuGrid(Graphics g) { + if (count == 0) { _grid = grid.GenerateGrid(); int r = 0; - for (int row = 0; row < 5; row += 2) - { - for (int col = 0; col < 4; col += 2) - { + for (int row = 0; row < 5; row += 2) { + for (int col = 0; col < 4; col += 2) { if (r == 0) r = new Random().Next(); if (r % 2 == 1) @@ -118,10 +107,8 @@ void DrawSudokuGrid(Graphics g) r /= 2; } } - for (int row = 1; row < 5; row += 2) - { - for (int col = 1; col < 4; col += 2) - { + for (int row = 1; row < 5; row += 2) { + for (int col = 1; col < 4; col += 2) { if (r == 0) r = new Random().Next(); if (r % 2 == 1) @@ -129,10 +116,8 @@ void DrawSudokuGrid(Graphics g) r /= 2; } } - for (int row = 0; row < 5; row += 2) - { - for (int col = 1; col < 4; col += 2) - { + for (int row = 0; row < 5; row += 2) { + for (int col = 1; col < 4; col += 2) { if (r == 0) r = new Random().Next(); if (r % 2 == 1) @@ -140,10 +125,8 @@ void DrawSudokuGrid(Graphics g) r /= 2; } } - for (int row = 1; row < 5; row += 2) - { - for (int col = 0; col < 4; col += 2) - { + for (int row = 1; row < 5; row += 2) { + for (int col = 0; col < 4; col += 2) { if (r == 0) r = new Random().Next(); if (r % 2 == 1) @@ -153,12 +136,9 @@ void DrawSudokuGrid(Graphics g) } if (r % 2 == 0) _grid[4, 4] = 0; - for (int row = 0; row < 5; row++) - { - for (int col = 0; col < 4; col++) - { - if (_grid[row, col] == 0) - { + for (int row = 0; row < 5; row++) { + for (int col = 0; col < 4; col++) { + if (_grid[row, col] == 0) { _grid[8 - row, 8 - col] = 0; _grid[8 - col, row] = 0; _grid[col, 8 - row] = 0; @@ -171,14 +151,12 @@ void DrawSudokuGrid(Graphics g) // draw square g.DrawRectangle(_penThick, _board); - int spacingX = _board.Width/9; - int spacingY = _board.Height/9; + int spacingX = _board.Width / 9; + int spacingY = _board.Height / 9; - for (int col = 0; col < 9; col++) - { - for (int row = 0; row < 9; row++) - { - _boardArray[row, col] = new Rectangle(_board.Left + col*spacingX, _board.Top + row*spacingY, spacingX, spacingY); + for (int col = 0; col < 9; col++) { + for (int row = 0; row < 9; row++) { + _boardArray[row, col] = new Rectangle(_board.Left + col * spacingX, _board.Top + row * spacingY, spacingX, spacingY); } } g.FillRectangle(Brushes.Yellow, _boardArray[0, 3]); @@ -234,81 +212,65 @@ void DrawSudokuGrid(Graphics g) g.FillRectangle(Brushes.Yellow, _boardArray[8, 7]); g.FillRectangle(Brushes.Yellow, _boardArray[8, 8]); - for (int i = 0; i < 10; i++) - { - if (i % 3 == 0) - { - g.DrawLine(_penThick, _board.Left, _board.Top + spacingY * i, _board.Right, _board.Top + spacingY*i); + for (int i = 0; i < 10; i++) { + if (i % 3 == 0) { + g.DrawLine(_penThick, _board.Left, _board.Top + spacingY * i, _board.Right, _board.Top + spacingY * i); g.DrawLine(_penThick, _board.Left + spacingX * i, _board.Top, _board.Left + spacingX * i, _board.Bottom); - } - else - { - g.DrawLine(Pens.Black, _board.Left, _board.Top + spacingY * i, _board.Right, _board.Top + spacingY*i); + } else { + g.DrawLine(Pens.Black, _board.Left, _board.Top + spacingY * i, _board.Right, _board.Top + spacingY * i); g.DrawLine(Pens.Black, _board.Left + spacingX * i, _board.Top, _board.Left + spacingX * i, _board.Bottom); } } - for (int col = 0; col < 9; col++) - { - for (int row = 0; row < 9; row++) - { + for (int col = 0; col < 9; col++) { + for (int row = 0; row < 9; row++) { int val = _grid[row, col]; - if (val != 0) - { - g.DrawString(val.ToString(), _sudukoFont, Brushes.Black, _board.Left + col*spacingX + 4, _board.Top + row*spacingY + 1, new StringFormat()); - } - else - { + if (val != 0) { + g.DrawString(val.ToString(), _sudukoFont, Brushes.Black, _board.Left + col * spacingX + 4, _board.Top + row * spacingY + 1, new StringFormat()); + } else { DrawGridHints(g, row, col); } - } + } } } #endregion #region void DrawGridHints(Graphics g, int row, int col) - void DrawGridHints(Graphics g, int row, int col) - { - int spacingX = _board.Width/9; - int spacingY = _board.Height/9; + void DrawGridHints(Graphics g, int row, int col) { + int spacingX = _board.Width / 9; + int spacingY = _board.Height / 9; int length; string hints = ""; string hintss = ""; string hintsss = ""; - for (int i = 0; i < 9; i++) - { + for (int i = 0; i < 9; i++) { if (gridHints[row, col, i] != 0) hints = hints + gridHints[row, col, i].ToString() + ","; } - if (hints.Length > 0) - { - hints = hints.Remove(hints.Length - 1, 1); + if (hints.Length > 0) { + hints = hints.Remove(hints.Length - 1, 1); } System.Drawing.Brush brush = Brushes.Maroon; - if (hints.Length == 1) - { + if (hints.Length == 1) { brush = Brushes.Blue; } - if (hints.Length > 8) - { + if (hints.Length > 8) { length = hints.Length - 8; hintss = hints.Remove(0, 8); hints = hints.Remove(8, length); - if (hintss.Length > 8) - { + if (hintss.Length > 8) { hintsss = hintss.Remove(0, 8); hintss = hintss.Remove(8, 1); - g.DrawString(hintsss.ToString(), _hintsFont, brush, _board.Left + col*spacingX + 1, _board.Top + row*spacingY + 14, new StringFormat()); + g.DrawString(hintsss.ToString(), _hintsFont, brush, _board.Left + col * spacingX + 1, _board.Top + row * spacingY + 14, new StringFormat()); } - g.DrawString(hintss.ToString(), _hintsFont, brush, _board.Left + col*spacingX + 1, _board.Top + row*spacingY + 8, new StringFormat()); + g.DrawString(hintss.ToString(), _hintsFont, brush, _board.Left + col * spacingX + 1, _board.Top + row * spacingY + 8, new StringFormat()); } - g.DrawString(hints.ToString(), _hintsFont, brush, _board.Left + col*spacingX + 1, _board.Top + row*spacingY + 2, new StringFormat()); + g.DrawString(hints.ToString(), _hintsFont, brush, _board.Left + col * spacingX + 1, _board.Top + row * spacingY + 2, new StringFormat()); } #endregion #region private void About_Paint(object sender, System.Windows.Forms.PaintEventArgs e) - private void About_Paint(object sender, System.Windows.Forms.PaintEventArgs e) - { + private void About_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { String S1 = "by Tony Brix"; String S2 = "UziTech@gmail.com"; String S3 = "Special Thanks to"; diff --git a/HowToPlay.cs b/HowToPlay.cs index 5c5cbe9..e316e5e 100644 --- a/HowToPlay.cs +++ b/HowToPlay.cs @@ -4,13 +4,11 @@ using System.ComponentModel; using System.Windows.Forms; -namespace Sudoku -{ +namespace Sudoku { /// /// Summary description for HowToPlay. /// - public class HowToPlay : System.Windows.Forms.Form - { + public class HowToPlay : System.Windows.Forms.Form { #region private stuff private System.Windows.Forms.Label HowTo; private System.Windows.Forms.Label KeysHowTo; @@ -24,8 +22,7 @@ public class HowToPlay : System.Windows.Forms.Form private System.ComponentModel.Container components = null; #endregion #region public HowToPlay() - public HowToPlay() - { + public HowToPlay() { // // Required for Windows Form Designer support // @@ -40,16 +37,13 @@ public HowToPlay() /// /// Clean up any resources being used. /// - protected override void Dispose( bool disposing ) - { - if( disposing ) - { - if(components != null) - { + protected override void Dispose(bool disposing) { + if (disposing) { + if (components != null) { components.Dispose(); } } - base.Dispose( disposing ); + base.Dispose(disposing); } #endregion #region Windows Form Designer generated code @@ -57,94 +51,93 @@ protected override void Dispose( bool disposing ) /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// - private void InitializeComponent() - { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(HowToPlay)); - this.HowTo = new System.Windows.Forms.Label(); - this.KeysHowTo = new System.Windows.Forms.Label(); - this.HLColorHowTo = new System.Windows.Forms.Label(); - this.HighLightColorHowTo = new System.Windows.Forms.Label(); - this.OkBtn = new System.Windows.Forms.Button(); - this.label1 = new System.Windows.Forms.Label(); - this.SuspendLayout(); - // - // HowTo - // - this.HowTo.ForeColor = System.Drawing.Color.Red; - this.HowTo.Location = new System.Drawing.Point(8, 8); - this.HowTo.Name = "HowTo"; - this.HowTo.RightToLeft = System.Windows.Forms.RightToLeft.No; - this.HowTo.Size = new System.Drawing.Size(168, 48); - this.HowTo.TabIndex = 0; - this.HowTo.Text = " Enter numbers 1 - 9 in each row, column and 3x3 square."; - // - // KeysHowTo - // - this.KeysHowTo.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); - this.KeysHowTo.Location = new System.Drawing.Point(8, 56); - this.KeysHowTo.Name = "KeysHowTo"; - this.KeysHowTo.Size = new System.Drawing.Size(168, 80); - this.KeysHowTo.TabIndex = 1; - this.KeysHowTo.Text = " Select a cell by clicking on it with your mouse or moving to it with your arro" + - "w keys."; - // - // HLColorHowTo - // - this.HLColorHowTo.ForeColor = System.Drawing.Color.Yellow; - this.HLColorHowTo.Location = new System.Drawing.Point(8, 120); - this.HLColorHowTo.Name = "HLColorHowTo"; - this.HLColorHowTo.Size = new System.Drawing.Size(168, 32); - this.HLColorHowTo.TabIndex = 2; - this.HLColorHowTo.Text = " Space Bar clears the number."; - // - // HighLightColorHowTo - // - this.HighLightColorHowTo.ForeColor = System.Drawing.Color.Lime; - this.HighLightColorHowTo.Location = new System.Drawing.Point(8, 152); - this.HighLightColorHowTo.Name = "HighLightColorHowTo"; - this.HighLightColorHowTo.Size = new System.Drawing.Size(168, 48); - this.HighLightColorHowTo.TabIndex = 3; - this.HighLightColorHowTo.Text = " If the cell is highlighted with yellow that means the number is wrong."; - // - // OkBtn - // - this.OkBtn.BackColor = System.Drawing.SystemColors.HotTrack; - this.OkBtn.DialogResult = System.Windows.Forms.DialogResult.OK; - this.OkBtn.ForeColor = System.Drawing.Color.Purple; - this.OkBtn.Location = new System.Drawing.Point(64, 248); - this.OkBtn.Name = "OkBtn"; - this.OkBtn.Size = new System.Drawing.Size(48, 23); - this.OkBtn.TabIndex = 0; - this.OkBtn.Text = "Ok"; - this.OkBtn.UseVisualStyleBackColor = false; - // - // label1 - // - this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); - this.label1.Location = new System.Drawing.Point(8, 200); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(168, 48); - this.label1.TabIndex = 4; - this.label1.Text = "Use template mode to create your own SudoKu puzzle."; - // - // HowToPlay - // - this.AutoScaleBaseSize = new System.Drawing.Size(7, 15); - this.BackColor = System.Drawing.Color.Navy; - this.ClientSize = new System.Drawing.Size(184, 280); - this.Controls.Add(this.label1); - this.Controls.Add(this.OkBtn); - this.Controls.Add(this.HighLightColorHowTo); - this.Controls.Add(this.HLColorHowTo); - this.Controls.Add(this.KeysHowTo); - this.Controls.Add(this.HowTo); - this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; - this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); - this.Name = "HowToPlay"; - this.ShowInTaskbar = false; - this.Text = "How To Play"; - this.ResumeLayout(false); + private void InitializeComponent() { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(HowToPlay)); + this.HowTo = new System.Windows.Forms.Label(); + this.KeysHowTo = new System.Windows.Forms.Label(); + this.HLColorHowTo = new System.Windows.Forms.Label(); + this.HighLightColorHowTo = new System.Windows.Forms.Label(); + this.OkBtn = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // HowTo + // + this.HowTo.ForeColor = System.Drawing.Color.Red; + this.HowTo.Location = new System.Drawing.Point(8, 8); + this.HowTo.Name = "HowTo"; + this.HowTo.RightToLeft = System.Windows.Forms.RightToLeft.No; + this.HowTo.Size = new System.Drawing.Size(168, 48); + this.HowTo.TabIndex = 0; + this.HowTo.Text = " Enter numbers 1 - 9 in each row, column and 3x3 square."; + // + // KeysHowTo + // + this.KeysHowTo.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); + this.KeysHowTo.Location = new System.Drawing.Point(8, 56); + this.KeysHowTo.Name = "KeysHowTo"; + this.KeysHowTo.Size = new System.Drawing.Size(168, 80); + this.KeysHowTo.TabIndex = 1; + this.KeysHowTo.Text = " Select a cell by clicking on it with your mouse or moving to it with your arro" + + "w keys."; + // + // HLColorHowTo + // + this.HLColorHowTo.ForeColor = System.Drawing.Color.Yellow; + this.HLColorHowTo.Location = new System.Drawing.Point(8, 120); + this.HLColorHowTo.Name = "HLColorHowTo"; + this.HLColorHowTo.Size = new System.Drawing.Size(168, 32); + this.HLColorHowTo.TabIndex = 2; + this.HLColorHowTo.Text = " Space Bar clears the number."; + // + // HighLightColorHowTo + // + this.HighLightColorHowTo.ForeColor = System.Drawing.Color.Lime; + this.HighLightColorHowTo.Location = new System.Drawing.Point(8, 152); + this.HighLightColorHowTo.Name = "HighLightColorHowTo"; + this.HighLightColorHowTo.Size = new System.Drawing.Size(168, 48); + this.HighLightColorHowTo.TabIndex = 3; + this.HighLightColorHowTo.Text = " If the cell is highlighted with yellow that means the number is wrong."; + // + // OkBtn + // + this.OkBtn.BackColor = System.Drawing.SystemColors.HotTrack; + this.OkBtn.DialogResult = System.Windows.Forms.DialogResult.OK; + this.OkBtn.ForeColor = System.Drawing.Color.Purple; + this.OkBtn.Location = new System.Drawing.Point(64, 248); + this.OkBtn.Name = "OkBtn"; + this.OkBtn.Size = new System.Drawing.Size(48, 23); + this.OkBtn.TabIndex = 0; + this.OkBtn.Text = "Ok"; + this.OkBtn.UseVisualStyleBackColor = false; + // + // label1 + // + this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); + this.label1.Location = new System.Drawing.Point(8, 200); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(168, 48); + this.label1.TabIndex = 4; + this.label1.Text = "Use template mode to create your own SudoKu puzzle."; + // + // HowToPlay + // + this.AutoScaleBaseSize = new System.Drawing.Size(7, 15); + this.BackColor = System.Drawing.Color.Navy; + this.ClientSize = new System.Drawing.Size(184, 280); + this.Controls.Add(this.label1); + this.Controls.Add(this.OkBtn); + this.Controls.Add(this.HighLightColorHowTo); + this.Controls.Add(this.HLColorHowTo); + this.Controls.Add(this.KeysHowTo); + this.Controls.Add(this.HowTo); + this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; + this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.Name = "HowToPlay"; + this.ShowInTaskbar = false; + this.Text = "How To Play"; + this.ResumeLayout(false); } #endregion diff --git a/SudokuGrid.cs b/SudokuGrid.cs index 3e638bd..3e801c9 100644 --- a/SudokuGrid.cs +++ b/SudokuGrid.cs @@ -2,13 +2,11 @@ using System.Xml; using System.Xml.XPath; -namespace Sudoku -{ +namespace Sudoku { /// /// Summary description for SudokuGrid. /// - public class SudokuGrid - { + public class SudokuGrid { #region Global Variables bool _ACH = false; bool _EH = false; @@ -17,11 +15,11 @@ public class SudokuGrid bool _E = false; bool _M = true; bool _H = false; - int [,,] _gridHints = new int[9,9,9]; // initialize array - int [,,] _solvedHints = new int[9,9,9]; - int [,] _grid = new int[9,9]; // initialize array - int [,] _knownElements = new int[9,9]; // initialize array - int [,] _solvedGrid = new int[9,9]; // initialize array + int[,,] _gridHints = new int[9, 9, 9]; // initialize array + int[,,] _solvedHints = new int[9, 9, 9]; + int[,] _grid = new int[9, 9]; // initialize array + int[,] _knownElements = new int[9, 9]; // initialize array + int[,] _solvedGrid = new int[9, 9]; // initialize array int _Level = 0; bool _2Slns = false; bool _2SlnsShow = false; @@ -30,20 +28,17 @@ public class SudokuGrid XmlNodeList _nodes = null; #endregion #region public void twoSlns(bool twoSlns) - public void twoSlns(bool twoSlns) - { + public void twoSlns(bool twoSlns) { _2Slns = twoSlns; } #endregion #region public void twoSlnsShow(bool twoSlnsShow) - public void twoSlnsShow(bool twoSlnsShow) - { + public void twoSlnsShow(bool twoSlnsShow) { _2SlnsShow = twoSlnsShow; } #endregion #region public SudokuGrid(XmlDocument xdoc) - public SudokuGrid(XmlDocument xdoc) - { + public SudokuGrid(XmlDocument xdoc) { _nodes = xdoc.SelectNodes("//Hints/Rows/*"); ParseHintNodes(); @@ -52,41 +47,30 @@ public SudokuGrid(XmlDocument xdoc) } #endregion #region public SudokuGrid() - public SudokuGrid() - { + public SudokuGrid() { } #endregion #region public void ParseHintNodes() - public void ParseHintNodes() - { + public void ParseHintNodes() { int row = 0; if (_nodes == null) return; - foreach (XmlNode n in _nodes) - { - if (row == 9) - { - if (n.InnerText != "") - { + foreach (XmlNode n in _nodes) { + if (row == 9) { + if (n.InnerText != "") { CheckBoxes(Convert.ToInt32(n.InnerText)); } - } - else - { - string[] rowValues = n.InnerText.Split(new char[]{','}); + } else { + string[] rowValues = n.InnerText.Split(new char[] { ',' }); int col = 0; - foreach (string num in rowValues) - { - if (num == "-") - { + foreach (string num in rowValues) { + if (num == "-") { _grid[row, col] = 0; - } - else - { + } else { _grid[row, col] = Convert.ToInt32(num); - _knownElements[row,col] = Convert.ToInt32(num); + _knownElements[row, col] = Convert.ToInt32(num); } col++; @@ -97,33 +81,23 @@ public void ParseHintNodes() } #endregion #region public void ParseGuessNodes() - public void ParseGuessNodes() - { + public void ParseGuessNodes() { if (_nodes == null) return; int row = 0; - foreach (XmlNode n in _nodes) - { - if (row == 9) - { - if (n.InnerText != "") - { + foreach (XmlNode n in _nodes) { + if (row == 9) { + if (n.InnerText != "") { CheckBoxes(Convert.ToInt32(n.InnerText));////////////////// } - } - else - { - string[] rowValues = n.InnerText.Split(new char[]{','}); + } else { + string[] rowValues = n.InnerText.Split(new char[] { ',' }); int col = 0; - foreach (string num in rowValues) - { - if (num == "-") - { + foreach (string num in rowValues) { + if (num == "-") { //_grid[row, col] = 0; - } - else - { + } else { _grid[row, col] = Convert.ToInt32(num); } @@ -135,37 +109,31 @@ public void ParseGuessNodes() } #endregion #region public void hard(bool tf) - public void hard(bool tf) - { + public void hard(bool tf) { _hard = tf; } #endregion #region public void easy(bool tf) - public void easy(bool tf) - { + public void easy(bool tf) { _easy = tf; } #endregion #region public void ClearHints() - public void ClearHints() - { + public void ClearHints() { for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) - ClearRCHints(i,j); + ClearRCHints(i, j); } #endregion #region public void ClearRCHints() - public void ClearRCHints(int row, int col) - { + public void ClearRCHints(int row, int col) { for (int i = 0; i < 9; i++) - _gridHints[row,col,i] = 0; + _gridHints[row, col, i] = 0; } #endregion #region public int[,,] GridHints - public int[,,] GridHints - { - get - { + public int[,,] GridHints { + get { return _gridHints; } } @@ -175,8 +143,7 @@ public void ClearRCHints(int row, int col) /// Check to make sure all of the grid is finished /// /// - public bool GridCompleted() - { + public bool GridCompleted() { for (int row = 0; row < 9; row++) for (int col = 0; col < 9; col++) if (this[row, col] == 0) return false; @@ -188,13 +155,10 @@ public bool GridCompleted() /// Check to make sure all of the grid is finished /// /// - public bool CheckAll() - { + public bool CheckAll() { for (int row = 0; row < 9; row++) - for (int col = 0; col < 9; col++) - { - if (CheckCell(row, col) == false) - { + for (int col = 0; col < 9; col++) { + if (CheckCell(row, col) == false) { return false; } } @@ -202,136 +166,107 @@ public bool CheckAll() } #endregion #region public bool CheckCell(int currentrow, int currentcol) - public bool CheckCell(int currentrow, int currentcol) - { + public bool CheckCell(int currentrow, int currentcol) { int val = this[currentrow, currentcol]; // checks current cell - + // precondition, cells of 0 are okay - if (val == 0) - { + if (val == 0) { return true; } - for (int row = 0; row < 9; row++) - { - if (row != currentrow) - { - if (this[row, currentcol] == val) - { + for (int row = 0; row < 9; row++) { + if (row != currentrow) { + if (this[row, currentcol] == val) { return false; } } } - for (int col = 0; col < 9; col++) - { - if (col != currentcol) - { - if (this[currentrow, col] == val) - { + for (int col = 0; col < 9; col++) { + if (col != currentcol) { + if (this[currentrow, col] == val) { return false; } } } - int cornerrow = currentrow/3 * 3; - int cornercol = currentcol/3 * 3; - for (int row = cornerrow; row < cornerrow + 3; row++) - { - for (int col = cornercol; col < cornercol + 3; col++) - { - if (col != currentcol || row != currentrow) - { - if (this[row, col] == val) - { + int cornerrow = currentrow / 3 * 3; + int cornercol = currentcol / 3 * 3; + for (int row = cornerrow; row < cornerrow + 3; row++) { + for (int col = cornercol; col < cornercol + 3; col++) { + if (col != currentcol || row != currentrow) { + if (this[row, col] == val) { return false; } } } } - return true; + return true; } #endregion #region public void Generate() - public void Generate() - { - int [,] S = new int [9,9]; + public void Generate() { + int[,] S = new int[9, 9]; S = GenerateGrid(); int r = new Random((int)DateTime.Now.Ticks).Next(); - zero: - for (int row = 0; row < 9; row++) - for (int col = 0; col < 9; col++) - _knownElements[row, col] = S[row, col]; - for (int row = 0; row < 5; row += 2) - { - for (int col = 0; col < 4; col += 2) - { + zero: + for (int row = 0; row < 9; row++) + for (int col = 0; col < 9; col++) + _knownElements[row, col] = S[row, col]; + for (int row = 0; row < 5; row += 2) { + for (int col = 0; col < 4; col += 2) { if (r % 2 == 1) _knownElements[row, col] = 0; r /= 2; if (r == 0) - r = new Random((int)DateTime.Now.Ticks).Next(); + r = new Random((int)DateTime.Now.Ticks).Next(); } } - for (int row = 1; row < 5; row += 2) - { - for (int col = 1; col < 4; col += 2) - { + for (int row = 1; row < 5; row += 2) { + for (int col = 1; col < 4; col += 2) { if (r % 2 == 1) _knownElements[row, col] = 0; r /= 2; if (r == 0) - r = new Random((int)DateTime.Now.Ticks).Next(); + r = new Random((int)DateTime.Now.Ticks).Next(); } } - for (int row = 0; row < 5; row += 2) - { - for (int col = 1; col < 4; col += 2) - { + for (int row = 0; row < 5; row += 2) { + for (int col = 1; col < 4; col += 2) { if (r % 2 == 1) _knownElements[row, col] = 0; r /= 2; if (r == 0) - r = new Random((int)DateTime.Now.Ticks).Next(); + r = new Random((int)DateTime.Now.Ticks).Next(); } } - for (int row = 1; row < 5; row += 2) - { - for (int col = 0; col < 4; col += 2) - { + for (int row = 1; row < 5; row += 2) { + for (int col = 0; col < 4; col += 2) { if (r % 2 == 1) _knownElements[row, col] = 0; r /= 2; if (r == 0) - r = new Random((int)DateTime.Now.Ticks).Next(); ; + r = new Random((int)DateTime.Now.Ticks).Next(); ; } } if (r % 2 == 0) _knownElements[4, 4] = 0; int solved = 0; int count = 0; - do - { + do { count++; - for (int row = 0; row < 5; row++) - { - for (int col = 0; col < 5; col++) - { - if (_knownElements[row, col] == 0 && solved == 0) - { - if (count > 1) - { - if (_solvedGrid[row, col] == 0) - { + for (int row = 0; row < 5; row++) { + for (int col = 0; col < 5; col++) { + if (_knownElements[row, col] == 0 && solved == 0) { + if (count > 1) { + if (_solvedGrid[row, col] == 0) { _knownElements[row, col] = S[row, col]; _knownElements[8 - row, 8 - col] = S[8 - row, 8 - col]; _knownElements[8 - col, row] = S[8 - col, row]; _knownElements[col, 8 - row] = S[col, 8 - row]; solved = 1; } - } - else - { + } else { _knownElements[8 - row, 8 - col] = 0; _knownElements[8 - col, row] = 0; _knownElements[col, 8 - row] = 0; @@ -339,94 +274,83 @@ public void Generate() } } } - if (count > 1 && solved == 0) - { + if (count > 1 && solved == 0) { Generate(); return; } solved = 0; - }while ((_hard?false:!Solve(true, true, true))); + } while ((_hard ? false : !Solve(true, true, true))); int zeros = 0; - for (int row = 0; row < 9; row++) - { - for (int col = 0; col < 9; col++) - { + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { _grid[row, col] = _knownElements[row, col]; if (_grid[row, col] == 0) zeros++; } } - if (_easy) - { + if (_easy) { if (zeros > 30) goto zero; - } - else if (zeros < 30) + } else if (zeros < 30) goto zero; } #endregion #region public int[,] GenerateGrid() - public int[,] GenerateGrid() - { - int [,] S = new int [9,9]; - int a,b,c=0,d=0,n=0,f=0,g,h,i=0,j,k,l,p=0; + public int[,] GenerateGrid() { + int[,] S = new int[9, 9]; + int a, b, c = 0, d = 0, n = 0, f = 0, g, h, i = 0, j, k, l, p = 0; + int[] a9 = new int[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + Random r; a = 0; - AA: - b = 0; - BB: - if (a % 3 == 0) - { - c = 0; - d = 2; - } - else if (a % 3 == 1) - { - c = -1; - d = 1; - } - else if (a % 3 == 2) - { - c = -2; - d = 0; - } - if (b % 3 == 0) - { + AA: + b = 0; + BB: + if (a % 3 == 0) { + c = 0; + d = 2; + } else if (a % 3 == 1) { + c = -1; + d = 1; + } else if (a % 3 == 2) { + c = -2; + d = 0; + } + if (b % 3 == 0) { n = 0; f = 2; - } - else if (b % 3 == 1) - { + } else if (b % 3 == 1) { n = -1; f = 1; - } - else if (b % 3 == 2) - { + } else if (b % 3 == 2) { n = -2; f = 0; } - int r = new Random((int)DateTime.Now.Ticks).Next(); - g = r % 9 + 1; - h = (r % 2 == 0? -1 : 1); + r = new Random((int)DateTime.Now.Ticks); + for (i = 8; i >= 0; i--) { + h = r.Next(i); + int temp = a9[i]; + a9[i] = a9[h]; + a9[h] = temp; + } + g = 0; i = 0; - RND: - i++; - g += h; - if (i == 10) - { + RND: + i++; + g += 1; + if (g == 10) + g = 1; + h = a9[g-1]; + if (i == 10) { p++; - if (a > 6 || p == 5000) - { + if (a > 6 || p == 5000) { a = 6; - if (p == 5000) - { + if (p == 5000) { a = 0; p = 0; } - for (int temp = a; temp < 9; temp++) - { - for (int temp1 = 0; temp1 < 9; temp1++) - { - S[temp,temp1] = 0; + for (int temp = a; temp < 9; temp++) { + for (int temp1 = 0; temp1 < 9; temp1++) { + S[temp, temp1] = 0; } } goto AA; @@ -435,19 +359,14 @@ public void Generate() S[a, temp] = 0; goto AA; } - if (g == 10) - g = 1; - if (g == 0) - g = 9; j = c; - JJ: - k = n; - KK: - if (j != 0||k != 0) - { - if (g == S[a + j,b + k]) - goto RND; - } + JJ: + k = n; + KK: + if (j != 0 || k != 0) { + if (h == S[a + j, b + k]) + goto RND; + } k++; if (k < f + 1) goto KK; @@ -455,13 +374,13 @@ public void Generate() if (j < d + 1) goto JJ; l = 0; - LL: - if ((g == S[a,l]&&b != l)||(g == S[l,b]&&a != l)) - goto RND; + LL: + if ((h == S[a, l] && b != l) || (h == S[l, b] && a != l)) + goto RND; l++; if (l < 9) goto LL; - S[a,b] = g; + S[a, b] = h; b++; if (b < 9) goto BB; @@ -472,8 +391,7 @@ public void Generate() } #endregion #region public bool CheckEmpty() - public bool CheckEmpty() - { + public bool CheckEmpty() { for (int row = 0; row < 9; row++) for (int col = 0; col < 9; col++) if (this[row, col] != 0) @@ -482,8 +400,7 @@ public bool CheckEmpty() } #endregion #region public bool CheckKnown() - public bool CheckKnown() - { + public bool CheckKnown() { for (int row = 0; row < 9; row++) for (int col = 0; col < 9; col++) if (_knownElements[row, col] != 0) @@ -492,8 +409,7 @@ public bool CheckKnown() } #endregion #region public void ChangeHints(int row, int col, int num) - public void ChangeHints(int row, int col, int num) - { + public void ChangeHints(int row, int col, int num) { if (_gridHints[row, col, num - 1] != 0) _gridHints[row, col, num - 1] = 0; else @@ -501,39 +417,29 @@ public void ChangeHints(int row, int col, int num) } #endregion #region public int[,,] CalculateHints(int[,] grid) - public int[,,] CalculateHints(int[,] grid) - { - int [,,] gridHints = new int[9,9,9]; - for (int row = 0; row < 9; row++) - { - for (int col = 0; col < 9; col++) - { - if (grid[row, col] == 0) - { + public int[,,] CalculateHints(int[,] grid) { + int[,,] gridHints = new int[9, 9, 9]; + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { + if (grid[row, col] == 0) { // check row column square // check row - int[] possibleValues = new int[]{1,2,3,4,5,6,7,8,9}; - for (int i = 0; i < 9; i++) - { - if (grid[row,i] != 0) - { - possibleValues[grid[row,i] - 1] = 0; + int[] possibleValues = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + for (int i = 0; i < 9; i++) { + if (grid[row, i] != 0) { + possibleValues[grid[row, i] - 1] = 0; } - if (grid[i, col] != 0) - { - possibleValues[grid[i,col] - 1] = 0; + if (grid[i, col] != 0) { + possibleValues[grid[i, col] - 1] = 0; } - int[] quad = new int[] {row / 3 * 3, col / 3 * 3}; - for (int j = quad[0]; j < quad[0]+3; j++) - { - for (int k = quad[1]; k < quad[1]+3; k++) - { - if (grid[j, k] != 0) - { - possibleValues[grid[j,k] - 1] = 0; + int[] quad = new int[] { row / 3 * 3, col / 3 * 3 }; + for (int j = quad[0]; j < quad[0] + 3; j++) { + for (int k = quad[1]; k < quad[1] + 3; k++) { + if (grid[j, k] != 0) { + possibleValues[grid[j, k] - 1] = 0; } } } @@ -541,8 +447,7 @@ public void ChangeHints(int row, int col, int num) // now place in array //int count = 0; - for (int vals = 0; vals < 9; vals++) - { + for (int vals = 0; vals < 9; vals++) { //if (possibleValues[vals] != 0) //{ gridHints[row, col, vals/*count*/] = possibleValues[vals]; @@ -556,40 +461,30 @@ public void ChangeHints(int row, int col, int num) } #endregion #region public void CalculateHints() - public void CalculateHints() - { + public void CalculateHints() { ClearHints(); - for (int row = 0; row < 9; row++) - { - for (int col = 0; col < 9; col++) - { - if (_grid[row, col] == 0) - { + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { + if (_grid[row, col] == 0) { // check row column square // check row - int[] possibleValues = new int[]{1,2,3,4,5,6,7,8,9}; - for (int i = 0; i < 9; i++) - { - if (_grid[row,i] != 0) - { - possibleValues[_grid[row,i] - 1] = 0; + int[] possibleValues = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + for (int i = 0; i < 9; i++) { + if (_grid[row, i] != 0) { + possibleValues[_grid[row, i] - 1] = 0; } - if (_grid[i, col] != 0) - { - possibleValues[_grid[i,col] - 1] = 0; + if (_grid[i, col] != 0) { + possibleValues[_grid[i, col] - 1] = 0; } int[] quad = FindQuadStart(row, col); - for (int j = quad[0]; j < quad[0]+3; j++) - { - for (int k = quad[1]; k < quad[1]+3; k++) - { - if (_grid[j, k] != 0) - { - possibleValues[_grid[j,k] - 1] = 0; + for (int j = quad[0]; j < quad[0] + 3; j++) { + for (int k = quad[1]; k < quad[1] + 3; k++) { + if (_grid[j, k] != 0) { + possibleValues[_grid[j, k] - 1] = 0; } } } @@ -597,8 +492,7 @@ public void CalculateHints() // now place in array //int count = 0; - for (int vals = 0; vals < 9; vals++) - { + for (int vals = 0; vals < 9; vals++) { //if (possibleValues[vals] != 0) //{ _gridHints[row, col, vals/*count*/] = possibleValues[vals]; @@ -611,62 +505,51 @@ public void CalculateHints() } #endregion #region public void Clear() - public void Clear() - { - for (int row = 0; row < 9; row++) - { - for (int col = 0; col < 9; col++) - { + public void Clear() { + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { this[row, col] = 0; } } // Also clear known elements - for (int row = 0; row < 9; row++) - { - for (int col = 0; col < 9; col++) - { + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { _knownElements[row, col] = 0; } } } #endregion #region public int[] FindQuadStart(int row, int col) - public int[] FindQuadStart(int row, int col) - { + public int[] FindQuadStart(int row, int col) { int val1 = row / 3 * 3; int val2 = col / 3 * 3; - return new int[]{val1, val2}; + return new int[] { val1, val2 }; } #endregion #region public void SetSolvedGrid(int row, int col, int val) - public void SetSolvedGrid(int row, int col, int val) - { + public void SetSolvedGrid(int row, int col, int val) { _solvedGrid[row, col] = val; } #endregion #region public void SetKnownElement(int row, int col, int val) - public void SetKnownElement(int row, int col, int val) - { + public void SetKnownElement(int row, int col, int val) { _knownElements[row, col] = val; } #endregion #region public int GetSolvedGrid(int row, int column) - public int GetSolvedGrid(int row, int column) - { + public int GetSolvedGrid(int row, int column) { return _solvedGrid[row, column]; } #endregion #region public int GetKnownElement(int row, int column) - public int GetKnownElement(int row, int column) - { + public int GetKnownElement(int row, int column) { return _knownElements[row, column]; } #endregion #region public bool IsKnownElement(int row, int col) - public bool IsKnownElement(int row, int col) - { + public bool IsKnownElement(int row, int col) { if (_knownElements[row, col] != 0) return true; @@ -674,47 +557,36 @@ public bool IsKnownElement(int row, int col) } #endregion #region public void CalcSolvedHints() - public void CalcSolvedHints() - { + public void CalcSolvedHints() { for (int row = 0; row < 9; row++) for (int col = 0; col < 9; col++) for (int num = 0; num < 9; num++) _solvedHints[row, col, num] = 0; - for (int row = 0; row < 9; row++) - { - for (int col = 0; col < 9; col++) - { - if (_solvedGrid[row, col] == 0) - { - int[] possibleValues = new int[]{1,2,3,4,5,6,7,8,9}; - for (int i = 0; i < 9; i++) - { - if (_solvedGrid[row,i] != 0) - { - possibleValues[_solvedGrid[row,i] - 1] = 0; + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { + if (_solvedGrid[row, col] == 0) { + int[] possibleValues = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + for (int i = 0; i < 9; i++) { + if (_solvedGrid[row, i] != 0) { + possibleValues[_solvedGrid[row, i] - 1] = 0; } - if (_solvedGrid[i, col] != 0) - { - possibleValues[_solvedGrid[i,col] - 1] = 0; + if (_solvedGrid[i, col] != 0) { + possibleValues[_solvedGrid[i, col] - 1] = 0; } int[] quad = FindQuadStart(row, col); - for (int j = quad[0]; j < quad[0]+3; j++) - { - for (int k = quad[1]; k < quad[1]+3; k++) - { - if (_solvedGrid[j, k] != 0) - { - possibleValues[_solvedGrid[j,k] - 1] = 0; + for (int j = quad[0]; j < quad[0] + 3; j++) { + for (int k = quad[1]; k < quad[1] + 3; k++) { + if (_solvedGrid[j, k] != 0) { + possibleValues[_solvedGrid[j, k] - 1] = 0; } } } } - for (int vals = 0; vals < 9; vals++) - { + for (int vals = 0; vals < 9; vals++) { _solvedHints[row, col, vals] = possibleValues[vals]; } } @@ -723,14 +595,11 @@ public void CalcSolvedHints() } #endregion #region public bool Solve(bool solve, bool known, bool gen) - public bool Solve(bool solve, bool known, bool gen) - { + public bool Solve(bool solve, bool known, bool gen) { _Level++; bool gridChange; - if (known) - { - if (!CheckKnown()) - { + if (known) { + if (!CheckKnown()) { for (int row = 0; row < 9; row++) for (int col = 0; col < 9; col++) _knownElements[row, col] = this[row, col]; @@ -740,13 +609,10 @@ public bool Solve(bool solve, bool known, bool gen) _solvedGrid[row, col] = _knownElements[row, col]; } CalcSolvedHints(); - do - { + do { gridChange = false; - for (int row = 0; row < 9; row++) - { - for (int col = 0; col < 9; col++) - { + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { int temp = 0; int tempcol; int temprow; @@ -755,155 +621,111 @@ public bool Solve(bool solve, bool known, bool gen) int crow = row / 3 * 3; int ccol = col / 3 * 3; int tempi = 0; - if (_solvedGrid[row, col] == 0) - { - for (int i = 0; i < 9; i++) - { - if (_solvedHints[row, col, i] == 0) - { + if (_solvedGrid[row, col] == 0) { + for (int i = 0; i < 9; i++) { + if (_solvedHints[row, col, i] == 0) { tempi++; - } - else - { + } else { temprow = 0; tempcol = 0; tempcell = 0; tempone = 0; - for (int j = 0; j < 9; j++) - { + for (int j = 0; j < 9; j++) { if (_solvedHints[row, j, i] != 0) tempcol++; if (_solvedHints[j, col, i] != 0) temprow++; - if (_solvedHints[row, col, j] != 0) - { + if (_solvedHints[row, col, j] != 0) { tempone++; temp = j + 1; } } - for (int k = 0; k < 3; k++) - { - for (int l = 0; l < 3; l++) - { + for (int k = 0; k < 3; k++) { + for (int l = 0; l < 3; l++) { if (_solvedHints[crow + k, ccol + l, i] != 0) tempcell++; } } - if (tempcol == 1) - { + if (tempcol == 1) { _solvedGrid[row, col] = i + 1; gridChange = true; CalcSolvedHints(); - } - else if (temprow == 1) - { + } else if (temprow == 1) { _solvedGrid[row, col] = i + 1; gridChange = true; CalcSolvedHints(); - } - else if (tempcell == 1) - { + } else if (tempcell == 1) { _solvedGrid[row, col] = i + 1; gridChange = true; CalcSolvedHints(); - } - else if (tempone == 1) - { + } else if (tempone == 1) { _solvedGrid[row, col] = temp; gridChange = true; CalcSolvedHints(); } } } - if (tempi == 9) - { - if (!solve) - { - System.Windows.Forms.MessageBox.Show("There is no solution\nto this puzzle.","No Solution",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Exclamation); + if (tempi == 9) { + if (!solve) { + System.Windows.Forms.MessageBox.Show("There is no solution\nto this puzzle.", "No Solution", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); } _Level--; return false; - + } } } } - if (!gridChange) - { - for (int row = 0; row < 9; row++) - { - for (int col = 0; col < 9; col++) - { - if (_solvedGrid[row, col] == 0) - { - if (!gen) - { + if (!gridChange) { + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { + if (_solvedGrid[row, col] == 0) { + if (!gen) { int hints = 0; int num1 = 0; - int num2 = 0; - for (int i = 0; i < 9; i++) - { - if (_solvedHints[row, col, i] != 0) - { + int num2 = 0; + for (int i = 0; i < 9; i++) { + if (_solvedHints[row, col, i] != 0) { hints++; - if (hints == 1) - { + if (hints == 1) { num1 = i + 1; - } - else if (hints == 2) - { + } else if (hints == 2) { num2 = i + 1; } } } - if (hints == 2) - { - int[,] tempg = new int[9,9]; - for (int tempr = 0; tempr < 9; tempr++) - { - for (int tempc = 0; tempc < 9; tempc++) - { + if (hints == 2) { + int[,] tempg = new int[9, 9]; + for (int tempr = 0; tempr < 9; tempr++) { + for (int tempc = 0; tempc < 9; tempc++) { tempg[tempr, tempc] = _solvedGrid[tempr, tempc]; } } _solvedGrid[row, col] = num1; - if (Solve(true, false, false)) - { - for (int tempr = 0; tempr < 9; tempr++) - { - for (int tempc = 0; tempc < 9; tempc++) - { + if (Solve(true, false, false)) { + for (int tempr = 0; tempr < 9; tempr++) { + for (int tempc = 0; tempc < 9; tempc++) { _solvedGrid[tempr, tempc] = tempg[tempr, tempc]; } } _solvedGrid[row, col] = num2; - if (Solve(true, false, false)) - { - if (!_2Slns) - { - if (System.Windows.Forms.MessageBox.Show("There is more than one solution to this puzzle.\n Do you want to see one solution?","Two Solutions",System.Windows.Forms.MessageBoxButtons.YesNo,System.Windows.Forms.MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.Yes) - { + if (Solve(true, false, false)) { + if (!_2Slns) { + if (System.Windows.Forms.MessageBox.Show("There is more than one solution to this puzzle.\n Do you want to see one solution?", "Two Solutions", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.Yes) { _2SlnsShow = true; } _2Slns = true; } - if (!_2SlnsShow) - { - for (int tempr = 0; tempr < 9; tempr++) - { - for (int tempc = 0; tempc < 9; tempc++) - { + if (!_2SlnsShow) { + for (int tempr = 0; tempr < 9; tempr++) { + for (int tempc = 0; tempc < 9; tempc++) { _solvedGrid[tempr, tempc] = tempg[tempr, tempc]; } } } - } - else - { - for (int tempr = 0; tempr < 9; tempr++) - { - for (int tempc = 0; tempc < 9; tempc++) - { + } else { + for (int tempr = 0; tempr < 9; tempr++) { + for (int tempc = 0; tempc < 9; tempc++) { _solvedGrid[tempr, tempc] = tempg[tempr, tempc]; } } @@ -912,43 +734,31 @@ public bool Solve(bool solve, bool known, bool gen) } _Level--; return true; - } - else - { - for (int tempr = 0; tempr < 9; tempr++) - { - for (int tempc = 0; tempc < 9; tempc++) - { + } else { + for (int tempr = 0; tempr < 9; tempr++) { + for (int tempc = 0; tempc < 9; tempc++) { _solvedGrid[tempr, tempc] = tempg[tempr, tempc]; } } _solvedGrid[row, col] = num2; - if (Solve(true, false, false)) - { + if (Solve(true, false, false)) { _Level--; return true; - } - else - { - for (int tempr = 0; tempr < 9; tempr++) - { - for (int tempc = 0; tempc < 9; tempc++) - { + } else { + for (int tempr = 0; tempr < 9; tempr++) { + for (int tempc = 0; tempc < 9; tempc++) { _solvedGrid[tempr, tempc] = tempg[tempr, tempc]; } } - if (_Level == 1) - { - System.Windows.Forms.MessageBox.Show("There is no solution\nto this puzzle.","No Solution",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Exclamation); + if (_Level == 1) { + System.Windows.Forms.MessageBox.Show("There is no solution\nto this puzzle.", "No Solution", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); } _Level--; return false; } } } - } - else - { + } else { _Level--; return false; } @@ -956,31 +766,25 @@ public bool Solve(bool solve, bool known, bool gen) } } } - }while (gridChange); + } while (gridChange); _Level--; return true; } #endregion #region public void ClearWrong() - public void ClearWrong() - { - for (int currentrow = 0; currentrow < 9; currentrow++) - { - for (int currentcol = 0; currentcol < 9; currentcol++) - { + public void ClearWrong() { + for (int currentrow = 0; currentrow < 9; currentrow++) { + for (int currentcol = 0; currentcol < 9; currentcol++) { int val = this[currentrow, currentcol]; // checks current cell bool legal = true; // precondition, cells of 0 are okay - if (val != 0 && !IsKnownElement(currentrow, currentcol)) - { + if (val != 0 && !IsKnownElement(currentrow, currentcol)) { // check column first - for (int row = 0; row < 9; row++) - { + for (int row = 0; row < 9; row++) { if (row != currentrow) // skip current row { - if (this[row, currentcol] == val) - { + if (this[row, currentcol] == val) { if (!IsKnownElement(row, currentcol)) this[row, currentcol] = 0; legal = false; @@ -989,12 +793,10 @@ public void ClearWrong() } // check row next - for (int col = 0; col < 9; col++) - { + for (int col = 0; col < 9; col++) { if (col != currentcol) // skip current column { - if (this[currentrow, col] == val) - { + if (this[currentrow, col] == val) { if (!IsKnownElement(currentrow, col)) this[currentrow, col] = 0; legal = false; @@ -1002,16 +804,13 @@ public void ClearWrong() } } - int cornerrow = currentrow/3 * 3; - int cornercol = currentcol/3 * 3; - for (int row = cornerrow; row < cornerrow + 3; row++) - { - for (int col = cornercol; col < cornercol + 3; col++) - { + int cornerrow = currentrow / 3 * 3; + int cornercol = currentcol / 3 * 3; + for (int row = cornerrow; row < cornerrow + 3; row++) { + for (int col = cornercol; col < cornercol + 3; col++) { if (col != currentcol || row != currentrow) // skip current column, row { - if (this[row, col] == val) - { + if (this[row, col] == val) { if (!IsKnownElement(row, col)) this[row, col] = 0; legal = false; @@ -1028,97 +827,86 @@ public void ClearWrong() } #endregion #region public int this[int row, int column] - public int this[int row, int column] - { - get - { + public int this[int row, int column] { + get { return _grid[row, column]; } - set - { + set { _grid[row, column] = value; } } #endregion #region public void CheckBoxes(int CM) - public void CheckBoxes(int CM) - { - if(CM % 2 == 1) + public void CheckBoxes(int CM) { + if (CM % 2 == 1) _ACH = true; else _ACH = false; - CM/=2; - if(CM % 2 == 1) + CM /= 2; + if (CM % 2 == 1) _EH = true; else _EH = false; - CM/=2; - if(CM % 2 == 1) + CM /= 2; + if (CM % 2 == 1) _T = true; else _T = false; - CM/=2; - if(CM % 2 == 1) + CM /= 2; + if (CM % 2 == 1) _TM = true; else _TM = false; - CM/=2; - if(CM % 2 == 1) + CM /= 2; + if (CM % 2 == 1) _E = true; else _E = false; - CM/=2; - if(CM % 2 == 1) + CM /= 2; + if (CM % 2 == 1) _M = true; else _M = false; - CM/=2; - if(CM % 2 == 1) + CM /= 2; + if (CM % 2 == 1) _H = true; else _H = false; } #endregion #region public bool ACH() - public bool ACH() - { + public bool ACH() { return _ACH; } #endregion #region public bool EH() - public bool EH() - { + public bool EH() { return _EH; } #endregion #region public bool TM() - public bool TM() - { + public bool TM() { return _TM; } #endregion #region public bool T() - public bool T() - { + public bool T() { return _T; } #endregion #region public bool E() - public bool E() - { + public bool E() { return _E; } #endregion #region public bool M() - public bool M() - { + public bool M() { return _M; } #endregion #region public bool H() - public bool H() - { + public bool H() { return _H; } #endregion diff --git a/SudokuReader.cs b/SudokuReader.cs index 1bc1d82..2915cb4 100644 --- a/SudokuReader.cs +++ b/SudokuReader.cs @@ -2,43 +2,34 @@ using System.IO; using System.Xml; -namespace Sudoku -{ +namespace Sudoku { /// /// Summary description for SudokuReader. /// - public class SudokuReader - { + public class SudokuReader { #region Global Variables static private SudokuReader _reader = new SudokuReader(); - XmlDocument _xDoc = new XmlDocument(); + XmlDocument _xDoc = new XmlDocument(); #endregion #region static public SudokuReader Reader - static public SudokuReader Reader - { - get - { + static public SudokuReader Reader { + get { return _reader; } } #endregion #region public SudokuReader() - public SudokuReader() - { + public SudokuReader() { } #endregion #region public SudokuGrid Read(string filename) - public SudokuGrid Read(string filename) - { - try - { + public SudokuGrid Read(string filename) { + try { _xDoc.Load(filename); SudokuGrid grid = new SudokuGrid(_xDoc); return grid; - } - catch (Exception ex) - { + } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("Couldn't Load the Grid in " + filename + "-->" + ex.ToString()); } SudokuGrid Grid = new SudokuGrid(); @@ -49,39 +40,34 @@ public SudokuGrid Read(string filename) } #endregion #region public SudokuGrid Restart() - public SudokuGrid Restart() - { + public SudokuGrid Restart() { SudokuGrid grid = new SudokuGrid(_xDoc); return grid; } #endregion #region public void Save(string filename, SudokuGrid grid, int CM) - public void Save(string filename, SudokuGrid grid, int CM) - { + public void Save(string filename, SudokuGrid grid, int CM) { _xDoc = new XmlDocument(); FillDocument(_xDoc, grid, CM); SaveToDisk(filename); } #endregion #region public void Save(string filename, int[,] grid, int CM) - public void Save(string filename, int[,] grid, int CM) - { + public void Save(string filename, int[,] grid, int CM) { _xDoc = new XmlDocument(); FillDocument(_xDoc, grid, CM); SaveToDisk(filename); } #endregion #region public void SaveTemplate(string filename, SudokuGrid grid) - public void SaveTemplate(string filename, SudokuGrid grid) - { + public void SaveTemplate(string filename, SudokuGrid grid) { _xDoc = new XmlDocument(); FillTemplate(_xDoc, grid); SaveToDisk(filename); } #endregion #region public void FillDocument(XmlDocument doc, SudokuGrid grid, int CM) - public void FillDocument(XmlDocument doc, SudokuGrid grid, int CM) - { + public void FillDocument(XmlDocument doc, SudokuGrid grid, int CM) { XmlNode rows1 = doc.CreateNode(XmlNodeType.Element, "Rows", ""); XmlNode rows2 = doc.CreateNode(XmlNodeType.Element, "Rows", ""); XmlNode sudoku = doc.CreateNode(XmlNodeType.Element, "Sudoku", ""); @@ -93,47 +79,34 @@ public void FillDocument(XmlDocument doc, SudokuGrid grid, int CM) guesses.AppendChild(rows1); hints.AppendChild(rows2); - for (int row = 0; row < 10; row++) - { + for (int row = 0; row < 10; row++) { XmlNode node = doc.CreateNode(XmlNodeType.Element, "Row", ""); XmlNode node2 = doc.CreateNode(XmlNodeType.Element, "Row", ""); rows1.AppendChild(node2); rows2.AppendChild(node); - if (row == 9) - { + if (row == 9) { node2.InnerText += CM.ToString(); - } - else - { - for (int col = 0; col < 9; col++) - { - if (grid[row, col] == 0) - { + } else { + for (int col = 0; col < 9; col++) { + if (grid[row, col] == 0) { node.InnerText += "-,"; node2.InnerText += "-,"; - } - else - { - if (grid.IsKnownElement(row, col)) - { + } else { + if (grid.IsKnownElement(row, col)) { node.InnerText += grid[row, col] + ","; node2.InnerText += "-,"; - } - else - { + } else { node.InnerText += "-,"; node2.InnerText += grid[row, col] + ","; } } } - if (node.InnerText.Length > 0) - { + if (node.InnerText.Length > 0) { node.InnerText = node.InnerText.Remove(node.InnerText.Length - 1, 1); } - if (node2.InnerText.Length > 0) - { + if (node2.InnerText.Length > 0) { node2.InnerText = node2.InnerText.Remove(node2.InnerText.Length - 1, 1); } } @@ -141,8 +114,7 @@ public void FillDocument(XmlDocument doc, SudokuGrid grid, int CM) } #endregion #region public void FillDocument(XmlDocument doc, int[,] grid, int CM) - public void FillDocument(XmlDocument doc, int[,] grid, int CM) - { + public void FillDocument(XmlDocument doc, int[,] grid, int CM) { XmlNode rows1 = doc.CreateNode(XmlNodeType.Element, "Rows", ""); XmlNode rows2 = doc.CreateNode(XmlNodeType.Element, "Rows", ""); XmlNode sudoku = doc.CreateNode(XmlNodeType.Element, "Sudoku", ""); @@ -155,48 +127,37 @@ public void FillDocument(XmlDocument doc, int[,] grid, int CM) hints.AppendChild(rows2); - for (int row = 0; row < 10; row++) - { - XmlNode node = doc.CreateNode(XmlNodeType.Element,"Row", ""); - XmlNode node2 = doc.CreateNode(XmlNodeType.Element,"Row", ""); + for (int row = 0; row < 10; row++) { + XmlNode node = doc.CreateNode(XmlNodeType.Element, "Row", ""); + XmlNode node2 = doc.CreateNode(XmlNodeType.Element, "Row", ""); rows1.AppendChild(node2); rows2.AppendChild(node); - if (row == 9) - { + if (row == 9) { node2.InnerText += CM.ToString(); - } - else - { - for (int col = 0; col<9; col++) - { - if (grid[row, col] == 0) - { + } else { + for (int col = 0; col < 9; col++) { + if (grid[row, col] == 0) { node.InnerText += "-" + ","; node2.InnerText += "-" + ","; - } - else - { + } else { node.InnerText += grid[row, col] + ","; node2.InnerText += "-" + ","; } } - if (node.InnerText.Length > 0) - { - node.InnerText = node.InnerText.Remove(node.InnerText.Length -1 , 1); + if (node.InnerText.Length > 0) { + node.InnerText = node.InnerText.Remove(node.InnerText.Length - 1, 1); } - if (node2.InnerText.Length > 0) - { - node2.InnerText = node2.InnerText.Remove(node2.InnerText.Length -1 , 1); + if (node2.InnerText.Length > 0) { + node2.InnerText = node2.InnerText.Remove(node2.InnerText.Length - 1, 1); } } } } #endregion #region public void FillTemplate(XmlDocument doc, SudokuGrid grid) - public void FillTemplate(XmlDocument doc, SudokuGrid grid) - { + public void FillTemplate(XmlDocument doc, SudokuGrid grid) { XmlNode sudoku = doc.CreateNode(XmlNodeType.Element, "Sudoku", ""); XmlNode hints = doc.CreateNode(XmlNodeType.Element, "Hints", ""); XmlNode rows = doc.CreateNode(XmlNodeType.Element, "Rows", ""); @@ -204,40 +165,33 @@ public void FillTemplate(XmlDocument doc, SudokuGrid grid) sudoku.AppendChild(hints); hints.AppendChild(rows); - for (int row = 0; row < 9; row++) - { + for (int row = 0; row < 9; row++) { XmlNode node = doc.CreateNode(XmlNodeType.Element, "Row", ""); rows.AppendChild(node); - for (int col = 0; col < 9; col++) - { - if (grid[row, col] == 0) - { + for (int col = 0; col < 9; col++) { + if (grid[row, col] == 0) { node.InnerText += "-,"; - } - else - { + } else { node.InnerText += grid[row, col] + ","; } } - if (node.InnerText.Length > 0) - { - node.InnerText = node.InnerText.Remove(node.InnerText.Length -1 , 1); + if (node.InnerText.Length > 0) { + node.InnerText = node.InnerText.Remove(node.InnerText.Length - 1, 1); } } } #endregion #region private void SaveToDisk(string filename) - private void SaveToDisk(string filename) - { + private void SaveToDisk(string filename) { XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.Default); xmlWriter.IndentChar = '\t'; xmlWriter.Indentation = 1; xmlWriter.Formatting = Formatting.Indented; - _xDoc.Save(xmlWriter); + _xDoc.Save(xmlWriter); xmlWriter.Close(); }