C語言實(shí)現(xiàn)掃雷小程序
前言
《掃雷》是一款大眾類的益智小游戲,于1992年發(fā)行。游戲目標(biāo)是在最短的時間內(nèi)根據(jù)點(diǎn)擊格子出現(xiàn)的數(shù)字找出所有非雷格子,同時避免踩雷,踩到一個雷即全盤皆輸。
多文件形式
在實(shí)現(xiàn)游戲的首先,需要創(chuàng)建test.c game.c game.h三個文件。
test.c主要進(jìn)行游戲的測試。
game.c主要進(jìn)行游戲內(nèi)部一些函數(shù)的具體實(shí)現(xiàn)。
game.h主要是一些聲明,宏定義。
游戲邏輯
1、打印簡易菜單
2、定義及初始化數(shù)組
3、隨機(jī)生成布置雷
4、玩家排雷
游戲?qū)崿F(xiàn)
打印簡易菜單
打印菜單讓玩家選擇,輸入1為開始游戲,輸入0位退出游戲
void meun() { printf("*********************************\n"); printf("*********************************\n"); printf("*********** 1. play ***********\n"); printf("*********** 0. exit ***********\n"); printf("*********************************\n"); printf("*********************************\n"); }
初始化數(shù)組
程序生成兩個99的方塊矩陣,mine數(shù)組用來保存雷的布局,show數(shù)組用來展示玩家實(shí)時的排雷進(jìn)度。
mine數(shù)組中,‘1'代表雷,'0‘代表無雷。
show數(shù)組中,'‘代表雷,'#'代表無雷。
//初始化數(shù)組 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); 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; } } }
打印方塊矩陣
玩家每確定一次排雷坐標(biāo),就要在屏幕上顯示排雷情況。
void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; for (i = 0; i < 10; 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"); } }
布置雷
利用C語言庫中的rand函數(shù)生成隨機(jī)數(shù)來布置雷。
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() % col + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } }
玩家排雷
玩家輸入排雷的坐標(biāo),程序會在屏幕顯示當(dāng)前坐標(biāo)周圍8個方塊存在的雷的數(shù)量。
int MineCount(char mine[ROWS][COLS], int x, int y) { return (mine[x + 1][y + 1] + mine[x + 1][y] + mine[x + 1][y - 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x - 1][y + 1] + mine[x - 1][y] + mine[x - 1][x - 1]) - 8 * '0'; } void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int win = 0; int x = 0; int y = 0; while (win < row * col - EASY_COUNT) { printf("請輸入要排查的坐標(biāo):>"); scanf("%d %d", &x, &y); if (x <= row && x >= 1 && y <= col && y >= 1) { if (mine[x][y] == '0') { int count = MineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } else { printf("很遺憾,你被炸死了\n"); break; } } else { printf("下標(biāo)錯誤,請重新輸入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); DisplayBoard(mine, ROW, COL); } }
完整代碼
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void meun() { printf("*********************************\n"); printf("*********************************\n"); printf("*********** 1. play ***********\n"); printf("*********** 0. exit ***********\n"); printf("*********************************\n"); printf("*********************************\n"); } void game() { char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; //初始化數(shù)組 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); //布置雷 SetMine(mine, ROW, COL); DisplayBoard(mine, ROW, COL); DisplayBoard(show, ROW, COL); //排雷 FindBoard(mine, show, ROW, COL); DisplayBoard(show, ROW, COL); } int main() { srand((unsigned int)time(NULL)); int input = 0; do { meun(); printf("請選擇:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戲\n"); break; default: printf("選擇錯誤,請重新輸入\n"); break; } } while (input); return 0; }
game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" 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 DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; for (i = 0; i < 10; 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 = EASY_COUNT; int x = 0; int y = 0; while (count) { x = rand() % row + 1; y = rand() % col + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } } int MineCount(char mine[ROWS][COLS], int x, int y) { return (mine[x + 1][y + 1] + mine[x + 1][y] + mine[x + 1][y - 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x - 1][y + 1] + mine[x - 1][y] + mine[x - 1][x - 1]) - 8 * '0'; } void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int win = 0; int x = 0; int y = 0; while (win < row * col - EASY_COUNT) { printf("請輸入要排查的坐標(biāo):>"); scanf("%d %d", &x, &y); if (x <= row && x >= 1 && y <= col && y >= 1) { if (mine[x][y] == '0') { int count = MineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } else { printf("很遺憾,你被炸死了\n"); break; } } else { printf("下標(biāo)錯誤,請重新輸入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); DisplayBoard(mine, ROW, COL); } }
game.h
#pragma once #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 9 #define COL 9 #define EASY_COUNT 10 #define ROWS ROW+2 #define COLS COL+2 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 FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中vector的模擬實(shí)現(xiàn)實(shí)例詳解
vector是表示可變大小數(shù)組的序列容器,它也采用連續(xù)存儲空間來存儲元素,因此可以采用下標(biāo)對vector的元素進(jìn)行訪問,這篇文章主要給大家介紹了關(guān)于C++中vector模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2021-11-11Visual?Studio?Code?配置C、C++?文件debug調(diào)試環(huán)境的詳細(xì)過程
這篇文章主要介紹了Visual?Studio?Code?配置C、C++?文件debug調(diào)試環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02C++基于單鏈表實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++基于單鏈表實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05C++實(shí)現(xiàn)訪問者模式的基礎(chǔ)介紹
訪問者模式表示一個作用于某對象結(jié)構(gòu)中各元素的操作,它使我們可以在不改變各元素的類的前提下定義作用于這些元素的新操作。對C++訪問者模式相關(guān)知識感興趣的朋友一起看看吧2021-09-09Cocos2d-x學(xué)習(xí)筆記之開發(fā)環(huán)境搭建
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之開發(fā)環(huán)境搭建,本文使用Visual Studio作為開發(fā)IDE,是不同于其它教程的,需要的朋友可以參考下2014-09-09