Guest User

Untitled

a guest
Apr 24th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.89 KB | None | 0 0
  1.  
  2.  
  3.  
  4. Public Class GameBoard
  5.  
  6. Dim Board(6, 5) As Integer
  7. Dim col0 As Integer = 0
  8. Dim col1 As Integer = 0
  9. Dim col2 As Integer = 0
  10. Dim col3 As Integer = 0
  11. Dim col4 As Integer = 0
  12. Dim col5 As Integer = 0
  13. Dim Col6 As Integer = 0
  14. Dim OffsetFromLeft As Integer
  15. Dim OffsetFromTop As Integer
  16. Dim Player1 As New Player(Form1.txtPlayer1Name.Text, 1)
  17. Dim Player2 As New Player(Form1.txtPlayer2Name.Text, 2)
  18. Dim currentPlayerColor As Integer = 1
  19. Dim currentPlayerName As String
  20.  
  21.  
  22.  
  23. 'Sets the current player to player1. Is called at load
  24. Private Sub setInitialPlayer(ByVal Player As Player)
  25.  
  26. currentPlayerName = Player.Name()
  27. currentPlayerColor = Player.Color
  28. End Sub
  29.  
  30. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  31.  
  32. setInitialPlayer(Player1)
  33. ClearBoardArray(Board)
  34.  
  35.  
  36.  
  37. End Sub
  38. 'Creates a red token picture box, with coordinates taken in as paramaters
  39. Private Sub createRedPicbox(ByVal OffsetFromLeft As Integer, ByVal OffsetFromTop As Integer)
  40. Dim ctlPicbox As New PictureBox
  41. ctlPicbox.BackgroundImage = My.Resources.red
  42. ctlPicbox.Location = New System.Drawing.Point(OffsetFromLeft, OffsetFromTop)
  43. ctlPicbox.Size = CType(New System.Drawing.Point(55, 55), Drawing.Size)
  44. Me.Controls.Add(ctlPicbox)
  45. End Sub
  46. 'Creates a black token picture box the same way
  47. Private Sub createBlackPicbox(ByVal OffsetFromLeft As Integer, ByVal OffsetFromTop As Integer)
  48. Dim ctlPicbox As New PictureBox
  49. ctlPicbox.BackgroundImage = My.Resources.black
  50. ctlPicbox.Location = New System.Drawing.Point(OffsetFromLeft, OffsetFromTop)
  51. ctlPicbox.Size = CType(New System.Drawing.Point(55, 55), Drawing.Size)
  52. Me.Controls.Add(ctlPicbox)
  53. End Sub
  54. 'Sets all values in the Integer array Board(,) to 9, which represents an empty space
  55. Private Sub ClearBoardArray(ByRef Board(,) As Integer)
  56. Dim row As Integer = 6
  57. Dim column As Integer
  58. Do While row >= 0
  59. For column = 5 To 0 Step -1
  60. Board(row, column) = 9
  61. Next
  62. row = -1
  63. column = 5
  64.  
  65. Loop
  66.  
  67.  
  68.  
  69. End Sub
  70. 'coordinates for picture box determined by the column corresponding to the button clicked, and the current row counter for that column
  71. Sub DropTokenPicture(ByVal Column As Integer, ByVal row As Integer)
  72. Select Case Column
  73. Case 0
  74. OffsetFromLeft = 14
  75. Case 1
  76. OffsetFromLeft = 85
  77. Case 2
  78. OffsetFromLeft = 150
  79. Case 3
  80. OffsetFromLeft = 220
  81. Case 4
  82. OffsetFromLeft = 290
  83. Case 5
  84. OffsetFromLeft = 360
  85. Case 6
  86. OffsetFromLeft = 425
  87.  
  88. End Select
  89. If row = 0 Then
  90. OffsetFromTop = 442
  91. ElseIf row = 1 Then
  92. OffsetFromTop = 377
  93. ElseIf row = 2 Then
  94. OffsetFromTop = 309
  95. ElseIf row = 3 Then
  96. OffsetFromTop = 240
  97. ElseIf row = 4 Then
  98. OffsetFromTop = 170
  99. ElseIf row = 5 Then
  100. OffsetFromTop = 100
  101. End If
  102.  
  103. If currentPlayerColor = 1 Then
  104. createRedPicbox(OffsetFromLeft, OffsetFromTop)
  105. Else
  106. createBlackPicbox(OffsetFromLeft, OffsetFromTop)
  107. End If
  108. End Sub
  109. 'Swaps the currentPlayerName and currentPlayerColor with the other player's
  110. Private Sub TakeTurn(ByRef currentPlayerName As String)
  111.  
  112. If currentPlayerName = Form1.txtPlayer1Name.Text Then
  113. currentPlayerName = Form1.txtPlayer2Name.Text
  114. currentPlayerColor = Player2.Color
  115. Else
  116. currentPlayerColor = Player1.Color
  117. currentPlayerName = Form1.txtPlayer1Name.Text
  118. End If
  119. End Sub
  120. 'Sets the corresponding slot to 1 for a red token (player 1) 2 for a black token (player 2)
  121. Private Sub DropToken(ByVal Column As Integer, ByVal row As Integer)
  122.  
  123. Board(Column, row) = currentPlayerColor
  124.  
  125.  
  126. End Sub
  127.  
  128. 'Event handler for the first column's button click event. Calls the DropTokenPicture DropToken checkWinner and takeTurn methods, also increments the column's row counter(col0)
  129. Private Sub btnCol0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol0.Click
  130. Try
  131. DropTokenPicture(0, col0)
  132. DropToken(0, col0)
  133. checkWinner(Board, 0, col0)
  134. col0 += 1
  135. TakeTurn(currentPlayerName)
  136. Catch ex As Exception
  137. Dim Str As String = "Column Full"
  138. MessageBox.Show(Str)
  139. End Try
  140. End Sub
  141.  
  142. Private Sub btnCol1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol1.Click
  143. Try
  144. DropTokenPicture(1, col1)
  145. DropToken(1, col1)
  146. checkWinner(Board, 1, col1)
  147. col1 += 1
  148. TakeTurn(currentPlayerName)
  149. Catch ex As Exception
  150. Dim Str As String = "Column Full"
  151. MessageBox.Show(Str)
  152. End Try
  153.  
  154. End Sub
  155.  
  156. Private Sub btnCol2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol2.Click
  157. Try
  158. DropTokenPicture(2, col2)
  159. DropToken(2, col2)
  160. checkWinner(Board, 2, col2)
  161. col2 += 1
  162. TakeTurn(currentPlayerName)
  163. Catch ex As Exception
  164. Dim Str As String = "Column Full"
  165. MessageBox.Show(Str)
  166. End Try
  167. End Sub
  168.  
  169. Private Sub btnCol3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol3.Click
  170. Try
  171. DropTokenPicture(3, col3)
  172. DropToken(3, col3)
  173. checkWinner(Board, 3, col3)
  174. col3 += 1
  175. TakeTurn(currentPlayerName)
  176. Catch ex As Exception
  177. Dim Str As String = "Column Full"
  178. MessageBox.Show(Str)
  179. End Try
  180. End Sub
  181.  
  182. Private Sub btnCol4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol4.Click
  183. Try
  184. DropTokenPicture(4, col4)
  185. DropToken(4, col4)
  186. checkWinner(Board, 4, col4)
  187. col4 += 1
  188. TakeTurn(currentPlayerName)
  189. Catch ex As Exception
  190. Dim Str As String = "Column Full"
  191. MessageBox.Show(Str)
  192. End Try
  193. End Sub
  194.  
  195. Private Sub btnCol5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol5.Click
  196. Try
  197. DropTokenPicture(5, col5)
  198. DropToken(5, col5)
  199. checkWinner(Board, 5, col5)
  200. col5 += 1
  201. TakeTurn(currentPlayerName)
  202. Catch ex As Exception
  203. Dim Str As String = "Column Full"
  204. MessageBox.Show(Str)
  205. End Try
  206. End Sub
  207.  
  208. Private Sub btnCol6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCol6.Click
  209. Try
  210. DropTokenPicture(6, Col6)
  211. DropToken(6, Col6)
  212.  
  213. checkWinner(Board, 6, Col6)
  214. 'INSERT CODE TO DISPLAY UPDATE DB, DISPLAY NEW SCOREBOARD AND CLOSE THE GAMEBOARD FORM
  215. Col6 += 1
  216. TakeTurn(currentPlayerName)
  217. Catch ex As Exception
  218. Dim Str As String = "Column Full"
  219. MessageBox.Show(Str)
  220. End Try
  221. End Sub
  222. 'Checks horizontally around the token that was just placed for tree adjacent matching tokens. returns true if there are 4 in a row
  223. Private Function checkHorizontal(ByRef Board As Integer(,), ByVal Column As Integer, ByVal Row As Integer) As Boolean
  224. Dim numcheckleft As Integer = 0
  225. Dim numcheckright As Integer = 0
  226. Dim result As Boolean
  227. Dim numtokens As Integer = 1
  228. Dim coltemp1 As Integer = Column
  229. Dim coltemp2 As Integer = Column
  230. Select Case Column
  231. Case 0
  232. numcheckleft = 0
  233. numcheckright = 3
  234. Case 1
  235. numcheckleft = 1
  236. numcheckright = 2
  237. Case 2
  238. numcheckleft = 2
  239. numcheckright = 3
  240. Case 3
  241. numcheckleft = 3
  242. numcheckright = 3
  243. Case 4
  244. numcheckleft = 3
  245. numcheckright = 2
  246. Case 5
  247. numcheckleft = 3
  248. numcheckright = 1
  249. Case 6
  250. numcheckleft = 3
  251. numcheckright = 0
  252.  
  253. End Select
  254.  
  255. Do Until numcheckleft = 0
  256.  
  257. If pieceMatches(Board, coltemp1 - 1, Row) = True Then
  258. numtokens = numtokens + 1
  259. Else
  260. Exit Do
  261. End If
  262. numcheckleft = numcheckleft - 1
  263. coltemp1 = coltemp1 - 1
  264. Loop
  265.  
  266. Do Until numcheckright = 0
  267.  
  268. If pieceMatches(Board, coltemp2 + 1, Row) = True Then
  269. numtokens = numtokens + 1
  270. Else
  271. Exit Do
  272. End If
  273.  
  274. numcheckright = numcheckright - 1
  275. coltemp2 = coltemp2 + 1
  276. Loop
  277.  
  278. If numtokens >= 4 Then
  279. result = True
  280. Else
  281. result = False
  282.  
  283. End If
  284.  
  285. numcheckleft = 0
  286. numtokens = 0
  287. Return result
  288. End Function
  289. 'Checks vertically around the token that was just placed for tree adjacent matching tokens. returns true if there are 4 in a row
  290. Private Function checkVertical(ByRef Board As Integer(,), ByVal Column As Integer, ByVal Row As Integer) As Boolean
  291. Dim numcheckup As Integer = 0
  292. Dim numcheckdown As Integer = 0
  293. Dim result As Boolean
  294. Dim numtokens As Integer = 1
  295. Dim rowtemp1 As Integer = Row
  296. Dim rowtemp2 As Integer = Row
  297.  
  298. Select Case Row
  299. Case 0
  300. numcheckdown = 0
  301. numcheckup = 3
  302. Case 1
  303. numcheckdown = 1
  304. numcheckup = 2
  305. Case 2
  306. numcheckdown = 2
  307. numcheckup = 3
  308. Case 3
  309. numcheckdown = 3
  310. numcheckup = 2
  311. Case 4
  312. numcheckdown = 3
  313. numcheckup = 1
  314. Case 5
  315. numcheckdown = 3
  316. numcheckup = 0
  317.  
  318. End Select
  319.  
  320. Do Until numcheckdown = 0
  321.  
  322. If pieceMatches(Board, Column, rowtemp1 - 1) = True Then
  323. numtokens = numtokens + 1
  324. Else
  325. Exit Do
  326. End If
  327. numcheckdown = numcheckdown - 1
  328. rowtemp1 = rowtemp1 - 1
  329. Loop
  330.  
  331. Do Until numcheckup = 0
  332. If pieceMatches(Board, Column, Row + 1) = True Then
  333. numtokens = numtokens + 1
  334. Else
  335. Exit Do
  336. End If
  337. numcheckup = numcheckup - 1
  338. rowtemp2 = rowtemp2 + 1
  339. Loop
  340.  
  341. If numtokens = 4 Then
  342. result = True
  343. Else
  344. result = False
  345.  
  346. End If
  347. numcheckup = 0
  348. numcheckdown = 0
  349. numtokens = 0
  350. Return result
  351. End Function
  352. 'Checks diagonally around the token that was just placed for tree adjacent matching tokens. returns true if there are 4 in a row
  353. Private Function checkDiagonal(ByRef Board As Integer(,), ByVal Column As Integer, ByVal Row As Integer) As Boolean
  354. Dim numcheckupright As Integer = 0
  355. Dim numcheckdownleft As Integer = 0
  356. Dim result As Boolean
  357. Dim numtokens As Integer = 1
  358. Dim rowtemp1 As Integer = Row
  359. Dim rowtemp2 As Integer = Row
  360. Dim coltemp1 As Integer = Column
  361. Dim coltemp2 As Integer = Column
  362.  
  363. Select Case Column
  364. Case 0
  365. If Row = 2 Then
  366. numcheckupright = 3
  367. numcheckdownleft = 0
  368. End If
  369. Case 1
  370. If Row = 3 Then
  371. numcheckupright = 2
  372. numcheckdownleft = 1
  373. ElseIf Row = 2 Then
  374. numcheckupright = 3
  375. numcheckdownleft = 1
  376. ElseIf Row = 1 Then
  377. numcheckupright = 3
  378. numcheckdownleft = 1
  379. End If
  380. Case 2
  381. If Row = 4 Then
  382. numcheckupright = 1
  383. numcheckdownleft = 2
  384. ElseIf Row = 3 Then
  385. numcheckupright = 2
  386. numcheckdownleft = 2
  387. ElseIf Row = 2 Then
  388. numcheckupright = 3
  389. numcheckdownleft = 2
  390. ElseIf Row = 1 Then
  391. numcheckupright = 3
  392. numcheckdownleft = 1
  393. End If
  394. Case 3
  395. If Row = 5 Then
  396. numcheckupright = 0
  397. numcheckdownleft = 3
  398. ElseIf Row = 4 Then
  399. numcheckupright = 1
  400. numcheckdownleft = 3
  401. ElseIf Row = 3 Then
  402. numcheckupright = 2
  403. numcheckdownleft = 3
  404. ElseIf Row = 2 Then
  405. numcheckupright = 3
  406. numcheckdownleft = 2
  407. ElseIf Row = 1 Then
  408. numcheckupright = 3
  409. numcheckdownleft = 1
  410. End If
  411. Case 4
  412. If Row = 5 Then
  413. numcheckupright = 0
  414. numcheckdownleft = 3
  415. ElseIf Row = 4 Then
  416. numcheckupright = 1
  417. numcheckdownleft = 3
  418. ElseIf Row = 3 Then
  419. numcheckupright = 2
  420. numcheckdownleft = 3
  421. ElseIf Row = 2 Then
  422. numcheckupright = 2
  423. numcheckdownleft = 2
  424. ElseIf Row = 1 Then
  425. numcheckupright = 2
  426. numcheckdownleft = 1
  427. End If
  428.  
  429. Case 5
  430. If Row = 5 Then
  431. numcheckupright = 0
  432. numcheckdownleft = 3
  433. ElseIf Row = 4 Then
  434. numcheckupright = 1
  435. numcheckdownleft = 3
  436. ElseIf Row = 3 Then
  437. numcheckupright = 1
  438. numcheckdownleft = 3
  439. ElseIf Row = 2 Then
  440. numcheckupright = 1
  441. numcheckdownleft = 2
  442. End If
  443. Case 6
  444. If Row = 5 Then
  445. numcheckupright = 0
  446. numcheckdownleft = 3
  447. ElseIf Row = 4 Then
  448. numcheckupright = 0
  449. numcheckdownleft = 3
  450. ElseIf Row = 3 Then
  451. numcheckupright = 0
  452. numcheckdownleft = 3
  453. End If
  454. End Select
  455.  
  456. Do Until numcheckupright = 0
  457.  
  458. If pieceMatches(Board, coltemp1 + 1, rowtemp1 + 1) = True Then
  459. numtokens = numtokens + 1
  460. Else
  461. Exit Do
  462. End If
  463. coltemp1 = coltemp1 + 1
  464. rowtemp1 = rowtemp1 + 1
  465. Loop
  466.  
  467. Do Until numcheckdownleft = 0
  468. If pieceMatches(Board, coltemp2 - 1, rowtemp2 - 1) = True Then
  469. numtokens = numtokens + 1
  470. Else
  471. Exit Do
  472. End If
  473. numcheckdownleft = numcheckdownleft - 1
  474. coltemp2 = coltemp2 - 1
  475. rowtemp2 = rowtemp2 - 1
  476. Loop
  477.  
  478. If numtokens = 4 Then
  479. result = True
  480. Else
  481. result = False
  482.  
  483. End If
  484. numcheckupright = 0
  485. numcheckdownleft = 0
  486. numtokens = 0
  487. Return result
  488. End Function
  489. 'Calls all three checking functions. returns true if one of them returned true
  490. Private Function checkWinner(ByRef Board As Integer(,), ByVal Column As Integer, ByVal Row As Integer) As Boolean
  491. Dim result As Boolean = False
  492. If checkHorizontal(Board, Column, Row) Or checkVertical(Board, Column, Row) Or checkDiagonal(Board, Column, Row) Then
  493. MessageBox.Show(currentPlayerName & " has won!")
  494. result = True
  495. End If
  496. Return result
  497. End Function
  498. 'Checks to see that a token in the specified location matches the current player's token color
  499. Private Function pieceMatches(ByRef Board As Integer(,), ByVal x As Integer, ByVal y As Integer) As Boolean
  500. If Board(x, y) = currentPlayerColor Then
  501. Return True
  502. Else
  503. Return False
  504. End If
  505. End Function
  506. End Class
Add Comment
Please, Sign In to add comment