Chess board program.

I'm supposed to be writing a program for a programming class that involves taking an infinitely scalable chess board, and randomly placing queen pieces on it in such a way that no two can take each other. I've got almost all of my code busted out, it displays and all, but I keep getting an issue where one of my queens doesn't show up (still interacts with the board, kind of), and I get a random 'X' floating around.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <cstdio>

using namespace std;

#define BOARD_W 4
#define BOARD_H 4

void generate(char[][BOARD_H]);
void display(char[][BOARD_H]);
bool validate(char);
bool validate(char[][BOARD_H], int, int);
void mark(char[][BOARD_H], int, int);
bool check(char[][BOARD_H]);
//bool check(char[][BOARD_H],int,int);

int main()
{
    bool finish = false, valid_reply = false;
    char board[BOARD_W][BOARD_H];
    char reply;
    int count1, count2;
    
    while(finish != true)
    {
                 for(count1 = 0; count1 < BOARD_W; count1++)
                 {
                            for(count2 = 0; count2 < BOARD_H; count2++)
                            {
                                       board[count1][count2] = '.';
                            }
                 }
                 generate(board);
                 display(board);
                 
                 //Asks for another go, and ends if told no.
                 while(valid_reply != true)
                 {
                                   cout << "\n\nWould you like to generate another configuration? (y/n)";
                                   cin >> reply;
                                   valid_reply = validate(reply);
                                   if(reply == 'n')
                                       finish = true;
                                   else
                                       finish = false;
                 }
                 valid_reply = false;
    }
}

void generate(char board[][BOARD_H])
{
     int W;
     int H;
     bool valid = false;
     do
     {
          W = rand()%(BOARD_W);
          H = rand()%(BOARD_H);
          while(board[W][H] != '.')
          {
                          W = rand()%BOARD_W;
                          H = rand()%BOARD_H;
          }
          mark(board, W, H);
          board[W][H] = 'Q';
          cout << endl << H + 1 << " " << W + 1 << " " << check(board) << endl; //DEBUG LINE  */
     }
     while(check(board) != true);
}

void display(char board[][BOARD_H])
{
     int count, count1;
     for(count1 = 0; count1 < BOARD_H; count1 ++)
     {
                cout << "\n";
                for(count = 0; count < BOARD_W; count++)
                {
                          cout << " -";
                }
                cout << " \n";
                for(count = 0; count < BOARD_W; count++)
                {
                          cout << "|" << board[count][count1];
                }
                cout << "|";
     }
     cout << "\n";
     for(count = 0; count < BOARD_W; count++)
     {
                cout << " -";
     }
}

bool validate(char reply)
{
     if(reply == 'y' || reply == 'n')
              return true;
     else
              return false;
}

void mark(char board[][BOARD_H], int W, int H)
{
     int W1, H1, count1;
     W1 = W;
     H1 = H;
     while(W1 > 0 && H1 > 0)
     {
            W1-- && H1--;
            board[W1][H1] = 'X';
     }
     W1 = W;
     H1 = H;
     while(W1 <= BOARD_W && H1 > 0)
     {
            W1++ && H1--;
            board[W1][H1] = 'X';
     }
     W1 = W;
     H1 = H;
     while(W1 > 0 && H1 <= BOARD_H)
     {
            W1-- && H1++;
            board[W1][H1] = 'X';
     }
     W1 = W;
     H1 = H;
     while(W1 <= BOARD_W && H1 <= BOARD_H)
     {
            W1++ && H1++;
            board[W1][H1] = 'X';
     }
     for(count1 = 0; count1 < BOARD_W; count1++)
     {
                board[count1][H] = 'X';
     }
     for(count1 = 0; count1 < BOARD_H; count1++)
     {
                board[W][count1] = 'X';
     }
     board[W][H] = 'Q';
     return;
}

bool check(char board[][BOARD_H])
{
     bool done = true;
     int count, count2;
     for(count = 0; count < BOARD_W; count++)
     {
               for(count2 = 0; count2 < BOARD_H; count2++)
               {
                          if(board[count][count2] == '.')
                                                  done = false;
               }
     }
     return done;
}

