Can someone help me simplify this?

This is a discussion on Can someone help me simplify this? within the C Programming forums, part of the General Programming Boards category; This function seems really bulky and I'm pretty sure that it can be simplified down. Can someone help me do ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    12

    Can someone help me simplify this?

    This function seems really bulky and I'm pretty sure that it can be simplified down. Can someone help me do this?

    Code:
    int sd_solve(int x, int y, int n){              //Checks if n is a valid number to input
    
        int i,j,k;
                                                    //Checking the boxes
                                                    //For this, we will divide the sudoku into
                                                    //nine sections (top-left, top-mid, top-right,
                                                    //mid-left, mid-mid, mid-right, bottom-left
                                                    //bottom-mid, bottom-right) and check if n is
                                                    //already in the section that we are trying to
                                                    //solve.
    
        if (y<3) {                                //If n will be in the top section
            if (x<3) {                                //If n will be in the top-left
                for(i=0;i<3;i++){
                    for(j=0;j<3;j++){
                        if(n==sd_finish[i][j]) {
                            return 0;
                        }
                    }
                }
            }
            else if(x<6) {                           //If n will be in the top-mid
                for(i=3;i<6;i++){
                     for(j=0;j<3;j++){
                        if(n==sd_finish[i][j]) {
                            return 0;
                        }
                    }
                }
            }
            else {                                      //If n will be in the top-right
                for(i=6;i<9;i++){
                     for(j=0;j<3;j++){
                        if(n==sd_finish[i][j]) {
                            return 0;
                        }
                    }
                }
            }
        }
        else if(y<6) {                           //If n will be in the mid section
            if(x<3) {                                //If n will be in the mid-left
                for(i=0;i<3;i++){
                     for(j=3;j<6;j++){
                        if(n==sd_finish[i][j]) {
                            return 0;
                        }
                    }
                }
            }
            else if(x<6) {                           //If n will be in the mid-mid
                for(i=3;i<6;i++) {
                    for(j=3;j<6;j++) {
                        if(n==sd_finish[i][j]) {
                            return 0;
                        }
                    }
                }
            }
            else {                                      //If n will be in the mid-right
                for(i=6;i<9;i++) {
                    for(j=3;j<6;j++) {
                        if(n==sd_finish[i][j]) {
                            return 0;
                        }
                    }
                }
            }
        }
        else {                                      //If n will be in the bottom section
            if(x<3) {                                //If n will be in the bottom-left
                for(i=0;i<3;i++) {
                    for(j=6;j<9;j++) {
                        if(n==sd_finish[i][j]) {
                            return 0;
                        }
                    }
                }
            }
            else if(x<6) {                           //If n will be in the bottom-mid
                for(i=3;i<6;i++) {
                    for(j=6;j<9;j++) {
                        if(n==sd_finish[i][j]) {
                            return 0;
                        }
                    }
                }
            }
            else {                                      //If n will be in the bottom-right
                for(i=6;i<9;i++) {
                    for(j=6;j<9;j++) {
                        if(n==sd_finish[i][j]) {
                            return 0;
                        }
                    }
                }
            }
        }
    
        for (j = 0; j < 9; j++) {                   //Checks the row and column n is in
            if (n == sd_finish[j][y] || n == sd_finish[x][j]) {
                return 0;
            }
        }
    
        return n;                                   //If everything is fine, it returns n
    }

    It's part of a Sudoku code and another function is basically calling this function with a possible value n when it sees a blank and checks the box the number will be in and the horizontal and vertical possibilities.

    I'm almost positive this can be simplified it's just that I can't figure it out :/

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Didn't I just show you how to simplify that yesterday?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simplify recursive function
    By MattJ812 in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2011, 04:31 PM
  2. Simplify Fraction Help
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2009, 01:17 PM
  3. Need to simplify/shorten this code. Help.
    By Lonck in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 03:23 AM
  4. Can someone simplify this statement from this thead
    By cdalten in forum C Programming
    Replies: 1
    Last Post: 01-30-2006, 07:04 AM
  5. simplify this?
    By markg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 08:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21