首页 > 编程 > .NET > 正文

VB.NET实现五子棋的人工智能(2)

2024-07-10 13:02:39
字体:
来源:转载
供稿:网友
四,处理鼠标事件

'*****************************************************************************
'** 模块名称: themousedown
'**
'** 描述: 此函数主要实行以下功能:
'** 1. 判定当前游戏标志是否有效。
'** 2. 将实际坐标转化成虚拟坐标。
'** 3. 绘制玩家的棋子。
'** 4. 执行检查获胜函数。
'** 5. 执行电脑算法函数。
'**
'*****************************************************************************
sub themousedown(byval x as integer, byval y as integer)
if theplayflag = false then
exit sub
end if
'检查游戏状态是否有效
dim i, j as integer
dim zhx, zhy as integer
zhx = int((x - 10) / 30)
zhy = int((y - 10) / 30)
for i = 0 to 9
for j = 0 to 9
if table(zhx, zhy) > 0 then
exit sub
end if
next
next
'检查当前鼠标点击的格子是否有效
dim mycolor as color
dim g as system.drawing.graphics
g = picturebox1.creategraphics
mycolor = color.white
dim brush1 as system.drawing.brush = new solidbrush(mycolor)
g.fillellipse(brush1, zhx * 30 + 10, zhy * 30 + 10, 30, 30)
'绘制玩家的棋子
table(zhx, zhy) = 2
for i = 0 to 191
if cwin(zhx, zhy, i) = true then
cflag(i) = false
end if
next
'重设电脑的获胜标志
checkwin()
'检查当前玩家是否获胜
diannao()
'调用电脑算法
end sub

  五、获胜检查算法。

'*****************************************************************************
'** 模块名称: checkwin
'**
'** 描述: 此模块执行以下功能:
'** 1. 检查是否和棋。
'** 2. 检查电脑是否获胜。
'** 3. 检查玩家是否获胜。
'**
'*****************************************************************************
sub checkwin()
dim i, j, k, m, n as integer
dim ca as integer
dim pa as integer
dim cnormal as integer = 0
for i = 0 to 191
if cflag(i) = false then
cnormal = cnormal + 1
end if
next
if cnormal = 190 then
label1.visible = true
label1.text = "和棋,请重新开始!"
picturebox1.refresh()
theplayflag = false
exit sub
end if
'设定和棋规则
for i = 0 to 191
if cflag(i) = true then
ca = 0
for j = 0 to 9
for k = 0 to 9
if table(j, k) = 1 then
if cwin(j, k, i) = true then
ca = ca + 1
end if
end if
next
next
if ca = 5 then
label1.visible = true
label1.text = "电脑获胜,请重新开始"
picturebox1.refresh()
theplayflag = false
exit sub
end if
end if
next
'检查电脑是否获胜
for i = 0 to 191
if pflag(i) = true then
pa = 0
for j = 0 to 9
for k = 0 to 9
if table(j, k) = 2 then
if pwin(j, k, i) = true then
pa = pa + 1
end if
end if
next
next
if pa = 5 then
label1.visible = true
label1.text = "玩家获胜,请重新开始"
picturebox1.refresh()
theplayflag = false
exit sub
end if
end if
next
'检查玩家是否获胜
end sub

 六、电脑算法

'*****************************************************************************
'** 模块名称: diannao
'**
'** 描述: 此程序主要执行以下功能:
'** 1. 初始化赋值系统。
'** 2. 赋值加强算法。
'** 3. 计算电脑和玩家的最佳攻击位。
'** 4. 比较电脑和玩家的最佳攻击位并决定电脑的最佳策略。
'** 5. 执行检查获胜函数。
'**
'*****************************************************************************

