VB icon

An 8 queen puzzle solver

Email
Submitted on: 1/3/2015 1:48:00 AM
By: KYG (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 1637
 
     solves the 8 queen puzzle using random generator and recursion. results in very fast answers for a chess board of any size, specified in the code
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: An 8 queen puzzle solver
// Description:solves the 8 queen puzzle using random generator and recursion. results in very fast answers for a chess board of any size, specified in the code
// By: KYG (from psc cd)
//**************************************

//By koon yiak
//feel free to use the code and
//to tell me about possible improvements
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
const int chessboardsize=40;	//size of chessboard
enum{empty, queen, used};	//status
struct move{				//movement direction
	int x;
	int y;
};
struct chessboard{			//chessboard type
int size;
int square[chessboardsize][chessboardsize];
int usedsquares;
int noqueens;
};
void setqueenmovdi(move *whatmove, chessboard *board, move *curcoor);
void main()
{
	move curcoor;
	move NE; NE.x=1; NE.y=1;	//define digonal movements
	move NW; NW.x=-1; NW.y=1;	
	move SE; SE.x=1; SE.y=-1;	
	move SW; SW.x=-1; SW.y=-1;
	
	chessboard board;
	board.size=chessboardsize;
//	srand(time(0));
	while(board.noqueens!=board.size ){
		board.usedsquares=0;
		board.noqueens=0;
		for(int i=0;i<board.size;i++)for(int j=0;j<board.size;j++)board.square[i][j]=0;
		while(board.usedsquares<board.size*board.size)
		{
			curcoor.x=rand()%board.size;
			curcoor.y=rand()%board.size;;
			if(board.square[curcoor.x][curcoor.y]==empty){
				board.square[curcoor.x][curcoor.y]=queen;
				++board.usedsquares;
				++board.noqueens;
			}else continue;
			for(int horiz=0;horiz<board.size;horiz++)if(board.square[horiz][curcoor.y]==empty){board.square[horiz][curcoor.y]=used;++board.usedsquares;}
			for(int verti=0;verti<board.size;verti++)if(board.square[curcoor.x][verti]==empty){board.square[curcoor.x][verti]=used;++board.usedsquares;}
			board.square[curcoor.x][curcoor.y]=queen;
			setqueenmovdi(&NE,&board,&curcoor);
			setqueenmovdi(&NW,&board,&curcoor);
			setqueenmovdi(&SE,&board,&curcoor);
			setqueenmovdi(&SW,&board,&curcoor);
		}
	}
	cout<<endl;
	for(int i2=0;i2<board.size;i2++){
		for(int j2=0;j2<board.size;j2++)cout<<board.square[i2][j2];
		cout<<endl;
	}
}
void setqueenmovdi(move *whatmove,chessboard *board,move *curcoor)
{
	move next;
	next.x=curcoor->x+whatmove->x; next.y=curcoor->y+whatmove->y;
	if(next.x<0||next.x>board->size-1||next.y<0||next.y>board->size-1)return;
	if(board->square[next.x][next.y]==empty){
		board->square[next.x][next.y]=used;
		++board->usedsquares;
	}
	setqueenmovdi(whatmove,board,&next);
	return;
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.