小游戏——扫雷




实现一个扫雷游戏
1.设置两个数组:mine[ROW][COL]表示布雷,show[ROW][COL]显示扫雷情况(显示周围有几个雷);
因为统计四周,边缘位置不好实现,所以把二维数组的行和列都加二,这样无论是否在边缘都可以当做一种情况来实现。
2.初始化mine和show,show->*,mine->0;
3.setmine布雷,电脑随机设置20个雷;
4.显示游戏面板,在游戏面板上输入坐标;
5.如果有雷,表示输了,游戏结束;无雷继续游戏,这时要统计周围雷的个数,如果有0个雷,就把周围区域置成‘’,直到赢的个数等于ROW*COL-雷的个数,那么就赢了。

//1.game.h
#ifndef _GAME_H_
#define _GAME_H_

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<string.h>//memset的头文件
#pragma warning(disable:4996)

#define ROW 10
#define COL 10
#define MINE_NUM 20


void menu();//菜单
void game(); //玩游戏
void setMine(char mine[ROW + 2][COL + 2], int row, int col);//布雷
int getMine(char mine[ROW + 2][COL + 2], int row, int col, int  x, int y);//统计周围雷的个数
void display(char board[ROW + 2][COL + 2], int row, int col);//显示游戏面板(mine和show)





#endif

//2.main.c
#include"game.h"
void menu()
{
	printf("************************\n");
	printf("***********1.play********\n");
	printf("*************2.exit******\n");
	printf("************************\n");
}
int main()
{
	int select = 0;
	do
	{
		menu();
		printf("please select:\n");
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			game();
			break;
		case 2:
			exit(0);
	   default:
		   printf("error,please Enter Again NO<1/2>\n");
		   break;
		}
		} while (1);//跳出循环;
		system("pause");
		return 0;
}

//3.game,c
#include"game.h"


生成1-10的随机数:
//static int getRandomNum(int start, int end)
//{
//	return(rand() % (end - start + 1) + start);
//}
//布雷:
void setMine(char mine[ROW + 2][COL + 2], int row, int col)
{
	int num = MINE_NUM;
	srand((unsigned long)time(NULL));
	do{
		//int x = getRandomNum(1, ROW);//生成1-10的随机数
		//int y = getRandomNum(1, COL);//生成1-10的随机数
		int x = rand() % 10;
		int y = rand() % 10;
		if (mine[x][y] == '0')//有雷
		{
			mine[x][y] = '1';
			num--;
		}
	} while (num);
}
//统计该位置周围雷的个数:
int getMine(char mine[ROW + 2][COL + 2], int row, int col, int  x, int y)
{
	return ((mine[x - 1][y - 1] - '0')
		+ (mine[x - 1][y] - '0')
		+ (mine[x - 1][y + 1] - '0')
		+ (mine[x][y - 1] - '0')
		+ (mine[x][y + 1] - '0')
		+ (mine[x + 1][y - 1] - '0')
		+ (mine[x + 1][y] - '0')
		+ (mine[x + 1][y + 1] - '0'));
}



//显示mine和show
void display(char board[ROW + 2][COL + 2], int row, int col)
{
	int i = 1;
	int j = 1;
	printf("   ");
	for (i = 1; i<=COL; i++)//输出每一列
	{
		printf("%3d", i);//这里输出1-10;
	}
	printf("\n");
	for (i = 0; i <= COL; i++){
		printf("---");//输出---;
	}
	printf("\n");

	for (i = 1; i <=ROW; i++)//输出行
	{
		printf("%2d|", i);
		for (j = 1; j <=COL; j++)
		{
			printf("%2c|", board[i][j]);//这里输出*
		}
		printf("\n");

	}

}



