Click here to Skip to main content
11,490,801 members (76,082 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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.
Posted 7-Nov-12 16:31pm
Edited 7-Nov-12 17:02pm
enhzflep40.6K
v2
Comments
Mohibur Rashid at 8-Nov-12 23:27pm
   
school teach you how how to write code, you will have to find out the solution. If you dont know how to solve this one then you are doomed. even most dumbest one in my school solved it.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
enhzflep at 8-Nov-12 21:59pm
   
(Originally posted by OP as a solution)

Those just tell me how to place them on the board they don't tell me how to write code to solve the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 476
1 Andy Lanng 288
2 Sascha Lefèvre 240
3 OriginalGriff 178
4 Maciej Los 160
0 Sergey Alexandrovich Kryukov 9,713
1 OriginalGriff 8,475
2 Sascha Lefèvre 3,544
3 Maciej Los 3,106
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2015
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