国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

VB.NET實(shí)現(xiàn)五子棋的人工智能(2)

2024-07-10 13:02:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
四,處理鼠標(biāo)事件

'*****************************************************************************
'** 模塊名稱: themousedown
'**
'** 描述: 此函數(shù)主要實(shí)行以下功能:
'** 1. 判定當(dāng)前游戲標(biāo)志是否有效。
'** 2. 將實(shí)際坐標(biāo)轉(zhuǎn)化成虛擬坐標(biāo)。
'** 3. 繪制玩家的棋子。
'** 4. 執(zhí)行檢查獲勝函數(shù)。
'** 5. 執(zhí)行電腦算法函數(shù)。
'**
'*****************************************************************************
sub themousedown(byval x as integer, byval y as integer)
if theplayflag = false then
exit sub
end if
'檢查游戲狀態(tài)是否有效
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
'檢查當(dāng)前鼠標(biāo)點(diǎn)擊的格子是否有效
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
'重設(shè)電腦的獲勝標(biāo)志
checkwin()
'檢查當(dāng)前玩家是否獲勝
diannao()
'調(diào)用電腦算法
end sub

  五、獲勝檢查算法。

'*****************************************************************************
'** 模塊名稱: checkwin
'**
'** 描述: 此模塊執(zhí)行以下功能:
'** 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 = "和棋,請(qǐng)重新開始!"
picturebox1.refresh()
theplayflag = false
exit sub
end if
'設(shè)定和棋規(guī)則
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 = "電腦獲勝,請(qǐng)重新開始"
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 = "玩家獲勝,請(qǐng)重新開始"
picturebox1.refresh()
theplayflag = false
exit sub
end if
end if
next
'檢查玩家是否獲勝
end sub

 六、電腦算法

'*****************************************************************************
'** 模塊名稱: diannao
'**
'** 描述: 此程序主要執(zhí)行以下功能:
'** 1. 初始化賦值系統(tǒng)。
'** 2. 賦值加強(qiáng)算法。
'** 3. 計(jì)算電腦和玩家的最佳攻擊位。
'** 4. 比較電腦和玩家的最佳攻擊位并決定電腦的最佳策略。
'** 5. 執(zhí)行檢查獲勝函數(shù)。
'**
'*****************************************************************************

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
'初始化賦值數(shù)組
''' ******** 電腦加強(qiáng)算法 ********
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
''' ******** 電腦加強(qiáng)算法結(jié)束 ********

' ******** 賦值系統(tǒng) ********
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
''' ******** 賦值系統(tǒng)結(jié)束 ********
''' ******** 分值比較算法 ********
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
''' ******** 分值比較算法結(jié)束 ********
checkwin()
end sub

  七、繪制棋子

'*****************************************************************************
'** 模塊名稱: yuandian
'**
'** 描述: 此函數(shù)主要進(jìn)行電腦棋子的繪制。
'**
'*****************************************************************************

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
注冊(cè)會(huì)員,創(chuàng)建你的web開發(fā)資料庫(kù),
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 绥德县| 晋江市| 丹凤县| 集贤县| 石楼县| 长兴县| 鄂托克前旗| 蓝田县| 正安县| 化隆| 苍南县| 九龙坡区| 浑源县| 六安市| 阳谷县| 东海县| 瓦房店市| 榆社县| 保山市| 石狮市| 剑川县| 南通市| 同德县| 馆陶县| 耒阳市| 平山县| 简阳市| 华池县| 金山区| 黎川县| 定日县| 台北县| 丰原市| 洛浦县| 班戈县| 罗田县| 木兰县| 松原市| 张家港市| 兴义市| 嘉黎县|