扫雷

实现基本的功能:
布置雷雷
排雷雷
踩雷雷,炸死
排雷雷成功判断
第一次踩雷雷不炸死

如果当前坐标不是雷雷,当前坐标的周围也没雷雷,就会向外扩展搜索。

描述:

使⽤用C语⾔言在Windows环境的控制台中模拟实现扫雷游戏

技术特点:

         C语⾔、二维数组、函数、随机数⽣生成、预处理指令

代码如下:

game.h:

#ifndef __GAME_H__
#define __GAME_H__
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<string.h>
#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

#define EASY_COUNT 10


void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);//初始化
void DisplayBoard(char board[ROWS][COLS], int row, int col);//展示
void SetMine(char board[ROWS][COLS], int row, int col);//设置雷
int GetMineCount(char board[ROWS][COLS], int x, int y);//获取雷的数量
char * spread(char board[ROWS][COLS], int x, int y);
#endif//__GAME_H__
game.c:

#include "game.h";
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set){
	for (int i = 0; i < rows;i++){
		for (int j = 0; j < cols;j++){
			board[i][j] = set;
			}		
	}	
}
void DisplayBoard(char board[ROWS][COLS], int row, int col){
	printf(" ");
     for (int i = 1; i <= row; i++){
		printf(" %d", i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++){
		printf("%d", i);
		for (int j = 1; j <=col; j++){
			printf(" %c", board[i][j]);
			}
		printf("\n");

	}

}
void SetMine(char board[ROWS][COLS], int row, int col){
	int x = 0;
	int y = 0;
	int count = 0;
	srand((unsigned int)time(NULL));
	while (count < EASY_COUNT)
	{
		x = rand() % 9 + 1;
		y = rand() % 9 + 1;
		if (board[x][y] != 1 + '0'){
			board[x][y] = 1 + '0';
			count++;
		}
	} 
	
}
int GetMineCount(char board[ROWS][COLS], int x, int y){
	return (board[x][y - 1] + board[x-1][y-1] + board[x-1][y] + board[x-1][y+1] + board[x][y + 1] + 
		board[x+1][y]+ board[x+1][y - 1] + board[x+1][y+1])-8*'0';
}
char * spread(char board[ROWS][COLS], int x, int y){
	char  arr[4] = { board[x][y - 1], board[x - 1][y - 1], board[x - 1][y], board[x + 1][y - 1] };
	return arr;
}

test.c:

#include "game.h"
void menu()
{
	printf("************************************\n");
	printf("******  1 play   0 exit  ***********\n");
	printf("************************************\n");
}
void play(){
	int ret=0;
	int count1 = 1;
	char board1[ROWS][COLS] = {0};
	char show[ROWS][COLS] = {0};
	InitBoard(board1, ROWS, COLS,'0');
	InitBoard(show, ROWS, COLS, '*');
	SetMine(board1, ROW, COL);
	//DisplayBoard(board1, ROW, COL);
	DisplayBoard(show, ROW, COL);
	while (ret<(ROW*COL - EASY_COUNT))
	{
		
		int x = 0;
		int y = 0;
		printf("请输入坐标\n");
		scanf("%d", &x);
		scanf("%d", &y);
       if (x>=1&&x<=9&&y>=1&&y<=9){
		   if (board1[x][y] == 1 + '0'&&count1 != 1){
			   printf("炸死了");
			   break;
			}
		   else{
			   if (board1[x][y] == 1 + '0'&&count1 == 1){
				   SetMine(board1, ROW, COL);
				   //DisplayBoard(board1, ROW, COL);
				  }
			   int count = GetMineCount(board1, x, y);
			   show[x][y] = count + '0';
			   char * p = spread(board1, x, y);
			   show[x][y - 1] = *p;
			   show[x - 1][y - 1] = *(p + 1);
			   show[x - 1][y] = *(p + 2);
			   show[x + 1][y - 1] = *(p + 3);
			   DisplayBoard(show, ROW, COL);
			   count1++;
		   }
		       
	}
		else{
			printf("输入的坐标不合法,请重新输入\n");
		}

	}
	if (ret==(ROW*COL - EASY_COUNT)){
		printf("哇哦:恭喜你排雷成功!");
	}
}

int main(){
	int input = 0;
	do{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch(input)
		{
		case 1:
		    play();
			break;
		case 0:
			printf("退出游戏");
			break;
		default:
			printf("选择错误,请重新选择");
			break;
		}
	} while(input);
	
	return 0;
}





猜你喜欢

转载自blog.csdn.net/sx2448826571/article/details/78785713