Click here to Skip to main content

Member 9581529 asked:

Open original thread
I have been working on this problem for awhile now and there seems to be a problem with the "good" function which is supposed to check the the board each time a new Queen is laid to make sure it is legal. I believe it has something to do with the final two loops of the function but I can not figure out what. It always seems to allow a Queens to be places diagonally of each other. (Diagonal from top right to bottom left.) PLEASE HELP! Thanks!
 
bool good()
{
    int counter = 0;
    for(int i = 0; i < MAX; i++)
    {
        for(int n = 0; n < MAX; n++)
        {
            if(board[i][n] == 1)
            {
                counter++;
                if(counter >= 2)
                {
                    return false;
                }
            }
        }
        counter = 0;
    }
    for(int n = 0; n < MAX; n++)
    {
        for(int i = 0; i < MAX; i++)
        {
            if(board[i][n] == 1)
            {
                counter++;
                if(counter >= 2)
                {
                    return false;
                }
            }
        }
        counter = 0;
    }
    int n = 0;
    for(int s = 0; s < MAX; s++)
    {
        for(int i = s; i < MAX; i++)
        {
                if(board[i][n] == 1)
                {
                    counter++;
                    if(counter >= 2)
                    {
                        return false;
                    }
                }
            n++;
        }
        n = 0;
        counter = 0;
    }
    int i = 0;
    for(int s = 0; s < MAX; s++)
    {
        for(int n = s; n < MAX; n++)
        {
                if(board[i][n] == 1)
                {
                    counter++;
                    if(counter >= 2)
                    {
                        return false;
                    }
                }
            i++;
        }
        i = 0;
        counter = 0;
    }
    n = 0;
    for(int s = 7; s <= 0; s--)
    {
        for(int i = s; i <= 0; i--)
        {
            if(board[i][n] == 1)
            {
                counter++;
                if(counter >= 2)
                {
                    return false;
                }
            }
            n++;
        }
        n = 0;
        counter = 0;
    }
    i = 0;
    for(int s = 7; s < MAX; s++)
    {
        for(int n = s; n <= 0; n--)
        {
            if(board[i][n] == 1)
            {
                counter++;
                if(counter >= 2)
                {
                    return false;
                }
            }
            i++;
        }
        i = 0;
        counter = 0;
    }
    return true;
}
 
Edit: Code wrapped with <pre> tags.
Tags: C++

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100