Important alert: (current site time 7/15/2013 10:13:07 PM EDT)
 

VB icon

ASP 8 Queens solution

Email
Submitted on: 6/9/2000 8:03:44 AM
By: Richard Lindsay 
Level: Beginner
User Rating: By 5 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 17867
 
     To find the solutions to the 8 queens puzzle. The puzzle asks "How can you place 8 Queens on a chess board so that no Queen can attack any other queen?"
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: ASP 8 Queens solution
' Description:To find the solutions to the 8 queens puzzle. The puzzle asks "How can you place 8 Queens on a chess board so that no Queen can attack any other queen?"
' By: Richard Lindsay
'
' Inputs:All the user has to input is the number of solutions they would like to see. (There are 92 possible ways to arrange 8Queens on a chess board so that no Queen can attack any other queen.
'
' Returns:The result is a series of graphical displays that show how to arrange 8 Queens on a chess board so that no Queen can attack any other queen.
'
' Assumes:A recursive Algorithm is used. I copied my own VB code (also available on PlanetSourceCode) Only minor changes were required to run it as ASP. I generally do the "Hello World" exercise then I use the 8 Queens puzzle as my way of learning any new programming language. It forces me to learn most of the basic language structures (multi dimentional arrays, Functions, Display and/or printing, getting input etc.) By the time I finish the 8 Queens puzzle I usually feel pretty comfortable with a new language.
'
' Side Effects:There are nearly 17,000,000 different ways to arrange 8 Queens on a chess board so if you ask to see all 92 solutions this code has to try them all. On a slower computer that can take 2 or 3 minutes since this is a script not compiled code.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6222&lngWId=4'for details.'**************************************

<%@ LANGUAGE=VBScript %>
<HTML><BODY>
<% Server.ScriptTimeOut=200 %>
<% If Request.Form("NumSolutions") = "" Then %>
<FORM method=Post>
This web page can show you all the solutions to the 8-Queens puzzle. There are 92 
possible solutions. Please enter the number of solutions you would like to see then 
click the submit button.<BR><BR>
This demonstrates the use of functions in an ASP page. A recursive algorithim 
is used to solve the problem. If you are familiar with VB and want to do a compairison
you can look at my 8 Queens solution in the VB section of www.planetsourcecode.com. 
This is almost a pure cut and paste. It only took about 10 minutes to make it into 
an ASP application. The only real changes are in the Sub UpdateDisplay() other than 
that all I did was delete all the data typing since in VBScript everything is a Variant.<BR><BR>
<INPUT name=NumSolutions>
<INPUT type=submit value="Submit">
<% Else %>
<%
Dim NumToFind
Dim mblnBoard(7, 7)
Dim solutions
NumToFind = Request.Form ("NumSolutions")
bln8Queens(0)
Function blnCheckUp(ByVal intRow, ByVal intColumn) 
blnCheckUp = True
Do Until intRow = 0 Or intColumn = 0
intRow = intRow - 1
intColumn = intColumn - 1
If mblnBoard(intRow, intColumn) Then
blnCheckUp = False
Exit Function
End If
Loop
End Function
Function blnCheckDown(ByVal intRow, ByVal intColumn) 
blnCheckDown = True
Do Until intRow = 7 Or intColumn = 0
intRow = intRow + 1
intColumn = intColumn - 1
If mblnBoard(intRow, intColumn) Then
blnCheckDown = False
Exit Function
End If
Loop
End Function
Function blnCheckAcross(ByVal intRow, ByVal intColumn) 
blnCheckAcross = True
Do Until intColumn = 0
intColumn = intColumn - 1
If mblnBoard(intRow, intColumn) Then
blnCheckAcross = False
Exit Function
End If
Loop
End Function
Function blnIsSafe(ByVal intRow, ByVal intColumn) 
blnIsSafe = blnCheckUp(intRow, intColumn) And blnCheckDown(intRow, intColumn) And blnCheckAcross(intRow, intColumn)
End Function
Sub UpdateDisplay()
	Dim intRow
	Dim intColumn 
	Response.Write "<HR>Solution #" & solutions & "<BR>"
	Response.Write "<table border=1 align=Left width=190 cellborder=1 cellspacing=0>"
	For intRow = 0 to 7
		Response.Write "<TR>"
		For intColumn = 0 to 7
			If (intColumn + intRow) MOD 2 = 1 Then
				Response.Write "<td bgcolor =""#000000"""
			Else
				Response.Write "<td" 
			End If
			If mblnBoard(intRow,intColumn) = True Then
				Response.Write " align=center><font color =""#FF0066""> Q </font></td>"
			Else
				Response.Write ">   </td>"
			End If
		Next
		Response.Write "</tr>"
	Next
	Response.Write "</table><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>"
End Sub
Function bln8Queens(ByVal intColumn) 
Dim intRow
Do
If intColumn = 7 And blnIsSafe(intRow, intColumn) Then
mblnBoard(intRow, intColumn) = True
	solutions = solutions + 1
UpdateDisplay 
If CInt(solutions) >= CInt(Request.Form ("NumSolutions")) Then 
		bln8Queens = True
mblnBoard(intRow, intColumn) = False
Exit Function 
End If
mblnBoard(intRow, intColumn) = False
intRow = intRow + 1
Else 
If blnIsSafe(intRow, intColumn) Then 
mblnBoard(intRow, intColumn) = True 
bln8Queens = bln8Queens(intColumn + 1)
mblnBoard(intRow, intColumn) = False
End If 
intRow = intRow + 1 
End If
Loop Until bln8Queens = True Or intRow = 8 
End Function
%>
<% End If %>
</BODY></HTML>


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
10/11/2000 3:59:02 PMArindam Basu

Excellent Code, but really complex for me to understand.
(If this comment was disrespectful, please report it.)

 
7/15/2001 3:19:18 AMSteven Noonan

This code doesn't work for me :(
I type in 1 and it comes up blank!
(If this comment was disrespectful, please report it.)

 
2/14/2003 9:28:35 PM

I am a chess player and I can't figure it out by hand. I don't have ASP but do have VB6. Can you send me the VB6 code?
(If this comment was disrespectful, please report it.)

 
3/20/2003 8:36:00 PM

Bravo!
(If this comment was disrespectful, please report it.)

 

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.