void game()
{
	int win = 0;//已排雷个数
	char mine[ROW + 2][COL + 2];//布雷
	char show[ROW + 2][COL + 2];//扫雷
	memset(show, '*', (ROW + 2)*(COL + 2));
	memset(mine, '0', (ROW + 2)*(COL + 2));//初始化棋盘;
	setMine(mine, ROW + 2, COL + 2);//布雷
	do{
		system("CLS");//清屏
		int x, y;
		display(show, ROW + 2, COL + 2);//加2便于统计边缘雷的个数;
		printf("please input<x,y>:");
		scanf("%d%d", &x, &y);
		if ((x >= 1 && x <= ROW) && (y >= 1 && y <= COL))
		{
			display(show, ROW + 2, COL + 2);//显示雷
			if (mine[x][y] == '1')//有雷
			{
				printf("regretly,you lost!\n");
				display(mine, ROW + 2, COL + 2);//显示雷
				break;
			}
			else//无雷,显示雷周围的个数
			{
				int count = getMine(mine, ROW + 2, COL + 2, x, y);
				show[x][y] = count + '0';
				win++;
				if (count==0)
				{
					show[x - 1][y - 1]
						= show[x - 1][y]
						= show[x - 1][y + 1]
						= show[x][y + 1]
						= show[x + 1][y + 1]
						= show[x + 1][y]
						= show[x + 1][y - 1]
						= show[x][y - 1] = ' ';
				}
			
				 if(win == ROW*COL - 20)//赢的次数为80次就赢了。
				{
					printf("congratulations,you won!");
					display(mine, ROW + 2, COL + 2);
					break;
				}
			}
		}
		else
		{
			printf("Enter error,please try again\n");
		}
	} while (1);
}

打印结果如下:





以下是一个简单的扫雷小游戏的 JAVA 代码,实现了基本的游戏功能: ``` import java.util.Random; import java.util.Scanner; public class Minesweeper { private static final int ROWS = 10; private static final int COLS = 10; private static final int BOMBS = 10; private static final char COVERED = '-'; private static final char UNCOVERED = ' '; private static final char BOMB = '*'; private char[][] board; private boolean[][] bombPositions; public Minesweeper() { board = new char[ROWS][COLS]; bombPositions = new boolean[ROWS][COLS]; initBoard(); initBombs(); } private void initBoard() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i][j] = COVERED; } } } private void initBombs() { Random random = new Random(); int count = 0; while (count < BOMBS) { int row = random.nextInt(ROWS); int col = random.nextInt(COLS); if (!bombPositions[row][col]) { bombPositions[row][col] = true; count++; } } } private int countAdjacentBombs(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 < ROWS && j >= 0 && j < COLS && bombPositions[i][j]) { count++; } } } return count; } public boolean playMove(int row, int col) { if (bombPositions[row][col]) { board[row][col] = BOMB; return false; } int adjacentBombs = countAdjacentBombs(row, col); board[row][col] = (char) ('0' + adjacentBombs); if (adjacentBombs == 0) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < ROWS && j >= 0 && j < COLS && board[i][j] == COVERED) { playMove(i, j); } } } } return true; } public void playGame() { Scanner scanner = new Scanner(System.in); boolean gameOver = false; while (!gameOver) { printBoard(); System.out.print("Enter row and column (e.g. 1 2): "); int row = scanner.nextInt() - 1; int col = scanner.nextInt() - 1; if (row < 0 || row >= ROWS || col < 0 || col >= COLS) { System.out.println("Invalid row or column!"); continue; } gameOver = !playMove(row, col); if (checkWin()) { System.out.println("You win!"); gameOver = true; } } printBoard(); } private boolean checkWin() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (board[i][j] == COVERED && !bombPositions[i][j]) { return false; } } } return true; } private void printBoard() { System.out.print(" "); for (int j = 0; j < COLS; j++) { System.out.print((j + 1) + " "); } System.out.println(); for (int i = 0; i < ROWS; i++) { System.out.print((i + 1) + " "); for (int j = 0; j < COLS; j++) { System.out.print(board[i][j] == UNCOVERED && bombPositions[i][j] ? BOMB : board[i][j]); System.out.print(" "); } System.out.println(); } } public static void main(String[] args) { Minesweeper minesweeper = new Minesweeper(); minesweeper.playGame(); } } ``` 代码中定义了游戏的行数,列数和雷数,以及用来表示板块状态的字符常量。在 `Minesweeper` 类中,通过二维数组 `board` 和 `bombPositions` 来表示游戏面板和雷的位置。通过 `initBoard` 和 `initBombs` 方法初始化游戏面板和雷的位置。 在 `countAdjacentBombs` 方法中,根据当前位置的周围八个位置计算雷的个数。 在 `playMove` 方法中,根据当前位置是否有雷计算该位置的状态,并根据周围是否有雷递归更新周围的位置状态。 在 `playGame` 方法中,通过 `Scanner` 类读取玩家的输入,并根据输入更新游戏状态,直到游戏结束。 在 `checkWin` 方法中,检查游戏是否胜利。 在 `printBoard` 方法中,输出游戏面板。 在 `main` 方法中,创建 `Minesweeper` 对象并调用 `playGame` 方法开始游戏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值