can someone improve it (chess game)

May 23, 2010 at 3:09pm
I've got almost all of my code busted out, but one of my queens doesn't show up (still interacts with the board, kind of), and i get a random 'X' floating around.. please, someone should look at my code and find a little thing that keeps throwing me for a loop..

And i want someone to help improve my code using classes.. thanks

i posted my code in the beginners forum but, the respond is very limited..
http://www.cplusplus.com/forum/beginner/24147/


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
#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;
}
May 23, 2010 at 3:42pm
could not reproduce your problem
May 24, 2010 at 3:13am
yes, i know i'm not suppose to re-post my problem.. but, i posted it here because i want to compile my code on the Unix Environment and the respond in beginners forums is very limited..
May 24, 2010 at 3:19am
A good place to post problems like this is the General C++ forum. But... don't repost or ask it to be moved.

I get no Xs floating around.

-Albatross
May 24, 2010 at 4:00am
1
2
3
4
bool validate(char reply)
{
     return (reply == 'y' || reply == 'n');
}
May 24, 2010 at 4:14am
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
void initBoard(char** board, int w, int h)
{
    for(int i(0); i < w; ++i)
    {
        for(int j(0); j < w; ++j)
        {
            board[i][j] = '.';
        }
    }
}

int main()
{
    char board[BOARD_W][BOARD_H];

    bool finish(false);
    while(!finish)
    {
        initBoard(board, BOARD_W, BOARD_H)
        generate(board);
        display(board);

        //Asks for another go, and ends if told no.
        char reply(0);
        while(true)
        {
            cout << "\n\nWould you like to generate another configuration? (y/n)";
            cin >> reply;
            if (validate(reply) && reply == 'n')
            {
                finish = true;
                break;
            }
        }
    }
}
May 24, 2010 at 4:23am
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
void initBoard(char** board, int w, int h)
{
    for(int i(0); i < w; ++i)
        for(int j(0); j < w; ++j)
            board[i][j] = '.';
}
void generate(char** board, int w, int h)
{
    do
    {
        int x(0);
        int y(0);
        do
        {
            x = rand() % w;
            y = rand() % h;
        } while(board[x][y] != '.');
        mark(board, x, y);
        board[x][y] = 'Q';
    }
    while(!check(board));
}
int main()
{
    char board[BOARD_W][BOARD_H];

    bool finish(false);
    while(!finish)
    {
        initBoard(board, BOARD_W, BOARD_H);
        generate(board, BOARD_W, BOARD_H);
        display(board);

        //Asks for another go, and ends if told no.
        char reply(0);
        while(true)
        {
            cout << "\n\nWould you like to generate another configuration? (y/n)";
            cin >> reply;
            if (validate(reply) && reply == 'n')
            {
                finish = true;
                break;
            }
        }
    }
}
Last edited on May 24, 2010 at 4:30am
Topic archived. No new replies allowed.