C語言實作掃雷小遊戲
通過實作掃雷小遊戲來提高自己的程式設計能力,是對數組知識點的鞏固與強化;
首先建立3個檔案寫代碼,以保證條理清晰;1,頭檔案(game.h),2.game.c 和test.c 檔案
1.game.h 檔案主要是預處理資訊和函數所用頭檔案。
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
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);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);
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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{//列印列号
for (int i = 0; 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 count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;//1-9
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
//'0'-'0'=0
//'1'-'0'=1
//assica碼表轉換原理
int get_mine_count(char mine[ROWS][COLS],int x,int y) {
return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1]
+ mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0';
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0, y = 0;
int win = 0;
//9*9-10=71
while (win<row * col - EASY_COUNT) {
printf("請輸入要查雷的坐标:>");
scanf("%d%d",&x,&y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
//坐标合法
//1.踩雷
if (mine[x][y] == '1') {
printf("很遺憾,你被雷炸死了\n");
DisplayBoard(mine,row,col);
break;
}
else {//不是雷
//計算xy坐标周圍有多少雷
int count = get_mine_count(mine,x,y);
show[x][y] = count + '0';
DisplayBoard(show,row,col);
win++;
}
}
else {
printf("輸入非法坐标,請重新輸入!\n");
}
}if (win == row * col - EASY_COUNT) {
printf("恭喜你,排雷成功!\n");
DisplayBoard(mine, row, col);
}
}
test.c主要寫掃雷遊戲的主體架構代碼
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
menu() {
printf("******************************\n");
printf("*******1.PLAY 0.exit*********\n");
printf("******************************\n");
}
void game() {
//1.布置雷的資訊
char mine[ROWS][COLS] = {0};
//2.排出雷的資訊
char show[ROWS][COLS] = {0};
//初始化
InitBoard(mine,ROWS,COLS,'0');
InitBoard(show, ROWS, COLS, '*');
//列印棋盤
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine,ROW,COL);
DisplayBoard(mine, ROW, COL);
//掃雷
FindMine(mine,show,ROW,COL);
}
void test() {
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("請選擇:>");
scanf("%d",&input);
switch (input)
{
case 1:
game();
break;
case 2:
printf("退出遊戲!\n");
break;
default:
printf("輸入錯誤,請重新選擇\n");
break;
}
} while (input);
}
int main() {
test();
return 0;
}
*注意事項
剛開始的時候不能向玩家展示地雷棋盤,而且注意裡面assca碼的轉換,因為一開始裡面存的是字元型,要轉化為int 型
‘0’-‘0’=1
‘3’-‘0’=3
謝謝大家,覺得有用的話就點個贊吧!