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;
}
| |