Download presentation
Presentation is loading. Please wait.
1
計算機程式及實習 課堂作業之口頭報告ppt製作
題目:OX井字遊戲 期末報告 學校:南台科技大學 班級:自控一甲 姓名:陳力維 學號:4A212097 老師:謝慶存
2
OX井字遊戲:題目說明:玩法說明 一進到畫面即可開始遊戲,遊戲玩法就跟小時候玩的井字遊戲一樣,O先開始,在九宮格內任一位子按下,接著換X在剩下的位子按下,重複動作後若O或X先連成一條線(不管直的、橫的、斜的),即會跳出一個對話視窗方塊顯示「遊戲結束!O(X)贏」,之後按結束即可關閉遊戲。
3
表單(form)設計:建立輸出入介面 btn2 btn3 btn1 btn5 btn6 btn4 btn8 btn9 btn7
Button2
4
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 For i As Integer = 0 To 2 For j As Integer = 0 To 2 board(i, j) = -3 Next j Next i End Sub 11.Function CheckWin() Dim total, totall As Integer For i As Integer = 0 To 2 total = 0 : totall = 0 For j As Integer = 0 To 2 total += board(i, j) totall += board(i, j) Next j If total = 0 Or totall = 0 Then MsgBox(“遊戲結束!O贏") Return True End If If total = 3 Or totall = 3 Then MsgBox("遊戲結束!X贏") Return True End If
5
27.Next i total = board(0, 0) + board(1, 1) + board(2, 2) totall = board(2, 0) + board(1, 1) + board(0, 2) If total = 0 Or totall = 0 Then MsgBox("遊戲結束!O贏") Return True End If If total = 3 Or totall = 3 Then MsgBox("遊戲結束!X贏") Return True End If Return False End Function 40.Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click If turn Then btn1.Text = "O" board(0, 0) = 0 Else btn1.Text = "X" board(0, 0) = 1 End If turn = Not turn CheckWin() End Sub
6
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 btn2.Text = "O" board(0, 1) = 0 Else btn2.Text = "X" board(0, 1) = 1 End If turn = Not turn CheckWin() End Sub Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click If turn Then btn3.Text = "O" board(0, 2) = 0 Else btn3.Text = "X" board(0, 2) = 1 End If turn = Not turn CheckWin() End Sub
7
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 If turn Then btn4.Text = "O" board(1, 0) = 0 Else btn4.Text = "X" board(1, 0) = 1 End If turn = Not turn CheckWin() End Sub Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click If turn Then btn5.Text = "O" board(1, 1) = 0 Else btn5.Text = "X" board(1, 1) = 1 End If turn = Not turn CheckWin() End Sub
8
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 If turn Then btn6.Text = "O" board(1, 2) = 0 Else btn6.Text = "X" board(1, 2) = 1 End If turn = Not turn CheckWin() End Sub Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click If turn Then btn7.Text = "O" board(2, 0) = 0 Else btn7.Text = "X" board(2, 0) = 1 End If turn = Not turn CheckWin() End Sub
9
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 If turn Then btn8.Text = "O" board(2, 1) = 0 Else btn8.Text = "X" board(2, 1) = 1 End If turn = Not turn CheckWin() End Sub Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click If turn Then btn9.Text = "O" board(2, 2) = 0 Else btn9.Text = "X" board(2, 2) = 1 End If turn = Not turn CheckWin() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click End End Sub 140.End Class
10
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列:遊戲結束,關閉視窗。
11
心得感言 Visual Basic在高一的計算機概論課就有接觸到過的,而且也在大學上了一整個學期,現在可以自己製作一些簡單的小程式,不管是身分證字號檢測器或者音樂播放器都可以自己來。謝謝老師這學期能教我們這麼多Visual Basic 的程式應用讓我們也可以製作簡單而且屬於自己的小遊戲。
12
參考文獻 Visual Basic 2010基礎必修課 林義証/蔡文龍/張傑瑞/何叡 著 碁峯資訊
Similar presentations