Thread: sudoku solver

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    sudoku solver

    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.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    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;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  8. #8
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14
    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:

    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");
    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.

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me with my sudoku solver
    By Tharps in forum Game Programming
    Replies: 6
    Last Post: 12-17-2011, 11:45 PM
  2. Sudoku solver...
    By matsp in forum C++ Programming
    Replies: 24
    Last Post: 02-14-2011, 08:30 PM
  3. Sudoku solver
    By M-S-H in forum C Programming
    Replies: 10
    Last Post: 12-15-2009, 03:26 PM
  4. sudoku solver
    By manav in forum Game Programming
    Replies: 11
    Last Post: 02-03-2008, 10:38 PM
  5. Help for a sudoku Solver
    By axilleask in forum C Programming
    Replies: 3
    Last Post: 11-26-2007, 04:28 PM