sub diannao()
dim i, j, k, m, n as integer
dim dc as integer
dim cab as integer
dim pab as integer
for i = 0 to 9
for j = 0 to 9
pscore(i, j) = 0
cscore(i, j) = 0
next
next
'初始化赋值数组
''' ******** 电脑加强算法 ********
for i = 0 to 191
if cflag(i) = true then
cab = 0
for j = 0 to 9
for k = 0 to 9
if table(j, k) = 1 then
if cwin(j, k, i) = true then
cab = cab + 1
end if
end if
next
next
select case cab
case 3
for m = 0 to 9
for n = 0 to 9
if table(m, n) = 0 then
if cwin(m, n, i) = true then
cscore(m, n) = cscore(m, n) + 5
end if
end if
next
next
case 4
for m = 0 to 9
for n = 0 to 9
if table(m, n) = 0 then
if cwin(m, n, i) = true then
yuandian(m * 30 + 10, n * 30 + 10)
table(m, n) = 1
for dc = 0 to 191
if pwin(m, n, dc) = true then
pflag(dc) = false
checkwin()
exit sub
end if
next
end if
end if
next
next
end select
end if
next
for i = 0 to 191
if pflag(i) = true then
pab = 0
for j = 0 to 9
for k = 0 to 9
if table(j, k) = 2 then
if pwin(j, k, i) = true then
pab = pab + 1
end if
end if
next
next
select case pab
case 3
for m = 0 to 9
for n = 0 to 9
if table(m, n) = 0 then
if pwin(m, n, i) = true then
pscore(m, n) = pscore(m, n) + 30
end if
end if
next
next
case 4
for m = 0 to 9
for n = 0 to 9
if table(m, n) = 0 then
if pwin(m, n, i) = true then
yuandian(m * 30 + 10, n * 30 + 10)
table(m, n) = 1
for dc = 0 to 191
if pwin(m, n, dc) = true then
pflag(dc) = false
checkwin()
exit sub
end if
next
end if
end if
next
next
end select
end if
next
''' ******** 电脑加强算法结束 ********

' ******** 赋值系统 ********
for i = 0 to 191
if cflag(i) = true then
for j = 0 to 9
for k = 0 to 9
if table(j, k) = 0 then
if cwin(j, k, i) = true then
for m = 0 to 9
for n = 0 to 9
if table(m, n) = 1 then
if cwin(m, n, i) = true then
cscore(j, k) = cscore(j, k) + 1
end if
end if
next
next
end if
end if
next
next
end if
next
for i = 0 to 191
if pflag(i) = true then
for j = 0 to 9
for k = 0 to 9
if table(j, k) = 0 then
if pwin(j, k, i) = true then
for m = 0 to 9
for n = 0 to 9
if table(m, n) = 2 then
if pwin(m, n, i) = true then
pscore(j, k) = pscore(j, k) + 1
end if
end if
next
next
end if
end if
next
next
end if
next
''' ******** 赋值系统结束 ********
''' ******** 分值比较算法 ********
dim a, b, c, d as integer
dim cs as integer = 0
dim ps as integer = 0
for i = 0 to 9
for j = 0 to 9
if cscore(i, j) > cs then
cs = cscore(i, j)
a = i
b = j
end if
next
next
for i = 0 to 9
for j = 0 to 9
if pscore(i, j) > ps then
ps = pscore(i, j)
c = i
d = j
end if
next
next
if cs > ps then
yuandian(a * 30 + 10, b * 30 + 10)
table(a, b) = 1
for i = 0 to 191
if pwin(a, b, i) = true then
pflag(i) = false
end if
next
else
yuandian(c * 30 + 10, d * 30 + 10)
table(c, d) = 1
for i = 0 to 191
if pwin(c, d, i) = true then
pflag(i) = false
end if
next
end if
''' ******** 分值比较算法结束 ********
checkwin()
end sub

  七、绘制棋子

'*****************************************************************************
'** 模块名称: yuandian
'**
'** 描述: 此函数主要进行电脑棋子的绘制。
'**
'*****************************************************************************

sub yuandian(byval x as integer, byval y as integer)
dim mycolor as color
dim g as system.drawing.graphics
g = picturebox1.creategraphics
dim zhx, zhy as integer
zhx = int((x - 10) / 30)
zhy = int((y - 10) / 30)
mycolor = color.black
dim brush1 as system.drawing.brush = new solidbrush(mycolor)
g.fillellipse(brush1, zhx * 30 + 10, zhy * 30 + 10, 30, 30)
end sub
注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表