Public Class Form1
Private Const LASTCOLUMN = 6
Private Const LASTROW = 5
Private CurrentPlayer As Integer = 1
Private GridControls(LASTCOLUMN, LASTROW) As PictureBox
Private Grid(LASTCOLUMN, LASTROW) As Integer
Private Function ControlRow(row As Integer)
Return Me.TableLayoutPanel1.RowCount - row - 1
End Function
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddGridControls()
End Sub
Private Sub AddGridControls()
Me.SuspendLayout()
Me.TableLayoutPanel1.SuspendLayout()
For column = 0 To LASTCOLUMN
For row = 0 To LASTROW
Dim control = New PictureBox() With {
.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top,
.SizeMode = PictureBoxSizeMode.Zoom,
.Margin = New Padding(0)
}
GridControls(column, row) = control
DrawGrid(column, row)
Me.TableLayoutPanel1.Controls.Add(control, column, ControlRow(row))
Next
Next
Me.TableLayoutPanel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
Private Sub DrawGrid()
For column = 0 To LASTCOLUMN
For row = 0 To LASTROW
DrawGrid(column, row)
Next
Next
End Sub
Private Sub DrawGrid(column As Integer, row As Integer)
Select Case Grid(column, row)
Case 0
GridControls(column, row).Image = WindowsApplication1.My.Resources.Resources.EmptyCell
Case 1
GridControls(column, row).Image = WindowsApplication1.My.Resources.Resources.Player1Cell
Case 2
GridControls(column, row).Image = WindowsApplication1.My.Resources.Resources.Player2Cell
End Select
End Sub
Private Sub NextPlayer()
If (CurrentPlayer = 1) Then
CurrentPlayer = 2
Else
CurrentPlayer = 1
End If
End Sub
Private Sub Button_Click(sender As Button, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click
Dim column = Integer.Parse(sender.Tag)
PlayColumn(column)
End Sub
Private Sub PlayColumn(Column As Integer)
For Row = 0 To LASTROW
If (Grid(Column, Row) = 0) Then
Grid(Column, Row) = CurrentPlayer
NextPlayer()
DrawGrid(Column, Row)
Return
End If
Next
Throw New System.Exception("Column " & Column & " is full")
End Sub
End Class
现代码如上,能正常进行游戏,但在获胜时没法判定,求大神补一段判定获胜代码。
Private Const LASTCOLUMN = 6
Private Const LASTROW = 5
Private CurrentPlayer As Integer = 1
Private GridControls(LASTCOLUMN, LASTROW) As PictureBox
Private Grid(LASTCOLUMN, LASTROW) As Integer
Private Function ControlRow(row As Integer)
Return Me.TableLayoutPanel1.RowCount - row - 1
End Function
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddGridControls()
End Sub
Private Sub AddGridControls()
Me.SuspendLayout()
Me.TableLayoutPanel1.SuspendLayout()
For column = 0 To LASTCOLUMN
For row = 0 To LASTROW
Dim control = New PictureBox() With {
.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top,
.SizeMode = PictureBoxSizeMode.Zoom,
.Margin = New Padding(0)
}
GridControls(column, row) = control
DrawGrid(column, row)
Me.TableLayoutPanel1.Controls.Add(control, column, ControlRow(row))
Next
Next
Me.TableLayoutPanel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
Private Sub DrawGrid()
For column = 0 To LASTCOLUMN
For row = 0 To LASTROW
DrawGrid(column, row)
Next
Next
End Sub
Private Sub DrawGrid(column As Integer, row As Integer)
Select Case Grid(column, row)
Case 0
GridControls(column, row).Image = WindowsApplication1.My.Resources.Resources.EmptyCell
Case 1
GridControls(column, row).Image = WindowsApplication1.My.Resources.Resources.Player1Cell
Case 2
GridControls(column, row).Image = WindowsApplication1.My.Resources.Resources.Player2Cell
End Select
End Sub
Private Sub NextPlayer()
If (CurrentPlayer = 1) Then
CurrentPlayer = 2
Else
CurrentPlayer = 1
End If
End Sub
Private Sub Button_Click(sender As Button, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click
Dim column = Integer.Parse(sender.Tag)
PlayColumn(column)
End Sub
Private Sub PlayColumn(Column As Integer)
For Row = 0 To LASTROW
If (Grid(Column, Row) = 0) Then
Grid(Column, Row) = CurrentPlayer
NextPlayer()
DrawGrid(Column, Row)
Return
End If
Next
Throw New System.Exception("Column " & Column & " is full")
End Sub
End Class
现代码如上,能正常进行游戏,但在获胜时没法判定,求大神补一段判定获胜代码。