井字棋asp程序的设计与智能算法

XINXINWANLULU 2015-05-06 04:04:05
本程序主要目的是完成一个具有人机博弈功能的井字棋程序并通过开发过程加深本人对人机博弈智能算法的认识。本程序采用asp编写,采用极大极小值算法设计机器的预判能力。
程序设计主要思路:
1.获取棋盘的棋子位置
2.生成搜索树,步骤1获取电脑下子可能下到棋盘的所有位置,步骤2获取电脑下子后用户能够下子的位置
3.通过估值函数获取搜索树最底层每一个用户下子位置的价值
4.获取用户下子位置估值后结果的最大值,和电脑下子的最小值,最终机算出电脑下子的最佳步法
5.输出下子结果

下面是测试地址和程序代码。
测试地址:http://wap.qt3g.com/c.asp
代码如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>井字游戏 - 晴天建站</title>
</head>
<body>
<%
dim t,y,p,tk,zuidd
dim sto(8)
dim stb(8)
dim zuiy
str=Request.QueryString("str")
if str="" then
str="0,0,0,0,0,0,0,0,0" '当前棋盘棋子位置
end if



hzi(str)
for j=0 to t-1
'response.write(sto(j) & "<br />")
if guzhi(sto(j))=100 then
ajie=sto(j)
else
if guzhi(sto(j))=-100 then
bjie=sto(j)
end if
end if
bzi(sto(j))
for k=0 to y-1
guz=guzhi(stb(k))
'response.write(stb(k) & "估值"&guz&"<br />")
if k=0 then
zuix=guz
else
zuix=zuix&","&guz
end if
next
zuixx=zuixi(zuix)
'response.write("最小值" & zuixx & "<br />")
if j=0 then
zuidd=zuixx
else
zuidd=zuidd&","&zuixx
end if
next
if ajie<>"" then
youjie=ajie
else
if bjie<>"" then
youjie=ajie
else
call zuidi()
youjie=sto(zuiy)
'response.write("最优解" & youjie & "<br />")
end if
end if
if ajie<>"" then
response.write("你输了!</br>")
else
if bjie<>"" then
response.write("你赢了!</br>")
end if
end if
youjk=split(youjie,",")
youjk1=split(youjie,",")
for p=0 to 8
if youjk(p)="0" then
youjk1(p)="白子"
tk=join(youjk1,",")
youjk1(p)="0"
if ajie<>"" then
youjk(p)="空格"
else
youjk(p)="<a href='?str=" & tk & "'>空格</a>"
end if
end if
if p=2 or p=5 or p=8 then
youjk(p)=youjk(p)&"</br>"
end if
next

response.write join(youjk," ")&"</br><a href='?'>重新开局</a>"









sub hzi(str)
t=0
str=split(str,",")
for i=0 to 8
if str(i)="0" then
t=t+1
str(i)="黑子"
sto(t-1)=join(str,",")
str(i)="0"
end if
next
end sub

sub bzi(str)
y=0
str=split(str,",")
for i=0 to 8
if str(i)="0" then
y=y+1
str(i)="白子"
stb(y-1)=join(str,",")
str(i)="0"
end if
next
end sub


function zuixi(str)
stk=split(str,",")
for i=0 to ubound(stk)
if i=0 then
zuixi=stk(0)
else
if stk(i)<zuixi then
zuixi=stk(i)
end if
end if
next
end function

function zuidi()
dim zuidt,stk,i
stk=split(zuidd,",")
for i=0 to ubound(stk)
if i=0 then
zuiy=0
zuidt=stk(0)
else
if cint(stk(i))>cint(zuidt) then
zuidt=stk(i)
zuiy=i
end if
end if
next
end function


function guzhi(str)
dim a,b,i,j,zhi
a=0
b=0
stk=split(str,",")
if stk(0)="黑子" and stk(1)="黑子" and stk(2)="黑子" then zhi=100
if stk(0)="黑子" and stk(3)="黑子" and stk(6)="黑子" then zhi=100
if stk(0)="黑子" and stk(4)="黑子" and stk(8)="黑子" then zhi=100
if stk(6)="黑子" and stk(7)="黑子" and stk(8)="黑子" then zhi=100
if stk(1)="黑子" and stk(4)="黑子" and stk(7)="黑子" then zhi=100
if stk(2)="黑子" and stk(5)="黑子" and stk(8)="黑子" then zhi=100
if stk(2)="黑子" and stk(4)="黑子" and stk(6)="黑子" then zhi=100
if stk(3)="黑子" and stk(4)="黑子" and stk(5)="黑子" then zhi=100

if stk(0)="白子" and stk(1)="白子" and stk(2)="白子" then zhi=-100
if stk(0)="白子" and stk(3)="白子" and stk(6)="白子" then zhi=-100
if stk(0)="白子" and stk(4)="白子" and stk(8)="白子" then zhi=-100
if stk(6)="白子" and stk(7)="白子" and stk(8)="白子" then zhi=-100
if stk(1)="白子" and stk(4)="白子" and stk(7)="白子" then zhi=-100
if stk(2)="白子" and stk(5)="白子" and stk(8)="白子" then zhi=-100
if stk(2)="白子" and stk(4)="白子" and stk(6)="白子" then zhi=-100
if stk(3)="白子" and stk(4)="白子" and stk(5)="白子" then zhi=-100

if zhi="" then
sth=split(str,",")
for i=0 to 8
if sth(i)="0" then
sth(i)="黑子"
end if
next

if sth(0)="黑子" and sth(1)="黑子" and sth(2)="黑子" then a=a+1
if sth(0)="黑子" and sth(3)="黑子" and sth(6)="黑子" then a=a+1
if sth(0)="黑子" and sth(4)="黑子" and sth(8)="黑子" then a=a+1
if sth(6)="黑子" and sth(7)="黑子" and sth(8)="黑子" then a=a+1
if sth(1)="黑子" and sth(4)="黑子" and sth(7)="黑子" then a=a+1
if sth(2)="黑子" and sth(5)="黑子" and sth(8)="黑子" then a=a+1
if sth(2)="黑子" and sth(4)="黑子" and sth(6)="黑子" then a=a+1
if sth(3)="黑子" and sth(4)="黑子" and sth(5)="黑子" then a=a+1


for i=0 to 8
if stk(i)="0" then
stk(i)="白子"
end if
next
if stk(0)="白子" and stk(1)="白子" and stk(2)="白子" then b=b+1
if stk(0)="白子" and stk(3)="白子" and stk(6)="白子" then b=b+1
if stk(0)="白子" and stk(4)="白子" and stk(8)="白子" then b=b+1
if stk(6)="白子" and stk(7)="白子" and stk(8)="白子" then b=b+1
if stk(1)="白子" and stk(4)="白子" and stk(7)="白子" then b=b+1
if stk(2)="白子" and stk(5)="白子" and stk(8)="白子" then b=b+1
if stk(2)="白子" and stk(4)="白子" and stk(6)="白子" then b=b+1
if stk(3)="白子" and stk(4)="白子" and stk(5)="白子" then b=b+1

guzhi=a-b+20
else
guzhi=zhi
end if

end function


%>

</body>
</html>
...全文
269 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_aspnet 2015-05-14
  • 打赏
  • 举报
回复
snfqbujj1 2015-05-07
  • 打赏
  • 举报
回复
搞这么累,你去网上搜索一下JS橡棋吧,就那么点代码。 http://www.yxsss.com/apps/elfk/ 拼图 http://www.yxsss.com/apps/pintu/ 俄罗斯方块 给你参考!

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