Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 790843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:44:29+00:00 2026-05-14T21:44:29+00:00

I’m trying to create a sudoku game, for those that do not know what

  • 0

I’m trying to create a sudoku game, for those that do not know what it is. You have a 9×9 box that needs to be filled with numbers from 1-9, every number must be unique in its row and column, and also in the 3×3 box it is found. I ended up doing loads of looping within a 2 dimensional array.

But at some point it just stops, with no exceptions whatsoever, just breaks out and nothing happens, and it’s not always at the same position, but always goes past half way.

I was expecting a stack overflow exception at least.

Here’s my code:

public class Engine
{
    public int[,] Create()
    {
        int[,] outer = new int[9, 9];


        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                 outer[i, j] = GetRandom(GetUsed(outer, i, j));
            }
        }

        return outer;

    }

    List<int> GetUsed(int[,] arr, int x, int y)
    {
        List<int> usedNums = new List<int>();
        for (int i = 0; i < 9; i++)
        {
            if (arr[x, i] != 0 && i != y)
            {
                if(!usedNums.Contains(arr[x, i]))
                    usedNums.Add(arr[x, i]);
            }
        }

        for (int i = 0; i < 9; i++)
        {
            if (arr[i, y] != 0 && i != x)
            {
                if (!usedNums.Contains(arr[i, y]))
                    usedNums.Add(arr[i, y]);
            }
        }

        int x2 = 9 - (x + 1);
        int y2 = 9 - (y + 1);

        if (x2 <= 3)
            x2 = 2;
        else if (x2 > 3 && x2 <= 6)
            x2 = 5;
        else x2 = 8;

        if (y2 <= 3)
            y2 = 2;
        else if (y2 > 3 && y2 <= 6)
            y2 = 5;
        else y2 = 8;

        for (int i = x2 - 2; i < x2; i++)
        {
            for (int j = y2 - 2; j < y2; j++)
            {
                if (arr[i, j] != 0 && i != x && j != y)
                {
                    if (!usedNums.Contains(arr[i, j]))
                        usedNums.Add(arr[i, j]);
                }
            }
        }

        return usedNums;
    }

    int GetRandom(List<int> numbers)
    {
        Random r;
        int newNum;
        do
        {
            r = new Random();
            newNum = r.Next(1, 10);
        } while (numbers.Contains(newNum));

        return newNum;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:44:30+00:00Added an answer on May 14, 2026 at 9:44 pm

    It’s not breaking out, it’s getting stuck in an infinite loop.

    If I’m reading this right, it looks like you’re trying to create a Sudoku board to play on. Trouble is, it’s not as simple as you think. As far as I can see, you’re just going through it filling it with random unused values; the trouble is, not every such configuration is going to be valid – most of the time your random entries will eventually put you into an unsolvable board configuration.

    Then, when you try to select a random value for some square, the inner GetRandom function will just loop infinitely trying to pick a number (since getUsed will already have all of 1 - 9, the do..while will never exit).

    Easy way to see this for yourself: add this at the top of your GetRandom function:

    if (Enumerable.Range(1, 9).All(i => numbers.Contains(i)))
        Console.WriteLine("PROBLEM!");
    

    Now, if numbers has all of 1 to 9, it’ll tell you. (And then still proceed to get stuck in an infinite loop, but that’s your problem now ;))

    Also, just as a sidenote, it’s not good practice to keep making new Random() objects like that; better to just have a single instance of Random, initialize it in your constructor and then keep using Random.Next. At the very least, have Random r = new Random() at the top of the function, and then just have r.Next inside the loop.

    Ok, here’s a really simple example of getting into an unsolvable position, just in the first two rows:

    123|456|789
    456|123|X
    

    There’s no valid number left to put in the position marked X – see how that happened?
    Filling the grid with random unused numbers isn’t the same as filling it with “answers” – think of it this way, if you took a regular sudoku game and tried to solve it by just putting any random number that satisfied the rules in each blank square – you’d get stuck pretty soon, right? That’s exactly what’s happening to your program.

    I’d suggest you try writing a sudoku solver first, that takes a valid initial board configuration and tries to solve it – then you can move on to trying to make boards yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.