C語言的掃雷
文章目錄
- C語言的掃雷
- 掃雷的概述
- 掃雷游戲的頭檔案
- 掃雷的游戲界面
- 初始化雷盤
- 顯示雷盤
- 電腦隨機布置雷
- 排查雷
- 總代碼如下:
- 執行圖如下:
掃雷的概述
在我們許多計算機里面都有掃雷游戲,開始游戲時如果遇到地雷就會游戲結束,相反則會點開這個區域,并提示玩家周圍有雷的個數,如果周圍沒有雷就會展開,但在我這個代碼是掃雷的簡化版,(其實是自己的技識訓不夠)不能展開也不能標記只能寫一些簡單的代碼,在以后我會再寫一篇有關掃雷的完整版,
掃雷游戲的頭檔案
定義一些常量便于后面的代碼的編譯
#pragma once
#include <stdlib.h>
#include <time.h>
#include<stdio.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);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//排查二維陣列mine[x][y]周圍雷的數目回傳雷的個數
int GetMineCount(char mine[ROWS][COLS], int x, int y);
掃雷的游戲界面
首先玩家來到游戲界面輸入相關的數字:1為玩游戲,2為退出游戲,若輸入其他的數字則會判斷為輸入錯誤
void menu()
{
printf("*******************************\n");
printf("******* 1.play ********\n");
printf("******* 0.exit ********\n");
printf("*******************************\n");
}
初始化雷盤
先將陣列初始化,以便后面雷的隨機排放
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
顯示雷盤
列印雷盤,讓雷盤顯示在玩家眼中,同時將列數和行數列印出來以方便玩家選擇坐標
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i,j;
printf("--------------------\n");
for (int i = 0; i <= col; i++)
{
printf("%d ",i);
}
printf("\n");
for (i=1;i<=row;i++)
{
printf("%d ",i);
for (j = 1; j <= col; 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() % row + 1;
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
排查雷
玩家輸入一個坐標,看一個mine陣列在這個坐標上是否是雷如果是就游戲結束
如果不是就檢查周圍雷的個數,再將該坐標周圍雷的個數放置在陣列show里面
int GetMineCount(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 win = 0;
int sum = row * col - EASY_COUNT;
int x=0, y=0;
while (win < sum)
{
printf("請選擇坐標>");
scanf("%d %d",&x,&y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("哈哈哈,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("輸入的坐標錯誤\n");
}
}
if (win == sum)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
總代碼如下:
#pragma once
#include <stdlib.h>
#include <time.h>
#include<stdio.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);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//排查二維陣列mine[x][y]周圍雷的數目回傳雷的個數
int GetMineCount(char mine[ROWS][COLS], int x, int y);
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
int GetMineCount(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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i,j;
printf("--------------------\n");
for (int i = 0; i <= col; i++)
{
printf("%d ",i);
}
printf("\n");
for (i=1;i<=row;i++)
{
printf("%d ",i);
for (j = 1; j <= col; 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() % row + 1;
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;
int sum = row * col - EASY_COUNT;
int x=0, y=0;
while (win < sum)
{
printf("請選擇坐標>");
scanf("%d %d",&x,&y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("哈哈哈,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("輸入的坐標錯誤\n");
}
}
if (win == sum)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
void menu()
{
printf("*******************************\n");
printf("******* 1.play ********\n");
printf("******* 0.exit ********\n");
printf("*******************************\n");
}
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine,ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine( mine,show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("請選擇輸入>");
scanf("%d",&input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("選擇錯誤\n");
break;
}
} while (input!=0);
return 0;
}
執行圖如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/291953.html
標籤:其他
