上次发的有点BUG  这次的好多了
而且可玩性也强很多
//Andy
//最后修改:2008-11-20
//扫雷游戏v1.2
//  p x y 扫雷
//  b x y 插旗(如果在原来插旗的位置上再插旗就会把旗拿走)
import java.util.Scanner;
public class MineSweeping{ public static void main(String[] args){
MineSweeping a = new MineSweeping(30);
//a.showCb("Show everything");
a.startGame();
}
//显示棋盘所有内容(用于测试)
private void showCb(Object obj)
{
for(int i=0;i<cb.length;i++){
for(int j=0;j<cb[i].length;j++)
System.out.print(cb[i][j].value+" ");
System.out.println();
}
}
//构造
public MineSweeping(int mines){
initCb();
this.mines=mines;
flagNumbs=mines;
initMines();
initMinesNums();
}
//开始游戏
public void startGame(){
System.out.println("======MineSweeping======");
do{
showCb();
String chess = new Scanner(System.in).nextLine();
move(chess);
}while(!isWin());
showCb();
System.out.println("You Win");
}
//走子
private void move(String info){
char type = info.charAt(0);
int x = info.charAt(2)-'0'+1;
int y = info.charAt(4)-'0'+1;
if(type == 'b')
moveFlag(x,y);
else if(type == 'p')
moveChess(x,y);
}
//插棋
private void moveFlag(int x,int y){
if(cb[x][y].flag == 0 && flagNumbs > 0){
cb[x][y].flag = 1;
--flagNumbs;
}
else if(cb[x][y].flag == 1 && flagNumbs >= 0){
cb[x][y].flag = 0;
++flagNumbs;
}
System.out.println("可用旗数:"+flagNumbs);
}
//翻子
private void moveChess(int x,int y){
if(x >= 1 && x <= cb.length-1 && y >=1 && y<= cb.length-1){
if(cb[x][y].flag == 0 && cb[x][y].value == '0'){
cb[x][y].flag = -1;
//递归清除是0的子
moveChess(x-1,y);
moveChess(x+1,y);
moveChess(x,y-1);
moveChess(x,y+1);
moveChess(x+1,y+1);
moveChess(x-1,y-1);
moveChess(x+1,y-1);
moveChess(x-1,y+1);
}
else if(cb[x][y].flag == 0 && cb[x][y].value > '0' && cb[x][y].value < '9'){
cb[x][y].flag = -1;
//递归终止条件1:该子不为0
return;
}
else if(cb[x][y].flag == 0 && cb[x][y].value == 'M'){
cb[x][y].flag = -1;
gameOver();
} }
//递归终止条件2:到达边界
return;
}
//游戏结束
private void gameOver(){
showCb();
System.out.println("You Lose");
System.exit(0);
}
//判断输赢
private boolean isWin(){
boolean flag = false;
if(flagNumbs == 0){
flag = true;
for(int i=1;i<cb.length;i++)
for(int j=1;j<cb[i].length;j++){
if(cb[i][j].flag == 1 && cb[i][j].value != 'M' || cb[i][j].flag == 0){
flag = false;
}
}
if(flag == true) return true;
}
return false;
}
//显示棋盘
private void showCb(){
for(int i=0;i<cb.length;i++){
for(int j=0;j<cb[i].length;j++)
//flag=-1代表翻开
if(cb[i][j].flag == -1){
//雷数是0就显示空格
if(cb[i][j].value == '0' && i >= 1 && j >= 1)
System.out.print(" "+" ");
else
System.out.print(cb[i][j].value+" ");
}
//flag=0代表没翻开
else if(cb[i][j].flag == 0)
System.out.print("#"+" ");
//flag=1代表插了旗
else if(cb[i][j].flag == 1)
System.out.print("F"+" ");
System.out.println();
}

}
//初始化棋盘
private void initCb(){
cb = new MyChar[11][11];
for(int i=0;i<cb.length;i++)
for(int j=0;j<cb[i].length;j++)
cb[i][j] = new MyChar();
cb[0][0].value = ' ';
cb[0][0].flag = -1;
for(int i=1;i<11;i++){
cb[0][i].value = (char)(48+(i-1));cb[0][i].flag=-1;
cb[i][0].value = (char)(48+(i-1));cb[i][0].flag=-1;
}
}  

//产生地雷
private void initMines(){
int i=0;
if(mines > 100) 
mines = 100;
while(i<mines){
int x = (int)(Math.random()*10+1);
int y = (int)(Math.random()*10+1);
if(cb[x][y].value == 'M')
continue;
cb[x][y].value = 'M';
++i;
}
}
//产生雷数信息
private void initMinesNums(){
for(int i=1;i<cb.length;i++)
for(int j=1;j<cb[i].length;j++){
if(cb[i][j].value != 'M'){
//左
if(i >= 1 && i <= cb.length-1 && j-1 >= 1 && j-1 <= cb[i].length-1)
if(cb[i][j-1].value == 'M') cb[i][j].value += 1;
//右
if(i >= 1 && i <= cb.length-1 && j+1 >= 1 && j+1 <= cb[i].length-1)
if(cb[i][j+1].value == 'M') cb[i][j].value += 1;
//上
if(i-1 >= 1 && i-1 <= cb.length-1 && j >= 1 && j <= cb[i].length-1)
if(cb[i-1][j].value == 'M') cb[i][j].value += 1;
//下
if(i+1 >= 1 && i+1 <= cb.length-1 && j >= 1 && j <= cb[i].length-1)
if(cb[i+1][j].value == 'M') cb[i][j].value += 1;
//左上
if(i-1 >= 1 && i-1 <= cb.length-1 && j-1 >= 1 && j-1 <= cb[i].length-1)
if(cb[i-1][j-1].value == 'M') cb[i][j].value += 1;
//右上
if(i-1 >= 1 && i-1 <= cb.length-1 && j+1 >= 1 && j+1 <= cb[i].length-1)
if(cb[i-1][j+1].value == 'M') cb[i][j].value += 1;
//左下
if(i+1 >= 1 && i+1 <= cb.length-1 && j-1 >= 1 && j-1 <= cb[i].length-1)
if(cb[i+1][j-1].value == 'M') cb[i][j].value += 1;
//右下
if(i+1 >= 1 && i+1 <= cb.length-1 && j+1 >= 1 && j+1 <= cb[i].length-1)
if(cb[i+1][j+1].value == 'M') cb[i][j].value += 1;
}
}
}
//棋盘
private MyChar[][] cb;
//地雷数
private int mines;
//旗数
private int flagNumbs;
}class MyChar{
public char value;
public int flag;
public MyChar(){
value = '0';
//flag=0代表没翻开,-1代表翻开,1代表插了棋
flag = 0;
}
}

解决方案 »

  1.   

    上次发的有点BUG  这次的好多了 
    而且可玩性也强很多
      

  2.   

    我发现楼主喜欢写命令行下的小游戏啊,为什么不写个GUI的,可玩性多好啊?
      

  3.   

    我这边RUN了 好几个楼主的程序 为什么 nextLine();  这个总说有问题
      

  4.   

    我两个程序都跑过啊  都没问题
    而且我也没定义nextLine方法啊?
      

  5.   


    呵呵,楼主的目的不是写游戏,是练习,给楼主出个游戏题目:壁虎
    http://www.comp.nus.edu.sg/~cs1101x/3_ca/labs/lab5/MineSweeper.html
    此链接中的第一个题目是扫雷,也就是楼主刚刚写的那个,第二个题目壁虎,楼主试试看?