/*bool check(char board[][BOARD_H], int W, int H)
{
     int W1, H1, count1;
     W1 = W;
     H1 = H;
     while(W1 > 0 && H1 > 0)
     {
            W1-- && H1--;
            if(board[W1][H1] == 'Q')
                             return false;
     }
     W1 = W;
     H1 = H;
     while(W1 <= BOARD_W && H1 > 0)
     {
            W1++ && H1--;
            if(board[W1][H1] == 'Q')
                             return false;
     }
     W1 = W;
     H1 = H;
     while(W1 > 0 && H1 <= BOARD_H)
     {
            W1-- && H1++;
            if(board[W1][H1] == 'Q')
                             return false;
     }
     W1 = W;
     H1 = H;
     while(W1 <= BOARD_W && H1 <= BOARD_H)
     {
            W1++ && H1++;
            if(board[W1][H1] == 'Q')
                             return false;
     }
     for(count1 = 0; count1 < BOARD_W; count1++)
     {
                if(board[W1][H1] == 'Q')
                             return false;
     }
     for(count1 = 0; count1 < BOARD_H; count1++)
     {
                if(board[W1][H1] == 'Q')
                             return false;
     }
     return true;
}
*/


I realize it's probably very sloppy, and 1 of my includes is unneccessary. Right now, I just need a sounding board, someone to look it over and find that one little thing that keeps throwing me for a loop.
i think you are trying to develop the famous 8 queen problem. am i correct?
where no queen comes in the way of any other queen.

see you are generating the positions randomly, there is no guarantee that the queens will be placed this way.. it can take any amount of time for this.. because your algo is based on chance.. secondly if it didnt find any position on any specific row then what will happen, it will go to next row.. if i am correct?
Yes, just with the ability to scale it (thus my #defines). Any Ideas what could possibly be up with my code?
i suggest you should recreate your logic using back tracking.. i wrote this problem some time back and you can have a look at it:

http://www.codeproject.com/KB/cpp/8Queen_Problem.aspx


i create it on windows though with a very good interface, if you are working on windows then you can compile the code and look at it.
i am posting something which will be useful to you. check the next post.
take a board:
BOOL **g_QueenArray; //allocate memory to according to your #defines

1
2
3
4
5
6
//allocate memory, instead of g_BoardSize use your #defines.
g_QueenArray = (BOOL**)calloc(g_BoardSize, sizeof(BOOL));
for(i = 0;i < g_BoardSize;i++)
{
    *(g_QueenArray + i) = (BOOL*)calloc(g_BoardSize, sizeof(BOOL));
} 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//this is the main function, it will return when it places all the queens correctly in g_QueenArray.
//otherwise it will return false. X and Y are row and column positions. see if you find anything useful in this and can create your own program.
int Backtracking(HWND hWnd, int Pos_X, int Pos_Y)
{
    int New_Y;
    RECT    rect;    

    if(g_BoardSize == 0)
        return TRUE;

    if(Pos_X == g_BoardSize)
        return TRUE;

    while(Pos_Y < g_BoardSize)
    {
        if(!SetNextQueen(Pos_X, Pos_Y, &New_Y))
            return false;

        int OneBoxSize = WND_WIDTH / g_BoardSize;

        Pos_Y = New_Y;    

        rect.left = Pos_Y * OneBoxSize;
        rect.right = (Pos_Y + 1) * (OneBoxSize);
        rect.top = Pos_X * OneBoxSize;
        rect.bottom = (Pos_X + 1) * OneBoxSize;
        
        InvalidateRect(hWnd, &rect, TRUE);
        //UpdateWindow(hWnd);
        Sleep(400);
        if(g_Stop)
        {
            g_Stop = FALSE;
            return TRUE;
        }

        if(Backtracking(hWnd, Pos_X + 1, 0))
        {
            return TRUE;
        }
        else
        {
            rect.left = Pos_Y * OneBoxSize;
            rect.right = (Pos_Y + 1) * (OneBoxSize);
            rect.top = Pos_X * OneBoxSize;
            rect.bottom = (Pos_X + 1) * OneBoxSize;

            g_QueenArray[Pos_X][Pos_Y++] = FALSE;
            
            InvalidateRect(hWnd, &rect, TRUE);
            UpdateWindow(hWnd);
            Sleep(400);
            
            if(Pos_Y == g_BoardSize)
                return false;
        }
    }
    
    return TRUE;
} 
Holy crap. I understood... 15% of that.

I'm doing this for an OOP course in college. My primary language is VB.Net (foraying into C/C++, no windows yet), so none of that made any sense.

The page seems to have a bit of helpful info, though. Thanks.
lets make it a bit easy.. wat say??? if it solves your problem..

actually your design will not be able to solve the problem which are doing.

see the problem is this:
for each row you calculate a position where the queen can fit. but after 3-4 rows there will be no position left to fit the queen and your program will move to next row. so some rows will be left blank. correct??

in this case you need to go back to previous rows and recheck the positions and again move forward.. if still you didn't succeed then again move back and reposition.. this process will continue till you place all your queens.

and this process is called back tracking.. you move back and again rethink and move forward..till you succeed.
Topic archived. No new replies allowed.