Advertisement
jacknpoe

Eight Queens (N Queens) bigger chessboard

Nov 12th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.51 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. namespace jacknpoe {
  5.     namespace games {
  6.         typedef enum {
  7.             EQR_Success,
  8.             EQR_Fail,
  9.             EQR_TooMuchQueens,
  10.             EQR_LackOfQueens,
  11.             EQR_NoChessboard,
  12.             EQR_ErrorAlloc
  13.         } EQ_Result;
  14.  
  15.         char* EQ_ResultDescription[] = {
  16.             (char *) "Success",
  17.             (char *) "Fail",
  18.             (char *) "Too much queens",
  19.             (char *) "Lack of queens",
  20.             (char *) "No chessboard",
  21.             (char *) "Allocation fail"
  22.         };
  23.  
  24.         typedef enum {
  25.             EQA_Vertical,    // (—)
  26.             EQA_Horizontal,  // (|)
  27.             EQA_Backslash,   // (\)
  28.             EQA_Slash        // (/)
  29.         } EQ_AttackAngle;
  30.  
  31.         char* EQ_AttackAngleDescription[] = {
  32.             (char *) "Vertical",
  33.             (char *) "Horizontal",
  34.             (char *) "Backslash",
  35.             (char *) "Slash"
  36.         };
  37.  
  38.         typedef struct EQ_Square {
  39.             char x, y;
  40.         } EQ_Square;
  41.        
  42.         typedef struct EQ_Attack {
  43.             EQ_Square a, b;
  44.             EQ_AttackAngle atackangle;
  45.             EQ_Attack *next;
  46.         } EQ_Attack;
  47.        
  48.         class EightQueens {
  49.         private:
  50.             bool *Chessboard;
  51.             char Size;
  52.             EQ_Attack *First;
  53.  
  54.             void freeAttackNode( EQ_Attack *node);
  55.             void freeAllAttackNodes( void);
  56.             bool insertAttackNode( EQ_Attack **node, char x1, char y1, char x2, char y2, EQ_AttackAngle a);
  57.         public:
  58.             EightQueens( char size = 8);
  59.             ~EightQueens( void);
  60.             bool setSize( char size);
  61.             char getSize( void);
  62.             bool setSquare( char x, char y, bool queen);
  63.             bool getSquare( char x, char y);
  64.             int getNQueens( void);
  65.             EQ_Square* getQueens( void);
  66.             void resetChessboard( void);
  67.             EQ_Attack* getFirstAttack( void);
  68.             EQ_Result check( void);
  69.         };
  70.  
  71.         void EightQueens::freeAttackNode( EQ_Attack *node){
  72.             if( node->next != NULL) freeAttackNode( node->next);
  73.             std::free( node);
  74.         }
  75.  
  76.         void EightQueens::freeAllAttackNodes( void) {
  77.             if ( First != NULL) freeAttackNode( First);
  78.             First = NULL;
  79.         }
  80.  
  81.         EightQueens::EightQueens( char size) {
  82.             Size = 0; Chessboard = NULL; First = NULL;
  83.             setSize( size); resetChessboard();
  84.         }
  85.        
  86.         EightQueens::~EightQueens( void) { free( Chessboard); freeAllAttackNodes(); }
  87.  
  88.         bool EightQueens::setSize( char size) {
  89.             if( size < 4) return false;
  90.             Chessboard = (bool *) std::realloc( Chessboard, sizeof( bool) * size * size);
  91.             if( Chessboard == NULL) { Size = 0; return false; }
  92.             Size = size; return true;
  93.         }
  94.  
  95.         char EightQueens::getSize( void) { return Size; }
  96.  
  97.         bool EightQueens::setSquare( char x, char y, bool queen) {
  98.             if( x < 0 or y < 0 or x >= Size or y >= Size) return false;
  99.             Chessboard[ x * Size + y] = queen; return true;
  100.         }
  101.        
  102.         bool EightQueens::getSquare( char x, char y){
  103.             if( x < 0 or y < 0 or x >= Size or y >= Size) return false;
  104.             return Chessboard[ x * Size + y];
  105.         }
  106.  
  107.         int EightQueens::getNQueens( void){
  108.             int accumulator = 0;
  109.             for( int i = 0; i < Size * Size; i++ ) accumulator += ( Chessboard[ i] ? 1 : 0);
  110.             return accumulator;
  111.         }
  112.        
  113.         void EightQueens::resetChessboard( void){
  114.             for( int i = 0; i < Size * Size; i++ ) Chessboard[ i] = false;
  115.         }
  116.  
  117.         EQ_Attack* EightQueens::getFirstAttack( void) { return First; }
  118.  
  119.         EQ_Square* EightQueens::getQueens( void) {
  120.             int nQueens, current = 0; EQ_Square *buffer;
  121.             nQueens = getNQueens();
  122.             buffer = (EQ_Square *) malloc( sizeof( EQ_Square) * nQueens);
  123.             if( buffer == NULL) return NULL;
  124.             for( char y = 0; y < Size; y++)
  125.                 for( char x = 0; x < Size; x++)
  126.                     if( getSquare( x, y)) {
  127.                         buffer[ current].x = x;
  128.                         buffer[ current].y = y;
  129.                         current++;
  130.                     }
  131.             return buffer;
  132.         }
  133.  
  134.         bool EightQueens::insertAttackNode( EQ_Attack **node, char x1, char y1, char x2, char y2, EQ_AttackAngle a){
  135.             EQ_Attack *temp;
  136.             temp = (EQ_Attack *) malloc( sizeof( EQ_Attack));
  137.             *node = temp;
  138.             if( temp == NULL) return false;
  139.             temp->a.x = x1; temp->a.y = y1;
  140.             temp->b.x = x2; temp->b.y = y2;
  141.             temp->atackangle = a; temp->next = NULL;
  142.         }
  143.  
  144.         EQ_Result EightQueens::check( void) {
  145.             int nQueens; EQ_Attack **next, *current; EQ_Square *queens;
  146.  
  147.             if( Size < 4) return EQR_NoChessboard;
  148.  
  149.             nQueens = getNQueens();
  150.             if( nQueens < Size) return EQR_LackOfQueens;
  151.             if( nQueens > Size) return EQR_TooMuchQueens;
  152.            
  153.             freeAllAttackNodes(); next = &First;
  154.             queens = getQueens();
  155.             for( int a = 0; a < Size; a++)
  156.                 for( int b = a+1; b < Size; b++)    {
  157.                     if( queens[a].x == queens[b].x) {
  158.                         insertAttackNode( next, queens[a].x, queens[a].y, queens[b].x, queens[b].y, EQA_Vertical);
  159.                         if( *next == NULL) return EQR_ErrorAlloc;
  160.                         next = &((*next)->next);
  161.                     }
  162.  
  163.                     if( queens[a].y == queens[b].y) {
  164.                         insertAttackNode( next, queens[a].x, queens[a].y, queens[b].x, queens[b].y, EQA_Horizontal);
  165.                         if( *next == NULL) return EQR_ErrorAlloc;
  166.                         next = &((*next)->next);
  167.                     }
  168.  
  169.                     if( ( queens[a].x - queens[b].x) == ( queens[a].y - queens[b].y) ) {
  170.                         insertAttackNode( next, queens[a].x, queens[a].y, queens[b].x, queens[b].y, EQA_Backslash);
  171.                         if( *next == NULL) return EQR_ErrorAlloc;
  172.                         next = &((*next)->next);
  173.                     }
  174.  
  175.                     if( ( queens[a].x - queens[b].x) == ( queens[b].y - queens[a].y) ) {
  176.                         insertAttackNode( next, queens[a].x, queens[a].y, queens[b].x, queens[b].y, EQA_Slash);
  177.                         if( *next == NULL) return EQR_ErrorAlloc;
  178.                         next = &((*next)->next);
  179.                     }
  180.             }
  181.             return ( next == &First ? EQR_Success : EQR_Fail);
  182.         }
  183.     }
  184. }
  185.  
  186. using namespace jacknpoe::games;
  187.  
  188. int main( void) {
  189.     EightQueens myGame;
  190.     EQ_Result result;
  191.  
  192.     for( char n = 0; n < 3; n++) {
  193.         myGame.resetChessboard();
  194.  
  195.         if( n == 0) {
  196.             myGame.setSquare( 6, 0, true);
  197.             myGame.setSquare( 0, 1, true);
  198.             myGame.setSquare( 2, 2, true);
  199.             myGame.setSquare( 5, 3, true);
  200.             myGame.setSquare( 3, 4, true);
  201.             myGame.setSquare( 1, 5, true);
  202.             myGame.setSquare( 4, 6, true);
  203.             myGame.setSquare( 7, 7, true);
  204.         }
  205.         if( n == 1) {
  206.             myGame.setSquare( 5, 0, true);
  207.             myGame.setSquare( 3, 1, true);
  208.             myGame.setSquare( 6, 2, true);
  209.             myGame.setSquare( 0, 3, true);
  210.             myGame.setSquare( 7, 4, true);
  211.             myGame.setSquare( 1, 5, true);
  212.             myGame.setSquare( 4, 6, true);
  213.             myGame.setSquare( 2, 7, true);
  214.         }
  215.  
  216.         if( n == 2) {
  217.             myGame.setSquare( 5, 0, true);
  218.             myGame.setSquare( 7, 1, true);
  219.             myGame.setSquare( 6, 2, true);
  220.             myGame.setSquare( 0, 3, true);
  221.             myGame.setSquare( 4, 4, true);
  222.             myGame.setSquare( 1, 5, true);
  223.             myGame.setSquare( 3, 6, true);
  224.             myGame.setSquare( 2, 7, true);
  225.         }
  226.  
  227.         std::cout << std::endl;
  228.  
  229.         std::cout << "   0   1   2   3   4   5   6   7" << std::endl << std::endl;
  230.         for( char y = 0; y < 8; y ++)
  231.         {
  232.             for( char y1 = 0; y1 < 3; y1++) {
  233.                 if( y1 == 1) std::cout <<  (int) y << " ";
  234.                     else std::cout << "  ";
  235.                 for( char x = 0; x < 8; x++) {
  236.                     std::cout << ( myGame.getSquare( x, y) ? (char) 219 : (char) 176)
  237.                               << ( myGame.getSquare( x, y) ? (char) 219 : (char) 176)
  238.                               << ( myGame.getSquare( x, y) ? (char) 219 : (char) 176)
  239.                               << " ";
  240.                 }
  241.                 std::cout << std::endl;
  242.             }
  243.             std::cout << std::endl;
  244.         }
  245.         std::cout << std::endl;
  246.    
  247.         result = myGame.check();
  248.         std::cout << "Result: " << EQ_ResultDescription[ result ] << std::endl << std::endl;
  249.    
  250.         if( result == EQR_Fail)
  251.         {
  252.             EQ_Attack *attack;
  253.             attack = myGame.getFirstAttack();
  254.    
  255.             while( attack != NULL) {
  256.                 std::cout << "[" << (int) attack->a.x << "." << (int) attack->a.y << "] - ["
  257.                           << (int) attack->b.x << "." << (int) attack->b.y << "] - "
  258.                              << EQ_AttackAngleDescription[ attack->atackangle] << std::endl;
  259.                 attack = attack->next;
  260.             }
  261.             std::cout << std::endl;
  262.         }
  263.    
  264.         system( "PAUSE");
  265.     }
  266.  
  267.     return 0;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement