Skip to content

Instantly share code, notes, and snippets.

@fabiosoft
Created February 2, 2017 10:10
Show Gist options
  • Select an option

  • Save fabiosoft/b41067106bebf1498399f4eb9826e4de to your computer and use it in GitHub Desktop.

Select an option

Save fabiosoft/b41067106bebf1498399f4eb9826e4de to your computer and use it in GitHub Desktop.
C# Sudoku Generator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static int[,] grid = new int[9, 9];
static string s;
static void Init(ref int[,] grid)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
grid[i, j] = (i * 3 + i / 3 + j) % 9 + 1;
}
}
}
static void Draw(ref int[,] grid, out string _s)
{
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
s += grid[x, y].ToString() + " ";
}
s += "\n";
}
Console.WriteLine(s);
_s = s;
s = "";
}
static void ChangeTwoCell(ref int[,] grid, int findValue1, int findValue2)
{
int xParm1, yParm1, xParm2, yParm2;
xParm1 = yParm1 = xParm2 = yParm2 = 0;
for (int i = 0; i < 9; i += 3)
{
for (int k = 0; k < 9; k += 3)
{
for (int j = 0; j < 3; j++)
{
for (int z = 0; z < 3; z++)
{
if (grid[i + j, k + z] == findValue1)
{
xParm1 = i + j;
yParm1 = k + z;
}
if (grid[i + j, k + z] == findValue2)
{
xParm2 = i + j;
yParm2 = k + z;
}
}
}
grid[xParm1, yParm1] = findValue2;
grid[xParm2, yParm2] = findValue1;
}
}
}
static void Update(ref int[,] grid, int shuffleLevel)
{
for (int repeat = 0; repeat < shuffleLevel; repeat++)
{
Random rand = new Random(Guid.NewGuid().GetHashCode());
Random rand2 = new Random(Guid.NewGuid().GetHashCode());
ChangeTwoCell(ref grid, rand.Next(1, 9), rand2.Next(1, 9));
}
}
static void Main(string[] args)
{
s = "";
string ç1kt1;
Init(ref grid);
Update(ref grid, 10);
Draw(ref grid, out ç1kt1);
Console.ReadKey();
}
}
}
@smailliwnosaj
Copy link

Both cases of .Next(1, 9) need to be .Next(1, 10). Otherwise, it leaves a 9 in the top right and bottom left corner of every Sudoku it creates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment