jhylands

3D tick tack toe

Apr 23rd, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.38 KB | None | 0 0
  1. Module Module1
  2.     Dim box(26) As String
  3.     Dim win As Integer
  4.     Sub Main()
  5. start:
  6.         startgame()
  7.         Do While 1 = 1
  8.             player1()
  9.             If won() <> 0 Then GoTo result
  10.             player2()
  11.             If won() <> 0 Then GoTo result
  12.         Loop
  13. result:
  14.         bord()
  15.         If won() = 1 Then
  16.             Console.WriteLine("Player 1 has won!")
  17.         ElseIf won() = 2 Then
  18.             Console.WriteLine("Player 2 has won!")
  19.         ElseIf won() = 3 Then
  20.             Console.WriteLine("Player ... oh it's a draw!")
  21.         End If
  22.         Console.WriteLine("Would you like to play again? (y/n)")
  23.         If Console.ReadLine() = "y" Then
  24.             GoTo start
  25.         End If
  26.     End Sub
  27.  
  28.     Public Sub player1()
  29. start:
  30.         Dim place As Integer
  31.         bord()
  32.         Console.WriteLine("Player 1 's go please choose a number that is not already taken.")
  33.         numberedbord()
  34.         place = Console.ReadLine()
  35.         If box(place) = "-" Then
  36.             box(place) = "X"
  37.  
  38.  
  39.         Else
  40.             Console.WriteLine("That place is taken.")
  41.             GoTo start
  42.         End If
  43.     End Sub
  44.     Public Sub player2()
  45. start2:
  46.         Dim place As Integer
  47.         bord()
  48.         Console.WriteLine("Player 2 's go please choose a number that is not already taken.")
  49.         numberedbord()
  50.         place = Console.ReadLine()
  51.         If box(place) = "-" Then
  52.             box(place) = "O"
  53.  
  54.         Else
  55.             Console.WriteLine("That place is taken.")
  56.             GoTo start2
  57.         End If
  58.     End Sub
  59.     Public Sub bord()
  60.         Console.WriteLine(box(0) & "|" & box(1) & "|" & box(2) & "  " & box(9) & "|" & box(10) & "|" & box(11) & "  " & box(18) & "|" & box(19) & "|" & box(20) & "  ")
  61.         Console.WriteLine(box(3) & "|" & box(4) & "|" & box(5) & "  " & box(12) & "|" & box(13) & "|" & box(14) & "  " & box(21) & "|" & box(22) & "|" & box(23) & "  ")
  62.         Console.WriteLine(box(6) & "|" & box(7) & "|" & box(8) & "  " & box(15) & "|" & box(16) & "|" & box(17) & "  " & box(24) & "|" & box(25) & "|" & box(26) & "  ")
  63.     End Sub
  64.     Public Sub numberedbord()
  65.         Console.WriteLine("0|1|2    9 |10|11    18|19|20")
  66.         Console.WriteLine("3|4|5    12|13|14    21|22|23")
  67.         Console.WriteLine("6|7|8    15|16|17    24|25|26")
  68.     End Sub
  69.     Public Sub startgame()
  70.         Dim i As Integer
  71.         Console.Clear()
  72.         For i = 0 To 26
  73.             box(i) = "-"
  74.         Next
  75.         Console.WriteLine("Instructions: ")
  76.         Console.WriteLine("The bord: ")
  77.         numberedbord()
  78.         Console.WriteLine("To play each player pickes it in turn chosing where to go. To make it easyer you pick a number of the bord above.")
  79.         Console.WriteLine("The bord is in 3D so you have to imagen that box number 9 is behind box number 0")
  80.         Console.WriteLine("This means that there are more ways to win for example:")
  81.         box(0) = "X"
  82.         box(13) = "X"
  83.         box(26) = "X"
  84.         bord()
  85.         Console.WriteLine()
  86.         For i = 0 To 26
  87.             box(i) = "-"
  88.         Next
  89.  
  90.     End Sub
  91.     Public Function won()
  92.         Dim winner As String
  93.         Dim i As Integer
  94.         winner = ""
  95.         'across
  96.         For i = 0 To 26 Step 3
  97.             If box(i) = box(i + 1) And box(i + 1) = box(i + 2) Then
  98.                 If box(i) <> "-" Then
  99.                     winner = box(i)
  100.                 End If
  101.             End If
  102.  
  103.         Next
  104.         'down
  105.         For i = 0 To 2
  106.             If box(i) = box(i + 3) And box(i + 3) = box(i + 6) Then
  107.                 If box(i) <> "-" Then
  108.                     winner = box(i)
  109.                 End If
  110.             End If
  111.         Next
  112.         For i = 9 To 11
  113.             If box(i) = box(i + 3) And box(i + 3) = box(i + 6) Then
  114.                 If box(i) <> "-" Then
  115.                     winner = box(i)
  116.                 End If
  117.             End If
  118.         Next
  119.         For i = 18 To 20
  120.             If box(i) = box(i + 3) And box(i + 3) = box(i + 6) Then
  121.                 If box(i) <> "-" Then
  122.                     winner = box(i)
  123.                 End If
  124.             End If
  125.         Next
  126.         'back
  127.         For i = 0 To 8
  128.             If box(i) = box(i + 9) And box(i + 9) = box(i + 18) Then
  129.                 If box(i) <> "-" Then
  130.                     winner = box(i)
  131.                 End If
  132.             End If
  133.         Next
  134.  
  135.         '2d diagonal vertical (2DDV)
  136.         For i = 0 To 18 Step 9
  137.             If box(i) = box(i + 4) And box(i + 4) = box(i + 8) Then
  138.                 If box(i) <> "-" Then
  139.                     winner = box(i)
  140.                 End If
  141.             ElseIf box(i + 2) = box(i + 4) And box(i + 4) = box(i + 6) Then
  142.                 If box(i + 2) <> "-" Then
  143.                     winner = box(i + 2)
  144.                 End If
  145.             End If
  146.         Next
  147.  
  148.         '2DDH horizontal
  149.         If box(0) = box(10) And box(10) = box(20) Then
  150.             If box(0) <> "-" Then
  151.                 winner = box(0)
  152.             End If
  153.         ElseIf box(3) = box(13) And box(13) = box(23) Then
  154.             If box(3) <> "-" Then
  155.                 winner = box(0)
  156.             End If
  157.         ElseIf box(6) = box(16) And box(16) = box(26) Then
  158.             If box(6) <> "-" Then
  159.                 winner = box(0)
  160.             End If
  161.         End If
  162.  
  163.         '3DD
  164.         If box(0) = box(13) And box(13) = box(26) Then
  165.             If box(0) <> "-" Then
  166.                 winner = box(0)
  167.             End If
  168.         ElseIf box(2) = box(13) And box(13) = box(24) Then
  169.             If box(2) <> "-" Then
  170.                 winner = box(2)
  171.             End If
  172.         ElseIf box(6) = box(13) And box(13) = box(20) Then
  173.             If box(13) <> "-" Then
  174.                 winner = box(13)
  175.             End If
  176.         ElseIf box(8) = box(13) And box(13) = box(18) Then
  177.             If box(13) <> "-" Then
  178.                 winner = box(13)
  179.             End If
  180.         End If
  181.  
  182.        
  183.  
  184.  
  185.         If box(0) <> "-" And box(1) <> "-" And box(2) <> "-" And box(3) <> "-" And box(4) <> "-" And box(5) <> "-" And box(6) <> "-" And box(7) <> "-" And box(8) <> "-" Then
  186.             winner = "D"
  187.         End If
  188.         Console.WriteLine(winner)
  189.         If winner = "X" Then
  190.             won = "1"
  191.         ElseIf winner = "O" Then
  192.             won = "2"
  193.         ElseIf winner = "D" Then
  194.             won = "3"
  195.         Else
  196.             won = "0"
  197.         End If
  198.     End Function
  199. End Module
Advertisement
Add Comment
Please, Sign In to add comment