does anyone have any idea? how to make a sudoku solver from user inputs? i've been able to get the board into the program, but now im having trouble solving it.
This is a discussion on sudoku solver within the C Programming forums, part of the General Programming Boards category; does anyone have any idea? how to make a sudoku solver from user inputs? i've been able to get the ...
does anyone have any idea? how to make a sudoku solver from user inputs? i've been able to get the board into the program, but now im having trouble solving it.
There's lots of info about it on the net. Read up on it and give it a try, then post your code if you need help.
i've checked out the interent, and i've found some programs, but i havent been able to make any sense out of them.
here is what i have so far. as you will see, any real sudoku solving is lacking, but i have no idea where to begin.
Code:#include <stdio.h> #include <stdlib.h> void draw_board(void); void value_input(int sudoku [81], char blank_board [81]); void entered_board(int*sudoku); void solve_board_row_column(int sudoku_protect); void solve_board_block(); void solve_board_block_check_row_column(); void solve_board_block_check_row_column_block(); int main(void) { int board_space = 0; int board_space_A1C3 = 0; int sudoku_space = 0; int sudoku [81]; int sudoku_protect = sudoku; char blank_board [81]; char *str_blank_board [201] = {"A", "B","C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1", "R1", "S1", "T1", "U1", "V1", "W1", "X1", "Y1", "Z1", "A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2", "I2", "J2", "K2", "L2", "M2", "N2", "O2", "P2", "Q2", "R2", "S2", "T2", "U2", "V2", "W2", "X2", "Y2", "Z2", "A3", "B3", "C3"}; printf("this program will solve any sudoku puzzle you enter\n\n"); printf("here is the board...\n\n\n"); draw_board(); do { printf("what is the value of %s?\n", str_blank_board [board_space]); board_space++; scanf("%d", & sudoku[sudoku_space]); printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); draw_board(); sudoku_space++; }while (sudoku_space <= 80); printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); printf("here is the board you entered\n\n"); entered_board(sudoku); solve_board_row_column(sudoku_protect); return 0; } void draw_board (void) { printf("A B C D E F G H I\n"); printf("J K L M N O P Q R\n"); printf("S T U V W X Y Z A1\n\n"); printf("B1 C1 D1 E1 F1 G1 H1 I1 J1\n"); printf("K1 L1 M1 N1 O1 P1 Q1 R1 S1\n"); printf("T1 U1 V1 W1 X1 Y1 Z1 A2 B2\n\n"); printf("C2 D2 E2 F2 G2 H2 I2 J2 K2\n"); printf("L2 M2 N2 O2 P2 Q2 R2 S2 T2\n"); printf("U2 V2 W2 X2 Y2 Z2 A3 B3 C3\n\n"); printf("please enter the numbers on your board to their corresponding location\non the board on the screen. If no value enter a zero (0)\n\n"); return; } void entered_board(int*sudoku) { int sudoku_space_void = 0; do{ printf("%d", sudoku[sudoku_space_void]); sudoku_space_void++; printf(" %d", sudoku[sudoku_space_void]); sudoku_space_void++; printf(" %d", sudoku[sudoku_space_void]); sudoku_space_void++; printf(" %d", sudoku[sudoku_space_void]); sudoku_space_void++; printf(" %d", sudoku[sudoku_space_void]); sudoku_space_void++; printf(" %d", sudoku[sudoku_space_void]); sudoku_space_void++; printf(" %d", sudoku[sudoku_space_void]); sudoku_space_void++; printf(" %d", sudoku[sudoku_space_void]); sudoku_space_void++; printf(" %d\n", sudoku[sudoku_space_void]); sudoku_space_void++; if ((sudoku_space_void == 27) || (sudoku_space_void == 54) || (sudoku_space_void == 81)) { printf("\n"); } }while (sudoku_space_void <= 80); return; } // fix!!!!!!!!!!!!!!!!! void solve_board_row_column(int*sudoku_protect) { return; }
You need a dup array to hold rhe old board, while you try new values in the new board. The solving logic is simple, but you need some way to backtrack. Read up on that in Wikipedia. The natural way to do backtrack is with recursion, but it can be done with iterative looping, just harder to code.
You need a function to test a value on it's row, column and box (of 9 sqrs.). Think about that, and come back. I've jacked up my finger, and can hardly keyboard tonight.
You also need to decide whether you want a brute-force solver (which is fairly simple, all that is needed is a set of nested loops) or a systematic solver (one that uses specific strategies to avoid guess-work, which will be more difficult to code but will typically work faster and also can be configured to give "hints" to a user).
The fact that your solve_board_row_column function is completely empty, says that you've come here for help too early.
You won't learn anything unless you spend enough time thinking about the problem to be able to make a start on your own.
Being "stuck" from the get-go is merely a symptom of not spending enough time thinking about the problem.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
> i've checked out the interent, and i've found some programs,
Mmmm-kay...
> but i havent been able to make any sense out of them.
No, really!?
If your first and only reaction to each programming problem is "google source code", then you're
a) not cut out to be a programmer
b) not going to make it as a programmer anyway.
You can't just learn this stuff "matrix style in a few seconds, then pronounce that you know kung fu". It just doesn't work.
Stacked against someone who really knows how to program, you would find your ass being well and truly kicked every single time, and you wouldn't have a clue how they were doing it.
Learning is a long hard graft, and you're just failing miserably.
Post your own code, in your own time, or just don't bother.
In fact, you should probably go back to the beginning and start again, if you've been skipping out on a whole lot of learning by using "easy way out google" for all your assignments.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
This will not help you solve the problem, but it may save you time (and a headache) in the near future. Instead of labeling each space the way you did, I suggest you use the numbering system used for annotating a chess or go game. Use A1 for the first column first row space, and increase the letter for each column and the number for each row. For example, F8 would be the fifth column, 8th row. This will ensure that you are not confused when you get to the point (in your current set up) where A1 is referring to the ninth column instead of the first column.
Or, if you want it to be even more simple, you can label them like so:
Either of the two methods are much more easy to keep track of, and will prevent you from wanting to rip your own brain out when you can't remember what you labeled each space without looking it up on a chart.Code:printf("A1 A2 A3 B1 B2 B3 C1 C2 C3\n"); printf("A4 A5 A6 B4 B5 B6 C4 C5 C6\n"); printf("A7 A8 A9 B7 B8 B9 C7 C8 C9\n\n");
I think I like the first method better because it will allow for column and row checking easier when setting up your loops.
If you want to be really fancy, you can use both so you can run through the chess annotation when checking rows and columns, and the block annotation when checking blocks. I'll leave it up to you to figure out how this can be done.
-VASH