package sll;
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.out;
public class sl{
//埋地雷
private static void mai_lei(char[][] d, int row, int col,int num) {
int i = 0,j = 0,x = 0,y =0;
for (int k = 0; k <num ; k++) {
x=new Random().nextInt(row); //这儿是生成的小于row的整数,nextInt方法的参数值要是大于0的整数
y=new Random().nextInt(col); //这儿是生成的小于col的整数,nextInt方法的参数值要是大于0的整数
if (d[x][y] == '0')
{
d[x][y] = '1';
}
else {
k--;
}
}
}
//打印x
private static void da_yin(char[][] c, int row, int col,int num) {
int i = 0, j = 0;
out.print("\n");
for (i = 0; i < row ; i++) {
for (j = 0; j < col ; j++) {
out.print(c[i][j]);
}
out.print("\n");
}
out.print("\n");
}
//统计剩余雷的个数
private static int suan_lei(char[][] c, int row, int col) {
int i = 0, j = 0, count = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (c[i][j] == '*')
{
count++;
}
}
}
return count;
}
private static int maine_map(char[][] map, int xx, int yy,int A,int B) {
int num=0;
if((xx-1>0)&&(xx+1<A)&&(yy-1>0)&&(yy+1<A)) {
if((xx-1>=0)&&(yy-1>=0)&&(map[xx-1][yy-1]=='1')) num++;
if((xx-1>=0)&&(yy+0>=0)&&(map[xx-1][yy]=='1')) num++;
if((xx-1>=0)&&(yy+1<B) &&(map[xx-1][yy+1]=='1')) num++;
if((xx+0>=0)&&(yy-1>=0)&&(map[xx][yy-1]=='1')) num++;
if((xx+0>=0)&&(yy+1<B) &&(map[xx][yy+1]=='1')) num++;
if((xx+1<A)&&(yy-1>=0) &&(map[xx+1][yy-1]=='1')) num++;
if((xx+1<A)&&(yy+0>=0) &&(map[xx+1][yy]=='1')) num++;
if((xx+1<A)&&(yy+1<B) &&(map[xx+1][yy+1]=='1')) num++;}
return num;
}
public static void main(String[] args) {
while (true){
Scanner in = new Scanner(System.in);
int ROW;
int COL;
int NUM;
System.out.println("**********扫雷游戏********");
System.out.println("请输入地雷区域的高(5-10):");
ROW = in.nextInt();
System.out.println("请输入地雷区域的宽(5-10):");
COL = in.nextInt();
out.println("请输入地雷的数量(少于高X宽):");
NUM= in.nextInt();
out.println("地雷已经埋好了,开始游戏吧");
char a[][]=new char[ROW][COL];
int i = 0;
int j = 0;
int row = ROW+2;
int col = COL+2;
int num = NUM;
int x = 0;
int y = 0;
char [][] xian=new char [row][col];
char[][] ying=new char[row][col];
for (i = 1; i < row-1; i++)
for (j = 1; j < col - 1; j++) //给两个函数初始化赋值
{
xian[i][j] = '*';
ying[i][j] = '0';
}
while (true) {
mai_lei(ying, row, col, num);
//da_yin(ying, row, col); //嗯~为了方便调试,偷看雷的实际位置
da_yin(xian, row, col,num); //玩家看到的界
while (true) {
out.print("请输入你要扫的X坐标:");
x = in.nextInt();
out.println();
out.print("请输入你要扫的Y坐标:");
y = in.nextInt();
if (ying[x][y] == '1') {
out.print("恭喜你!你被炸死了\n");
da_yin(ying, row, col, num);
out.print("是否继续y/n ");
String pan=in.next();
if (pan.equalsIgnoreCase("y")){
break;
}else {
return;
}
} else {
out.println("恭喜你,请继续游戏!");
int num_map=maine_map(ying, row, col,ROW,COL);
xian[x][y] = (char)num_map;
da_yin(xian, row, col,num_map);
}
if ((suan_lei(xian, row, col)) == row - 2) {
out.print("恭喜你,你已获胜\n");
return;
}
da_yin(xian, row, col,num);
}
}
}
}
}
Javaaoleibb
最新推荐文章于 2023-10-30 22:05:24 发布