📄 mainform.cs
字号:
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace MySudoku
{
public partial class MainForm : Form
{
private Button[,] graphicSudokuBoard;
int gridSize = 25;
enum Level { Easy, Medium, Hard} ;
Level level = Level.Hard;
public MainForm()
{
InitializeComponent();
GridSizeCombo.SelectedIndex = 3;
GridLevelCombo.SelectedIndex = 2;
FillTimoutCombo();
CreateSudokuBoard();
}
void DestroySudokuBoard()
{
if (graphicSudokuBoard == null) return;
for (int row = 0; row < gridSize; row++)
for (int col = 0; col < gridSize; col++)
{
Controls.Remove(graphicSudokuBoard[row, col]);
graphicSudokuBoard[row, col] = null;
}
}
private void CreateSudokuBoard()
{
int sudokuTableMargin = 13;// this.solveButton.Left;
int x, y = 40;
int tabIndex = 0, cellHeight = 10;
float fontsize = 10;
if (gridSize == 4) { y = 75; sudokuTableMargin = 120; cellHeight = 100; fontsize = 35; }
if (gridSize == 9) { y = 55; sudokuTableMargin = 100; cellHeight = 50; fontsize = 20; }
if (gridSize == 16) { y = 45; sudokuTableMargin = 80; cellHeight = 30; fontsize = 8; }
if (gridSize == 25)
{
sudokuTableMargin = 80; cellHeight = 20; fontsize = 8;
}
this.graphicSudokuBoard = new Button[gridSize, gridSize];
for (int row = 0; row < gridSize; row++)
{
x = sudokuTableMargin;
if ((row != 0) && ((row % Math.Sqrt(gridSize)) == 0))
y += 7;
for (int col = 0; col < gridSize; col++)
{
Button cell = new Button();
cell.Font = new Font(cell.Font.FontFamily, fontsize, FontStyle.Bold);
cell.Height = cellHeight;
cell.Width = cell.Height;
cell.KeyDown += new KeyEventHandler(cell_KeyDown);
// cell.TextChanged += new EventHandler(this.textBox_TextChanged);
cell.TabIndex = tabIndex++;
if ((col != 0) && ((col % Math.Sqrt(gridSize)) == 0))
x += 7;
cell.Location = new Point(x, y);
this.Controls.Add(cell);
this.graphicSudokuBoard[row, col] = cell;
cellHeight = cell.Height;
x += cell.Width - 1;
if (col % Math.Sqrt(gridSize) == Math.Sqrt(gridSize) - 1)
x++;
}
y += cellHeight - 1;
if (row % Math.Sqrt(gridSize) == Math.Sqrt(gridSize) - 1)
y++;
}
groupBox1.Text = "Undefined Puzzle.";
}
bool isvalid(char c)
{
string st="";
if (gridSize<=9) return char.IsDigit(c);
if (gridSize >= 16)
{
st = "123456789ABCDEFG";
if (gridSize == 25)
st += "HIJKLMNOP";
}
return st.Contains(c.ToString());
}
bool MakeConflicts(Button b)
{
for (int i = 0; i < gridSize; i++)
for (int j = 0; j < gridSize; j++)
if (graphicSudokuBoard[i, j] == b)
{
for (int k = 0; k < gridSize; k++)
{
if (k == j) continue;
if (graphicSudokuBoard[i, k].Text == b.Text)
return true;
}
for (int k = 0; k < gridSize; k++)
{
if (k == i) continue;
if (graphicSudokuBoard[k, j].Text == b.Text)
return true;
}
int dx = j - (j % (int)Math.Sqrt(gridSize));
int dy = i - (i % (int)Math.Sqrt(gridSize));
for (int r = dy; r < dy + (int)Math.Sqrt(gridSize); r++)
for (int s = dx; s < dx + (int)Math.Sqrt(gridSize); s++)
{
if ((r == i) || (s == j)) continue;
if (graphicSudokuBoard[r, s].Text == b.Text)
return true;
}
}
return false;
}
void SetButton(Button b, char c)
{
if (char.IsLetter(c))
c = char.ToUpper(c);
if (isvalid(c))
{
b.Text = c.ToString();
b.BackColor = Color.LightSteelBlue;
if (MakeConflicts(b))
{
b.BackColor = Color.IndianRed;
MessageBox.Show("Assigning that value will cause a conflict.","Warning",MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
MessageBox.Show("Invalid Input", "Error");
}
private void cell_KeyDown(object sender, KeyEventArgs e)
{
if (!enable) return;
Button b = (Button)sender;
if (e.KeyCode == Keys.Delete) { b.Text = ""; b.BackColor = SystemColors.ControlLight; return; }
char c = (char)e.KeyValue;
SetButton(b, c);
groupBox1.Text = "Undefined Puzzle.";
}
void Clear()
{
for (int row = 0; row < gridSize; row++)
for (int col = 0; col < gridSize; col++)
{
enable = true;
graphicSudokuBoard[row, col].Text = "";
graphicSudokuBoard[row, col].BackColor = SystemColors.ControlLight;
}
}
private void ClearButton_Click(object sender, EventArgs e)
{
Clear();
}
private void ExitButton_Click(object sender, EventArgs e)
{
Close();
}
string[] Easygrid4x4 = { "1.3....4....4..." };
string[] Mediumgrid4x4 = { "1.3....4....4..." };
string[] Hardgrid4x4 = { "1.3....4....4..." };
string[] Easygrid9x9 = {"..1............2........3.........4........5.........6....7.......8...........9.." };
string[] Hardgrid9x9 = {"5.......9.2.1...7...8...3...4.6.........5.......2.7.1...3...8...6...4.2.9.......5",
"6.....7.3.4.8.................5.4.8.7..2.....1.3.......2.....5.....7.9......1....",
"4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......", "52...6.........7.13...........4..8..6......5...........418.........3..2...87.....", "6.....8.3.4.7.................5.4.7.3..2.....1.6.......2.....5.....8.6......1....", "48.3............71.2.......7.5....6....2..8.............1.76...3.....4......5....", "....14....3....2...7..........9...3.6.1.............8.2.....1.4....5.6.....7.8...", "......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.", "6.2.5.........3.4..........43...8....1....2........7..5..27...........81...6.....", ".524.........7.1..............8.2...3.....6...9.5.....1.6.3...........897........", "6..3.2....5.....1..........7.26............543.........8.15........4.2........7..", ".6.5.1.9.1...9..539....7....4.8...7.......5.8.817.5.3.....5.2............76..8...", "..5...987.4..5...1..7......2...48....9.1.....6..2.....3..6..2.......9.7.......5..", "3.6.7...........518.........1.4.5...7.....6.....2......2.....4.....8.3.....5.....", "1.....3.8.7.4..............2.3.1...........958.........5.6...7.....8.2...4.......", "6..3.2....4.....1..........7.26............543.........8.15........4.2........7..", "....3..9....2....1.5.9..............1.2.8.4.6.8.5...2..75......4.1..6..3.....4.6.", "45.....3....8.1....9...........5..9.2..7.....8.........1..4..........7.2...6..8..", ".237....68...6.59.9.....7......4.97.3.7.96..2.........5..47.........2....8.......", "..84...3....3.....9....157479...8........7..514.....2...9.6...2.5....4......9..56"};
string[] Mediumgrid9x9 = {"4...3.......6..8..........1....5..9..8....6...7.2........1.27..5.3....4.9........",
"7.8...3.....2.1...5.........4.....263...8.......1...9..9.6....4....7.5...........",
"7.8...3.....6.1...5.........4.....263...8.......1...9..9.2....4....7.5...........",
"3.7.4...........918........4.....7.....16.......25..........38..9....5...2.6.....",
"5..7..6....38...........2..62.4............917............35.8.4.....1......9....",
"4..7..6....38...........2..62.5............917............43.8.5.....1......9....",
".4..1.2.......9.7..1..........43.6..8......5....2.....7.5..8......6..3..9........",
"7.5.....2...4.1...3.........1.6..4..2...5...........9....37.....8....6...9.....8.",
"......41.9..3.....3...5.....48..7..........62.1.......6..2....5.7....8......9....",
"7.5.....2...4.1...3.........1.6..4..2...5...........9....37.....9....8...8.....6.",
".8..1......5....3.......4.....6.5.7.89....2.....3.....2.....1.9..67........4.....",
"8.9...3.....7.1...5.........7.....263...9.......1...4..6.2....4....8.5...........",
"6.9.....8...7.1...4............6...4.2.....3..3....5...1.5...7.8...9..........2..",
"......41.9..3.....3...2.....48..7..........52.1.......5..2....6.7....8......9....",
"1...48....5....9....6...3.....57.2..8.3.........9............4167..........2.....",
"7.8...3.....6.1...4.........6.....253...8.......1...9..9.5....2....7.4...........",
".2.4.37.........32........4.4.2...7.8...5.........1...5.....9...3.9....7..1..86.."};
string[] Easygrid16x16 = { "..9..GD4..E.A6F.4.F.36...5.1G7E21.6.F2..B9A...8..D...1..46.F..........F.8153.4B76.1..C8.9..2..3.E.4D6..3.C7A8.2.38..472.6....CG5D..G.8EA34F.C51B...62..1A.B.F3.97..B.4.F5.9E....A..8..B...1C4.DG......7.F2....C3..7..A6.18.DB.9E865..3..E..9.....G.2...E.A......" };
string[] Mediumgrid16x16 = { "" };
string[] Hardgrid16x16 ={".63B.EC..A..8....847..A6..B....9.....81.D.G...7E.......7..98...CF.D.....AC..2.......D.....E1..5.CE......6...GF.31A.9...B8G7.4..D2.E...45....69.F.7......E..A...5..94..6......D.....63..F79.5...A....E6.D.1...2.8...3G.FA56.......D.C...9...B1.6..2..B.5C9.....34",
".B.293.F..C.......7.B..5......C..9..C...247.F...EF..6....9B.3D..F...58G...........B3......2F1.7.....E...1.8..C.D...1...3.D...G..4.6...2.3..9A.8.12..G.86.F......A7....C...419.G......E..5....7437..........B.3.C.8...DF......E96.E.6...9......D8..G..7..C..4...A"};
string[] Easygrid25X25 = { "...9....P....5.....I...HLD...2.....N.....MA..C........P..A...9...C..J....F..F...J.......B..9.....A......6......F.....N...8..J......O......6....D.....9F...M........1..K.9.....E...1...9...O.....M..F..2...5..1........I..G....3......D....J......O...N..........P......4......6.....7...C......O....E....L.......I..........G5.....O4.4..K........31H....GB...LH...G6....E..7............K...86.......5...DI......J....2..I....A...9.....96..FE.......D......7...8...4.....9........K....NOB..............F.....9.......1.......J......58...A....9.....G1....B....3...35........F.......2...4....7....P........8......K......A...L...O...3......." };
string[] Mediumgrid25x25 ={ "D4K..LG.A.IP1..8MN.H.......J.9.CP3.MKH..472...6G.FP..3...B5..ED...A.FG1.8NMI...E.8M.H.7....C..B..9L.15..M..94N...GFI.JD6P..EK.I.F.6O..J.M..G..4B..7.PH....HB9..I5.....O..J..23N.8N.L5.4HP9..76.F.....1.G6..7.K..M.DF2.J.G1...9..8...9BC1....H...D.I...K6...KF...5.2.....E3..69MPDHAL9..OGMI.3.1A.K...8N.25.....G6DHCP1J..48.L..O7...E5D..87.K9....M....G..O.J.H3....4.N.7..F.M....8I...F.1.....JM8.G..B.9....PD.469.DF....E..1...8M5....3.N8..H.3D.K.L.I...1.GE....E..3P.8B..A...L.G...4H.OG.C...7....6...KJOED.8.I.9....E..O2.875.N1..MHGF.PB..D..JLC...E..G.64FA.3.I..M.G9PH..3..I.JD.O..1EB7C1PE53F..7.G.O..BHI.JN4..3.62.M.....J..D...7895...",
"A......7M...3..G..H..JFB61.2..3HG....7.ACNE.I...58.G...E..K8...N.4B9..D....J.F.B..1..DC.8L762....94E..H...I.A..B.9..J.8DKG..C.....2.35....H..74..6.AD.B.4CM7....3.1.I....F5......G..B.......2..8D5..CE..8..91JG...7D.F..2.....H.BF.7A....8.6G.E493B..I.1...DE.AK.B.5F.N....C..2..9....M9.1..286CA.B..7...5E.....8..A......GD.....76....B..98.H...2..5E.L..1...C..2.4.....5.IH3O.A..8.F....5.8C6..4...........G1.E69G....3..2....1...4...7..A........3..9.5.....8.D..C.4...F...L....3...2....K3.2.7ED.C18B...A9.H..65D..165.C.A...7.8H....4..9...3OF.2G.......I..4B.....C5H...D.E.....2.1N6....A..8.7....9.4B.5.....C.2.3..M.E....H...6........7.F"};
string[] Hardgrid25x25 = {"PA..C1F7B..2H..9...E6...M.56....PIH.8MFN23A.C.EBG.3.G..48.MDE.BP..OL7.K...H...H...J.6.A...41..F.I.9...7...5.G.6I.C9KHBN....F..83..F.5..HCAJ.E.O.2...LB.B.7.C6.DI..5M.3......P.O..D.FP...M86.KBHC4..3...JJ.COP.3....EID7.BF.8.G6....5..E.8K.N3GOL.9P.7.4.C....9.B4.N.CP82IO5..KJM...DI.8..7.E..M..AN4...O9K.GL....G.6.2.4..3..I....C8..M.NGA..9......C.38.EPI........M.L5...ND..1.94H..A...3..1N..P92..G...5.6M.8..8..IJ.63L...M..HK..1..C.72G1.EMC.IJ.......D.3.5.....E.O2.K.G1..B..34LNHA.H.M...P...D....I..1O.2E.K......C.3.A.P..F..O.5.1.L89..37IDJ..HC4E..5.PF..N6.D.PO.2L48.73I6AN.B1GCJH.C...B...H1......7.4........NE....O.5LF1.DIC...K.7.",
"...G...9..4.....6F..L8...CEIN.HDM.OF.1L..A..9PJ.4......A...L..JBN.2.D.1...HP49...JB23.AD..7E..C5F...A1H....F.N5....I.BL...26.....7..C.6...H4B..1....I5.F.P...I..B..7.5.L...9....L6A...5OF.8P...K.NE..734B2.E..L...1J.5....O7.K.APO.5.CB1.P....3EM....2L.H.2..MJ.A...9.3.7......P.8C.....CF.DPG62N.E...OH.M.J.DL..OM..IE.B8..NH...3..K.CO1F.B.N.AH..P.78.JE...DE..6.....H......4M.KIB9..N.J..6......C..1.5.G..H.....75LG...6..1..CI..4.E..9K..6.....HGN.O2P.4.......OA..IP849...2.K3...7GN....G....N...P.D9....A...C1J...M.NAFE.4..23.7....8L......J.H9CD1LP..GO....4ME48NK..5.M......JL.......9.I.OG....835.A.DH..P.....35......L.J..E....8IG.67B",
".CH3.L..D4F..I.........1.G..K.....7.O.......L.42N.A.D...5O8B...39G.4..I...L1E2OB....A8N..6FCD9.H...GIN..4..3.....J..5OM....9....JEMP..9G8..D7......BA.B.M9..L....6...2.FI.KJ....LN.386A..BH....O.J.2..7.7A6..2..3N.....4KP..G8HIC.1.....J.CK3....HB..5L4...H......P.I.57....G..21B4....7.....EP1....3.2..A...59.1FI.L8.MJG.B..AH....ENI..D....O3K..A.P9..8F....O....A..E2.8.HN.J7.P.....MC..P....H2...A7..ID...1.6.....M.J.14.BDG.3.O9F2........CG.........4...M..HDL...8N7.......FK2C.E..B..KP2I.F.3...N7J.E.6..L85D.E...C.4.7B..I....A.1..MJ..M..7..D.A..4...ONBG..29.7.6..K..P..O....D...8...2IN..GE1.D..8...MK.CPJH.FG..8B.2.L..KH.....1.5E..",
"...G...9..4.....6F.1L8...CEIN.HDM.OF.1L..A.59PJ.4......A...L..JBN.2.D.1...HP49...JB23.AD..7E..C5F...A1H....F.N5....I.BL...26.....7..C.6...H4BF.1....I5.F.P...I..B..7.5.L...9....L6A...5OF.8P.D.KJNEB.734B2.E..L...1J.5....O7.K.APO.5.CB1.P....3EM....2L.H.2..MJ.A...9.3.7.....NP.8C.A...CF.DPG62N.E...OH.M.J.DL..OM..IE.B8..NH...3..K.CO1F9B.N.AH..P.78.JE...DE..6.....H......4M.KIB9..N.J..6......C..1.5.G9.H.....75LG...6..1A.CI..4.E..9K..6.....HGN.O2P.4.......OA..IP849...2.K3..H7GN....G....N...P.D9.O..A.6.C1J..BM.NAFE.4.923.7....8L.67...JIH9CD1LP..GOK...4ME48NKE.5.M......JL.......9.I.OG....835.A.DH..P.....351.....L.J..E....8IG.67B"};
void SetCPsol(string sol)
{
if (sol.Length != gridSize * gridSize) return;
for (int i = 0; i < gridSize; i++)
for (int j = 0; j < gridSize; j++)
graphicSudokuBoard[i, j].Text = sol[i * gridSize + j].ToString();
}
void genGrid(string grid)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -