#include <iostream.h>

// Function Prototype

void increment_chessboard(int num, int array[30], int reset_value) ;
int test_chessboard(int num, int array[30]) ;
void print_chessboard(int num, int array[30]) ;

// ********************************************************
void main(void)
{
int chessboard[30] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   			1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} ;

   int num = 0 ;
   int result ;
   int counter = 0 ;
   int reset_value = 1 ;

   cout << "Please input the size of the chess board you wish to use." ;
   cout << endl << endl ;
   cout << "Example, if you want a 4x4 size chess board input the number 4." ;
   cout << endl << endl ;
   cout << "Input number (maximum size 30) >> " ;
   cin >> num ;

   double loop = 1 ;
	for(int i =0; i<num; i++)
   {
   	loop = loop * num ;
   }

   cout << "This program will display all acceptable " ;
   cout << "results for the Queens Problem." ;
   cout << endl << endl ;

	for(long int a=0; a<loop; a++)
   {
      result = test_chessboard(num, chessboard) ;

      if(result == 1)
		{
	   	print_chessboard(num, chessboard) ;
         counter++ ;
      }

   	increment_chessboard(num, chessboard, reset_value) ;
   }

   cout << endl << endl ;
   cout << "Number of possible combinations is : " << counter ;
   cout << endl << endl ;
   cout << "This program has finished, press any key to end." ;

   }
// *************************************************************
/*This function where the queens are placed on the chess board.*/
//**************************************************************

void increment_chessboard(int num, int array[30], int reset_value)
{
   int counter = num - 1 ;

	while(array[counter] >= num)
   {
   	array[counter] = reset_value ;

      counter = counter - 1;
   }

	array[counter]++ ;
}


// ***********************************************************
/*This function with test the current chess board to see it any of the different queen peices can see each other.
A result of 1 equals true, correct and acceptable result.
A result of 0 equals an unaccaptable result.*/
//**************************************************************

int test_chessboard(int num, int array[30])
{
int result = 1 ;

   for(int a=0; a<(num -1); a++)
   {
   	for(int i=a+1; i<num; i++)
   	{
   		if(array[a] == array[i])
         {
         	result = 0 ;
         }
   	}
}

	for(int b=0; b<(num -1); b++)
   {
		for(int c=b+1; c<num; c++)
		{
			if(array[c] == (array[b] + (c-b)))
			{
				result = 0 ;
			}
		}
	}

	for(int d=0; d<(num -1); d++)
	{
		for(int e=d+1; e<num; e++)
		{
			if(array[d] == (array[e] + (e-d)))
         {
         	result = 0 ;
         }
   	}
   }
	return result ;
}

// *************************************************************
/*This function just prints the current values for each queens piece on the chess board.*/
//**************************************************************

void print_chessboard(int num, int array[30])
{
   for(int a=0; a<num; a++)
   {
		cout << array[a] << " " ;
   }
   	cout << endl ;
}
