【正文】
ld)。 mineColumn = (numberOfRowsInMineField)。 if ((mineRow + 1 != currentColumn) || (mineColumn + 1 != currentRow)) { if (blocks[mineColumn + 1][mineRow + 1].hasMine()) { row。 // mine is already there, don39。t repeat for same block } // plant mine at this location blocks[mineColumn + 1][mineRow + 1].plantMine()。 } // exclude the user clicked location else { row。 } } int nearByMineCount。 // count number of mines in surrounding blocks ... } private boolean areMinesSet。 // check if mines are planted in blocksblocks[row][column].setOnClickListener(new OnClickListener(){ @Override public void onClick(View view) {... // set mines on first click if (!areMinesSet) { areMinesSet = true。 setMines(currentRow, currentColumn)。 } }})。private void setMines(int currentRow, int currentColumn){ // set mines excluding the location where user clicked Random rand = new Random()。 int mineRow, mineColumn。 for (int row = 0。 row totalNumberOfMines。 row++) { mineRow = (numberOfColumnsInMineField)。 mineColumn = (numberOfRowsInMineField)。 if ((mineRow + 1 != currentColumn) || (mineColumn + 1 != currentRow)) { if (blocks[mineColumn + 1][mineRow + 1].hasMine()) { row。 // mine is already there, don39。t repeat for same block } // plant mine at this location blocks[mineColumn + 1][mineRow + 1].plantMine()。 } // exclude the user clicked location else { row。 } } int nearByMineCount。 // count number of mines in surrounding blocks ...} 在磚塊上做標(biāo)記我們進(jìn)一步討論將磚塊標(biāo)記為旗子、問號和取消標(biāo)記的方法。實現(xiàn)的方法直接明了,當(dāng)我們收到非左右鍵點擊的長點擊事件時,我們檢查磚塊現(xiàn)在的狀態(tài)。如果磚塊上沒有標(biāo)記,就在上面插旗子(表示內(nèi)有地雷),如果磚塊上插著旗子,就在上面標(biāo)問號(懷疑但不確定有地雷),而如果磚塊上面有問號標(biāo)記,就清除標(biāo)記。我們只需要一些條件判斷句就能實現(xiàn): blocks[row][column].setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(View view) { // simulate a leftright (middle) click // if it is a long click on an opened mine then // open all surrounding blocks ... // if clicked block is enabled, clickable or flagged if (blocks[currentRow][currentColumn].isClickable() amp。amp。 (blocks[currentRow][currentColumn].isEnabled() || blocks[currentRow][currentColumn].isFlagged())) { // for long clicks set: // 1. empty blocks to flagged // 2. flagged to question mark // 3. question mark to blank // case 1. set blank block to flagged if (!blocks[currentRow][currentColumn].isFlagged() amp。amp。 !blocks[currentRow][currentColumn].isQuestionMarked()) { blocks[currentRow][currentColumn].setBlockAsDisabled(false)。 blocks[currentRow][currentColumn].setFlagIcon(true)。 blocks[currentRow][currentColumn].setFlagged(true)。 minesToFind。 //reduce mine count updateMineCountDisplay()。 } // case 2. set flagged to question mark elseif (!blocks[currentRow][currentColumn].isQuestionMarked()) { blocks[currentRow][currentColumn].setBlockAsDisabled(true)。 blocks[currentRow][currentColumn].setQuestionMarkIcon(true)。 blocks[currentRow][currentColumn].setFlagged(false)。 blocks[currentRow][currentColumn].setQuestionMarked(true)。 minesToFind++。 // increase mine count updateMineCountDisplay()。 } // case 3. change to blank square else { blocks[currentRow][currentColumn].setBlockAsDisabled(true)。 blocks[currentRow][currentColumn].clearAllIcons()。 blocks[currentRow][currentColumn].setQuestionMarked(false)。 // if it is flagged then increment mine count if (blocks[currentRow][currentColumn].isFlagged()) { minesToFind++。 // increase mine count updateMineCountDisplay()。 } // remove flagged status blocks[currentRow][currentColumn].setFlagged(false)。 } updateMineCountDisplay()。 // update mine display } returntrue。 } })。 這一步非常重要。每次點擊后都要檢查游戲的狀態(tài),確保我們不會錯過任何點擊和任何磚塊。當(dāng)點擊的磚塊下邊有地雷時,我們就輸了。當(dāng)所有包含地雷的磚塊上插了旗子時,我們就贏了。代碼如下: // check status of the game at each stepif (blocks[currentRow + previousRow][currentColumn + previousColumn].hasMine()){ // oops game over finishGame(currentRow + previousRow, currentColumn + previousColumn)。}// did we win the gameif (checkGameWin()){ // mark game as win winGame()。}privateboolean checkGameWin(){ for (int row = 1。 row numberOfRowsInMineField + 1。 row++) { for (int column = 1。 column numberOfColumnsInMineField + 1。 column++) { if (!blocks[row][column].hasMine() amp。amp。 blocks[row][column].isCovered()) { returnfalse。 } } } returntrue。}圖36圖37 測試/玩法玩游戲的方法和Windows版是一樣的。請留意以下關(guān)鍵點: 點擊微笑按鈕,開始一個新游戲點擊或者觸摸一個磚塊來打開它,這和Windows中的鼠標(biāo)左鍵功能相同。點擊或者保持觸摸一個磚塊達(dá)到一秒,則對其進(jìn)行標(biāo)記,包括旗幟、問號或者清除標(biāo)記。這和Windows中的鼠標(biāo)右鍵功能相同。點擊或者保持觸摸一個打開的數(shù)字磚塊,則將周圍的磚塊都打開(周圍的所有地雷已經(jīng)插了旗子),這和Windows中的中間鍵功能相同。沒有插旗磚塊的圖標(biāo),用F字母表示旗幟。親自實現(xiàn)并試玩,反饋你的體驗。圖385 心得體會 通過這十二周的學(xué)習(xí),我們首次對于安卓系統(tǒng)的開發(fā)起源、發(fā)展過程、未來趨勢以及應(yīng)用方面進(jìn)行了初步的文字性的了解,后在一些參考文獻(xiàn)的輔助下下載了并搭建了用于android開發(fā)的eclipes集成環(huán)境,并第一次建立運行簡單程序hello world。隨后我們更進(jìn)一步學(xué)習(xí)了解了android系統(tǒng)中較復(fù)雜的MicroJobs應(yīng)用程序。在對其主要文件進(jìn)行分析后,在此基礎(chǔ)上我們開始嘗試寫一些簡單程序,如計算器程序。在對這一實際問題進(jìn)行操作并解決一些問題后,我們對于android程序的編寫有了更深一步的認(rèn)識。再最后的三周,我們學(xué)習(xí)了較為復(fù)雜的掃雷游戲。這個游戲中,在編程語言、操作界面等方面都增加了難度。 可以說我們在這一段時間的學(xué)習(xí)中,不僅增加了對于android系統(tǒng)的知識,更重要的是,通過自學(xué)提高了自身能力。在對一項新事物,從陌生到了解再到掌握,這些都是之前從未經(jīng)歷過的挑戰(zhàn)。參考文獻(xiàn) Google Android SDK開發(fā)范例大全 Google Android開發(fā)入門與實戰(zhàn) 深入淺出分析Android的核心框架運行機制