控制台版扫雷游戏

控制台版扫雷游戏

1、二维数组实现扫雷界面
2、格子类实现数据操作

package game.bean;
/**
* 格子实体类
* @author ThinkPad
*/
public class Grid {
  //属性
private   char   content;
private   boolean  type;
//封装
public char getContent() {
	return content;
}
public void setContent(char content) {
	this.content = content;
}
public boolean isType() {
	return type;
}
public void setType(boolean type) {
	this.type = type;
}
}

package game.core;
import java.util.Random;
import game.bean.Grid;

/**
 * 与游戏核心类
 * @author ThinkPad
 *包含游戏核心功能:雷区制定,布雷实现,踩雷事件,显示雷区
 */
public class Core {
//定义一个存放格子对象的二维数组
Grid[][]   grid=new  Grid[9][9];
//创建雷区方法
public  void  creat() {
for(int i=0;i<grid.length;i++) {
	for(int j=0;j<grid[i].length;j++) {
		grid[i][j]=new  Grid();
		grid[i][j].setContent(' ');
		grid[i][j].setType(false);
	}
}
}
//显示界面方法
public   void  show() {
	System.out.println("xy 0 1 2 3 4 5 6 7 8 ");
	for(int i=0;i<grid.length;i++) {
		System.out.print(i+" ");
		for(int j=0;j<grid[i].length;j++) {
			if(grid[i][j].isType()) {
				System.out.print(grid[i][j].getContent()+" ");
			}else {
			System.out.print("[]");
			}
		}
		System.out.println();
	}
}

//随机布雷方法
int cent=10;
public  void  setMine() {
	
	do{
	Random   ran=new   Random();
	int x=ran.nextInt(9);
	int y=ran.nextInt(9);
	if(grid[x][y].getContent()!='*') {
		grid[x][y].setContent('*');
		cent--;
	}
	}while(cent>0);
}

//产生数字
public  void  produceDigital() {
	for(int i=0;i<grid.length;i++) {
		for(int j=0;j<grid[i].length;j++) {
			int z=48;
			if(grid[i][j].getContent()!='*') {
				//int  x=i-1;
				//int  y=j-1;
				for(int x=i-1;x<i+2;x++) {
						for(int y=j-1;y<j+2;y++) {
							if(x>=0&&x<9&&y>=0&&y<9&&grid[x][y].getContent()=='*') {
								z++;
							}
						}
				}
				if(z==48) {
					grid[i][j].setContent(' ');
				}else {
					grid[i][j].setContent((char)z);
				}
				}
		}
	}
}

//打开盒子
public  boolean  click(int x,int y) {
	if(grid[x][y].isType()) {
		//盒子类已经打开
		return true;
	}else {
		if(grid[x][y].getContent()=='*') {
			//是雷直接显示
			show();
			return false;
		}else {
			if(grid[x][y].getContent()==' ') {
				//是空格
				grid[x][y].setType(true);
					clickBlank(x,y);
					return true;
			}else {
				//是数字
				grid[x][y].setType(true);
				return true;
			}
		}		
	}
}
//判断空白的格子
public void  clickBlank(int x,int y) {
	if(x-1>-1&&x-1<9&&grid[x-1][y].getContent()!='*') {
		click(x-1,y);
	}
	if(x+11>-1&&x+1<9&&grid[x+1][y].getContent()!='*') {
		click(x+1,y);
	}
	if(y-1>-1&&y-1<9&&grid[x][y-1].getContent()!='*') {
		click(x,y-1);
	}
	if(y+1>-1&&y+1<9&&grid[x][y+1].getContent()!='*') {
		click(x,y+1);
	}
}
//计算没开的盒子
public  int  detection() {
	int a=0;
	for(int i=0;i<grid.length;i++) {
		for(int j=0;j<grid[i].length;j++) {
			if(grid[i][j].isType()==false) {
				a++;
			}
		}

	}
	return a;
}
//点到雷后,显示所有的格子
public void   showAll() {
	for(int i=0;i<grid.length;i++) {
		for(int j=0;j<grid[i].length;j++) {
			grid[i][j].setType(true);
		}
	}
}


}

package game.test;

import java.util.Scanner;

import game.core.Core;

/**
 * 测试类
 * @author ThinkPad
 *
 */
