Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Module Module1
- '''' Empty = 0, Player1 = 1, Player2 = 2
- Private Board(,) As Byte = {{0, 0, 0, 0, 0, 0}, _
- {0, 0, 0, 0, 0, 0}, _
- {0, 0, 0, 0, 0, 0}, _
- {0, 0, 0, 0, 0, 0}, _
- {0, 0, 0, 0, 0, 0}, _
- {0, 0, 0, 0, 0, 0}, _
- {0, 0, 0, 0, 0, 0}}
- '' If False = PlayerOnes Turn, True = PlayerTwos Turn
- Private Player2sTurn As Boolean = False
- Private Function CheckLeft(ByVal Column As Byte, ByVal Row As Byte, Optional ByVal FinalCheck As Boolean = False) As Boolean
- Dim CheckingFor As Byte = 0
- If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
- If FinalCheck Then
- For i = 0 To 3
- Try
- If Board(Column - i, Row) <> CheckingFor Then Return False
- Catch ex As Exception
- Return False
- End Try
- Next
- Else
- For i = 1 To 3
- Try
- If Board(Column - i, Row) <> CheckingFor Then
- Return CheckRight(Column - i, Row, True)
- End If
- Catch ex As Exception
- Return CheckRight(Column - i + 1, Row, True)
- End Try
- Next
- End If
- Return True
- End Function
- Private Function CheckDown(ByVal Column As Byte, ByVal Row As Byte) As Boolean
- Dim CheckingFor As Byte = 0
- If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
- For i = 1 To 3
- Try
- If Board(Column, Row - i) <> CheckingFor Then Return False
- Catch ex As Exception
- Return False
- End Try
- Next
- Return True
- End Function
- Private Function CheckRight(ByVal Column As Byte, ByVal Row As Byte, Optional ByVal FinalCheck As Boolean = False) As Boolean
- Dim CheckingFor As Byte = 0
- If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
- If FinalCheck Then
- For i = 0 To 3
- Try
- If Board(Column + i, Row) <> CheckingFor Then Return False
- Catch ex As Exception
- Return False
- End Try
- Next
- Else
- For i = 1 To 3
- Try
- If Board(Column + i, Row) <> CheckingFor Then
- Return CheckLeft(Column + i, Row, True)
- End If
- Catch ex As Exception
- Return CheckLeft(Column + i - 1, Row, True)
- End Try
- Next
- End If
- Return True
- End Function
- Private Function CheckUpLeft(ByVal Column As Byte, ByVal Row As Byte, Optional FinalCheck As Boolean = False) As Boolean
- Dim CheckingFor As Byte = 0
- If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
- If FinalCheck Then
- For i = 0 To 3
- Try
- If Board(Column - i, Row + i) <> CheckingFor Then Return False
- Catch ex As Exception
- Return False
- End Try
- Next
- Else
- For i = 1 To 3
- Try
- If Board(Column - i, Row + i) <> CheckingFor Then
- Return CheckDownRight(Column - i, Row + i, True)
- End If
- Catch ex As Exception
- Return CheckDownRight(Column - i + 1, Row + i - 1, True)
- End Try
- Next
- End If
- Return True
- End Function
- Private Function CheckUpRight(ByVal Column As Byte, ByVal Row As Byte, Optional FinalCheck As Boolean = False) As Boolean
- Dim CheckingFor As Byte = 0
- If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
- If FinalCheck Then
- For i = 0 To 3
- Try
- If Board(Column + i, Row + i) <> CheckingFor Then Return False
- Catch ex As Exception
- Return False
- End Try
- Next
- Else
- For i = 1 To 3
- Try
- If Board(Column + i, Row + i) <> CheckingFor Then
- Return CheckDownLeft(Column + i, Row + i, True)
- End If
- Catch ex As Exception
- Return CheckDownLeft(Column + i - 1, Row + i - 1, True)
- End Try
- Next
- End If
- Return True
- End Function
- Private Function CheckDownLeft(ByVal Column As Byte, ByVal Row As Byte, Optional FinalCheck As Boolean = False) As Boolean
- Dim CheckingFor As Byte = 0
- If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
- If FinalCheck Then
- For i = If(FinalCheck, 0, 1) To 3
- Try
- If Board(Column - i, Row - i) <> CheckingFor Then Return False
- Catch ex As Exception
- Return False
- End Try
- Next
- Else
- For i = If(FinalCheck, 0, 1) To 3
- Try
- If Board(Column - i, Row - i) <> CheckingFor Then
- Return CheckUpRight(Column - i, Row - i, True)
- End If
- Catch ex As Exception
- Return CheckUpRight(Column - i + 1, Row - i + 1, True)
- End Try
- Next
- End If
- Return True
- End Function
- Private Function CheckDownRight(ByVal Column As Byte, ByVal Row As Byte, Optional FinalCheck As Boolean = False) As Boolean
- Dim CheckingFor As Byte = 0
- If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
- If FinalCheck Then
- For i = 0 To 3
- Try
- If Board(Column + i, Row - i) <> CheckingFor Then Return False
- Catch ex As Exception
- Return False
- End Try
- Next
- Else
- For i = 1 To 3
- Try
- If Board(Column + i, Row - i) <> CheckingFor Then
- Return CheckUpLeft(Column + i, Row - i, True)
- End If
- Catch ex As Exception
- Return CheckUpLeft(Column + i - 1, Row - i + 1, True)
- End Try
- Next
- End If
- Return True
- End Function
- Private Function Win(ByVal Column As Byte, ByVal Row As Byte) As Boolean
- If CheckDown(Column, Row) Then Return True
- If CheckLeft(Column, Row) Then Return True
- If CheckRight(Column, Row) Then Return True
- If CheckUpLeft(Column, Row) Then Return True
- If CheckUpRight(Column, Row) Then Return True
- If CheckDownLeft(Column, Row) Then Return True
- If CheckDownRight(Column, Row) Then Return True
- Return False
- End Function
- Private Function PlaceChip(ByVal Column As Byte) As Byte
- For i = 0 To 5
- If Board(Column, i) = 0 Then
- If Player2sTurn Then Board(Column, i) = 2 Else Board(Column, i) = 1
- Return i
- End If
- Next
- Return 0
- End Function
- Private Function IsColumnFull(ByVal Column As Byte) As Boolean
- For i = 5 To 5
- If Board(Column, i) = 0 Then Return False
- Next
- Return True
- End Function
- Sub Main()
- While True
- If Player2sTurn Then
- '' Player Twos Logic
- ''''''''''''' Make sure user picks a valif column
- Console.WriteLine("2 - Pick a column (1-7)")
- Dim Column As Byte = 0
- Try
- Column = Console.ReadLine
- Catch ex As Exception
- End Try
- While Column < 1 OrElse Column > 7
- Console.WriteLine("2 - Invalid number, Pick a column (1-7)")
- Try
- Column = Console.ReadLine
- Catch ex As Exception
- End Try
- End While
- Column -= 1
- ''''''''''''' Make sure the column the user picks isnt full
- If IsColumnFull(Column) Then
- Console.WriteLine("Column full, Pick another column")
- Continue While
- Else
- Dim Row As Byte = PlaceChip(Column)
- PrintBoard()
- ''''''''''''' Checks to see if player won
- If Win(Column, Row) Then
- Console.WriteLine("PLAYER 2 WON!")
- Else
- Player2sTurn = False
- End If
- End If
- Else
- '' Player Ones Logic
- ''''''''''''' Make sure user picks a valif column
- Console.WriteLine("1 - Pick a column (1-7)")
- Dim Column As Byte = 0
- Try
- Column = Console.ReadLine
- Catch ex As Exception
- End Try
- While Column < 1 OrElse Column > 7
- Console.WriteLine("1 - Invalid number, Pick a column (1-7)")
- Try
- Column = Console.ReadLine
- Catch ex As Exception
- End Try
- End While
- Column -= 1
- ''''''''''''' Make sure the column the user picks isnt full
- If IsColumnFull(Column) Then
- Console.WriteLine("Column full, Pick another column")
- Continue While
- Else
- Dim Row As Byte = PlaceChip(Column)
- PrintBoard()
- ''''''''''''' Checks to see if player won
- If Win(Column, Row) Then
- Console.WriteLine("PLAYER 1 WON!")
- Else
- Player2sTurn = True
- End If
- End If
- End If
- End While
- End Sub
- '''''' Displays the GameBoard
- Private Sub PrintBoard()
- Console.Clear()
- For y = 5 To 0 Step -1
- For x = 0 To 6
- Console.Write(Board(x, y) & " ")
- Next
- Console.Write(vbCrLf)
- Next
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment