Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Module Module1
- 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}}
- Private Turn As Byte = 1
- Private Function Vertical(ByVal C As Byte, ByVal R As Byte) As Boolean
- If R > 2 Then
- For i = 1 To 3
- If Board(C, R - i) <> Turn Then Return False
- Next
- Return True
- End If
- Return False
- End Function
- Private Function Horizontal(ByVal C As Byte, ByVal R As Byte) As Boolean
- For i = -3 To 3
- If C + i < 0 OrElse C + i > 6 Then
- Continue For
- Else
- Dim C4 As Boolean = True
- For n = 0 To 3
- If C + i + n < 0 OrElse C + i + n > 6 Then
- C4 = False
- Exit For
- Else
- If Board(C + i + n, R) <> Turn Then
- C4 = False
- Exit For
- End If
- End If
- Next
- If C4 Then Return True
- End If
- Next
- Return False
- End Function
- Private Function Diagonal1(ByVal C As Byte, ByVal R As Byte) As Boolean
- For i = -3 To 3
- If C + i < 0 OrElse C + i > 6 OrElse R + i < 0 OrElse R + i > 5 Then
- Continue For
- Else
- Dim C4 As Boolean = True
- For n = 0 To 3
- If C + i + n < 0 OrElse C + i + n > 6 OrElse R + i + n < 0 OrElse R + i + n > 5 Then
- C4 = False
- Exit For
- Else
- If Board(C + i + n, R + i + n) <> Turn Then
- C4 = False
- Exit For
- End If
- End If
- Next
- If C4 Then Return True
- End If
- Next
- Return False
- End Function
- Private Function Diagonal2(ByVal C As Byte, ByVal R As Byte) As Boolean
- For i = -3 To 3
- If C + i < 0 OrElse C + i > 6 OrElse R - i < 0 OrElse R - i > 5 Then
- Continue For
- Else
- Dim C4 As Boolean = True
- For n = 0 To 3
- If C + i + n < 0 OrElse C + i + n > 6 OrElse R - i - n < 0 OrElse R - i - n > 5 Then
- C4 = False
- Exit For
- Else
- If Board(C + i + n, R - i - n) <> Turn Then
- C4 = False
- Exit For
- End If
- End If
- Next
- If C4 Then Return True
- End If
- Next
- Return False
- End Function
- Private Function Win(ByVal C As Byte, ByVal R As Byte) As Boolean
- If Vertical(C, R) OrElse Horizontal(C, R) OrElse Diagonal1(C, R) OrElse Diagonal2(C, R) Then Return True
- Return False
- End Function
- Private Function PlaceChip(ByVal C As Byte) As Byte
- For i = 0 To 5
- If Board(C, i) = 0 Then
- If Turn = 2 Then Board(C, i) = 2 Else Board(C, i) = 1
- Return i
- End If
- Next
- End Function
- Private Function ColumnFull(ByVal C As Byte) As Boolean
- For i = 0 To 5
- If Board(C, i) = 0 Then Return False
- Next
- Return True
- End Function
- Sub Main()
- While True
- Dim C As Byte = 0
- Console.WriteLine("Player " & Turn & " Pick a Column (1-7)")
- Integer.TryParse(Console.ReadLine, C)
- While C < 1 OrElse C > 7
- Console.WriteLine("Player " & Turn & " Invalid number, Pick a Column (1-7)")
- Integer.TryParse(Console.ReadLine, C)
- End While
- C -= 1
- If ColumnFull(C) Then
- Console.WriteLine("Column full, Pick another Column")
- Continue While
- Else
- Dim Row As Byte = PlaceChip(C)
- PrintBoard()
- If Win(C, Row) Then
- Console.WriteLine("Player " & Turn & " WON!")
- Console.ReadKey()
- Exit While
- Else
- If Turn = 2 Then Turn = 1 Else Turn = 2
- End If
- End If
- End While
- End Sub
- 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