在excel中制作五子棋讲解
五子棋是一种两人对弈的纯策略型棋类游戏,通常双方分别使用黑白两色的棋子;下面是有在excel中制作五子棋讲解,欢迎参阅。
首先在excel表格模式时,画两个单选框将一个按钮,然后参照我的另外一篇百度经验”怎样通过excel计算出九宫格“,双击按钮进入到代码模式。
图中红色宽中主要讲述对棋盘的设置;
图中红色所讲述的是,在表1被激活时,单选框1被选中;
图中红色所讲述的是,若单选框1被选中时,鼠标所选的单元格被赋值为实心圆,赋值后,更换为单选框2被选中;若当单选框2被选中时,鼠标所选的单元格被赋值为空心圆,赋值后,更换为单选框1被选中。到此时,五子棋的下子程序已经完成了,剩余判断胜负的程序了。
我把判断胜负的程序,分为了4部分;
判断竖直胜利部分;
判断水平胜利部分;
判断左倾斜胜利部分;
判断右倾斜胜利部分;
每一部分原理都类似,这里只讲述竖直胜利的情况。
判断竖直胜负的原理:当黑子方走完最后一颗实心黑子时,只要判断以该颗棋子为中心的9颗竖直排列的棋子中,是否有5颗连续为实心黑子,若有,则胜利;
所有程序:
Private Sub CommandButton1_Click()
Range("E5:AD30").ClearContents
With Range("E5:AD30").Font
.Name = "宋体"
.FontStyle = "bold"
.Size = 12
End With
With ActiveSheet.Cells
.ColumnWidth = 2.5
.RowHeight = 17.5
End With
End Sub
Private Sub Worksheet_Activate()
OptionButton1.Value = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'五子棋部分
If ActiveCell.Row < 31 And ActiveCell.Row > 4 And ActiveCell.Column < 31 And ActiveCell.Column > 4 Then
If OptionButton1.Value = True And ActiveCell.Value = "" Then
ActiveCell.Value = "●"
OptionButton2.Value = True
ElseIf OptionButton2.Value = True And ActiveCell.Value = "" Then
ActiveCell.Value = "○"
OptionButton1.Value = True
End If
a = 0
'判断竖胜部分
For i = -4 To 4
If Sheet1.Cells(ActiveCell.Row, ActiveCell.Column - i) = ActiveCell.Value Then
a = a + 1
Else
a = 0
End If
If a = 5 Then
If ActiveCell.Value = "●" Then
MsgBox "黑方胜"
Range("E5:AD30").ClearContents
Else
MsgBox "白方胜"
Range("E5:AD30").ClearContents
End If
Exit For
End If
Next
'判断横胜部分
For i = -4 To 4
If Sheet1.Cells(ActiveCell.Row - i, ActiveCell.Column) = ActiveCell.Value Then
a = a + 1
Else
a = 0
End If
If a = 5 Then
If ActiveCell.Value = "●" Then
MsgBox "黑方胜"
Range("E5:AD30").ClearContents
Else
MsgBox "白方胜"
Range("E5:AD30").ClearContents
End If
Exit For
End If
Next
'判断左斜胜部分
For i = -4 To 4
If Sheet1.Cells(ActiveCell.Row - i, ActiveCell.Column - i) = ActiveCell.Value Then
a = a + 1
Else
a = 0
End If
If a = 5 Then
If ActiveCell.Value = "●" Then
MsgBox "黑方胜"
Range("E5:AD30").ClearContents
Else
MsgBox "白方胜"
Range("E5:AD30").ClearContents
End If
Exit For
End If
Next
'判断右斜胜部分
For i = -4 To 4
If Sheet1.Cells(ActiveCell.Row + i, ActiveCell.Column - i) = ActiveCell.Value Then
a = a + 1
Else
a = 0
End If
If a = 5 Then
If ActiveCell.Value = "●" Then
MsgBox "黑方胜"
Range("E5:AD30").ClearContents
Else
MsgBox "白方胜"
Range("E5:AD30").ClearContents
End If
Exit For
End If
Next
End If
End Sub