Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: C++ | Size: 2.68 KB | Hits: 47 | Expires: Never
Copy text to clipboard
  1. #include <iostream>
  2. #define ROW 10
  3. #define COL 10
  4. using namespace std;
  5.  
  6. void display_board();
  7. void populate();
  8. void checkDiag(char [][COL], int, int);
  9. void checkSides(char [][COL], int, int);
  10. bool checkNull(char [][COL]);
  11.  
  12. int main() {
  13.     int option;
  14.    
  15.     do {
  16.         populate();
  17.         cout << "Anoter configuration?" << endl;
  18.         cout << "1) Yes" << endl << "2) Quit" << endl;
  19.         cin >> option;
  20.     } while (option != 2);
  21.    
  22.     system("pause");
  23.     return 0;
  24. }
  25.  
  26. void display_board(char chessBoard[][COL]) {
  27.    
  28.     for (int i=0; i < ROW; i++) {
  29.         for (int j=0; j< COL; j++) {
  30.             cout << "|_" << chessBoard[i][j] << "_| ";
  31.         }
  32.         cout << endl;
  33.     }
  34. }
  35.  
  36.  
  37. void populate() { // Placing the first queen.
  38.     static char chessBoard[ROW][COL];
  39.     int x, y;
  40.  
  41.     do {
  42.         srand(time(NULL)); // random position
  43.         x = rand()%COL;
  44.         y = rand()%ROW;
  45.         while (chessBoard[x][y] != 'Q' && chessBoard[x][y] != 'x') {
  46.             chessBoard[x][y] = 'Q';
  47.         } // queen
  48.         checkSides(chessBoard, x, y); // fills horizontal/vertical with Xs
  49.         checkDiag(chessBoard, x, y); // diagonal checks
  50.         checkNull(chessBoard);
  51.     } while (checkNull);
  52.     display_board(chessBoard); // displays the board
  53. }
  54.  
  55.  
  56. // Checks if there's an empty space in the chessboard
  57. bool checkNull(char chessBoard[][COL]) {
  58.     bool isFilled = false;
  59.     for (int i = 0; i < ROW; i++) {
  60.         for (int j = 0; j < COL; j++) {
  61.             if (chessBoard[i][j] != NULL) {
  62.                 isFilled = true;
  63.                 break;
  64.             }// if
  65.         }// for j
  66.     }// for i  
  67.     return isFilled;
  68. }
  69.    
  70.  
  71. void checkSides(char chessBoard[][COL], int x, int y) {
  72.     for (int i =0; i < ROW; i++) {
  73.         if (chessBoard[i][y] != 'Q' && chessBoard[i][y] == NULL) {
  74.             chessBoard[i][y] = 'x'; //vertical
  75.         }
  76.     }
  77.     for (int i=0; i < COL; i++) {
  78.         if (chessBoard[x][i] != 'Q' && chessBoard[x][i] == NULL) {
  79.             chessBoard[x][i] = 'x'; // horizontal
  80.         }
  81.     }
  82. }
  83.  
  84. void checkDiag(char chessBoard[][COL], int x, int y) {
  85.     for (int i = 0 ; i < 10; i++) {
  86.         if (chessBoard[x+i][y+i] == NULL && chessBoard[x+i][y+i] != 'Q')
  87.             chessBoard[x+i][y+i] = 'x';  
  88.    
  89.         if (chessBoard[x+i][y-i] == NULL && chessBoard[x+i][y-i] != 'Q')
  90.             chessBoard[x+i][y-i] = 'x';
  91.    
  92.         if (chessBoard[x-i][y-i] == NULL && chessBoard[x+i][y-i] != 'Q')
  93.             chessBoard[x-i][y-i] = 'x';
  94.    
  95.         if (chessBoard[x-i][y+i] == NULL && chessBoard[x-i][y+i] != 'Q')            
  96.             chessBoard[x-i][y+i] = 'x';   // diagonal X
  97.     }
  98. }