TizzyT

Connect4 with logic fail -TizzyT

Jan 1st, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.61 KB | None | 0 0
  1. 'TASK: Find the problem in the connect4 program and fix it.
  2. 'Some instances of connect4 isn't detected as such.
  3.  
  4. Module Module1
  5.     '''' Empty = 0, Player1 = 1, Player2 = 2
  6.     Private Board(,) As Byte = {{0, 0, 0, 0, 0, 0}, _
  7.                                 {0, 0, 0, 0, 0, 0}, _
  8.                                 {0, 0, 0, 0, 0, 0}, _
  9.                                 {0, 0, 0, 0, 0, 0}, _
  10.                                 {0, 0, 0, 0, 0, 0}, _
  11.                                 {0, 0, 0, 0, 0, 0}, _
  12.                                 {0, 0, 0, 0, 0, 0}}
  13.  
  14.     '' If False = PlayerOnes Turn, True = PlayerTwos Turn
  15.     Private Player2sTurn As Boolean = False
  16.  
  17.     Private Function Win(ByVal Column As Byte, ByVal Row As Byte) As Boolean
  18.         Dim CheckingFor As Byte = 0
  19.         If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
  20.         If Column > 2 Then
  21.             For i = 1 To 3
  22.                 ' Checks Left
  23.                 If Board(Column - i, Row) <> CheckingFor Then Exit For
  24.                 If i = 3 Then Return True
  25.             Next
  26.         End If
  27.         If Column < 4 Then
  28.             For i = 1 To 3
  29.                 ' Checks Right
  30.                 If Board(Column + i, Row) <> CheckingFor Then Exit For
  31.                 If i = 3 Then Return True
  32.             Next
  33.         End If
  34.         If Row > 2 Then
  35.             For i = 1 To 3
  36.                 ' Checks Down
  37.                 If Board(Column, Row - i) <> CheckingFor Then Exit For
  38.                 If i = 3 Then Return True
  39.             Next
  40.         End If
  41.         If Column > 2 AndAlso Row < 4 Then
  42.             For i = 1 To 3
  43.                 'Check Up Left
  44.                 If Board(Column - i, Row + i) <> CheckingFor Then Exit For
  45.                 If i = 3 Then Return True
  46.             Next
  47.         End If
  48.         If Column < 4 AndAlso Row < 4 Then
  49.             For i = 1 To 3
  50.                 'Check Up Right
  51.                 If Board(Column + i, Row + i) <> CheckingFor Then Exit For
  52.                 If i = 3 Then Return True
  53.             Next
  54.         End If
  55.         If Column > 2 AndAlso Row > 2 Then
  56.             For i = 1 To 3
  57.                 'Check Down Left
  58.                 If Board(Column - i, Row - i) <> CheckingFor Then Exit For
  59.                 If i = 3 Then Return True
  60.             Next
  61.         End If
  62.         If Column < 4 AndAlso Row > 2 Then
  63.             For i = 1 To 3
  64.                 'Check Down Right
  65.                 If Board(Column + i, Row - i) <> CheckingFor Then Exit For
  66.                 If i = 3 Then Return True
  67.             Next
  68.         End If
  69.         Return False
  70.     End Function
  71.  
  72.     Private Function PlaceChip(ByVal Column As Byte) As Byte
  73.         For i = 0 To 5
  74.             If Board(Column, i) = 0 Then
  75.                 If Player2sTurn Then Board(Column, i) = 2 Else Board(Column, i) = 1
  76.                 Return i
  77.             End If
  78.         Next
  79.         Return 0
  80.     End Function
  81.  
  82.     Private Function IsColumnFull(ByVal Column As Byte) As Boolean
  83.         For i = 5 To 5
  84.             If Board(Column, i) = 0 Then Return False
  85.         Next
  86.         Return True
  87.     End Function
  88.  
  89.     Sub Main()
  90.         While True
  91.             If Player2sTurn Then
  92.                 '' Player Twos Logic
  93.                 ''''''''''''' Make sure user picks a valif column
  94.                 Console.WriteLine("2 - Pick a column (1-7)")
  95.                 Dim Column As Byte = Console.ReadLine
  96.                 While Column < 1 OrElse Column > 7
  97.                     Console.WriteLine("2 - Invalid number, Pick a column (1-7)")
  98.                     Column = Console.ReadLine
  99.                 End While
  100.                 Column -= 1
  101.                 ''''''''''''' Make sure the column the user picks isnt full
  102.                 If IsColumnFull(Column) Then
  103.                     Console.WriteLine("Column full, Pick another column")
  104.                     Continue While
  105.                 Else
  106.                     Dim Row As Byte = PlaceChip(Column)
  107.                     PrintBoard()
  108.                     ''''''''''''' Checks to see if player won
  109.                     If Win(Column, Row) Then
  110.                         Console.WriteLine("PLAYER 2 WON!")
  111.                     Else
  112.                         Player2sTurn = False
  113.                     End If
  114.                 End If
  115.             Else
  116.                 '' Player Ones Logic
  117.                 ''''''''''''' Make sure user picks a valif column
  118.                 Console.WriteLine("1 - Pick a column (1-7)")
  119.                 Dim Column As Byte = Console.ReadLine
  120.                 While Column < 1 OrElse Column > 7
  121.                     Console.WriteLine("1 - Invalid number, Pick a column (1-7)")
  122.                     Column = Console.ReadLine
  123.                 End While
  124.                 Column -= 1
  125.                 ''''''''''''' Make sure the column the user picks isnt full
  126.                 If IsColumnFull(Column) Then
  127.                     Console.WriteLine("Column full, Pick another column")
  128.                     Continue While
  129.                 Else
  130.                     Dim Row As Byte = PlaceChip(Column)
  131.                     PrintBoard()
  132.                     ''''''''''''' Checks to see if player won
  133.                     If Win(Column, Row) Then
  134.                         Console.WriteLine("PLAYER 1 WON!")
  135.                     Else
  136.                         Player2sTurn = True
  137.                     End If
  138.                 End If
  139.             End If
  140.         End While
  141.     End Sub
  142.  
  143.     Private Sub PrintBoard()
  144.         Console.Clear()
  145.         For y = 5 To 0 Step -1
  146.             For x = 0 To 6
  147.                 Console.Write(Board(x, y) & " ")
  148.             Next
  149.             Console.Write(vbCrLf)
  150.         Next
  151.     End Sub
  152. End Module
Advertisement
Add Comment
Please, Sign In to add comment