老程序員教你一天時間完成C語言掃雷遊戲

掃雷遊戲

在這裡插入圖片描述

今天我們就要設計一款這樣的遊戲,首先需要設置棋盤,然後對棋盤進行隨機佈雷,最後在排雷。

1.菜單

首先制作一個簡易菜單

int main() {
	int input;
	srand((unsigned int)time(NULL));
	do {
		menu();
		printf("請選擇:\n");
		scanf("%d", &input);
		switch (input) {
		case 1:
			game();
			break;
		case 0:
			printf("退出遊戲\n");
			break;
		default:
			printf("選擇錯誤\n");
			break;
		}

	} while(input);
	return 0;
}

效果如下:
在這裡插入圖片描述

2.初始化棋盤

首先需要設置兩個二維數組,分別存放雷的信息和排查出雷的信息

	char mine[ROWS][COLS] = { 0 };//存放雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查出雷的信息

然後對其進行初始化

//初始化棋盤

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;
		}
	}
}

3.打印棋盤

棋盤的打印可以直接遍歷二維數組,並加上一些分割線

void PrintBoard(char board[ROWS][COLS], int row, int col) {
	printf("--------掃雷--------\n");
	for (int i = 0;i <= 9;i++) {
		printf("%d ", i);
	}
	printf("\n");

	for (int i = 1;i < row+1 ;i++) {
		printf("%d ", i);
		for (int j = 1;j < col+1;j++) {
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("--------------------\n");

}

打印出來的棋盤如下

在這裡插入圖片描述

4.佈置雷

棋盤打印出來瞭,接下來就是在棋盤上隨機佈雷

void SetMine(char board[ROWS][COLS], int row, int col) {
	int count = EASY_COUNT;
	while (count) {
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] != '1') {
			board[x][y] = '1';
			count--;
		}	
	}
}

5.排查雷

排查雷采用玩傢輸入坐標,對比觀察該坐標是否有雷並給出提示,提示顯示該坐標周圍有幾個雷或者是否被炸死

//排查雷
int GetMineCount(char board[ROWS][COLS],int x,int y) {
	int count=0;
	for (int i = x - 1;i <= x + 1;i++) {
		for (int j = y - 1;j <= y + 1;j++) {
			count += (board[i][j]-'0');
		}
	}
	return count;
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x, y,win=0;
	while (win<row*col-EASY_COUNT) {
		printf("請輸入坐標\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			if (mine[x][y] != '1') {
				int count = GetMineCount(mine,x,y);
				show[x][y] = count + '0';
				PrintBoard(show, ROW, COL);
				win++;
			}
			else {
				printf("很遺憾,你被炸死瞭\n");
				PrintBoard(mine, ROW, COL);
				break;
			}
		}
		else {
			printf("坐標非法,請重新輸入\n");
		}
	}
	if (win == row * col - EASY_COUNT) {
		printf("恭喜你,排雷成功\n");
		PrintBoard(mine, ROW, COL);
	}
		
}

6.整體效果展示

在這裡插入圖片描述

7.代碼分享

7.1 test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu() {
	printf("*********************************\n");
	printf("*********    1. play   **********\n");
	printf("*********    0. exit   **********\n");
	printf("*********************************\n");
}
void game() {
	char mine[ROWS][COLS] = { 0 };//存放雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查出雷的信息
	//初始化棋盤
	InitBoard(mine, ROWS, COLS,'0');
	InitBoard(show, ROWS, COLS,'*');
	//打印棋盤
	//PrintBoard(mine, ROW, COL);
	PrintBoard(show, ROW, COL);

	//佈置雷
	SetMine(mine, ROW, COL);
	//PrintBoard(mine, ROW, COL);

	//排查雷
	FindMine(mine, show, ROW, COL);

}
int main() {
	int input;
	srand((unsigned int)time(NULL));
	do {
		menu();
		printf("請選擇:\n");
		scanf("%d", &input);
		switch (input) {
		case 1:
			game();
			break;
		case 0:
			printf("退出遊戲\n");
			break;
		default:
			printf("選擇錯誤\n");
			break;
		}

	} while(input);
	return 0;
}

7.2 game.c

#define _CRT_SECURE_NO_WARNINGS 1
#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 PrintBoard(char board[ROWS][COLS], int row, int col) {
	printf("--------掃雷--------\n");
	for (int i = 0;i <= 9;i++) {
		printf("%d ", i);
	}
	printf("\n");

	for (int i = 1;i < row+1 ;i++) {
		printf("%d ", i);
		for (int j = 1;j < col+1;j++) {
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("--------------------\n");

}

//設置雷

void SetMine(char board[ROWS][COLS], int row, int col) {
	int count = EASY_COUNT;
	while (count) {
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] != '1') {
			board[x][y] = '1';
			count--;
		}	
	}
}

//排查雷
int GetMineCount(char board[ROWS][COLS],int x,int y) {
	int count=0;
	for (int i = x - 1;i <= x + 1;i++) {
		for (int j = y - 1;j <= y + 1;j++) {
			count += (board[i][j]-'0');
		}
	}
	return count;
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x, y,win=0;
	while (win<row*col-EASY_COUNT) {
		printf("請輸入坐標\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			if (mine[x][y] != '1') {
				int count = GetMineCount(mine,x,y);
				show[x][y] = count + '0';
				PrintBoard(show, ROW, COL);
				win++;
			}
			else {
				printf("很遺憾,你被炸死瞭\n");
				PrintBoard(mine, ROW, COL);
				break;
			}
		}
		else {
			printf("坐標非法,請重新輸入\n");
		}
	}
	if (win == row * col - EASY_COUNT) {
		printf("恭喜你,排雷成功\n");
		PrintBoard(mine, ROW, COL);
	}
		
}

7.3 game.h

#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.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 PrintBoard(char board[ROWS][COLS], int row, int col);
//設置雷
void SetMine(char board[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col);

總結

本篇文章就到這裡瞭,希望能給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!

推薦閱讀: