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
|
#include <iostream>
#include <iomanip>
#include <array>
#include <random>
const int squares{8};
bool validMove(std::array<std::array<char, squares>, squares>&, int, int);
void cleanBoard(std::array<std::array<char, squares>, squares>&);
void printBoard(std::array<std::array<char, squares>, squares>&);
int main() {
std::array<std::array<char, squares>, squares> board;
std::array<std::array<int, squares>, squares> elimination = {22, 22, 22, 22, 22, 22, 22, 22,
22, 24, 24, 24, 24, 24, 24, 22,
22, 24, 26, 26, 26, 26, 24, 22,
22, 24, 26, 28, 28, 26, 24, 22,
22, 24, 26, 28, 28, 26, 24, 22,
22, 24, 26, 26, 26, 26, 24, 22,
22, 24, 24, 24, 24, 24, 24, 22,
22, 22, 22, 22, 22, 22, 22, 22};
int currentRow, currentColumn, testRow, testColumn, pieceCounter{0}, scale, elimRow, elimColumn;
bool complete{false};
// populate the board with 0s
cleanBoard(board);
while ( !complete ) {
int minElim{29};
for ( size_t i{0}; i < squares; i++ ) {
for ( size_t j{0}; j < squares; j++ ) {
if ( validMove(board, i, j) ) {
if ( elimination[i][j] < minElim ) {
minElim = elimination[i][j];
elimRow = i;
elimColumn = j;
}
}
}
}
board[elimRow][elimColumn] = 'Q';
++pieceCounter;
for ( size_t i{0}; i < squares; i++ ) {
if ( board[elimRow][i] == '0' )
board[elimRow][i] = '*';
}
for ( size_t i{0}; i < squares; i++ ) {
if ( board[i][elimColumn] == '0' )
board[i][elimColumn] = '*';
}
for ( size_t i{1}; i < squares - 1; i++ ) {
if ( validMove(board, elimRow + i, elimColumn + i) )
board[elimRow + i][elimColumn + i] = '*';
if ( validMove(board, elimRow - i, elimColumn - i) )
board[elimRow - i][elimColumn - i] = '*';
if ( validMove(board, elimRow + i, elimColumn - i) )
board[elimRow + i][elimColumn - i] = '*';
if ( validMove(board, elimRow - i, elimColumn + i) )
board[elimRow - i][elimColumn + i] = '*';
}
if ( pieceCounter == 8 )
complete = true;
}
printBoard(board);
}
bool validMove(std::array<std::array<char, squares>, squares>& gameBoard, int row, int col) {
return ( row < squares && row >= 0 && col < squares && col >= 0 && gameBoard[row][col] == '0');
}
void cleanBoard(std::array<std::array<char, squares>, squares>& gameBoard) {
for ( size_t i{0}; i < squares; i++ ) {
for ( size_t j{0}; j < squares; j++ ) {
gameBoard[i][j] = '0';
}
}
}
void printBoard(std::array<std::array<char, squares>, squares>& gameBoard) {
for ( size_t i{0}; i < squares; i++ ) {
for ( size_t j{0}; j < squares; j++ ) {
std::cout << std::setw(3) << gameBoard[i][j];
}
std::cout << std::endl;
}
}
|