TizzyT

Connect4 (crappy fixed?)

Jan 10th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 10.57 KB | None | 0 0
  1. Module Module1
  2.     '''' Empty = 0, Player1 = 1, Player2 = 2
  3.     Private Board(,) As Byte = {{0, 0, 0, 0, 0, 0}, _
  4.                                 {0, 0, 0, 0, 0, 0}, _
  5.                                 {0, 0, 0, 0, 0, 0}, _
  6.                                 {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.  
  11.     '' If False = PlayerOnes Turn, True = PlayerTwos Turn
  12.     Private Player2sTurn As Boolean = False
  13.  
  14.     Private Function CheckLeft(ByVal Column As Byte, ByVal Row As Byte, Optional ByVal FinalCheck As Boolean = False) As Boolean
  15.         Dim CheckingFor As Byte = 0
  16.         If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
  17.         If FinalCheck Then
  18.             For i = 0 To 3
  19.                 Try
  20.                     If Board(Column - i, Row) <> CheckingFor Then Return False
  21.                 Catch ex As Exception
  22.                     Return False
  23.                 End Try
  24.             Next
  25.         Else
  26.             For i = 1 To 3
  27.                 Try
  28.                     If Board(Column - i, Row) <> CheckingFor Then
  29.                         Return CheckRight(Column - i, Row, True)
  30.                     End If
  31.                 Catch ex As Exception
  32.                     Return CheckRight(Column - i + 1, Row, True)
  33.                 End Try
  34.             Next
  35.         End If
  36.         Return True
  37.     End Function
  38.  
  39.     Private Function CheckDown(ByVal Column As Byte, ByVal Row As Byte) As Boolean
  40.         Dim CheckingFor As Byte = 0
  41.         If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
  42.         For i = 1 To 3
  43.             Try
  44.                 If Board(Column, Row - i) <> CheckingFor Then Return False
  45.             Catch ex As Exception
  46.                 Return False
  47.             End Try
  48.         Next
  49.         Return True
  50.     End Function
  51.  
  52.     Private Function CheckRight(ByVal Column As Byte, ByVal Row As Byte, Optional ByVal FinalCheck As Boolean = False) As Boolean
  53.         Dim CheckingFor As Byte = 0
  54.         If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
  55.         If FinalCheck Then
  56.             For i = 0 To 3
  57.                 Try
  58.                     If Board(Column + i, Row) <> CheckingFor Then Return False
  59.                 Catch ex As Exception
  60.                     Return False
  61.                 End Try
  62.             Next
  63.         Else
  64.             For i = 1 To 3
  65.                 Try
  66.                     If Board(Column + i, Row) <> CheckingFor Then
  67.                         Return CheckLeft(Column + i, Row, True)
  68.                     End If
  69.                 Catch ex As Exception
  70.                     Return CheckLeft(Column + i - 1, Row, True)
  71.                 End Try
  72.             Next
  73.         End If
  74.         Return True
  75.     End Function
  76.  
  77.     Private Function CheckUpLeft(ByVal Column As Byte, ByVal Row As Byte, Optional FinalCheck As Boolean = False) As Boolean
  78.         Dim CheckingFor As Byte = 0
  79.         If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
  80.         If FinalCheck Then
  81.             For i = 0 To 3
  82.                 Try
  83.                     If Board(Column - i, Row + i) <> CheckingFor Then Return False
  84.                 Catch ex As Exception
  85.                     Return False
  86.                 End Try
  87.             Next
  88.         Else
  89.             For i = 1 To 3
  90.                 Try
  91.                     If Board(Column - i, Row + i) <> CheckingFor Then
  92.                         Return CheckDownRight(Column - i, Row + i, True)
  93.                     End If
  94.                 Catch ex As Exception
  95.                     Return CheckDownRight(Column - i + 1, Row + i - 1, True)
  96.                 End Try
  97.             Next
  98.         End If
  99.         Return True
  100.     End Function
  101.  
  102.     Private Function CheckUpRight(ByVal Column As Byte, ByVal Row As Byte, Optional FinalCheck As Boolean = False) As Boolean
  103.         Dim CheckingFor As Byte = 0
  104.         If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
  105.         If FinalCheck Then
  106.             For i = 0 To 3
  107.                 Try
  108.                     If Board(Column + i, Row + i) <> CheckingFor Then Return False
  109.                 Catch ex As Exception
  110.                     Return False
  111.                 End Try
  112.             Next
  113.         Else
  114.             For i = 1 To 3
  115.                 Try
  116.                     If Board(Column + i, Row + i) <> CheckingFor Then
  117.                         Return CheckDownLeft(Column + i, Row + i, True)
  118.                     End If
  119.                 Catch ex As Exception
  120.                     Return CheckDownLeft(Column + i - 1, Row + i - 1, True)
  121.                 End Try
  122.             Next
  123.         End If
  124.         Return True
  125.     End Function
  126.  
  127.     Private Function CheckDownLeft(ByVal Column As Byte, ByVal Row As Byte, Optional FinalCheck As Boolean = False) As Boolean
  128.         Dim CheckingFor As Byte = 0
  129.         If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
  130.         If FinalCheck Then
  131.             For i = If(FinalCheck, 0, 1) To 3
  132.                 Try
  133.                     If Board(Column - i, Row - i) <> CheckingFor Then Return False
  134.                 Catch ex As Exception
  135.                     Return False
  136.                 End Try
  137.             Next
  138.         Else
  139.             For i = If(FinalCheck, 0, 1) To 3
  140.                 Try
  141.                     If Board(Column - i, Row - i) <> CheckingFor Then
  142.                         Return CheckUpRight(Column - i, Row - i, True)
  143.                     End If
  144.                 Catch ex As Exception
  145.                     Return CheckUpRight(Column - i + 1, Row - i + 1, True)
  146.                 End Try
  147.             Next
  148.         End If
  149.         Return True
  150.     End Function
  151.  
  152.     Private Function CheckDownRight(ByVal Column As Byte, ByVal Row As Byte, Optional FinalCheck As Boolean = False) As Boolean
  153.         Dim CheckingFor As Byte = 0
  154.         If Player2sTurn Then CheckingFor = 2 Else CheckingFor = 1
  155.         If FinalCheck Then
  156.             For i = 0 To 3
  157.                 Try
  158.                     If Board(Column + i, Row - i) <> CheckingFor Then Return False
  159.                 Catch ex As Exception
  160.                     Return False
  161.                 End Try
  162.             Next
  163.         Else
  164.             For i = 1 To 3
  165.                 Try
  166.                     If Board(Column + i, Row - i) <> CheckingFor Then
  167.                         Return CheckUpLeft(Column + i, Row - i, True)
  168.                     End If
  169.                 Catch ex As Exception
  170.                     Return CheckUpLeft(Column + i - 1, Row - i + 1, True)
  171.                 End Try
  172.             Next
  173.         End If
  174.         Return True
  175.     End Function
  176.  
  177.     Private Function Win(ByVal Column As Byte, ByVal Row As Byte) As Boolean
  178.         If CheckDown(Column, Row) Then Return True
  179.         If CheckLeft(Column, Row) Then Return True
  180.         If CheckRight(Column, Row) Then Return True
  181.         If CheckUpLeft(Column, Row) Then Return True
  182.         If CheckUpRight(Column, Row) Then Return True
  183.         If CheckDownLeft(Column, Row) Then Return True
  184.         If CheckDownRight(Column, Row) Then Return True
  185.         Return False
  186.     End Function
  187.  
  188.     Private Function PlaceChip(ByVal Column As Byte) As Byte
  189.         For i = 0 To 5
  190.             If Board(Column, i) = 0 Then
  191.                 If Player2sTurn Then Board(Column, i) = 2 Else Board(Column, i) = 1
  192.                 Return i
  193.             End If
  194.         Next
  195.         Return 0
  196.     End Function
  197.  
  198.     Private Function IsColumnFull(ByVal Column As Byte) As Boolean
  199.         For i = 5 To 5
  200.             If Board(Column, i) = 0 Then Return False
  201.         Next
  202.         Return True
  203.     End Function
  204.  
  205.     Sub Main()
  206.         While True
  207.             If Player2sTurn Then
  208.                 '' Player Twos Logic
  209.                 ''''''''''''' Make sure user picks a valif column
  210.                 Console.WriteLine("2 - Pick a column (1-7)")
  211.                 Dim Column As Byte = 0
  212.                 Try
  213.                     Column = Console.ReadLine
  214.                 Catch ex As Exception
  215.                 End Try
  216.                 While Column < 1 OrElse Column > 7
  217.                     Console.WriteLine("2 - Invalid number, Pick a column (1-7)")
  218.                     Try
  219.                         Column = Console.ReadLine
  220.                     Catch ex As Exception
  221.                     End Try
  222.                 End While
  223.                 Column -= 1
  224.                 ''''''''''''' Make sure the column the user picks isnt full
  225.                 If IsColumnFull(Column) Then
  226.                     Console.WriteLine("Column full, Pick another column")
  227.                     Continue While
  228.                 Else
  229.                     Dim Row As Byte = PlaceChip(Column)
  230.                     PrintBoard()
  231.                     ''''''''''''' Checks to see if player won
  232.                     If Win(Column, Row) Then
  233.                         Console.WriteLine("PLAYER 2 WON!")
  234.                     Else
  235.                         Player2sTurn = False
  236.                     End If
  237.                 End If
  238.             Else
  239.                 '' Player Ones Logic
  240.                 ''''''''''''' Make sure user picks a valif column
  241.                 Console.WriteLine("1 - Pick a column (1-7)")
  242.                 Dim Column As Byte = 0
  243.                 Try
  244.                     Column = Console.ReadLine
  245.                 Catch ex As Exception
  246.                 End Try
  247.                 While Column < 1 OrElse Column > 7
  248.                     Console.WriteLine("1 - Invalid number, Pick a column (1-7)")
  249.                     Try
  250.                         Column = Console.ReadLine
  251.                     Catch ex As Exception
  252.                     End Try
  253.                 End While
  254.                 Column -= 1
  255.                 ''''''''''''' Make sure the column the user picks isnt full
  256.                 If IsColumnFull(Column) Then
  257.                     Console.WriteLine("Column full, Pick another column")
  258.                     Continue While
  259.                 Else
  260.                     Dim Row As Byte = PlaceChip(Column)
  261.                     PrintBoard()
  262.                     ''''''''''''' Checks to see if player won
  263.                     If Win(Column, Row) Then
  264.                         Console.WriteLine("PLAYER 1 WON!")
  265.                     Else
  266.                         Player2sTurn = True
  267.                     End If
  268.                 End If
  269.             End If
  270.         End While
  271.     End Sub
  272.  
  273.     '''''' Displays the GameBoard
  274.     Private Sub PrintBoard()
  275.         Console.Clear()
  276.         For y = 5 To 0 Step -1
  277.             For x = 0 To 6
  278.                 Console.Write(Board(x, y) & " ")
  279.             Next
  280.             Console.Write(vbCrLf)
  281.         Next
  282.     End Sub
  283. End Module
Advertisement
Add Comment
Please, Sign In to add comment