using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace
sudoku_cs
{
public
class
Game
event
ShowCluesEventHandler ShowClues;
delegate
void
ShowCluesEventHandler(
int
[][] grid);
ShowSolutionEventHandler ShowSolution;
ShowSolutionEventHandler(
private
List<
>[] HRow =
new
>[9];
>[] VRow =
>[] ThreeSquare =
[][] grid =
[9][];
Random r;
NewGame(Random rn)
this
.r = rn;
createNewGame();
}
initializeLists()
for
(
x = 0; x <= 8; x++)
HRow[x] =
>(
[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
VRow[x] =
ThreeSquare[x] =
[] row =
[9];
grid[x] = row;
createNewGame()
while
true
)
break1:
initializeLists();
y = 0; y <= 8; y++)
si = (y / 3) * 3 + (x / 3);
[] useful = HRow[y].Intersect(VRow[x]).Intersect(ThreeSquare[si]).ToArray();
if
(useful.Count() == 0)
goto
break1;
randomNumber = useful[
.r.Next(0, useful.Count())];
HRow[y].Remove(randomNumber);
VRow[x].Remove(randomNumber);
ThreeSquare[si].Remove(randomNumber);
grid[y][x] = randomNumber;
(y == 8 & x == 8)
break2;
};
break2:
(ShowClues !=
null
ShowClues(grid);
showGridSolution()
(ShowSolution !=
ShowSolution(grid);
System.ComponentModel;
System.Data;
System.Drawing;
System.Windows.Forms;
partial
Form1 : Form
Game game =
Game();
Random r =
Random();
Form1()
InitializeComponent();
Load += Form1_Load;
btnNew.Click += btnNew_Click;
btnSolution.Click += btnSolution_Click;
DataGridView1.Paint += DataGridView1_Paint;
ComboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
game.ShowClues += game_ShowClues;
game.ShowSolution += game_ShowSolution;
Form1_Load(System.Object sender, System.EventArgs e)
DataGridView1.Rows.Add(9);
ComboBox1.SelectedIndex = 0;
btnNew.PerformClick();
btnNew_Click(System.Object sender, System.EventArgs e)
game.NewGame(r);
DataGridView1_Paint(
object
sender, System.Windows.Forms.PaintEventArgs e)
e.Graphics.DrawLine(
Pen(Color.Black, 2), 75, 0, 75, 228);
Pen(Color.Black, 2), 150, 0, 150, 228);
Pen(Color.Black, 2), 0, 66, 228, 66);
Pen(Color.Black, 2), 0, 132, 228, 132);
btnSolution_Click(System.Object sender, System.EventArgs e)
game.showGridSolution();
ComboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
game_ShowClues(
[][] grid)
> cells =
[] {1,2,3,4,5,6,7,8,9});
c = 1; c <= 9 - (5 - ComboBox1.SelectedIndex); c++)
randomNumber = cells[r.Next(0, cells.Count())];
cells.Remove(randomNumber);
(cells.Contains(x + 1))
DataGridView1.Rows[y].Cells[x].Value = grid[y][x];
DataGridView1.Rows[y].Cells[x].Style.ForeColor = Color.Red;
DataGridView1.Rows[y].Cells[x].ReadOnly =
;
else
DataGridView1.Rows[y].Cells[x].Value =
""
DataGridView1.Rows[y].Cells[x].Style.ForeColor = Color.Black;
false
game_ShowSolution(
(DataGridView1.Rows[y].Cells[x].Style.ForeColor == Color.Black)
string
.IsNullOrEmpty(DataGridView1.Rows[y].Cells[x].Value.ToString()))
DataGridView1.Rows[y].Cells[x].Style.ForeColor = Color.Blue;
(grid[y][x].ToString() != DataGridView1.Rows[y].Cells[x].Value.ToString())