ASP 8 Queens solution

Submitted by:Tim Kelly

Date added:14 March, 2012

Category:ASP

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

Tags: queens solution

Code Snippet:

<%@ LANGUAGE=VBScript %>
<HTML><BODY>
<% Server.ScriptTimeOut=200 %>
<% If Request.Form("NumSolutions") = "" Then %>
<FORM method=Post>
<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>
 
 

Comments