Thread: Sudoku Array Problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    Sudoku Array Problem

    Greetings everyone.

    Starting off to the point: I am trying to build a program with C++ (very basic!) that will solve Sudokus. I know there are programs out there that will do this - but I wanted to try this as a challenge to see actually if I know what I am learning. There are three steps:

    (1) Entering all known numbers.
    (2) Displaying the board.
    (3) Solving.

    (1) was pretty easy to set up. I just created an array. I haven't started (3) yet; that'll probably take some logic and time. So that leaves me at (2) - shouldn't be too hard, right? Well...

    My problem lies with displaying the board. I used some for statements, as shown below:

    Code:
    while (option == 2)
        {
            for (i=0;i<9;i++){
                for (j=0;j<9;j++){
                cout << "[" << sudoku[i][j] << "]";  
                counter++;
                if ((counter % 9) == 0)
                {
                    cout << "\t";
                }      
            }      
            cin.get();
        }          
        
    }
    (some preliminary stuff: option is my menu variable to get the user's input of which of the 3 things to do. i and j are the variables to use when displaying the board. counter is a variable that I use to determine when to put a break in the line to have 9 lines of 9 cells of data, rather than one huge glop of 81 pieces of data!)

    However, when I do this, it'll display pieces of data I entered in step (1), but it will never stop! I can keep hitting ENTER forever. It will loop back (ie; the 10th row, 1st column will have the same data piece as 1st row, 1st column). Also, while the new line part with the variable counter works, only for lines 3 and onward. Lines 1 and 2 share the same line, but then it's good for lines 3, 4, 5, 6, 7, 8, and 9.

    So here are my two questions:

    (1) How can I get the array's display of data to end after 3 lines?
    (2) How can I get the second line to be on a seperate line than line one?

    Here is my full code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
        cout << "Sudoku Solving Program";
        int sudoku[9][9];
        int a,b = 0;
        for (a=0;a<9;a++){
            for (b=0;b<9;b++){
                sudoku[a][b] = 0; }
            }    
        int go = 1;
        int row = 0;
        int column = 0;
        int knownnumber = 0;
        int menu = 1;
        int option = 1;
        int counter = 0;
        int i, j = 0;
        
        while (menu == 1)
        { 
        cout << "\nWhat do you want to do?";
        cout << "\n1 = Enter a Number";
        cout << "\n2 = View Board";
        cout << "\n3 = Solve";
        cout << "\nYour choice: ";
        cin >> option;
        
        while (go == 1 && option == 1)
        {
            cout << "\nEnter the row of a number you know: ";
            cin >> row;
            cout << "Enter the column of that number: ";
            cin >> column;
            cout << "Enter the number: ";
            cin >> knownnumber;
            sudoku[row-1][column-1] = knownnumber;
            cout << "Keep going?  1 = Yes / 0 = No: ";
            cin >> go;
        }    
        
        while (option == 2)
        {
            for (i=0;i<9;i++){
                for (j=0;j<9;j++){
                cout << "[" << sudoku[i][j] << "]";  
                counter++;
                if ((counter % 9) == 0)
                {
                    cout << "\t";
                }      
            }      
            cin.get();
        }          
        
    }
    }
    }
    Critiques on the code are also welcome! Thank you to anyone that helps (much less reads this whole post! ).

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well...

    (1) The nested for loops are right. but they're nested in an infinite while loop which is wrong.
    (2) output a '\n' or use std::endl;
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Thanks major_small! I've fixed it now - only have step (3) left to program.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Ok, I've come back with a question ...

    I've got it set up to keep generating random numbers between 1-9 in all the array's cells.

    Code:
    int d = 0;
    int e = 0;
    for (d=0;d<9;d++){
          for (e=0;e<9;e++) {
               int r = rand() % 9+1;  // r = rand() % 100
               sudoku[d][e] = r; 
          }
    }
    But how can I get it so it keeps going until a set of statements is true (ie; until the top row numbers are not equal to each other && the 2nd row numbers aren't && all rows && all columns && all 3x3 grids)? Obviously there isn't a do...until function. Thanks again.
    Last edited by Ginger_Ale; 10-30-2005 at 05:42 PM.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just use another loop - have it loop until the set of statements is true
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    A for statement or if?

    ie;
    Code:
    if (sudoku[0][0] != sudoku[0][1])
    Obviously expanded more... since those two are in the same 3x3 grid, they can't equal each other. But when I start out, all cells are 0, so it will never enter the loop in the first place. Maybe some variable to see if all cells are 0 or it is the first time solving?
    Last edited by Ginger_Ale; 10-30-2005 at 05:46 PM.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, first, a for statement isn't a loop. it only goes through once. you might want to look into a do while loop.
    Code:
    do
    {
         //code block
    }while( /*condition is true*/ );
    these loops always iterate at least once. the condition is checked at the end of the loop, instead of at the beginning as it is in the other looping styles.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Ok! To prevent repitition when writing out the do...while statement, is there a way I can do it so that all 9 numbers in a row use up the numbers 1-9 and they are all different?

    Would the following work?
    Code:
    } while (sudoku[0][0] != sudoku[0][1] != sudoku [0][2] != sudoku[0][3] != sudoku[0][4] != sudoku[0][5] != sudoku[0][6] != sudoku[0][7] != sudoku[0][8]);
    Or would that only make it so that the adjacent numbers are not equal to that number? You've been a big help.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, you do actually have alot to check, so what I'd do is write a boolean function instead of writing all that in the loop... for example:

    Code:
    bool good();	//a boolean function
    ...
    do{
    ...
    }while(!good());	//while "not good"
    ....
    bool good()
    {
        //check everything you need here and return a true if things are the way you want them to be
        //or return a false if they're not.
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I saw this problem and decided I would do it also, since it seems like a good test. Instead of checking not equal, I was thinking of making a switch/case and increment the number if it comes up, and if it increments to 2, to go back and change it.

    Either that way, or make functons like sudoku[x][0] != sudoku[x][1] so I would have a horizontal check, a vertical check and a square check, and just run the functions as needed, that way I don't need to hard code all the different places and checking them against eachother.

    While loops with functions like I mentioned would be able to check it all rapidly. I am sure there are better ways, but would these seem to be on track.

  11. #11
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Sounds like a fun project, here, too.
    or make functons like sudoku[x][0] != sudoku[x][1] so I would have a horizontal check, a vertical check and a square check
    A good place to start, depending on how you're planning on solving the puzzles.

    I'm not going to go the random number route. At least not for the primary solving algorithm. I've never met a sudoku I had to play the "what if" game with, so I'll only add that as a backup, or if I can't quite mimic the algorithm I use to solve them.
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM