android扫雷程序
来源:互联网 发布:大数据报表展示平台 编辑:程序博客网 时间:2024/04/30 11:08
制作了扫雷程序,点击模拟鼠标左键,长按模拟鼠标右键,即标记。
首先是每个方块部分的代码:
/** * 设置初始化 */public void setDefaults(){isCovered = true;isMined = false;isFlagged = false;isQuestionMarked = false;isClickable = true;numberOfMinesInSurrounding = 0;this.setBackgroundResource(R.drawable.square_blue);setBoldFont();}// mark the block as disabled/opened// update the number of nearby minespublic void setNumberOfSurroundingMines(int number){this.setBackgroundResource(R.drawable.square_grey);updateNumber(number);}// set block as a mine underneathpublic void plantMine(){isMined = true;}// mine was opened// change the block icon and colorpublic void triggerMine(){setMineIcon(true);this.setTextColor(Color.RED);}// set mine icon for block// set block as disabled/opened if false is passedpublic void setMineIcon(boolean enabled){this.setText("M");if (!enabled){this.setBackgroundResource(R.drawable.square_grey);this.setTextColor(Color.RED);}else{this.setTextColor(Color.BLACK);}}// set mine as flagged// set block as disabled/opened if false is passedpublic void setFlagIcon(boolean enabled){this.setText("F");if (!enabled){this.setBackgroundResource(R.drawable.square_grey);this.setTextColor(Color.RED);}else{this.setTextColor(Color.BLACK);}}// set mine as question mark// set block as disabled/opened if false is passedpublic void setQuestionMarkIcon(boolean enabled){this.setText("?");if (!enabled){this.setBackgroundResource(R.drawable.square_grey);this.setTextColor(Color.RED);}else{this.setTextColor(Color.BLACK);}}// set block as disabled/opened if false is passed// else enable/close itpublic void setBlockAsDisabled(boolean enabled){if (!enabled){this.setBackgroundResource(R.drawable.square_grey);}else{this.setBackgroundResource(R.drawable.square_blue);}}// clear all icons/textpublic void clearAllIcons(){this.setText("");}// set font as boldprivate void setBoldFont(){this.setTypeface(null, Typeface.BOLD);}// uncover this blockpublic void OpenBlock(){// cannot uncover a mine which is not coveredif (!isCovered)return;setBlockAsDisabled(false);isCovered = false;// check if it has mineif (hasMine()){setMineIcon(false);}// update with the nearby mine countelse{setNumberOfSurroundingMines(numberOfMinesInSurrounding);}}// set text as nearby mine countpublic void updateNumber(int text){if (text != 0){this.setText(Integer.toString(text));// select different color for each number// we have already skipped 0 mine countswitch (text){case 1:this.setTextColor(Color.BLUE);break;case 2:this.setTextColor(Color.rgb(0, 100, 0));break;case 3:this.setTextColor(Color.RED);break;case 4:this.setTextColor(Color.rgb(85, 26, 139));break;case 5:this.setTextColor(Color.rgb(139, 28, 98));break;case 6:this.setTextColor(Color.rgb(238, 173, 14));break;case 7:this.setTextColor(Color.rgb(47, 79, 79));break;case 8:this.setTextColor(Color.rgb(71, 71, 71));break;case 9: this.setTextColor(Color.rgb(205, 205, 0));break;}}}
接着是主程序的代码:
这里着重介绍2个方法:
private void createMineField(){//边缘的都是没用的blocks = new Block[numberOfRowsInMineField + 2][numberOfColumnsInMineField + 2];for (int row = 0; row < numberOfRowsInMineField + 2; row++){for (int column = 0; column < numberOfColumnsInMineField + 2; column++){blocks[row][column] = new Block(this);blocks[row][column].setDefaults();// pass current row and column number as final int's to event listeners// this way we can ensure that each event listener is associated to // particular instance of block onlyfinal int currentRow = row;final int currentColumn = column;blocks[row][column].setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {//说明第一次点击,所以初始化计时器if (!isTimerStarted){startTimer();isTimerStarted = true;}//第一次点击之后才初始化,保证第一次不会立即爆炸if (!areMinesSet){areMinesSet = true;setMine(currentRow, currentColumn);}if (!blocks[currentRow][currentColumn].isFlagged()){rippleUncover(currentRow, currentColumn);if (blocks[currentRow][currentColumn].hasMine()){finishGame(currentRow,currentColumn);}if (checkGameWin()){// mark game as winwinGame();}}}});blocks[currentRow][currentColumn].setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {if (!blocks[currentRow][currentColumn].isCovered() && (blocks[currentRow][currentColumn].getNumberOfMinesInSorrounding() > 0) && !isGameOver){int nearbyFlaggedBlocks = 0;for (int previousRow = -1; previousRow < 2; previousRow++){for (int previousColumn = -1; previousColumn < 2; previousColumn++){if (blocks[currentRow + previousRow][currentColumn + previousColumn].isFlagged()){nearbyFlaggedBlocks++;}}}// if flagged block count is equal to nearby mine count// then open nearby blocksif (nearbyFlaggedBlocks == blocks[currentRow][currentColumn].getNumberOfMinesInSorrounding()){for (int previousRow = -1; previousRow < 2; previousRow++){for (int previousColumn = -1; previousColumn < 2; previousColumn++){// don't open flagged blocksif (!blocks[currentRow + previousRow][currentColumn + previousColumn].isFlagged()){// open blocks till we get numbered blockrippleUncover(currentRow + previousRow, currentColumn + previousColumn);// did we clicked a mineif (blocks[currentRow + previousRow][currentColumn + previousColumn].hasMine()){// oops game overfinishGame(currentRow + previousRow, currentColumn + previousColumn);}// did we win the gameif (checkGameWin()){// mark game as winwinGame();}}}}}return true;}// if clicked block is enabled, clickable or flaggedif (blocks[currentRow][currentColumn].isClickable() && (blocks[currentRow][currentColumn].isEnabled() || blocks[currentRow][currentColumn].isFlagged())){// case 1. set blank block to flaggedif (!blocks[currentRow][currentColumn].isFlagged() && !blocks[currentRow][currentColumn].isQuestionMarked()){blocks[currentRow][currentColumn].setBlockAsDisabled(false);blocks[currentRow][currentColumn].setFlagIcon(true);blocks[currentRow][currentColumn].setFlagged(true);minesToFind--; //reduce mine countupdateMineCountDisplay();}// case 2. set flagged to question markelse if (!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 countupdateMineCountDisplay();}// case 3. change to blank squareelse{blocks[currentRow][currentColumn].setBlockAsDisabled(true);blocks[currentRow][currentColumn].clearAllIcons();blocks[currentRow][currentColumn].setQuestionMarked(false);// if it is flagged then increment mine countif (blocks[currentRow][currentColumn].isFlagged()){minesToFind++; // increase mine countupdateMineCountDisplay();}// remove flagged statusblocks[currentRow][currentColumn].setFlagged(false);}updateMineCountDisplay(); // update mine display}return true;}});}}}
private void showMineField(){// remember we will not show 0th and last Row and Columns// they are used for calculation purposes onlyfor (int row = 1; row < numberOfRowsInMineField + 1; row++){TableRow tableRow = new TableRow(this); tableRow.setLayoutParams(new LayoutParams((blockDimension + 2 * blockPadding) * numberOfColumnsInMineField, blockDimension + 2 * blockPadding));for (int column = 1; column < numberOfColumnsInMineField + 1; column++){blocks[row][column].setLayoutParams(new LayoutParams( blockDimension + 2 * blockPadding, blockDimension + 2 * blockPadding)); blocks[row][column].setPadding(blockPadding, blockPadding, blockPadding, blockPadding);tableRow.addView(blocks[row][column]);}mineField.addView(tableRow,new TableLayout.LayoutParams( (blockDimension + 2 * blockPadding) * numberOfColumnsInMineField, blockDimension + 2 * blockPadding)); }}
话不多说,具体下载地址:http://download.csdn.net/detail/shensens/5579871
- android扫雷程序
- 扫雷程序
- 扫雷程序思想讲解
- 自动扫雷程序
- 自动扫雷程序
- 扫雷程序实现
- 我的扫雷程序
- 扫雷程序思想
- VC 扫雷程序
- C语言扫雷程序
- 扫雷小程序
- 扫雷的java程序
- 控制台版扫雷程序
- 一个扫雷程序
- 扫雷辅助程序
- windows程序扫雷程序设计
- 扫雷外挂程序开发
- c语言扫雷程序
- Windows From 禁止多次启动的方法
- Linux下SSH使用rsa认证方式省去输入密码
- 微信公众平台 php自动回复时中文乱码
- 微信控 控微信
- 标准I/O限制
- android扫雷程序
- 【MVC分页】页码
- ASCII对照表
- debian,Ubuntu 使用 xrandr 调整分辨率
- SEO需要每天维护网站哪些内容!?
- 【win7常用的15个快捷键组合一定要学会】
- android实现图片缩放 旋转的几种方法
- CURL多线程的严重错误
- mysql 查看表的类型