How to Create a Simple Tic Tack Toe Game using VB.Net

0
118

This tutorial is all about on How Create a Tic Tack Toe Game using VB.Net
Tic Tack Toe Game using VB.Net is a game in which players draw Xs and Os inside a set of nine squares. This tutorial about Tic Tack Toe Game is a simple game that you can create using Visual Basic.Net. So let’s get started.

  • First is open the Visual Basic, Select File on the menu, then click New and create a new project. Then a New Project Dialog will appear. You can rename your project, depending on what you like to name it. After that click OK
  • Then, design your form like this just like what I’ve shown you below.
    Add 12 Buttons, 2 Labels, and a Timer from the toolbox.
    Set the Property Enable of Timer to True and change its interval to 1.
    Set the Property Visible of the Labels to False.

  • After designing your form. Go to Code view and add this following declaration below the Public class Form1
  1. Dim random As String = 1
  • Then add this code for the Timer.
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2. random += 1
    3. If random = 10 Then
    4. random = 1
    5. End IfEnd Sub
  • Add this Following Sub so that the computer can make its own move.
    1. Public Sub PC()If random = 1 And Button1.Enabled = True Then
    2. Button1.Text = "O"
    3. Button1.Enabled = False
    4. End If
    5. If random = 2 And Button2.Enabled = True Then
    6. Button2.Text = "O"
    7. Button2.Enabled = False
    8. End If
    9. If random = 3 And Button3.Enabled = True Then
    10. Button3.Text = "O"
    11. Button3.Enabled = False
    12. End If
    13. If random = 4 And Button4.Enabled = True Then
    14. Button4.Text = "O"
    15. Button4.Enabled = False
    16. End If
    17. If random = 5 And Button5.Enabled = True Then
    18. Button5.Text = "O"
    19. Button5.Enabled = False
    20. End If
    21. If random = 6 And Button6.Enabled = True Then
    22. Button6.Text = "O"
    23. Button6.Enabled = False
    24. End If
    25. If random = 7 And Button7.Enabled = True Then
    26. Button7.Text = "O"
    27. Button7.Enabled = False
    28. End If
    29. If random = 8 And Button8.Enabled = True Then
    30. Button8.Text = "O"
    31. Button8.Enabled = False
    32. End If
    33. If random = 9 And Button9.Enabled = True Then
    34. Button9.Text = "O"
    35. Button9.Enabled = False
    36. End If
    37. End Sub
  • Then after that, this following codes.
    1. Public Sub Options()If Label1.Text = 1 Then
    2. If Button2.Text = "X" And Button3.Enabled = True Then
    3. With Button3
    4. .Text = "O"
    5. .Enabled = False
    6. End With
    7. ElseIf Button3.Text = "X" And Button2.Enabled = True Then
    8. With Button2
    9. .Text = "O"
    10. .Enabled = False
    11. End With
    12. ElseIf Button5.Text = "X" And Button9.Enabled = True Then
    13. With Button9
    14. .Text = "O"
    15. .Enabled = False
    16. End With
    17. ElseIf Button9.Text = "X" And Button5.Enabled = True Then
    18. With Button5
    19. .Text = "O"
    20. .Enabled = False
    21. End With
    22. ElseIf Button4.Text = "X" And Button7.Enabled = True Then
    23. With Button7
    24. .Text = "O"
    25. .Enabled = False
    26. End With
    27. ElseIf Button7.Text = "X" And Button4.Enabled = True Then
    28. With Button4
    29. .Text = "O"
    30. .Enabled = False
    31. End With
    32. ElseIf Button8.Text = "X" Or Button6.Text = "X" Then
    33. hit()
    34. End If
    35. End If
    36. If Label1.Text = 2 Then
    37. If Button1.Text = "X" And Button3.Enabled = True Then
    38. With Button3
    39. .Text = "O"
    40. .Enabled = False
    41. End With
    42. ElseIf Button1.Text = "X" And Button1.Enabled = True Then
    43. With Button1
    44. .Text = "O"
    45. .Enabled = False
    46. End With
    47. ElseIf Button5.Text = "X" And Button8.Enabled = True Then
    48. With Button8
    49. .Text = "O"
    50. .Enabled = False
    51. End With
    52. ElseIf Button8.Text = "X" And Button5.Enabled = True Then
    53. With Button5
    54. .Text = "O"
    55. .Enabled = False
    56. End With
    57. ElseIf Button6.Text = "X" Or Button4.Text = "X" Or Button7.Text = "X" Or Button9.Text = "X" Then
    58. hit()
    59. End If
    60.  
    61. End If
    62. If Label1.Text = 3 Then
    63. If Button1.Text = "X" And Button2.Enabled = True Then
    64. With Button2
    65. .Text = "O"
    66. .Enabled = False
    67. End With
    68. ElseIf Button2.Text = "X" And Button1.Enabled = True Then
    69. With Button1
    70. .Text = "O"
    71. .Enabled = False
    72. End With
    73. ElseIf Button6.Text = "X" And Button9.Enabled = True Then
    74. With Button9
    75. .Text = "O"
    76. .Enabled = False
    77. End With
    78. ElseIf Button9.Text = "X" And Button6.Enabled = True Then
    79. With Button6
    80. .Text = "O"
    81. .Enabled = False
    82. End With
    83. ElseIf Button5.Text = "X" And Button7.Enabled = True Then
    84. With Button7
    85. .Text = "O"
    86. .Enabled = False
    87. End With
    88. ElseIf Button7.Text = "X" And Button5.Enabled = True Then
    89. With Button5
    90. .Text = "O"
    91. .Enabled = False
    92. End With
    93. ElseIf Button8.Text = "X" Or Button4.Text = "X" Then
    94. hit()
    95. End If
    96.  
    97. End If
    98. If Label1.Text = 4 Then
    99. If Button1.Text = "X" And Button7.Enabled = True Then
    100. With Button7
    101. .Text = "O"
    102. .Enabled = False
    103. End With
    104. ElseIf Button7.Text = "X" And Button1.Enabled = True Then
    105. With Button1
    106. .Text = "O"
    107. .Enabled = False
    108. End With
    109. ElseIf Button5.Text = "X" And Button6.Enabled = True Then
    110. With Button6
    111. .Text = "O"
    112. .Enabled = False
    113. End With
    114. ElseIf Button6.Text = "X" And Button5.Enabled = True Then
    115. With Button5
    116. .Text = "O"
    117. .Enabled = False
    118. End With
    119. ElseIf Button2.Text = "X" Or Button3.Text = "X" Or Button8.Text = "X" Or Button9.Text = "X" Then
    120. hit()
    121. End If
    122.  
    123. End If
    124. If Label1.Text = 5 Then
    125. If Button1.Text = "X" And Button9.Enabled = True Then
    126. With Button9
    127. .Text = "O"
    128. .Enabled = False
    129. End With
    130. ElseIf Button9.Text = "X" And Button1.Enabled = True Then
    131. With Button1
    132. .Text = "O"
    133. .Enabled = False
    134. End With
    135. ElseIf Button2.Text = "X" And Button8.Enabled = True Then
    136. With Button8
    137. .Text = "O"
    138. .Enabled = False
    139. End With
    140. ElseIf Button8.Text = "X" And Button2.Enabled = True Then
    141. With Button2
    142. .Text = "O"
    143. .Enabled = False
    144. End With
    145. ElseIf Button3.Text = "X" And Button7.Enabled = True Then
    146. With Button7
    147. .Text = "O"
    148. .Enabled = False
    149. End With
    150. ElseIf Button7.Text = "X" And Button3.Enabled = True Then
    151. With Button3
    152. .Text = "O"
    153. .Enabled = False
    154. End With
    155. ElseIf Button6.Text = "X" And Button4.Enabled = True Then
    156. With Button4
    157. .Text = "O"
    158. .Enabled = False
    159. End With
    160. ElseIf Button4.Text = "X" And Button6.Enabled = True Then
    161. With Button6
    162. .Text = "O"
    163. .Enabled = False
    164. End With
    165. End If
    166.  
    167. End If
    168. If Label1.Text = 6 Then
    169. If Button3.Text = "X" And Button9.Enabled = True Then
    170. With Button9
    171. .Text = "O"
    172. .Enabled = False
    173. End With
    174. ElseIf Button9.Text = "X" And Button3.Enabled = True Then
    175. With Button3
    176. .Text = "O"
    177. .Enabled = False
    178. End With
    179. ElseIf Button5.Text = "X" And Button4.Enabled = True Then
    180. With Button4
    181. .Text = "O"
    182. .Enabled = False
    183. End With
    184. ElseIf Button4.Text = "X" And Button5.Enabled = True Then
    185. With Button5
    186. .Text = "O"
    187. .Enabled = False
    188. End With
    189. ElseIf Button1.Text = "X" Or Button2.Text = "X" Or Button7.Text = "X" Or Button8.Text = "X" Then
    190. hit()
    191. End If
    192.  
    193. End If
    194.  
    195. If Label1.Text = 7 Then
    196. If Button1.Text = "X" And Button4.Enabled = True Then
    197. With Button4
    198. .Text = "O"
    199. .Enabled = False
    200. End With
    201. ElseIf Button4.Text = "X" And Button1.Enabled = True Then
    202. With Button1
    203. .Text = "O"
    204. .Enabled = False
    205. End With
    206. ElseIf Button5.Text = "X" And Button3.Enabled = True Then
    207. With Button3
    208. .Text = "O"
    209. .Enabled = False
    210. End With
    211. ElseIf Button3.Text = "X" And Button5.Enabled = True Then
    212. With Button5
    213. .Text = "O"
    214. .Enabled = False
    215. End With
    216. ElseIf Button8.Text = "X" And Button9.Enabled = True Then
    217. With Button9
    218. .Text = "O"
    219. .Enabled = False
    220. End With
    221. ElseIf Button9.Text = "X" And Button8.Enabled = True Then
    222. With Button8
    223. .Text = "O"
    224. .Enabled = False
    225. End With
    226. ElseIf Button6.Text = "X" Or Button2.Text = "X" Then
    227. hit()
    228. End If
    229.  
    230. End If
    231.  
    232. If Label1.Text = 8 Then
    233. If Button2.Text = "X" And Button5.Enabled = True Then
    234. With Button5
    235. .Text = "O"
    236. .Enabled = False
    237. End With
    238. ElseIf Button5.Text = "X" And Button2.Enabled = True Then
    239. With Button2
    240. .Text = "O"
    241. .Enabled = False
    242. End With
    243. ElseIf Button9.Text = "X" And Button7.Enabled = True Then
    244. With Button7
    245. .Text = "O"
    246. .Enabled = False
    247. End With
    248. ElseIf Button7.Text = "X" And Button9.Enabled = True Then
    249. With Button9
    250. .Text = "O"
    251. .Enabled = False
    252. End With
    253. ElseIf Button6.Text = "X" Or Button3.Text = "X" Or Button1.Text = "X" Or Button4.Text = "X" Then
    254. hit()
    255. End If
    256.  
    257. End If
    258. If Label1.Text = 9 Then
    259. If Button6.Text = "X" And Button3.Enabled = True Then
    260. With Button3
    261. .Text = "O"
    262. .Enabled = False
    263. End With
    264. ElseIf Button3.Text = "X" And Button6.Enabled = True Then
    265. With Button6
    266. .Text = "O"
    267. .Enabled = False
    268. End With
    269. ElseIf Button5.Text = "X" And Button1.Enabled = True Then
    270. With Button1
    271. .Text = "O"
    272. .Enabled = False
    273. End With
    274. ElseIf Button1.Text = "X" And Button5.Enabled = True Then
    275. With Button5
    276. .Text = "O"
    277. .Enabled = False
    278. End With
    279. ElseIf Button8.Text = "X" And Button7.Enabled = True Then
    280. With Button7
    281. .Text = "O"
    282. .Enabled = False
    283. End With
    284. ElseIf Button7.Text = "X" And Button8.Enabled = True Then
    285. With Button8
    286. .Text = "O"
    287. .Enabled = False
    288. End With
    289. ElseIf Button2.Text = "X" Or Button4.Text = "X" Then
    290. hit()
    291. End If
    292.  
    293. End If
    294.  
    295. End Sub
  • This following code will check if there is a winner and will show the result on a message box.
    1. Public Sub CheckIfComputerWins()
    2. If Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "O" _
    3. Or Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "O" _
    4. Or Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "O" _
    5. Or Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "O" _
    6. Or Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "O" _
    7. Or Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "O" _
    8. Or Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "O" _
    9. Or Button7.Text = "O" And Button5.Text = "O" And Button3.Text = "O" Then
    10. MsgBox("Computer Wins! You lose")
    11. Button1.Text = ""
    12. Button1.Enabled = True
    13. Button2.Text = ""
    14. Button2.Enabled = True
    15. Button3.Text = ""
    16. Button3.Enabled = True
    17. Button4.Text = ""
    18. Button4.Enabled = True
    19. Button5.Text = ""
    20. Button5.Enabled = True
    21. Button6.Text = ""
    22. Button6.Enabled = True
    23. Button7.Text = ""
    24. Button7.Enabled = True
    25. Button8.Text = ""
    26. Button8.Enabled = True
    27. Button9.Text = ""
    28. Button9.Enabled = TrueElseIf Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" _
    29. Or Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "X" _
    30. Or Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" _
    31. Or Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" _
    32. Or Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" _
    33. Or Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" _
    34. Or Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" _
    35. Or Button7.Text = "X" And Button5.Text = "X" And Button3.Text = "X" Then
    36. MsgBox("Congratulations! You Win :)")
    37. Button1.Text = ""
    38. Button1.Enabled = True
    39. Button2.Text = ""
    40. Button2.Enabled = True
    41. Button3.Text = ""
    42. Button3.Enabled = True
    43. Button4.Text = ""
    44. Button4.Enabled = True
    45. Button5.Text = ""
    46. Button5.Enabled = True
    47. Button6.Text = ""
    48. Button6.Enabled = True
    49. Button7.Text = ""
    50. Button7.Enabled = True
    51. Button8.Text = ""
    52. Button8.Enabled = True
    53. Button9.Text = ""
    54. Button9.Enabled = True
    55. End If
    56. End Sub
  • Afterwards, we can now program the buttons of the game. Just add this following codes. it will handle the 9 buttons click event:
    1. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    2. Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, _
    3. Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button1.Click
    4.  
    5. sender.Text = "X"
    6. sender.Enabled = False
    7. Timer1.Stop()
    8. Label1.Text = sender.name
    9. Label1.Text = Label1.Text.Replace("Button", "")
    10. If random = Label1.Text Then
    11. random = random + 1
    12. End If
    13. If random > 0 Then
    14. PC()
    15. random = 0
    16. ElseIf random = 0 Then
    17. Options()
    18. End If
    19. Label2.Text = random
    20. CheckIfComputerWins()
    21. End Sub
  • We also need to add a code when the user doesn’t make a proper move.
    Just add this codes.

    1. Public Sub hit()
    2. For Each ctl As Control In Me.Controls
    3. If Label1.Text < 9 Then
    4. If (ctl.Name.StartsWith("Button" & Label1.Text + 1)) Then
    5. Dim btn As Button = DirectCast(ctl, Button)
    6. If btn.Enabled = True Then
    7. btn.Text = "O"
    8. btn.Enabled = False
    9. ElseLabel1.Text = Label1.Text + 1
    10. End IfEnd If
    11. Else
    12. If (ctl.Name.StartsWith("Button1")) Then
    13. Dim btn As Button = DirectCast(ctl, Button)
    14. If btn.Enabled = True Then
    15. btn.Text = "O"
    16. btn.Enabled = False
    17. Else
    18.  
    19. Label1.Text = Label1.Text + 1
    20. End If
    21.  
    22. End If
    23. End If
    24.  
    25. Next
    26.  
    27. End Sub
  • Now, we program the PC START Button. Add this codes.
    1. Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    2. Timer1.Stop()
    3. Button1.Text = "O"
    4. Label1.Text = 1Button1.Enabled = False
    5. Button10.Enabled = FalseEnd Sub
  • Finally, add this following codes to the Restart Button.

    1. Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    2. Button1.Text = ""
    3. Button1.Enabled = True
    4. Button2.Text = ""
    5. Button2.Enabled = True
    6. Button3.Text = ""
    7. Button3.Enabled = True
    8. Button4.Text = ""
    9. Button4.Enabled = True
    10. Button5.Text = ""
    11. Button5.Enabled = True
    12. Button6.Text = ""
    13. Button6.Enabled = True
    14. Button7.Text = ""
    15. Button7.Enabled = True
    16. Button8.Text = ""
    17. Button8.Enabled = True
    18. Button9.Text = ""
    19. Button9.Enabled = True
    20. Button10.Enabled = True
    21. Timer1.Start()End Sub
  • Click F5 to run the program and to play the game.
  • If you have any questions or suggestions about How to Create a Tic Tack Toe Game using VB.Net please contact me through our contact page.

  • Download How to Create a Tic Tack Toe Game using VB.Net Source code Here

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here