java基础:围棋小程序设计。思路是什么,感觉无从下手。就只会画棋盘棋子。吃子判定形式判定该怎么下手?

java基础:围棋小程序设计。思路是什么,感觉无从下手。就只会画棋盘棋子。吃子判定形式判定该怎么下手?,第1张

高深的算法就不告诉你了,用一个最简单的方法,围棋中判定被吃的方法是无气,那最简单的方法,就是用递归去判断当前子位置的子周围是否有气(空位),上下左右,遇见白字继续递归,递归边界则是黑子或气,有任何一个位置有气,就代表不被吃。

form1:

Private Sub Com1_Click(Index As Integer)

Select Case Index

Case 0

Label3.Caption = Combo1.Text

Send "5" + Combo1.Text

Case 1

Combo1.Text = ""

Case 2

Send "3"

ResetGame

If Server = 1 Then Pic1.Enabled = True Else Pic1.Enabled = False

Case 3

If Label2.Caption = "连线中" Then

Send "4"

DoEvents

End If

Winsock1.Close

Label2.Caption = "尚未连线"

Load Form2

Form2.Show

Case 4

If Label2.Caption = "连线中" Then

Send "4"

DoEvents

End If

Winsock1.Close

End

End Select

End Sub

Private Sub Form_Load()

Open "net5.txt" For Input As #1

Do

Input #1, Gdata

Combo1.AddItem Gdata

If EOF(1) Then Close #1: Exit Sub

Loop

End Sub

Private Sub Form_Unload(Cancel As Integer)

If Label2.Caption = "连线中" Then

Send "4"

DoEvents

End If

Winsock1.Close

End

End Sub

Private Sub Pic1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

If Button = 1 Then

sx = Fix(X / 21) + 1: sy = Fix(Y / 20) + 1

If map(sx, sy) <>0 Then Exit Sub

Call sndPlaySound("put.wav", 0)

map(sx, sy) = Server

DrawScreen

a = GameOver(Server)

If a = Server Then

Image1(0).Visible = True

Win(Server) = Win(Server) + 1

If Server = 1 Then Lost(2) = Lost(2) + 1 Else Lost(1) = Lost(1) + 1

Form1.Label1(2).Caption = "战绩:" + Str(Win(1)) + "胜" + Str(Lost(1)) + "败"

Form1.Label1(4).Caption = "战绩:" + Str(Win(2)) + "胜" + Str(Lost(2)) + "败"

End If

Send "1" + Chr(sx) + Chr(sy)

Pic1.Enabled = False

End If

End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

If Winsock1.State <>sckClosed Then Winsock1.Close

Winsock1.Accept requestID

Label2.Caption = "连线中"

OpenCom

Pic1.Enabled = True

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

GetMyData bytesTotal

End Sub

Private Sub Winsock1_Connect()

Label2.Caption = "连线中"

Send "2" + Label1(3).Caption

OpenCom

End Sub

form2:

Private Sub Command1_Click()

If Server = 1 Then

Form1.Winsock1.LocalPort = 5238

Form1.Winsock1.Listen

Form1.Label1(1).Caption = "黑子:" + Text1.Text

Form1.Label1(3).Caption = ""

Form1.Label2.Caption = "等待加入中"

Else

Form1.Winsock1.RemoteHost = Text2.Text

Form1.Winsock1.RemotePort = 5238

Form1.Winsock1.LocalPort = 0

Form1.Winsock1.Connect

Form1.Label1(1).Caption = ""

Form1.Label1(3).Caption = "白子:" + Text1.Text

Form1.Label2.Caption = "正在连线到主机"

End If

Form1.Label1(2).Caption = "战绩:" + Str(Win(1)) + "胜" + Str(Lost(1)) + "败"

Form1.Label1(4).Caption = "战绩:" + Str(Win(2)) + "胜" + Str(Lost(2)) + "败"

Form1.Show

Form1.Pic1.Enabled = False

Form1.Enabled = True

Form2.Hide

Unload Form2

ResetGame

End Sub

Private Sub Command2_Click()

End

End Sub

Private Sub Form_Load()

Form1.Enabled = False

CloseCom

Text2.Text = Form1.Winsock1.LocalIP

Server = 1

Win(1) = 0

Win(2) = 0

Lost(1) = 0

Lost(2) = 0

End Sub

Private Sub Option1_Click()

Label3.Caption = "你的主机IP"

Text2.Text = Form1.Winsock1.LocalIP

Server = 1

End Sub

Private Sub Option2_Click()

Label3.Caption = "连线主机IP"

Text2.Text = "0.0.0.0"

Server = 2

End Sub

模块:

Declare Sub BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long)

Declare Function sndPlaySound Lib "Winmm.dll" Alias "sndPlaySoundA" (ByVal SoundName As String, ByVal Flags As Long) As Long

Global Const srccopy = &HCC0020

Global Const srcand = &H8800C6

Global Const srcor = &HEE0086

Public map(1 To 15, 1 To 15) '棋盘

Public Win(1 To 2), Lost(1 To 2) '储存胜败次数

Public Server '存放目前为主机或加入连线

'主机=1 加入连线=2

Sub Main()

Load Form1

Load Form2

Form1.Show

Form2.Show

End Sub

Sub DrawScreen() '画出棋子

X = 3: Y = 2

For i = 1 To 15

For j = 1 To 15

