TizzyT

Connect4

Jan 11th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.99 KB | None | 0 0
  1. Module Module1
  2.     Private Board(,) As Byte = {{0, 0, 0, 0, 0, 0}, _
  3.                                 {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.     Private Turn As Byte = 1
  10.     Private Function Vertical(ByVal C As Byte, ByVal R As Byte) As Boolean
  11.         If R > 2 Then
  12.             For i = 1 To 3
  13.                 If Board(C, R - i) <> Turn Then Return False
  14.             Next
  15.             Return True
  16.         End If
  17.         Return False
  18.     End Function
  19.     Private Function Horizontal(ByVal C As Byte, ByVal R As Byte) As Boolean
  20.         For i = -3 To 3
  21.             If C + i < 0 OrElse C + i > 6 Then
  22.                 Continue For
  23.             Else
  24.                 Dim C4 As Boolean = True
  25.                 For n = 0 To 3
  26.                     If C + i + n < 0 OrElse C + i + n > 6 Then
  27.                         C4 = False
  28.                         Exit For
  29.                     Else
  30.                         If Board(C + i + n, R) <> Turn Then
  31.                             C4 = False
  32.                             Exit For
  33.                         End If
  34.                     End If
  35.                 Next
  36.                 If C4 Then Return True
  37.             End If
  38.         Next
  39.         Return False
  40.     End Function
  41.     Private Function Diagonal1(ByVal C As Byte, ByVal R As Byte) As Boolean
  42.         For i = -3 To 3
  43.             If C + i < 0 OrElse C + i > 6 OrElse R + i < 0 OrElse R + i > 5 Then
  44.                 Continue For
  45.             Else
  46.                 Dim C4 As Boolean = True
  47.                 For n = 0 To 3
  48.                     If C + i + n < 0 OrElse C + i + n > 6 OrElse R + i + n < 0 OrElse R + i + n > 5 Then
  49.                         C4 = False
  50.                         Exit For
  51.                     Else
  52.                         If Board(C + i + n, R + i + n) <> Turn Then
  53.                             C4 = False
  54.                             Exit For
  55.                         End If
  56.                     End If
  57.                 Next
  58.                 If C4 Then Return True
  59.             End If
  60.         Next
  61.         Return False
  62.     End Function
  63.     Private Function Diagonal2(ByVal C As Byte, ByVal R As Byte) As Boolean
  64.         For i = -3 To 3
  65.             If C + i < 0 OrElse C + i > 6 OrElse R - i < 0 OrElse R - i > 5 Then
  66.                 Continue For
  67.             Else
  68.                 Dim C4 As Boolean = True
  69.                 For n = 0 To 3
  70.                     If C + i + n < 0 OrElse C + i + n > 6 OrElse R - i - n < 0 OrElse R - i - n > 5 Then
  71.                         C4 = False
  72.                         Exit For
  73.                     Else
  74.                         If Board(C + i + n, R - i - n) <> Turn Then
  75.                             C4 = False
  76.                             Exit For
  77.                         End If
  78.                     End If
  79.                 Next
  80.                 If C4 Then Return True
  81.             End If
  82.         Next
  83.         Return False
  84.     End Function
  85.     Private Function Win(ByVal C As Byte, ByVal R As Byte) As Boolean
  86.         If Vertical(C, R) OrElse Horizontal(C, R) OrElse Diagonal1(C, R) OrElse Diagonal2(C, R) Then Return True
  87.         Return False
  88.     End Function
  89.     Private Function PlaceChip(ByVal C As Byte) As Byte
  90.         For i = 0 To 5
  91.             If Board(C, i) = 0 Then
  92.                 If Turn = 2 Then Board(C, i) = 2 Else Board(C, i) = 1
  93.                 Return i
  94.             End If
  95.         Next
  96.     End Function
  97.     Private Function ColumnFull(ByVal C As Byte) As Boolean
  98.         For i = 0 To 5
  99.             If Board(C, i) = 0 Then Return False
  100.         Next
  101.         Return True
  102.     End Function
  103.     Sub Main()
  104.         While True
  105.             Dim C As Byte = 0
  106.             Console.WriteLine("Player " & Turn & " Pick a Column (1-7)")
  107.             Integer.TryParse(Console.ReadLine, C)
  108.             While C < 1 OrElse C > 7
  109.                 Console.WriteLine("Player " & Turn & " Invalid number, Pick a Column (1-7)")
  110.                 Integer.TryParse(Console.ReadLine, C)
  111.             End While
  112.             C -= 1
  113.             If ColumnFull(C) Then
  114.                 Console.WriteLine("Column full, Pick another Column")
  115.                 Continue While
  116.             Else
  117.                 Dim Row As Byte = PlaceChip(C)
  118.                 PrintBoard()
  119.                 If Win(C, Row) Then
  120.                     Console.WriteLine("Player " & Turn & " WON!")
  121.                     Console.ReadKey()
  122.                     Exit While
  123.                 Else
  124.                     If Turn = 2 Then Turn = 1 Else Turn = 2
  125.                 End If
  126.             End If
  127.         End While
  128.     End Sub
  129.     Private Sub PrintBoard()
  130.         Console.Clear()
  131.         For y = 5 To 0 Step -1
  132.             For x = 0 To 6
  133.                 Console.Write(Board(x, y) & " ")
  134.             Next
  135.             Console.Write(vbCrLf)
  136.         Next
  137.     End Sub
  138. End Module
Advertisement
Add Comment
Please, Sign In to add comment