基於C語言實現的掃雷遊戲
阿新 • • 發佈:2018-11-14
函式實現的功能:
1.佈置雷:用rand函式生成隨機座標再雷陣裡隨機佈雷
2.排雷:踩到雷炸死,不是雷,統計周圍一圈有⼏個雷,並記錄資訊放到另一個數組
實現原理:
1.雷的資訊儲存到一個二維陣列mine裡
2.排出來的雷存到另一個二維陣列show裡
實現⼀個 9*9 的雷陣,要建立⼀個 11*11的陣列 ,目的是⽅便統計最外邊一週
的雷的個數。
程式碼:
測試部分test.c
遊戲標頭檔案game.h#include<stdio.h> #include<windows.h> #include "game.h" void menu() { printf("###########################\n"); printf("#####歡迎來到掃雷遊戲######\n"); printf("#### 1.play 0.exit ######\n"); printf("###########################\n"); } void game() { char mine[ROWS][COLS] = { 0 };//雷的資訊 char show[ROWS][COLS] = { 0 };//排查出的雷的資訊 Init(mine, ROWS, COLS,'0' ); Init(show, ROWS, COLS, '*');//初始化 //Print(mine, ROW, COL); Print(show, ROW, COL);//列印 //佈置雷 Setmine(mine, ROW, COL); Print(mine, ROW, COL); //排雷 Sweepmine(mine, show, ROW, COL); } void test() { srand((unsigned)time(NULL)); menu(); int input = 0; do { printf("請選擇:>"); scanf_s("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出遊戲\n"); break; default: printf("輸入有誤\n"); break; } } while (input); } int main() { test(); system("pause"); return 0; }
遊戲實現部分game.c#ifndef __GAME_H__ #define __GAME_H__ #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_Count 10 //#define MIDDLE_Count 30 //#define DIFFICULT_Count 60 void Init(char board[ROWS][COLS], int rows, int cols, char set); void Print(char board[ROWS][COLS], int row, int col); void Setmine(char board[ROWS][COLS], int row, int col); void Sweepmine(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col); int GetmineCount(char mine[ROWS][COLS], int x, int y); #endif
#include"game.h" void Init(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = set; } } } void Print(char board[ROWS][COLS], int row, int col)//不列印外圈,從1開始 { int i = 0; int j = 0; printf(" "); for (i = 1; 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; int x = 0; int y = 0; while (count) { x = rand() % row + 1; y = rand() % row + 1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } int GetmineCount(char mine[ROWS][COLS], int x, int y)//獲得周圍一圈雷的個數 { return 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] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0'; }
void Sweepmine(char board[ROWS][COLS], char show[ROWS][COLS],int row, int col)
{
int x = 0;
int y = 0;
int ret = 0;
while (ret != ROW*COL - EASY_Count)
{
printf("請輸入排查的座標:>");
scanf_s("%d%d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= row)
{
if (board[x][y] == '0')
{
int count = GetmineCount(board, x, y);
show[x][y] = count + '0';
ret++;
Print(show, ROW, COL);
}
else
{
break;
}
}
else
printf("輸入錯誤");
}
if (ret != ROW*COL - EASY_Count)
{
printf("很遺憾,你被炸死了\n");
}
else
{
printf("恭喜你掃雷成功\n");
}
}
}
效果展示:
遊戲部分做的不太完整,只是初步的一個實現,希望多多指點!