If map(i, j) = 1 Then BitBlt Form1.Pic1.hDC, X, Y, 21, 20, Form1.Pic2.hDC, 0, 0, srccopy

If map(i, j) = 2 Then BitBlt Form1.Pic1.hDC, X, Y, 21, 20, Form1.Pic2.hDC, 21, 0, srccopy

If map(i, j) = 3 Then BitBlt Form1.Pic1.hDC, X, Y, 21, 20, Form1.Pic2.hDC, 42, 0, srccopy

If map(i, j) = 4 Then BitBlt Form1.Pic1.hDC, X, Y, 21, 20, Form1.Pic2.hDC, 63, 0, srccopy

Y = Y + 20

Next j

Y = 2

X = X + 21

Next i

Form1.Pic1.Refresh

End Sub

Sub Send(mydata As String) '送出资料

Form1.Winsock1.SendData mydata

End Sub

Sub GetMyData(Size) '接受资料

Dim mydata As String

Form1.Winsock1.GetData mydata, vbString

Select Case Mid(mydata, 1, 1)

Case "1" '棋子的资料

If Server = 1 Then s = 2 Else s = 1

Call sndPlaySound("put.wav", 0)

sx = Asc(Mid(mydata, 2, 1))

sy = Asc(Mid(mydata, 3, 1))

map(sx, sy) = s + 2

DrawScreen

map(sx, sy) = s

a = GameOver(s)

Form1.Pic1.Enabled = True

If a = s Then

Form1.Image1(1).Visible = True

Form1.Pic1.Enabled = False

Lost(Server) = Lost(Server) + 1

Win(s) = Win(s) + 1

Form1.Label1(2).Caption = "战绩:" + Str(Win(1)) + "胜" + Str(Lost(1)) + "败"

Form1.Label1(4).Caption = "战绩:" + Str(Win(2)) + "胜" + Str(Lost(2)) + "败"

End If

Case "2" '名字资料

If Server = 1 Then

Form1.Label1(3).Caption = Mid(mydata, 2, Size - 1)

Send "2" + Form1.Label1(1).Caption

Else

Form1.Label1(1).Caption = Mid(mydata, 2, Size - 1)

End If

Case "3" '开新棋局送的资料

ResetGame

Case "4" '对方断线送的资料

Form1.Label2.Caption = "尚未连线"

Form1.Pic1.Enabled = False

CloseCom

Form1.Winsock1.Close

Case "5" '聊天视窗的资料

Form1.Label1(0).Caption = Mid(mydata, 2, Size - 1)

Case Else

End Select

End Sub

Function GameOver(a) '判断是否连成五颗

For i = 1 To 11

For j = 1 To 11

If map(i, j) = a And map(i + 1, j + 1) = a And map(i + 2, j + 2) = a And map(i + 3, j + 3) = a And map(i + 4, j + 4) = a Then GameOver = a: Exit Function

Next j

Next i

For i = 5 To 15

For j = 5 To 15

If map(i, j) = a And map(i - 1, j - 1) = a And map(i - 2, j - 2) = a And map(i - 3, j - 3) = a And map(i - 4, j - 4) = a Then GameOver = a: Exit Function

Next j

Next i

For i = 5 To 15

For j = 1 To 11

If map(i, j) = a And map(i - 1, j + 1) = a And map(i - 2, j + 2) = a And map(i - 3, j + 3) = a And map(i - 4, j + 4) = a Then GameOver = a: Exit Function

Next j

Next i

For i = 1 To 11

For j = 5 To 15

If map(i, j) = a And map(i + 1, j - 1) = a And map(i + 2, j - 2) = a And map(i + 3, j - 3) = a And map(i + 4, j - 4) = a Then GameOver = a: Exit Function

Next j

Next i

For i = 1 To 15

For j = 5 To 11

If map(i, j) = a And map(i, j - 1) = a And map(i, j - 2) = a And map(i, j - 3) = a And map(i, j - 4) = a Then GameOver = a: Exit Function

If map(i, j) = a And map(i, j + 1) = a And map(i, j + 2) = a And map(i, j + 3) = a And map(i, j + 4) = a Then GameOver = a: Exit Function

Next j

Next i

For i = 5 To 11

For j = 1 To 15

If map(i, j) = a And map(i + 1, j) = a And map(i + 2, j) = a And map(i + 3, j) = a And map(i + 4, j) = a Then GameOver = a: Exit Function

If map(i, j) = a And map(i - 1, j) = a And map(i - 2, j) = a And map(i - 3, j) = a And map(i - 4, j) = a Then GameOver = a: Exit Function

Next j

Next i

GameOver = 0

End Function

Sub ResetGame() '开新棋局

Form1.Pic1.Cls

For i = 1 To 15

For j = 1 To 15

map(i, j) = 0

Next j

Next i

Form1.Image1(0).Visible = False

Form1.Image1(1).Visible = False

End Sub

Sub CloseCom() '将能送出资料的控制项除能

Form1.Com1(0).Enabled = False

Form1.Com1(2).Enabled = False

End Sub

Sub OpenCom() '将能送出资料的控制项致能

Form1.Com1(0).Enabled = True

Form1.Com1(2).Enabled = True

End Sub

如果不懂要添加的控件,请留下邮箱,我将工程VBP文件发给你。

这里还有聊天功能。

希望能给点分!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12023445.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存