您好,登錄后才能下訂單哦!
本篇內容主要講解“C語言怎么實現簡單的掃雷功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C語言怎么實現簡單的掃雷功能”吧!
下面看代碼:
test.c
#define _CRT_SECURE_NO_WARNINGS #include"game.h" void test(); void menu(); void game(); int main() { test(); return 0; } void menu() { printf("*************************\n"); printf("****** 1.play ******\n"); printf("****** 0.exit ******\n"); printf("*************************\n"); } void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("請選擇: "); scanf("%d", &input); switch (input) { case 1: printf("掃雷游戲!\n"); game(); break; case 0: printf("已退出游戲!\n"); break; default: printf("輸入錯誤,請重新輸入!\n"); break; } } while (input); } void game() { //雷的信息存儲 //1.布置雷的信息 char mine[ROWS][COLS] = { 0 }; //11*11 //2.排查出的雷的信息 char show[ROWS][COLS] = { 0 }; //初始化 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); //打印棋盤 DisplyBoard(mine, ROW, COL); //DisplyBoard(show, ROW, COL); //布置雷 SetMine(mine, ROW, COL); DisplyBoard(mine, ROW, COL); //掃雷 FindMine(mine, show, ROW, COL); }
game.c
#define _CRT_SECURE_NO_WARNINGS #include"game.h" //'1'-'0'=1 //'3'-'0'=3 int get_mine_count(char mine[ROWS][COLS], int x, int y) { return mine[x - 1][y - 1] - '0' + mine[x][y - 1] - '0' + mine[x + 1][y - 1] - '0' + mine[x + 1][y] - '0' + mine[x + 1][y + 1] - '0' + mine[x][y + 1] - '0' + mine[x - 1][y + 1] - '0' + mine[x - 1][y] - '0'; } void InitBoard(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 DisplyBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; //打印列號 for (i = 0; i <= row; 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"); } } void SetMine(char board[ROWS][COLS], int row, int col) { int count = COUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0; while (win < ROW * COL - COUNT) { printf("請輸入坐標: "); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { //坐標合法 //1.踩雷 if (mine[x][y] == '1') { printf("你被炸死了!!!\n"); DisplyBoard(mine, row, col); break; } //不是雷 else { //計算x,y坐標周圍有幾個雷 int count = get_mine_count(mine, x, y); show[x][y] = count + '0'; DisplyBoard(show, row, col); win++; } } else { printf("坐標非法,請重新輸入!"); } } if (win == ROW * COL - COUNT) { printf("恭喜你,排雷成功!!!\n"); DisplyBoard(mine, row, col); } }
game.h
#define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define COUNT 80 #include<stdio.h> #include<stdlib.h> #include<time.h> void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); void DisplyBoard(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);
到此,相信大家對“C語言怎么實現簡單的掃雷功能”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。