随机布置地雷java_java 生成0-80之间10个不同的随机数

java 生成0-80之间10个不同的随机数

來源:互聯網  2012-01-28 01:57:19  評論

分類: 電腦/網絡 >> 程序設計 >> 其他編程語言

問題描述:

写一个扫雷的程序,但是布置地雷遇到了麻烦啊...

要求9*9个方格布置随机10个地雷~~

尽量减少运算量...

第一次自己动手写JAVA程序就遇到了挫折..郁闷~~

參考答案:

那就加个判断吧

public class Test1

{

public static void main(String[] args) {

int[] i=new int[10];

int j=1;

i[0]=(int)(Math.random()*81);

while(j

i[j]=(int)(Math.random()*81);

boolean b=true;

for(int k=0;k

if(i[j]==i[k]){

b=false;

}

}

if(b==true)

j++;

}

for(int x=0;x

System.out.println(i[x]);

}

}

这样可以了

[b]分类:[/b] 电脑/网络 >> 程序设计 >> 其他编程语言[br][b]问题描述:[/b][br]写一个扫雷的程序,但是布置地雷遇到了麻烦啊...

要求9*9个方格布置随机10个地雷~~

尽量减少运算量...

第一次自己动手写JAVA程序就遇到了挫折..郁闷~~[br][b]参考答案:[/b][br]那就加个判断吧

public class Test1

{

public static void main(String[] args) {

int[] i=new int[10];

int j=1;

i[0]=(int)(Math.random()*81);

while(j

i[j]=(int)(Math.random()*81);

boolean b=true;

for(int k=0;k

if(i[j]==i[k]){

b=false;

}

}

if(b==true)

j++;

}

for(int x=0;x

System.out.println(i[x]);

}

}

这样可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Java 实现扫雷游戏的代码示例: ```java import java.util.Random; import java.util.Scanner; public class Minesweeper { private static final int BOARD_SIZE = 10; private static final int NUM_MINES = 10; private static final char COVERED_CELL = '*'; private static final char MINE_CELL = 'X'; private static final char EMPTY_CELL = '-'; private static final char FLAGGED_CELL = 'F'; private char[][] gameBoard; private boolean[][] mineLocations; private int numUncoveredCells; public Minesweeper() { gameBoard = new char[BOARD_SIZE][BOARD_SIZE]; mineLocations = new boolean[BOARD_SIZE][BOARD_SIZE]; numUncoveredCells = 0; initializeBoard(); placeMines(); } public void playGame() { System.out.println("Welcome to Minesweeper!"); Scanner scanner = new Scanner(System.in); while (true) { printBoard(); System.out.println("Enter a row and column to uncover (e.g. 3 4), or enter -1 to flag/unflag a cell:"); int row = scanner.nextInt() - 1; int col = scanner.nextInt() - 1; if (row == -2 && col == -2) { System.out.println("Quitting game..."); break; } if (row == -1 && col == -1) { System.out.println("Enter a row and column to flag:"); row = scanner.nextInt() - 1; col = scanner.nextInt() - 1; flagCell(row, col); } else { if (uncoverCell(row, col)) { System.out.println("You lose!"); printBoard(); break; } else if (numUncoveredCells == BOARD_SIZE * BOARD_SIZE - NUM_MINES) { System.out.println("You win!"); printBoard(); break; } } } } private void initializeBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { gameBoard[i][j] = COVERED_CELL; } } } private void placeMines() { Random rand = new Random(); int minesPlaced = 0; while (minesPlaced < NUM_MINES) { int row = rand.nextInt(BOARD_SIZE); int col = rand.nextInt(BOARD_SIZE); if (!mineLocations[row][col]) { mineLocations[row][col] = true; minesPlaced++; } } } private void printBoard() { System.out.print(" "); for (int i = 1; i <= BOARD_SIZE; i++) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print((i + 1) + " "); for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(gameBoard[i][j] + " "); } System.out.println(); } } private boolean uncoverCell(int row, int col) { if (mineLocations[row][col]) { gameBoard[row][col] = MINE_CELL; return true; } else { int numAdjacentMines = countAdjacentMines(row, col); gameBoard[row][col] = Character.forDigit(numAdjacentMines, 10); numUncoveredCells++; if (numAdjacentMines == 0) { uncoverAdjacentCells(row, col); } return false; } } private int countAdjacentMines(int row, int col) { int count = 0; for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && mineLocations[i][j]) { count++; } } } return count; } private void uncoverAdjacentCells(int row, int col) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && gameBoard[i][j] == COVERED_CELL) { uncoverCell(i, j); } } } } private void flagCell(int row, int col) { if (gameBoard[row][col] == COVERED_CELL) { gameBoard[row][col] = FLAGGED_CELL; } else if (gameBoard[row][col] == FLAGGED_CELL) { gameBoard[row][col] = COVERED_CELL; } } public static void main(String[] args) { Minesweeper game = new Minesweeper(); game.playGame(); } } ``` 这个简单的实现中,我们使用了一个 10x10 的二维字符数组来表示游戏板。我们还有一个相同大小的二维布尔数组,用于跟踪哪些方格上有地雷。在每个游戏回合中,玩家输入要揭开的方格的行和列号,程序将检查这个位置是否有地雷。如果是,游戏结束,否则程序将显示该位置周围的地雷数量,并揭开与该位置相邻的所有空白方格。玩家还可以标记他们认为有地雷的方格。如果玩家揭开了所有不是地雷的方格,游戏结束,玩家获胜。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值