Using Stopwatch in C

0 votes

Is there a way to incorporate a timed function into this sudoku-solving code? I tried using the Stopwatch class, but I'm having trouble getting it to work because I'm not sure where to put it. I've tried a few other locations, but I always get problems. The code's main objective is accomplished, but I'd like to add a timer function to see how long it took to complete.

using System;

namespace SuDoKu
{
  public class SuDoKu
  {
    private int[,] grid;

    public SuDoKu()
    { 
        grid = new int[9, 9];
    } 
     static void Main(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 } };

        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                sdk.EnterToGrid(grd[i, j], new Point(i, j));

        Console.WriteLine("Original Grid"); 

        sdk.Display(); 

        sdk.Solve(sdk.NextAvailableCell(), new Point()); 

        Console.WriteLine("\n\n\nSolved Grid"); 

        sdk.Display(); 

        Console.WriteLine();
    }

    public void Display()
    {
        for(int i = 0; i < 9; i++)
        {
            Console.WriteLine();

            for(int j = 0; j < 9; j++)
            {
                if(ShowFromGrid(new Point(i, j)) == 0) {
                    Console.Write("0 ");
                }
                else
                {
                    Console.Write(ShowFromGrid(new 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) {
        grid[pos.X, pos.Y] = num;
    }

    public int ShowFromGrid(Point pos) {
        return grid[pos.X, pos.Y];
    }

    public void Solve(Point pos, Point prevPos) {

        if(pos.X < 9 && pos.Y < 9)
        {
            Point posPrev = new Point();

            if(grid[pos.X, pos.Y] == 0)
            {
                for(int i = 1; i <= 9; i++)
                {
                    if(IsThisNumberPossibleInTheGrid(i, pos))
                    {
                        grid[pos.X, pos.Y] = i;

                        posPrev.X = pos.X;

                        posPrev.Y = pos.Y;

                        Point posNext = NextAvailableCell();

                        if(!posNext.Equals(new Point()))

                            Solve(posNext, posPrev);
                    }
                }
                if(grid[pos.X, pos.Y] == 0)
                {
                    if(!prevPos.Equals(new Point()))

                        grid[prevPos.X, prevPos.Y] = 0; 

                    return;
                }
            }
        }
        else
        {
            Point posNext = NextAvailableCell();

            if(!posNext.Equals(new Point()))
                Solve(posNext, pos);
        }
    }

    public Point NextAvailableCell()
    {
        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                if(grid[i, j] == 0)
                    return new Point(i, j);

        return new Point();
    }

    public bool IsThisNumberPossibleInTheGrid(int num, Point pos) {

        if(IsThisNumberPossibleInTheBlock(num, pos))
        {
            for(int i = 0; i < 9; i++)
            {
                if(grid[i, pos.Y] == num && pos.X != i)
                {
                    return false;
                }

                if(grid[pos.X, i] == num && pos.Y != i)
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public bool IsThisNumberPossibleInTheBlock(int num, Point pos)
    {
        Point Grid = new Point();

        Grid.X = (pos.X >= 0 && pos.X <= 2) ? 0 : ((pos.X >= 3 && pos.X <= 5) ? 3 : ((pos.X >= 6 && pos.X <= 8) ? 6 : -1));
        Grid.Y = (pos.Y >= 0 && pos.Y <= 2) ? 0 : ((pos.Y >= 3 && pos.Y <= 5) ? 3 : ((pos.Y >= 6 && pos.Y <= 8) ? 6 : -1));

        if(!Grid.Equals(new Point()))
        {
            for(int i = Grid.X; i < Grid.X + 3; i++)
            {
                for(int j = Grid.Y; j < Grid.Y + 3; j++)
                {
                    if(grid[i, j] == num && !pos.Equals(new Point(i, j)))
                        return false;
                }
            }
            return true;
        }
        return false;
    }
}

  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)
    {
        return (this.X == p.X && this.Y == p.Y) ? true : false;
    }
  }
}
Jun 11, 2022 in C# by krishna
• 2,820 points
221 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C#

0 votes
1 answer

Why should I avoid using Properties in C#?

Jeff dislikes properties because they resemble fields, ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
433 views
0 votes
1 answer

What are the differences between C, C# and C++ in terms of real-world applications?

C is a bare-bones, straightforward, and clean ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
317 views
0 votes
1 answer

Which version of C# am I using

From developer cmd, type this csc -langversion:? This will ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,590 points
286 views
0 votes
1 answer

Examples of dynamic polymorphism in c#

Check the example of polymorphism below. We ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,590 points
614 views
0 votes
1 answer

Print Pdf in C#

Using an installed Adobe Reader or any ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,590 points
617 views
0 votes
1 answer

Is a Library a project or a C# source code file in Visual Studio?

A library project is a collection of ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
275 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
5,216 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
881 views
+6 votes
16 answers

How do backend of these really cool games work?

Most of the games these days don't ...READ MORE

answered Jul 19, 2018 in Career Counselling by Kalgi
• 52,360 points
10,179 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP