Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'TASK: Find the problem in the connect4 program and fix it.
- 'Some instances of connect4 isn't detected as such.
- 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 Win(ByVal Column As Byte, ByVal Row As Byte) As Boolean
- Dim CheckingFor As Byte = 0
- If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
- If Column > 2 Then
- For i = 1 To 3
- ' Checks Left
- If Board(Column - i, Row) <> CheckingFor Then Exit For
- If i = 3 Then Return True
- Next
- End If
- If Column < 4 Then
- For i = 1 To 3
- ' Checks Right
- If Board(Column + i, Row) <> CheckingFor Then Exit For
- If i = 3 Then Return True
- Next
- End If
- If Row > 2 Then
- For i = 1 To 3
- ' Checks Down
- If Board(Column, Row - i) <> CheckingFor Then Exit For
- If i = 3 Then Return True
- Next
- End If
- If Column > 2 AndAlso Row < 4 Then
- For i = 1 To 3
- 'Check Up Left
- If Board(Column - i, Row + i) <> CheckingFor Then Exit For
- If i = 3 Then Return True
- Next
- End If
- If Column < 4 AndAlso Row < 4 Then
- For i = 1 To 3
- 'Check Up Right
- If Board(Column + i, Row + i) <> CheckingFor Then Exit For
- If i = 3 Then Return True
- Next
- End If
- If Column > 2 AndAlso Row > 2 Then
- For i = 1 To 3
- 'Check Down Left
- If Board(Column - i, Row - i) <> CheckingFor Then Exit For
- If i = 3 Then Return True
- Next
- End If
- If Column < 4 AndAlso Row > 2 Then
- For i = 1 To 3
- 'Check Down Right
- If Board(Column + i, Row - i) <> CheckingFor Then Exit For
- If i = 3 Then Return True
- Next
- End If
- 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 = Console.ReadLine
- While Column < 1 OrElse Column > 7
- Console.WriteLine("2 - Invalid number, Pick a column (1-7)")
- Column = Console.ReadLine
- 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 = Console.ReadLine
- While Column < 1 OrElse Column > 7
- Console.WriteLine("1 - Invalid number, Pick a column (1-7)")
- Column = Console.ReadLine
- 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
- 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