public class test {

public static void main(String[] args) {
	Core  core=new  Core();
	core.creat();
	core.setMine();
	core.produceDigital();
	core.show();
	
	
	Scanner   scan=new  Scanner(System.in);
    int  x;
    int  y;
   while(true) {
    System.out.println("请输入X的值:");
	x=scan.nextInt();
	System.out.println("请输入Y的值:");
	y=scan.nextInt();
	boolean boo=core.click(x, y);
	if(!boo) {
		core.showAll();
		core.show();
		System.out.println("游戏失败!!!");
		break;
	}
	if(core.detection()==10) {
		core.showAll();
		core.show();
		System.out.println("游戏成功");
		break;
	}
	core.show();
   }
	
}
}
下面是一个简单的控制台扫雷游戏的C语言代码示例,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdbool.h> #define ROWS 10 // 行数 #define COLS 10 // 列数 #define MINE_NUM 10 // 雷数 int minefield[ROWS][COLS]; // 雷区 bool visible[ROWS][COLS]; // 可视区 // 初始化雷区 void initMinefield() { int i, j, count; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { minefield[i][j] = 0; visible[i][j] = false; } } // 随机生成雷 count = 0; while (count < MINE_NUM) { i = rand() % ROWS; j = rand() % COLS; if (minefield[i][j] == 0) { minefield[i][j] = -1; count++; } } // 计算每个格子周围的雷数 for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { if (minefield[i][j] == -1) { continue; } if (i > 0 && j > 0 && minefield[i-1][j-1] == -1) { minefield[i][j]++; } if (i > 0 && minefield[i-1][j] == -1) { minefield[i][j]++; } if (i > 0 && j < COLS-1 && minefield[i-1][j+1] == -1) { minefield[i][j]++; } if (j > 0 && minefield[i][j-1] == -1) { minefield[i][j]++; } if (j < COLS-1 && minefield[i][j+1] == -1) { minefield[i][j]++; } if (i < ROWS-1 && j > 0 && minefield[i+1][j-1] == -1) { minefield[i][j]++; } if (i < ROWS-1 && minefield[i+1][j] == -1) { minefield[i][j]++; } if (i < ROWS-1 && j < COLS-1 && minefield[i+1][j+1] == -1) { minefield[i][j]++; } } } } // 输出雷区 void printMinefield() { int i, j; printf(" "); for (j = 0; j < COLS; j++) { printf("%d ", j); } printf("\n"); printf(" "); for (j = 0; j < COLS; j++) { printf("- "); } printf("\n"); for (i = 0; i < ROWS; i++) { printf("%d|", i); for (j = 0; j < COLS; j++) { if (visible[i][j]) { if (minefield[i][j] == -1) { printf("* "); } else { printf("%d ", minefield[i][j]); } } else { printf(". "); } } printf("\n"); } } // 检查是否赢了 bool checkWin() { int i, j, count = 0; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { if (minefield[i][j] == -1 && visible[i][j]) { return false; } if (minefield[i][j] != -1 && visible[i][j]) { count++; } } } return count == ROWS * COLS - MINE_NUM; } // 处理输入的命令 void handleCommand(char cmd, int row, int col) { if (row < 0 || row >= ROWS || col < 0 || col >= COLS) { printf("Invalid position.\n"); return; } if (cmd == 'o') { // 打开格子 if (visible[row][col]) { printf("This position has already been opened.\n"); return; } visible[row][col] = true; if (minefield[row][col] == -1) { printf("Game over!\n"); printMinefield(); exit(0); } if (minefield[row][col] == 0) { int i, j; for (i = row-1; i <= row+1; i++) { for (j = col-1; j <= col+1; j++) { if (i >= 0 && i < ROWS && j >= 0 && j < COLS && minefield[i][j] != -1) { visible[i][j] = true; } } } } if (checkWin()) { printf("You win!\n"); printMinefield(); exit(0); } } else if (cmd == 'f') { // 标记格子为雷 visible[row][col] = true; } else { printf("Invalid command.\n"); } } int main() { srand(time(NULL)); initMinefield(); printf("Welcome to Minesweeper!\n"); printf("You can enter commands like 'o 3 4' to open the cell at row 3 and column 4.\n"); printf("You can also mark a cell as containing a mine by using the 'f' command.\n"); printf("Good luck!\n"); while (true) { printMinefield(); char cmd; int row, col; printf("Enter command: "); scanf("%c %d %d", &cmd, &row, &col); while (getchar() != '\n'); // 清空输入缓冲区 handleCommand(cmd, row, col); } return 0; } ``` 该代码实现了扫雷游戏的基本功能,包括随机生成雷区、计算每个格子周围的雷数、打开格子、标记格子为雷、检查是否赢了等。在控制台中运行该程序,便可以体验简单的扫雷游戏了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值