Advertisement
Guest User

VB.Net ; TicTacToe vs Cpu need help

a guest
Feb 10th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.20 KB | None | 0 0
  1. Public Class frmHumanVSComputer
  2. Private strSpeler1, strSpeler2 As String
  3. Private strOXO As String = "O"
  4. Private strX As String = "X"
  5. Private blnBoolean As Boolean = False
  6. Private objRandom As New Random
  7. Private intRandom As Integer
  8. Private Sub btnTerug_Click(sender As System.Object, e As System.EventArgs) Handles btnTerug.Click
  9. Me.Close()
  10. frmBegin.Show()
  11. End Sub
  12.  
  13. Private Sub btn1_Click(sender As System.Object, e As System.EventArgs) Handles btn1.Click
  14. btn1.Text = strOXO
  15. btn1.Enabled = False
  16. WieWintM()
  17. If blnBoolean = False Then
  18. Computer()
  19. End If
  20.  
  21. End Sub
  22.  
  23. Private Sub btn2_Click(sender As System.Object, e As System.EventArgs) Handles btn2.Click
  24. btn2.Text = strOXO
  25. btn2.Enabled = False
  26. WieWintM()
  27. If blnBoolean = False Then
  28. Computer()
  29. End If
  30. End Sub
  31.  
  32. Private Sub btn3_Click(sender As System.Object, e As System.EventArgs) Handles btn3.Click
  33. btn3.Text = strOXO
  34. btn3.Enabled = False
  35. WieWintM()
  36. If blnBoolean = False Then
  37. Computer()
  38. End If
  39. End Sub
  40.  
  41. Private Sub btn4_Click(sender As System.Object, e As System.EventArgs) Handles btn4.Click
  42. btn4.Text = strOXO
  43. btn4.Enabled = False
  44. WieWintM()
  45. If blnBoolean = False Then
  46. Computer()
  47. End If
  48. End Sub
  49.  
  50. Private Sub btn5_Click(sender As System.Object, e As System.EventArgs) Handles btn5.Click
  51. btn5.Text = strOXO
  52. btn5.Enabled = False
  53. WieWintM()
  54. If blnBoolean = False Then
  55. Computer()
  56. End If
  57. End Sub
  58.  
  59. Private Sub btn6_Click(sender As System.Object, e As System.EventArgs) Handles btn6.Click
  60. btn6.Text = strOXO
  61. btn6.Enabled = False
  62. WieWintM()
  63. If blnBoolean = False Then
  64. Computer()
  65. End If
  66. End Sub
  67.  
  68. Private Sub btn7_Click(sender As System.Object, e As System.EventArgs) Handles btn7.Click
  69. btn7.Text = strOXO
  70. btn7.Enabled = False
  71. WieWintM()
  72. If blnBoolean = False Then
  73. Computer()
  74. End If
  75. End Sub
  76.  
  77. Private Sub btn8_Click(sender As System.Object, e As System.EventArgs) Handles btn8.Click
  78. btn8.Text = strOXO
  79. btn8.Enabled = False
  80. WieWintM()
  81. If blnBoolean = False Then
  82. Computer()
  83. End If
  84. End Sub
  85.  
  86. Private Sub btn9_Click(sender As System.Object, e As System.EventArgs) Handles btn9.Click
  87. btn9.Text = strOXO
  88. btn9.Enabled = False
  89. WieWintM()
  90. If blnBoolean = False Then
  91. Computer()
  92. End If
  93. End Sub
  94.  
  95. Private Sub btnOpnieuw_Click(sender As System.Object, e As System.EventArgs) Handles btnOpnieuw.Click
  96. btn1.Text = String.Empty
  97. btn2.Text = String.Empty
  98. btn3.Text = String.Empty
  99. btn4.Text = String.Empty
  100. btn5.Text = String.Empty
  101. btn6.Text = String.Empty
  102. btn7.Text = String.Empty
  103. btn8.Text = String.Empty
  104. btn9.Text = String.Empty
  105. btn1.Enabled = True
  106. btn2.Enabled = True
  107. btn3.Enabled = True
  108. btn4.Enabled = True
  109. btn5.Enabled = True
  110. btn6.Enabled = True
  111. btn7.Enabled = True
  112. btn8.Enabled = True
  113. btn9.Enabled = True
  114. lblWin.Text = String.Empty
  115. btn1.BackColor = Color.FromKnownColor(KnownColor.Control)
  116. btn2.BackColor = Color.FromKnownColor(KnownColor.Control)
  117. btn3.BackColor = Color.FromKnownColor(KnownColor.Control)
  118. btn4.BackColor = Color.FromKnownColor(KnownColor.Control)
  119. btn5.BackColor = Color.FromKnownColor(KnownColor.Control)
  120. btn6.BackColor = Color.FromKnownColor(KnownColor.Control)
  121. btn7.BackColor = Color.FromKnownColor(KnownColor.Control)
  122. btn8.BackColor = Color.FromKnownColor(KnownColor.Control)
  123. btn9.BackColor = Color.FromKnownColor(KnownColor.Control)
  124. blnBoolean = False
  125.  
  126.  
  127.  
  128. End Sub
  129.  
  130. Private Sub frmHumanVSComputer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  131. strSpeler1 = InputBox("Geef de naam van de speler in", "Speler 1")
  132. strSpeler2 = "Computer"
  133.  
  134. lblSpeler1.Text = strSpeler1
  135. lblSpeler2.Text = strSpeler2
  136. btn1.BackColor = Color.FromKnownColor(KnownColor.Control)
  137. btn2.BackColor = Color.FromKnownColor(KnownColor.Control)
  138. btn3.BackColor = Color.FromKnownColor(KnownColor.Control)
  139. btn4.BackColor = Color.FromKnownColor(KnownColor.Control)
  140. btn5.BackColor = Color.FromKnownColor(KnownColor.Control)
  141. btn6.BackColor = Color.FromKnownColor(KnownColor.Control)
  142. btn7.BackColor = Color.FromKnownColor(KnownColor.Control)
  143. btn8.BackColor = Color.FromKnownColor(KnownColor.Control)
  144. btn9.BackColor = Color.FromKnownColor(KnownColor.Control)
  145.  
  146. End Sub
  147. Function WieWintM()
  148.  
  149. If btn1.Text = "X" And btn2.Text = "X" And btn3.Text = "X" Then
  150. lblWin.Text = strSpeler2 & " wint"
  151. pgb2.Increment(10)
  152. btnDisableM()
  153. btn1.BackColor = Color.Red
  154. btn2.BackColor = Color.Red
  155. btn3.BackColor = Color.Red
  156. blnBoolean = True
  157. End If
  158. If btn4.Text = "X" And btn5.Text = "X" And btn6.Text = "X" Then
  159. lblWin.Text = strSpeler2 & " wint"
  160. pgb2.Increment(10)
  161. btnDisableM()
  162. btn4.BackColor = Color.Red
  163. btn5.BackColor = Color.Red
  164. btn6.BackColor = Color.Red
  165. blnBoolean = True
  166. End If
  167. If btn7.Text = "X" And btn8.Text = "X" And btn9.Text = "X" Then
  168. lblWin.Text = strSpeler2 & " wint"
  169. pgb2.Increment(10)
  170. btnDisableM()
  171. btn7.BackColor = Color.Red
  172. btn8.BackColor = Color.Red
  173. btn9.BackColor = Color.Red
  174. blnBoolean = True
  175. End If
  176. If btn1.Text = "X" And btn4.Text = "X" And btn7.Text = "X" Then
  177. lblWin.Text = strSpeler2 & " wint"
  178. btnDisableM()
  179. btn1.BackColor = Color.Red
  180. btn4.BackColor = Color.Red
  181. btn7.BackColor = Color.Red
  182. blnBoolean = True
  183. End If
  184. If btn2.Text = "X" And btn5.Text = "X" And btn8.Text = "X" Then
  185. lblWin.Text = strSpeler2 & " wint"
  186. pgb2.Increment(10)
  187. btnDisableM()
  188. btn2.BackColor = Color.Red
  189. btn5.BackColor = Color.Red
  190. btn8.BackColor = Color.Red
  191. blnBoolean = True
  192. End If
  193. If btn3.Text = "X" And btn6.Text = "X" And btn9.Text = "X" Then
  194. lblWin.Text = strSpeler2 & " wint"
  195. pgb2.Increment(10)
  196. btnDisableM()
  197. btn3.BackColor = Color.Red
  198. btn6.BackColor = Color.Red
  199. btn9.BackColor = Color.Red
  200. blnBoolean = True
  201. End If
  202. If btn1.Text = "X" And btn5.Text = "X" And btn9.Text = "X" Then
  203. lblWin.Text = strSpeler2 & " wint"
  204. pgb2.Increment(10)
  205. btnDisableM()
  206. btn1.BackColor = Color.Red
  207. btn5.BackColor = Color.Red
  208. btn9.BackColor = Color.Red
  209. blnBoolean = True
  210. End If
  211. If btn3.Text = "X" And btn5.Text = "X" And btn7.Text = "X" Then
  212. lblWin.Text = strSpeler2 & " wint"
  213. pgb2.Increment(10)
  214. btnDisableM()
  215. btn3.BackColor = Color.Red
  216. btn5.BackColor = Color.Red
  217. btn7.BackColor = Color.Red
  218. blnBoolean = True
  219. End If
  220. If btn1.Text = "O" And btn2.Text = "O" And btn3.Text = "O" Then
  221. lblWin.Text = strSpeler1 & " wint"
  222. pgb1.Increment(10)
  223. btnDisableM()
  224. btn1.BackColor = Color.Yellow
  225. btn2.BackColor = Color.Yellow
  226. btn3.BackColor = Color.Yellow
  227. blnBoolean = True
  228. End If
  229. If btn4.Text = "O" And btn5.Text = "O" And btn6.Text = "O" Then
  230. lblWin.Text = strSpeler1 & " wint"
  231. pgb1.Increment(10)
  232. btnDisableM()
  233. btn4.BackColor = Color.Yellow
  234. btn5.BackColor = Color.Yellow
  235. btn6.BackColor = Color.Yellow
  236. blnBoolean = True
  237. End If
  238. If btn7.Text = "O" And btn8.Text = "O" And btn9.Text = "O" Then
  239. lblWin.Text = strSpeler1 & " wint"
  240. pgb1.Increment(10)
  241. btnDisableM()
  242. btn7.BackColor = Color.Yellow
  243. btn8.BackColor = Color.Yellow
  244. btn9.BackColor = Color.Yellow
  245. blnBoolean = True
  246. End If
  247. If btn1.Text = "O" And btn4.Text = "O" And btn7.Text = "O" Then
  248. lblWin.Text = strSpeler1 & " wint"
  249. pgb1.Increment(10)
  250. btnDisableM()
  251. btn1.BackColor = Color.Yellow
  252. btn4.BackColor = Color.Yellow
  253. btn7.BackColor = Color.Yellow
  254. blnBoolean = True
  255. End If
  256. If btn2.Text = "O" And btn5.Text = "O" And btn8.Text = "O" Then
  257. lblWin.Text = strSpeler1 & " wint"
  258. pgb1.Increment(10)
  259. btnDisableM()
  260. btn2.BackColor = Color.Yellow
  261. btn5.BackColor = Color.Yellow
  262. btn8.BackColor = Color.Yellow
  263. blnBoolean = True
  264. End If
  265. If btn3.Text = "O" And btn6.Text = "O" And btn9.Text = "O" Then
  266. lblWin.Text = strSpeler1 & " wint"
  267. pgb1.Increment(10)
  268. btnDisableM()
  269. btn3.BackColor = Color.Yellow
  270. btn6.BackColor = Color.Yellow
  271. btn9.BackColor = Color.Yellow
  272. blnBoolean = True
  273. End If
  274. If btn1.Text = "O" And btn5.Text = "O" And btn9.Text = "O" Then
  275. lblWin.Text = strSpeler1 & " wint"
  276. pgb1.Increment(10)
  277. btnDisableM()
  278. btn1.BackColor = Color.Yellow
  279. btn5.BackColor = Color.Yellow
  280. btn9.BackColor = Color.Yellow
  281. blnBoolean = True
  282. End If
  283. If btn3.Text = "O" And btn5.Text = "O" And btn7.Text = "O" Then
  284. lblWin.Text = strSpeler1 & " wint"
  285. pgb1.Increment(10)
  286. btnDisableM()
  287. btn3.BackColor = Color.Yellow
  288. btn5.BackColor = Color.Yellow
  289. btn7.BackColor = Color.Yellow
  290. blnBoolean = True
  291. End If
  292. If btn1.Text <> String.Empty And btn2.Text <> String.Empty And btn3.Text <> String.Empty And btn4.Text <> String.Empty And btn5.Text <> String.Empty And btn6.Text <> String.Empty And btn7.Text <> String.Empty And btn8.Text <> String.Empty And btn9.Text <> String.Empty And blnBoolean = False Then
  293. lblWin.Text = " Gelijkspel"
  294. pgb1.Increment(10)
  295. pgb2.Increment(10)
  296. btnDisableM()
  297.  
  298. End If
  299.  
  300. Return False
  301.  
  302. End Function
  303. Function btnDisableM()
  304. btn1.Enabled = False
  305. btn2.Enabled = False
  306. btn3.Enabled = False
  307. btn4.Enabled = False
  308. btn5.Enabled = False
  309. btn6.Enabled = False
  310. btn7.Enabled = False
  311. btn8.Enabled = False
  312. btn9.Enabled = False
  313. Return False
  314.  
  315. End Function
  316. Function Computer()
  317.  
  318.  
  319. intRandom = objRandom.Next(1, 10)
  320. If btn1.Enabled = False And btn2.Enabled = False And btn3.Enabled = False And btn4.Enabled = False And btn5.Enabled = False And btn6.Enabled = False And btn7.Enabled = False And btn8.Enabled = False And btn9.Enabled = False Then
  321. Exit Function
  322.  
  323. End If
  324. If intRandom = 1 Then
  325. If btn1.Enabled = True Then
  326. btn1.Text = strX
  327. btn1.Enabled = False
  328. Exit Function
  329. ElseIf btn1.Enabled = False Then
  330. Computer()
  331. End If
  332. End If
  333. If intRandom = 2 Then
  334. If btn2.Enabled = True Then
  335. btn2.Text = strX
  336. btn2.Enabled = False
  337. Exit Function
  338. ElseIf btn2.Enabled = False Then
  339. Computer()
  340. End If
  341. End If
  342. If intRandom = 3 Then
  343. If btn3.Enabled = True Then
  344. btn3.Text = strX
  345. btn3.Enabled = False
  346. Exit Function
  347. ElseIf btn3.Enabled = False Then
  348. Computer()
  349. End If
  350. End If
  351. If intRandom = 4 Then
  352. If btn4.Enabled = True Then
  353. btn4.Text = strX
  354. btn4.Enabled = False
  355. Exit Function
  356. ElseIf btn4.Enabled = False Then
  357. Computer()
  358. End If
  359. End If
  360. If intRandom = 5 Then
  361. If btn5.Enabled = True Then
  362. btn5.Text = strX
  363. btn5.Enabled = False
  364. Exit Function
  365. ElseIf btn5.Enabled = False Then
  366. Computer()
  367. End If
  368. End If
  369. If intRandom = 6 Then
  370. If btn6.Enabled = True Then
  371. btn6.Text = strX
  372. btn6.Enabled = False
  373. Exit Function
  374. ElseIf btn6.Enabled = False Then
  375. Computer()
  376. End If
  377. End If
  378. If intRandom = 7 Then
  379. If btn7.Enabled = True Then
  380. btn7.Text = strX
  381. btn7.Enabled = False
  382. Exit Function
  383. ElseIf btn7.Enabled = False Then
  384. Computer()
  385. End If
  386. End If
  387. If intRandom = 8 Then
  388. If btn8.Enabled = True Then
  389. btn8.Text = strX
  390. btn8.Enabled = False
  391. Exit Function
  392. ElseIf btn8.Enabled = False Then
  393. Computer()
  394. End If
  395. End If
  396. If intRandom = 9 Then
  397. If btn9.Enabled = True Then
  398. btn9.Text = strX
  399. btn9.Enabled = False
  400. Exit Function
  401. ElseIf btn9.Enabled = False Then
  402. Computer()
  403. End If
  404. End If
  405. WieWintM()
  406.  
  407.  
  408.  
  409. End Function
  410. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement