Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions windowsforms/Sudoku/CS/sudoku/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Windows.Forms;

namespace Sudoku
{
internal static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
71 changes: 71 additions & 0 deletions windowsforms/Sudoku/CS/sudoku/core/Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sudoku
{
public class Game
{
public event Action<int[][]> ShowClues;
public event Action<int[][]> ShowSolution;

private readonly List<int>[] _hRow = new List<int>[9];
private readonly List<int>[] _vRow = new List<int>[9];
private readonly List<int>[] _threeSquare = new List<int>[9];
private readonly int[][] _grid = new int[9][];

private Random _r;

public void NewGame(Random rn)
{
_r = rn;
CreateNewGame();
}

private void InitializeLists()
{
for (int x = 0; x <= 8; x++)
{
_hRow[x] = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
_vRow[x] = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
_threeSquare[x] = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
int[] row = new int[9];
_grid[x] = row;
}
}

private void CreateNewGame()
{
do
{
InitializeLists();
for (int y = 0; y <= 8; y++)
{
for (int x = 0; x <= 8; x++)
{
_grid[y][x] = 0;
int si = (y / 3) * 3 + (x / 3);
int[] useful = _hRow[y].Intersect(_vRow[x]).Intersect(_threeSquare[si]).ToArray();
if (useful.Any())
{
int randomNumber = useful[_r.Next(0, useful.Count())];
_hRow[y].Remove(randomNumber);
_vRow[x].Remove(randomNumber);
_threeSquare[si].Remove(randomNumber);
_grid[y][x] = randomNumber;
if (y == 8 && x == 8) break;
}
}
if (y == 8) break;
}
} while (_grid[8][8] == 0);

ShowClues?.Invoke(_grid);
}

public void ShowGridSolution()
{
ShowSolution?.Invoke(_grid);
}
}
}
193 changes: 193 additions & 0 deletions windowsforms/Sudoku/CS/sudoku/gui/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions windowsforms/Sudoku/CS/sudoku/gui/Form1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace Sudoku
{
public partial class Form1 : Form
{
private Game _game = new Game();
private readonly Random _r = new Random();

public Form1()
{
InitializeComponent();
_game.ShowClues += Game_ShowClues;
_game.ShowSolution += Game_ShowSolution;
}

private void Form1_Load(object sender, EventArgs e)
{
DataGridView1.Rows.Add(9);
ComboBox1.SelectedIndex = 0;
btnNew.PerformClick();
}

private void btnNew_Click(object sender, EventArgs e)
{
_game.NewGame(_r);
}

private void DataGridView1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Color.Black, 2), 75, 0, 75, 228);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 152, 0, 152, 228);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 0, 75, 228, 75);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 0, 153, 228, 153);
}

private void btnSolution_Click(object sender, EventArgs e)
{
_game.ShowGridSolution();
}

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
btnNew.PerformClick();
}

public void Game_ShowClues(int[][] grid)
{
for (int y = 0; y <= 8; y++)
{
List<int> cells = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
for (int c = 1; c <= 9 - (5 - ComboBox1.SelectedIndex); c++)
{
int randomNumber = cells[_r.Next(0, cells.Count)];
cells.Remove(randomNumber);
}
for (int x = 0; x <= 8; x++)
{
if (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 = true;
}
else
{
DataGridView1.Rows[y].Cells[x].Value = "";
DataGridView1.Rows[y].Cells[x].Style.ForeColor = Color.Black;
DataGridView1.Rows[y].Cells[x].ReadOnly = false;
}
}
}
}

public void Game_ShowSolution(int[][] grid)
{
for (int y = 0; y <= 8; y++)
{
for (int x = 0; x <= 8; x++)
{
if (DataGridView1.Rows[y].Cells[x].Style.ForeColor == Color.Black)
{
if (string.IsNullOrEmpty(DataGridView1.Rows[y].Cells[x].Value?.ToString()))
{
DataGridView1.Rows[y].Cells[x].Style.ForeColor = Color.Blue;
DataGridView1.Rows[y].Cells[x].Value = grid[y][x];
}
else
{
if (grid[y][x].ToString() != DataGridView1.Rows[y].Cells[x].Value.ToString())
{
DataGridView1.Rows[y].Cells[x].Style.ForeColor = Color.Blue;
DataGridView1.Rows[y].Cells[x].Value = grid[y][x];
}
}
}
}
}
}
}
}
Loading