AP C++
Eight Queens-Non Recursive
/********************************************************************
Notes:
C++ provides inline functions to
help reduce function-call overhead. This is especially
useful for small functions. The
inline qualifier requests (the compiler's option) that
the compiler generate a copy of the
function's code in place to avoid a function call.
The tradeoff is that multiple copies
of the function code are inserted in the program
(thus making the program larger)
rather than having a single copy of the function to
which control is passed each time
the function is called.
NOT TESTED
Using standard C++ arrays (non-ap
type). NOT TESTED
********************************************************************************************************************/
#include <iostream.h>
#include <iomanip.h>
#include <time.h>
#include <stdlib.h>
bool queenCheck( const char [][ 8 ], int, int );
void placeQueens( char [][ 8 ] );
void printBoard( const char [][ 8 ] );
void xConflictSquares( char [][ 8 ], int, int );
void xDiagonals( char [][ 8 ], int, int );
bool availableSquare( const char [][ 8 ] );
inline int validMove( const char board[][ 8 ], int row, int col ) //See note
above
{ return ( row >= 0 && row < 8 && col >= 0 && col < 8 ); }
int main()
{
char board [ 8 ][ 8 ] = { '\0' };
srand( time( 0 ) );
placeQueens( board );
printBoard( board );
return 0;
}
bool availableSquare( const char board[][ 8 ] )
{
for ( int row = 0; row < 8; ++row )
for ( int col = 0; col < 8; ++col )
if ( board[
row ][ col ] == '\0' )
return false;
// at least one open square is available
return true;
// no available squares
}
void placeQueens( char board[][ 8 ] )
{
const char QUEEN = 'Q';
int rowMove, colMove, queens = 0;
bool done = false;
while ( queens < 8 && !done )
{
rowMove = rand() % 8;
colMove = rand() % 8;
if ( queenCheck( board,
rowMove, colMove ) )
{
board[
rowMove ][ colMove ] = QUEEN;
xConflictSquares( board, rowMove, colMove );
++queens;
}
done = availableSquare( board );
}
}
void xConflictSquares( char board[][ 8 ], int row, int col )
{
for ( int loop = 0; loop < 8; ++loop )
{
// place an '*' in the row
occupied by the queen
if ( board[ row ][ loop ] == '\0' )
board[ row ][
loop ] = '*';
// place an '*' in the col occupied by
the queen
if ( board[ loop ][ col ] == '\0' )
board[ loop
][ col ] = '*';
}
// place an '*' in the diagonals occupied by the queen
xDiagonals( board, row, col );
}
bool queenCheck( const char board[][ 8 ], int row, int col )
{
int r = row, c = col;
// check row and column for a queen
for ( int d = 0; d < 8; ++d )
if ( board[ row ][ d ] == 'Q' ||
board[ d ][ col ] == 'Q' )
return false;
// check upper left diagonal for a queen
for ( int e = 0; e < 8 && validMove( board, --r, --c ); ++e )
if ( board[ r ][ c ] == 'Q' )
return false;
r = row;
c = col;
// check upper right diagonal for a queen
for ( int f = 0; f < 8 && validMove( board, --r, ++c ); ++f )
if ( board[ r ][ c ] == 'Q' )
return false;
r = row;
c = col;
// check lower left diagonal for a queen
for ( int g = 0; g < 8 && validMove( board, ++r, --c ); ++g )
if (board[ r ][ c ] == 'Q' )
return false;
r = row;
c = col;
// check lower right diagonal for a queen
for ( int h = 0; h < 8 && validMove( board, ++r, ++c ); ++h )
if ( board[ r ][ c ] == 'Q' )
return false;
return true;
// no queen in conflict
}
void xDiagonals( char board[][ 8 ], int row, int col )
{
int r = row, c = col;
// upper left diagonal
for ( int a = 0; a < 8 && validMove( board, --r, --c ); ++a )
board[ r ][ c ] = '*';
r = row;
c = col;
// upper right diagonal
for ( int b = 0; b < 8 && validMove( board, --r, ++c ); ++b )
board[ r ][ c ] = '*';
r = row;
c = col;
// lower left diagonal
for ( int d = 0; d < 8 && validMove( board, ++r, --c ); ++d )
board[ r ][ c ] = '*';
r = row;
c = col;
// lower right diagonal
for ( int e = 0; e < 8 && validMove( board, ++r, ++c ); ++e )
board[ r ][ c ] = '*';
}
void printBoard( const char board[][ 8 ] )
{
int queens = 0;
// header for columns
cout << " 0 1 2 3 4 5 6 7\n";
for ( int r = 0; r < 8; ++r )
{
cout << setw( 2 ) << r << ' ';
for ( int c = 0; c < 8; ++c )
{
cout <<
board[ r ][ c ] << ' ';
if ( board[ r
][ c ] == 'Q' )
++queens;
}
cout << '\n';
}
if ( queens == 8 )
cout << "\nEight Queens were placed
on the board!" << endl;
else
cout << '\n' << queens << " Queens
were placed on the board." << endl;
}
Sample Output