Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class GameBoard
- Dim Board(6, 5) As Integer
- Dim col0 As Integer = 0
- Dim col1 As Integer = 0
- Dim col2 As Integer = 0
- Dim col3 As Integer = 0
- Dim col4 As Integer = 0
- Dim col5 As Integer = 0
- Dim Col6 As Integer = 0
- Dim OffsetFromLeft As Integer
- Dim OffsetFromTop As Integer
- Dim Player1 As New Player(Form1.txtPlayer1Name.Text, 1)
- Dim Player2 As New Player(Form1.txtPlayer2Name.Text, 2)
- Dim currentPlayerColor As Integer = 1
- Dim currentPlayerName As String
- 'Sets the current player to player1. Is called at load
- Private Sub setInitialPlayer(ByVal Player As Player)
- currentPlayerName = Player.Name()
- currentPlayerColor = Player.Color
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- setInitialPlayer(Player1)
- ClearBoardArray(Board)
- End Sub
- 'Creates a red token picture box, with coordinates taken in as paramaters
- Private Sub createRedPicbox(ByVal OffsetFromLeft As Integer, ByVal OffsetFromTop As Integer)
- Dim ctlPicbox As New PictureBox
- ctlPicbox.BackgroundImage = My.Resources.red
- ctlPicbox.Location = New System.Drawing.Point(OffsetFromLeft, OffsetFromTop)
- ctlPicbox.Size = CType(New System.Drawing.Point(55, 55), Drawing.Size)
- Me.Controls.Add(ctlPicbox)
- End Sub
- 'Creates a black token picture box the same way
- Private Sub createBlackPicbox(ByVal OffsetFromLeft As Integer, ByVal OffsetFromTop As Integer)
- Dim ctlPicbox As New PictureBox
- ctlPicbox.BackgroundImage = My.Resources.black
- ctlPicbox.Location = New System.Drawing.Point(OffsetFromLeft, OffsetFromTop)
- ctlPicbox.Size = CType(New System.Drawing.Point(55, 55), Drawing.Size)
- Me.Controls.Add(ctlPicbox)
- End Sub
- 'Sets all values in the Integer array Board(,) to 9, which represents an empty space
- Private Sub ClearBoardArray(ByRef Board(,) As Integer)
- Dim row As Integer = 6
- Dim column As Integer
- Do While row >= 0
- For column = 5 To 0 Step -1
- Board(row, column) = 9
- Next
- row = -1
- column = 5
- Loop
- End Sub
- 'coordinates for picture box determined by the column corresponding to the button clicked, and the current row counter for that column
- Sub DropTokenPicture(ByVal Column As Integer, ByVal row As Integer)
- Select Case Column
- Case 0
- OffsetFromLeft = 14
- Case 1
- OffsetFromLeft = 85
- Case 2
- OffsetFromLeft = 150
- Case 3
- OffsetFromLeft = 220
- Case 4
- OffsetFromLeft = 290
- Case 5
- OffsetFromLeft = 360
- Case 6
- OffsetFromLeft = 425
- End Select
- If row = 0 Then
- OffsetFromTop = 442
- ElseIf row = 1 Then
- OffsetFromTop = 377
- ElseIf row = 2 Then
- OffsetFromTop = 309
- ElseIf row = 3 Then
- OffsetFromTop = 240
- ElseIf row = 4 Then
- OffsetFromTop = 170
- ElseIf row = 5 Then
- OffsetFromTop = 100
- End If
- If currentPlayerColor = 1 Then
- createRedPicbox(OffsetFromLeft, OffsetFromTop)
- Else
- createBlackPicbox(OffsetFromLeft, OffsetFromTop)
- End If
- End Sub
- 'Swaps the currentPlayerName and currentPlayerColor with the other player's
- Private Sub TakeTurn(ByRef currentPlayerName As String)
- If currentPlayerName = Form1.txtPlayer1Name.Text Then
- currentPlayerName = Form1.txtPlayer2Name.Text
- currentPlayerColor = Player2.Color
- Else
- currentPlayerColor = Player1.Color
- currentPlayerName = Form1.txtPlayer1Name.Text
- End If
- End Sub
- 'Sets the corresponding slot to 1 for a red token (player 1) 2 for a black token (player 2)
- Private Sub DropToken(ByVal Column As Integer, ByVal row As Integer)
- Board(Column, row) = currentPlayerColor
- End Sub
- 'Event handler for the first column's button click event. Calls the DropTokenPicture DropToken checkWinner and takeTurn methods, also increments the column's row counter(col0)
- Private Sub btnCol0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol0.Click
- Try
- DropTokenPicture(0, col0)
- DropToken(0, col0)
- checkWinner(Board, 0, col0)
- col0 += 1
- TakeTurn(currentPlayerName)
- Catch ex As Exception
- Dim Str As String = "Column Full"
- MessageBox.Show(Str)
- End Try
- End Sub
- Private Sub btnCol1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol1.Click
- Try
- DropTokenPicture(1, col1)
- DropToken(1, col1)
- checkWinner(Board, 1, col1)
- col1 += 1
- TakeTurn(currentPlayerName)
- Catch ex As Exception
- Dim Str As String = "Column Full"
- MessageBox.Show(Str)
- End Try
- End Sub
- Private Sub btnCol2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol2.Click
- Try
- DropTokenPicture(2, col2)
- DropToken(2, col2)
- checkWinner(Board, 2, col2)
- col2 += 1
- TakeTurn(currentPlayerName)
- Catch ex As Exception
- Dim Str As String = "Column Full"
- MessageBox.Show(Str)
- End Try
- End Sub
- Private Sub btnCol3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol3.Click
- Try
- DropTokenPicture(3, col3)
- DropToken(3, col3)
- checkWinner(Board, 3, col3)
- col3 += 1
- TakeTurn(currentPlayerName)
- Catch ex As Exception
- Dim Str As String = "Column Full"
- MessageBox.Show(Str)
- End Try
- End Sub
- Private Sub btnCol4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol4.Click
- Try
- DropTokenPicture(4, col4)
- DropToken(4, col4)
- checkWinner(Board, 4, col4)
- col4 += 1
- TakeTurn(currentPlayerName)
- Catch ex As Exception
- Dim Str As String = "Column Full"
- MessageBox.Show(Str)
- End Try
- End Sub
- Private Sub btnCol5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol5.Click
- Try
- DropTokenPicture(5, col5)
- DropToken(5, col5)
- checkWinner(Board, 5, col5)
- col5 += 1
- TakeTurn(currentPlayerName)
- Catch ex As Exception
- Dim Str As String = "Column Full"
- MessageBox.Show(Str)
- End Try
- End Sub
- Private Sub btnCol6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol6.Click
- Try
- DropTokenPicture(6, Col6)
- DropToken(6, Col6)
- checkWinner(Board, 6, Col6)
- 'INSERT CODE TO DISPLAY UPDATE DB, DISPLAY NEW SCOREBOARD AND CLOSE THE GAMEBOARD FORM
- Col6 += 1
- TakeTurn(currentPlayerName)
- Catch ex As Exception
- Dim Str As String = "Column Full"
- MessageBox.Show(Str)
- End Try
- End Sub
- 'Checks horizontally around the token that was just placed for tree adjacent matching tokens. returns true if there are 4 in a row
- Private Function checkHorizontal(ByRef Board As Integer(,), ByVal Column As Integer, ByVal Row As Integer) As Boolean
- Dim numcheckleft As Integer = 0
- Dim numcheckright As Integer = 0
- Dim result As Boolean
- Dim numtokens As Integer = 1
- Dim coltemp1 As Integer = Column
- Dim coltemp2 As Integer = Column
- Select Case Column
- Case 0
- numcheckleft = 0
- numcheckright = 3
- Case 1
- numcheckleft = 1
- numcheckright = 2
- Case 2
- numcheckleft = 2
- numcheckright = 3
- Case 3
- numcheckleft = 3
- numcheckright = 3
- Case 4
- numcheckleft = 3
- numcheckright = 2
- Case 5
- numcheckleft = 3
- numcheckright = 1
- Case 6
- numcheckleft = 3
- numcheckright = 0
- End Select
- Do Until numcheckleft = 0
- If pieceMatches(Board, coltemp1 - 1, Row) = True Then
- numtokens = numtokens + 1
- Else
- Exit Do
- End If
- numcheckleft = numcheckleft - 1
- coltemp1 = coltemp1 - 1
- Loop
- Do Until numcheckright = 0
- If pieceMatches(Board, coltemp2 + 1, Row) = True Then
- numtokens = numtokens + 1
- Else
- Exit Do
- End If
- numcheckright = numcheckright - 1
- coltemp2 = coltemp2 + 1
- Loop
- If numtokens >= 4 Then
- result = True
- Else
- result = False
- End If
- numcheckleft = 0
- numtokens = 0
- Return result
- End Function
- 'Checks vertically around the token that was just placed for tree adjacent matching tokens. returns true if there are 4 in a row
- Private Function checkVertical(ByRef Board As Integer(,), ByVal Column As Integer, ByVal Row As Integer) As Boolean
- Dim numcheckup As Integer = 0
- Dim numcheckdown As Integer = 0
- Dim result As Boolean
- Dim numtokens As Integer = 1
- Dim rowtemp1 As Integer = Row
- Dim rowtemp2 As Integer = Row
- Select Case Row
- Case 0
- numcheckdown = 0
- numcheckup = 3
- Case 1
- numcheckdown = 1
- numcheckup = 2
- Case 2
- numcheckdown = 2
- numcheckup = 3
- Case 3
- numcheckdown = 3
- numcheckup = 2
- Case 4
- numcheckdown = 3
- numcheckup = 1
- Case 5
- numcheckdown = 3
- numcheckup = 0
- End Select
- Do Until numcheckdown = 0
- If pieceMatches(Board, Column, rowtemp1 - 1) = True Then
- numtokens = numtokens + 1
- Else
- Exit Do
- End If
- numcheckdown = numcheckdown - 1
- rowtemp1 = rowtemp1 - 1
- Loop
- Do Until numcheckup = 0
- If pieceMatches(Board, Column, Row + 1) = True Then
- numtokens = numtokens + 1
- Else
- Exit Do
- End If
- numcheckup = numcheckup - 1
- rowtemp2 = rowtemp2 + 1
- Loop
- If numtokens = 4 Then
- result = True
- Else
- result = False
- End If
- numcheckup = 0
- numcheckdown = 0
- numtokens = 0
- Return result
- End Function
- 'Checks diagonally around the token that was just placed for tree adjacent matching tokens. returns true if there are 4 in a row
- Private Function checkDiagonal(ByRef Board As Integer(,), ByVal Column As Integer, ByVal Row As Integer) As Boolean
- Dim numcheckupright As Integer = 0
- Dim numcheckdownleft As Integer = 0
- Dim result As Boolean
- Dim numtokens As Integer = 1
- Dim rowtemp1 As Integer = Row
- Dim rowtemp2 As Integer = Row
- Dim coltemp1 As Integer = Column
- Dim coltemp2 As Integer = Column
- Select Case Column
- Case 0
- If Row = 2 Then
- numcheckupright = 3
- numcheckdownleft = 0
- End If
- Case 1
- If Row = 3 Then
- numcheckupright = 2
- numcheckdownleft = 1
- ElseIf Row = 2 Then
- numcheckupright = 3
- numcheckdownleft = 1
- ElseIf Row = 1 Then
- numcheckupright = 3
- numcheckdownleft = 1
- End If
- Case 2
- If Row = 4 Then
- numcheckupright = 1
- numcheckdownleft = 2
- ElseIf Row = 3 Then
- numcheckupright = 2
- numcheckdownleft = 2
- ElseIf Row = 2 Then
- numcheckupright = 3
- numcheckdownleft = 2
- ElseIf Row = 1 Then
- numcheckupright = 3
- numcheckdownleft = 1
- End If
- Case 3
- If Row = 5 Then
- numcheckupright = 0
- numcheckdownleft = 3
- ElseIf Row = 4 Then
- numcheckupright = 1
- numcheckdownleft = 3
- ElseIf Row = 3 Then
- numcheckupright = 2
- numcheckdownleft = 3
- ElseIf Row = 2 Then
- numcheckupright = 3
- numcheckdownleft = 2
- ElseIf Row = 1 Then
- numcheckupright = 3
- numcheckdownleft = 1
- End If
- Case 4
- If Row = 5 Then
- numcheckupright = 0
- numcheckdownleft = 3
- ElseIf Row = 4 Then
- numcheckupright = 1
- numcheckdownleft = 3
- ElseIf Row = 3 Then
- numcheckupright = 2
- numcheckdownleft = 3
- ElseIf Row = 2 Then
- numcheckupright = 2
- numcheckdownleft = 2
- ElseIf Row = 1 Then
- numcheckupright = 2
- numcheckdownleft = 1
- End If
- Case 5
- If Row = 5 Then
- numcheckupright = 0
- numcheckdownleft = 3
- ElseIf Row = 4 Then
- numcheckupright = 1
- numcheckdownleft = 3
- ElseIf Row = 3 Then
- numcheckupright = 1
- numcheckdownleft = 3
- ElseIf Row = 2 Then
- numcheckupright = 1
- numcheckdownleft = 2
- End If
- Case 6
- If Row = 5 Then
- numcheckupright = 0
- numcheckdownleft = 3
- ElseIf Row = 4 Then
- numcheckupright = 0
- numcheckdownleft = 3
- ElseIf Row = 3 Then
- numcheckupright = 0
- numcheckdownleft = 3
- End If
- End Select
- Do Until numcheckupright = 0
- If pieceMatches(Board, coltemp1 + 1, rowtemp1 + 1) = True Then
- numtokens = numtokens + 1
- Else
- Exit Do
- End If
- coltemp1 = coltemp1 + 1
- rowtemp1 = rowtemp1 + 1
- Loop
- Do Until numcheckdownleft = 0
- If pieceMatches(Board, coltemp2 - 1, rowtemp2 - 1) = True Then
- numtokens = numtokens + 1
- Else
- Exit Do
- End If
- numcheckdownleft = numcheckdownleft - 1
- coltemp2 = coltemp2 - 1
- rowtemp2 = rowtemp2 - 1
- Loop
- If numtokens = 4 Then
- result = True
- Else
- result = False
- End If
- numcheckupright = 0
- numcheckdownleft = 0
- numtokens = 0
- Return result
- End Function
- 'Calls all three checking functions. returns true if one of them returned true
- Private Function checkWinner(ByRef Board As Integer(,), ByVal Column As Integer, ByVal Row As Integer) As Boolean
- Dim result As Boolean = False
- If checkHorizontal(Board, Column, Row) Or checkVertical(Board, Column, Row) Or checkDiagonal(Board, Column, Row) Then
- MessageBox.Show(currentPlayerName & " has won!")
- result = True
- End If
- Return result
- End Function
- 'Checks to see that a token in the specified location matches the current player's token color
- Private Function pieceMatches(ByRef Board As Integer(,), ByVal x As Integer, ByVal y As Integer) As Boolean
- If Board(x, y) = currentPlayerColor Then
- Return True
- Else
- Return False
- End If
- End Function
- End Class
Add Comment
Please, Sign In to add comment