C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷功能
這是我跟著學(xué)習(xí)視頻完成的第一個(gè)小游戲,運(yùn)用到的知識(shí)不多都是數(shù)組相關(guān)的知識(shí),重要的是思路,在設(shè)計(jì)的時(shí)候要先繪制出大概的框圖,要知道游戲的根本,這樣會(huì)讓你寫(xiě)程序的時(shí)候更加方便。
下面看代碼:
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("請(qǐng)選擇: "); scanf("%d", &input); switch (input) { case 1: printf("掃雷游戲!\n"); game(); break; case 0: printf("已退出游戲!\n"); break; default: printf("輸入錯(cuò)誤,請(qǐng)重新輸入!\n"); break; } } while (input); } void game() { //雷的信息存儲(chǔ) //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; //打印列號(hào) for (i = 0; i <= row; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { //打印行號(hào) 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("請(qǐng)輸入坐標(biāo): "); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { //坐標(biāo)合法 //1.踩雷 if (mine[x][y] == '1') { printf("你被炸死了?。。n"); DisplyBoard(mine, row, col); break; } //不是雷 else { //計(jì)算x,y坐標(biāo)周圍有幾個(gè)雷 int count = get_mine_count(mine, x, y); show[x][y] = count + '0'; DisplyBoard(show, row, col); win++; } } else { printf("坐標(biāo)非法,請(qǐng)重新輸入!"); } } 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);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言模擬實(shí)現(xiàn)庫(kù)函數(shù)詳解
C語(yǔ)言庫(kù)函數(shù)是把自定義函數(shù)放到庫(kù)里,是別人把一些常用到的函數(shù)編完放到一個(gè)文件里,供程序員使用,下面讓我們一起來(lái)詳細(xì)了解它2022-07-07詳解C++中的內(nèi)聯(lián)函數(shù)和函數(shù)重載
這篇文章主要介紹了詳解C++中的內(nèi)聯(lián)函數(shù)和函數(shù)重載,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09OpenCV實(shí)現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01《戰(zhàn)狼》中兩軍作戰(zhàn)入侵代碼竟然是輸出星期幾的!
這篇文章主要介紹了《戰(zhàn)狼》中兩軍作戰(zhàn)入侵代碼竟然是輸出星期幾的,喜歡戰(zhàn)狼和編程的同學(xué)可以了解下。2017-11-11創(chuàng)建二叉樹(shù) 二叉樹(shù)如何刪除節(jié)點(diǎn)操作教程
本文將詳細(xì)介紹二叉樹(shù)的創(chuàng)建,節(jié)點(diǎn)刪除,節(jié)點(diǎn)增加等一系列操作方法,需要的朋友可以參考下2012-12-12C++基于灰度圖上色GrayToColorFromOther的實(shí)現(xiàn)
本文主要介紹了C++基于灰度圖上色GrayToColorFromOther的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07