vb四环棋的实现,平面四子棋
首先我们百度一下,什么是平面四子棋
相信很多小伙伴见到这幅图片都不陌生。
那么在代码中怎么实现呢?我们用vb代码为例子。
先看效果图
我们设计o和x是需要下的棋子,如果没有棋子,那就是空白字符。
首先先设计数组大小为
Dim a(7, 9) As String
我们可以选择1~8可以下棋子。
If ComboBox1.Text = "" And a(1, 1) = " " Then
SureMove(1, "x")
ElseIf ComboBox1.Text = "2" And a(1, 2) = " " Then
SureMove(2, "x")
ElseIf ComboBox1.Text = "3" And a(1, 3) = " " Then
SureMove(3, "x")
ElseIf ComboBox1.Text = "4" And a(1, 4) = " " Then
SureMove(4, "x")
ElseIf ComboBox1.Text = "5" And a(1, 5) = " " Then
SureMove(5, "x")
ElseIf ComboBox1.Text = "6" And a(1, 6) = " " Then
SureMove(6, "x")
ElseIf ComboBox1.Text = "7" And a(1, 7) = " " Then
SureMove(7, "x")
ElseIf ComboBox1.Text = "8" And a(1, 8) = " " Then
SureMove(8, "x")
ElseIf a(1, 1) = " " Then
SureMove(1, "x")
我从上面开始算下来,找到一个没有下过棋的数组位置。如果这一整列都下完了,那就不可以下这里了。
'找出第一个没有下过的地方下旗子
Private Sub SureMove(ByVal choose As Integer, ByVal c As String)
Dim top = 6
While a(top, choose) <> " "
top = top - 1
End While
a(top, choose) = c
Print()
If a(1, 1) <> " " Then
ComboBox1.Items.Remove("1")
ElseIf a(1, 2) <> " " Then
ComboBox1.Items.Remove("2")
ElseIf a(1, 3) <> " " Then
ComboBox1.Items.Remove("3")
ElseIf a(1, 4) <> " " Then
ComboBox1.Items.Remove("4")
ElseIf a(1, 5) <> " " Then
ComboBox1.Items.Remove("5")
ElseIf a(1, 6) <> " " Then
ComboBox1.Items.Remove("6")
ElseIf a(1, 7) <> " " Then
ComboBox1.Items.Remove("7")
ElseIf a(1, 8) <> " " Then
ComboBox1.Items.Remove("8")
End If
If IsFinish(top, choose, c) = True Then
If c = "o" Then
MsgBox("劳拉:耶,我赢了")
Else
MsgBox("劳拉:好吧,你赢了")
End If
ongame = False
Button1.Text = "开始"
End If
End Sub
检查一下是否某一方赢了。
这里我们需要
1’竖线检查
2 ‘横线检查
3 ‘斜对角1检查
4 ‘斜对角2检查
Private Function IsFinish(ByVal choosex As Integer, ByVal choosey As Integer, ByVal c As String) As Object
'竖线检查
Dim j = 0
For i = 1 To 6
If a(i, choosey) = c Then
j = j + 1
If j >= 4 Then
Return True
End If
Else
j = 0
End If
Next
'横线检查
j = 0
For i = 1 To 8
If a(choosex, i) = c Then
j = j + 1
If j >= 4 Then
Return True
End If
Else
j = 0
End If
Next
'斜对角1检查
j = 0
Dim p = choosex
Dim q = choosey
While p > 1 And p < 6 And q > 1 And q < 8
p = p - 1
q = q - 1
End While
While p >= 1 And p <= 6 And q >= 1 And q <= 8
If a(p, q) = c Then
j = j + 1
If j >= 4 Then
Return True
End If
Else
j = 0
End If
p = p + 1
q = q + 1
End While
'斜对角2检查
j = 0
p = choosex
q = choosey
While p > 1 And p < 6 And q > 1 And q < 8
p = p - 1
q = q + 1
End While
While p >= 1 And p <= 6 And q >= 1 And q <= 8
If a(p, q) = c Then
j = j + 1
If j >= 4 Then
Return True
End If
Else
j = 0
End If
p = p + 1
q = q - 1
End While
'全部情况都遍历了还是错误
Return False
End Function
有想要完整代码,或者有想要其他语言实现这个小游戏的,都可以找我。欢迎留言交流。q:2316773638
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/2874.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...