Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: C++ | Size: 2.73 KB | Hits: 87 | Expires: Never
This paste has a previous version, view the difference. 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.     for (int i=0; i < ROW; i++) {
  28.         for (int j=0; j< COL; j++) {
  29.             cout << "|_" << chessBoard[i][j] << "_| ";
  30.         }
  31.         cout << endl;
  32.     }
  33. }
  34.  
  35.  
  36. void populate() { // Placing the first queen.
  37.     static char chessBoard[ROW][COL];
  38.     int x, y;
  39.  
  40.     do {
  41.         srand(time(NULL)); // random position
  42.         x = rand()%COL;
  43.         y = rand()%ROW;
  44.         while (chessBoard[x][y] != 'Q' && chessBoard[x][y] != 'x') {
  45.             chessBoard[x][y] = 'Q';
  46.         } // queen
  47.         checkSides(chessBoard, x, y); // fills horizontal/vertical with Xs
  48.         checkDiag(chessBoard, x, y); // diagonal checks
  49.         checkNull(chessBoard);
  50.     } while (checkNull);
  51.     display_board(chessBoard); // displays the board
  52. }
  53.  
  54.  
  55. // Checks if there's an empty space in the chessboard
  56. bool checkNull(char chessBoard[][COL]) {
  57.     bool isFilled = false;
  58.     for (int i = 0; i < ROW; i++) {
  59.         for (int j = 0; j < COL; j++) {
  60.             if (chessBoard[i][j] != NULL) {
  61.                 isFilled = true;
  62.                 break;
  63.             }// if
  64.         }// for j
  65.     }// for i  
  66.     return isFilled;
  67. }
  68.    
  69. //fills vert/horizontal with Xs
  70. void checkSides(char chessBoard[][COL], int x, int y) {
  71.     for (int i =0; i < ROW; i++) {
  72.         if (chessBoard[i][y] != 'Q' && chessBoard[i][y] == NULL) {
  73.             chessBoard[i][y] = 'x'; //vertical
  74.         }
  75.     }
  76.     for (int i=0; i < COL; i++) {
  77.         if (chessBoard[x][i] != 'Q' && chessBoard[x][i] == NULL) {
  78.             chessBoard[x][i] = 'x'; // horizontal
  79.         }
  80.     }
  81. }
  82.  
  83. // fills with Xs diagonally
  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. }