Est-ce que quelqu'un sait comment je peux ajouter une fonction de minuterie à ce code qui résout des puzzles sudoku? J'ai essayé la classe Stopwatch mais je n'arrive pas à le faire fonctionner car je ne sais pas exactement où le mettre. J'ai essayé plusieurs endroits mais j'obtiens toujours des erreurs. Le but principal du code fonctionne mais j'aimerais ajouter une fonction de minuterie pour voir combien de temps le code a été exécuté.
utilisant System;
espace de noms SuDoKu
{
public class SuDoKu
{
private int[,] grille;
public SuDoKu()
{
grille = new int[9, 9];
}
statique vide Principal(string[] args)
{
SuDoKu sdk = new SuDoKu();
int[,] grd = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
pour(int i = 0; i < 9; i++)
pour(int j = 0; j < 9; j++)
sdk.EnterToGrid(grd[i, j], nouveau Point(i, j));
Console.WriteLine("Tableau d'origine");
sdk.Display();
sdk.Solve(sdk.NextAvailableCell(), nouveau Point());
Console.WriteLine("\n\n\nGrille résolue");
sdk.Display();
Console.WriteLine();
}
public void Display()
{
pour(int i = 0; i < 9; i++)
{
Console.WriteLine();
pour(int j = 0; j < 9; j++)
{
if(ShowFromGrid(nouveau Point(i, j)) == 0) {
Console.Write("0 ");
}
autre
{
Console.Write(ShowFromGrid(nouveau Point(i, j)) + " ");
}
if(j == 2 || j == 5)
{
Console.Write("| ");
}
}
if(i == 2 || i == 5) {
Console.Write("\n------|-------|------ ");
}
}
}
public void EnterToGrid(int num, Point pos) {
grille[pos.X, pos.Y] = num;
}
public int ShowFromGrid(Point pos) {
return grille[pos.X, pos.Y];
}
public void Solve(Point pos, Point prevPos) {
si(pos.X < 9 && pos.Y < 9)
{
Point posPrev = nouveau Point();
si(grille[pos.X, pos.Y] == 0)
{
pour(int i = 1; i <= 9; i++)
{
si(IsThisNumberPossibleInTheGrid(i, pos))
{
grille[pos.X, pos.Y] = i;
posPrev.X = pos.X;
posPrev.Y = pos.Y;
Point posNext = NextAvailableCell();
si(!posNext.Equals(nouveau Point()))
Solve(posNext, posPrev);
}
}
si(grille[pos.X, pos.Y] == 0)
{
si(!prevPos.Equals(nouveau Point()))
grille[prevPos.X, prevPos.Y] = 0;
revenir;
}
}
}
autre
{
Point posNext = NextAvailableCell();
si(!posNext.Equals(nouveau Point()))
Solve(posNext, pos);
}
}
public Point NextAvailableCell()
{
pour(int i = 0; i < 9; i++)
pour(int j = 0; j < 9; j++)
si(grille[i, j] == 0)
revenir nouveau Point(i, j);
revenir nouveau Point();
}
public bool IsThisNumberPossibleInTheGrid(int num, Point pos) {
si(IsThisNumberPossibleInTheBlock(num, pos))
{
pour(int i = 0; i < 9; i++)
{
si(grille[i, pos.Y] == num && pos.X != i)
{
revenir faux;
}
si(grille[pos.X, i] == num && pos.Y != i)
{
revenir faux;
}
}
revenir vrai;
}
revenir faux;
}
public bool IsThisNumberPossibleInTheBlock(int num, Point pos)
{
Point Grille = nouveau Point();
Grille.X = (pos.X >= 0 && pos.X <= 2) ? 0 : ((pos.X >= 3 && pos.X <= 5) ? 3 : ((pos.X >= 6 && pos.X <= 8) ? 6 : -1));
Grille.Y = (pos.Y >= 0 && pos.Y <= 2) ? 0 : ((pos.Y >= 3 && pos.Y <= 5) ? 3 : ((pos.Y >= 6 && pos.Y <= 8) ? 6 : -1));
si(!Grille.Equals(nouveau Point()))
{
pour(int i = Grille.X; i < Grille.X + 3; i++)
{
pour(int j = Grille.Y; j < Grille.Y + 3; j++)
{
si(grille[i, j] == num && !pos.Equals(nouveau Point(i, j)))
revenir faux;
}
}
revenir vrai;
}
revenir faux;
}
}
public class Point
{
public int X;
public int Y;
public Point()
{
this.X = -1;
this.Y = -1;
}
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
public bool Equals(Point p)
{
revenir (this.X == p.X && this.Y == p.Y) ? vrai : faux;
}
}
}