0

I want to make a program that will randomly generate a sudoku board to play. For those not familiar with the game, you are given a 9x9 gameboard with mostly empty spaces and some numbers pre-filled in. To complete it you must fill in the empty squares so there is 1-9 in every row, column, and 3x3 square, but only once.

The way I am currently imagining it is to use a 2d array. I use a for loop within a for loop to populate it, making sure that the number going into the square is never one that has already been used in the same row or column.

The problem I am having is how to check if the number is already used in the 3x3 part of the grid. I was thinking about using a 3d array and the third dimension is where the 3x3 data is stored, but I don't know a simple way to decide which array to check in for the current square.

I also don't know for sure if randomly generating the tiles the way I am will always produce a complete board. I am worried it might get to one of the last few tiles and find that no number between 1 and 9 will fit in the square because it is already used. I don't know how to check if this is a possibility.

I did do a minimal amount of looking at other questions on the topic but I don't want to accidentally come across the answer, I just need a pointer in the right direction. Also none of them seemed to be directly related to what I am asking.

Hopefully what I am asking makes sense, it is a little difficult to describe in text. I would appreciate it if you could give me a pointer in the right direction without giving me the answer. Also if you don't know much about sudoku (why did you click on this question) you can play it here: http://www.websudoku.com/ If you need clarification I will respond to comments as quickly as I can.

Supetorus
  • 79
  • 2
  • 10
  • Ofcoz there are too many things you will be confused with. I think if you really can't figure these out by yourself. You'd better check post like [this](http://stackoverflow.com/questions/6924216/how-to-generate-sudoku-boards-with-unique-solutions) – kenshinji Jul 04 '16 at 01:09

1 Answers1

1

I'll try to just give you some hints rather than giving you the answers outright.

First, its a great idea to use a 2d array -- that is exactly what a sudoku board is. As for your 3d array idea, it is a bit overcomplicated. Think about using mathematical functions to find the top corner of each 3x3 box (i.e. [0, 0], [0, 2], [2, 0], etc.) and use a for loop to traverse that 3x3 box (still in the 2d array). As for generating the board by putting numbers randomly in, it might not work, and board generation is maybe not as trivial as you might think provided you want each board to have only one correct solution. Make sure you can check board validity first, then take a look at the link posted by kenshinji.

ejaszewski
  • 11
  • 1
  • 1
  • Hmm, I think I was imagining something like this but I'm not sure how to find the top corner. I'll do some poking around and see if I can find a way to do this. Maybe the top corners will all be divisible by something interesting. This is sort of a background project so it will almost certainly be a while before I have a result. – Supetorus Jul 04 '16 at 14:31