計算機程式及實習 課堂作業之口頭報告ppt製作 題目:OX井字遊戲 期末報告 學校:南台科技大學 班級:自控一甲 姓名:陳力維 學號:4A212097 老師:謝慶存
OX井字遊戲:題目說明:玩法說明 一進到畫面即可開始遊戲,遊戲玩法就跟小時候玩的井字遊戲一樣,O先開始,在九宮格內任一位子按下,接著換X在剩下的位子按下,重複動作後若O或X先連成一條線(不管直的、橫的、斜的),即會跳出一個對話視窗方塊顯示「遊戲結束!O(X)贏」,之後按結束即可關閉遊戲。
表單(form)設計:建立輸出入介面 btn2 btn3 btn1 btn5 btn6 btn4 btn8 btn9 btn7 Button2
OX井字遊戲程式碼撰寫 1.Public Class Form1 2. Dim turn As Boolean = True 3. Dim board(2, 2) As Integer 4.Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 5. For i As Integer = 0 To 2 6. For j As Integer = 0 To 2 7. board(i, j) = -3 8. Next j 9. Next i 10. End Sub 11.Function CheckWin() 12. Dim total, totall As Integer 13. For i As Integer = 0 To 2 14. total = 0 : totall = 0 15. For j As Integer = 0 To 2 16. total += board(i, j) 17. totall += board(i, j) 18. Next j 19. If total = 0 Or totall = 0 Then 20. MsgBox(“遊戲結束!O贏") 21. Return True 22. End If 23. If total = 3 Or totall = 3 Then 24. MsgBox("遊戲結束!X贏") 25. Return True 26. End If
27.Next i 28. total = board(0, 0) + board(1, 1) + board(2, 2) 29. totall = board(2, 0) + board(1, 1) + board(0, 2) 30. If total = 0 Or totall = 0 Then 31. MsgBox("遊戲結束!O贏") 32. Return True 33. End If 34. If total = 3 Or totall = 3 Then 35. MsgBox("遊戲結束!X贏") 36. Return True 37. End If 38. Return False 39. End Function 40.Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click If turn Then 41. btn1.Text = "O" 42. board(0, 0) = 0 43. Else 44. btn1.Text = "X" 45. board(0, 0) = 1 46. End If 47. turn = Not turn 48. CheckWin() 49. End Sub
50. Private Sub btn2_Click(ByVal sender As System 50.Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click If turn Then 51. btn2.Text = "O" 52. board(0, 1) = 0 53. Else 54. btn2.Text = "X" 55. board(0, 1) = 1 56. End If 57. turn = Not turn 58. CheckWin() 59. End Sub 60. Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click 61. If turn Then 62. btn3.Text = "O" 63. board(0, 2) = 0 64. Else 65. btn3.Text = "X" 66. board(0, 2) = 1 67. End If 68. turn = Not turn 69. CheckWin() 70. End Sub
71. Private Sub btn4_Click(ByVal sender As System 71.Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click 72. If turn Then 73. btn4.Text = "O" 74. board(1, 0) = 0 75. Else 76. btn4.Text = "X" 77. board(1, 0) = 1 78. End If 79. turn = Not turn 80. CheckWin() 81. End Sub 82. Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click 83. If turn Then 84. btn5.Text = "O" 85. board(1, 1) = 0 86. Else 87. btn5.Text = "X" 88. board(1, 1) = 1 89. End If 90. turn = Not turn 91. CheckWin() 92. End Sub
93. Private Sub btn6_Click(ByVal sender As System 93.Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click 94. If turn Then 95. btn6.Text = "O" 96. board(1, 2) = 0 97. Else 98. btn6.Text = "X" 99. board(1, 2) = 1 100. End If 101. turn = Not turn 102. CheckWin() 103. End Sub 104. Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click 105. If turn Then 106. btn7.Text = "O" 107. board(2, 0) = 0 108. Else 109. btn7.Text = "X" 110. board(2, 0) = 1 111. End If 112. turn = Not turn 113. CheckWin() 114. End Sub
115. Private Sub btn8_Click(ByVal sender As System 115.Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click 116. If turn Then 117. btn8.Text = "O" 118. board(2, 1) = 0 119. Else 120. btn8.Text = "X" 121. board(2, 1) = 1 122. End If 123. turn = Not turn 124. CheckWin() 125. End Sub 126. Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click 127. If turn Then 128. btn9.Text = "O" 129. board(2, 2) = 0 130. Else 131. btn9.Text = "X" 132. board(2, 2) = 1 133. End If 134. turn = Not turn 135. CheckWin() 136. End Sub 137. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 138. End 139. End Sub 140.End Class
OX井字遊戲程式碼解說 第1~3列:宣告全域變數turn判斷是輪到「O」或「X」。 第4~10列:Form_Load()事件處理程序指定二維陣列的元素初值是-3。 第11~39列:CheckWin()函數判斷遊戲是否結束,在第13~17列是For/Next巢狀迴圈,第15~18列的內層迴圈計算每一列和欄的和,在第19~26列的2個If Then條件判斷是「O」贏,或「X」贏,第28~29列是2個對角線。 第40~136列:btn1~9_Click()事件處理程序的程式碼都很相似,使用If Then/Else條件判斷全域變數turn決定輪到誰,以便在Button控制項顯示「O」或「X」,和指定二維陣列指定位置元素的值,「O」是0;「X」是1。 第138列:遊戲結束,關閉視窗。
心得感言 Visual Basic在高一的計算機概論課就有接觸到過的,而且也在大學上了一整個學期,現在可以自己製作一些簡單的小程式,不管是身分證字號檢測器或者音樂播放器都可以自己來。謝謝老師這學期能教我們這麼多Visual Basic 的程式應用讓我們也可以製作簡單而且屬於自己的小遊戲。
參考文獻 Visual Basic 2010基礎必修課 林義証/蔡文龍/張傑瑞/何叡 著 碁峯資訊