C語言實(shí)現(xiàn)掃雷小游戲簡單版
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)掃雷小游戲的具體代碼,供大家參考,具體內(nèi)容如下
在vs2019創(chuàng)建新項(xiàng)目,然后添加兩個(gè)源文件test.c和game.c,接著創(chuàng)建一個(gè)頭文件game.h。
test.c:
#include "game.h" void game() { char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); SetMine(mine, ROW, COL); //DispalyBoard(mine, ROW, COL); DispalyBoard(show, ROW, COL); FindMine(mine, show, ROW, COL); } void menu() { printf("**************************\n"); printf("****** 1.play ******\n"); printf("****** 0.exit ******\n"); printf("**************************\n"); } 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("輸入錯(cuò)誤,請重新輸入\n"); break; } } while (input); return 0; }
game.c:
#include "game.h" void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) { board[i][j] = set; } } } void DispalyBoard(char board[ROWS][COLS], int row, int col) { int i = 0; for (i = 0; i <= 9; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { int j = 0; printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } } void SetMine(char board[ROW][COL], 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--; } } } 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 x = 0; int y = 0; int win = 0; while (win<row*col-EASY_COUNT) { printf("請輸入要排查的坐標(biāo):"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("很遺憾,你被炸死了\n"); DispalyBoard(mine, ROW, COL); break; } else { int count = GetMineCount(mine, x, y); show[x][y] = count + '0'; DispalyBoard(show, ROW, COL); win++; } } else { printf("坐標(biāo)非法,請重新輸入\n"); } } if (win == row * col - EASY_COUNT) { printf("排雷成功\n"); } }
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 DispalyBoard(char board[ROWS][COLS], int row, int col); void SetMine(char board[ROWS][COLS], int row, int col); void FindMine(char mine[ROW][COL], char show[ROW][COL], int row, int col);
運(yùn)行效果如圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言qsort函數(shù)用冒泡排序?qū)崿F(xiàn)過程詳解
qsort函數(shù)是由C語言提供的標(biāo)準(zhǔn)庫函數(shù), 它的實(shí)現(xiàn)思想是快速排序。這篇文章主要介紹了C語言中qsort函數(shù)用法及用冒泡排序?qū)崿F(xiàn)qsort函數(shù)功能,需要的可以參考一下2023-02-02cmake添加一個(gè)庫的實(shí)現(xiàn)步驟
本文主要介紹了cmake添加一個(gè)庫的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06C語言數(shù)據(jù)結(jié)構(gòu)之vector底層實(shí)現(xiàn)機(jī)制解析
向量(Vector)是一個(gè)封裝了動(dòng)態(tài)大小數(shù)組的順序容器(Sequence?Container)。跟任意其它類型容器一樣,它能夠存放各種類型的對象??梢院唵蔚恼J(rèn)為,向量是一個(gè)能夠存放任意類型的動(dòng)態(tài)數(shù)組2021-11-11MySQL的內(nèi)存表的基礎(chǔ)學(xué)習(xí)教程
這篇文章主要介紹了MySQL的內(nèi)存表的基礎(chǔ)學(xué)習(xí)教程,包括內(nèi)存表的創(chuàng)建以及使用限制等等,需要的朋友可以參考下2015-12-12C++如何調(diào)用opencv完成運(yùn)動(dòng)目標(biāo)捕捉詳解
OpenCV作為機(jī)器視覺開源庫,使用起來非常不錯(cuò),這篇文章主要給大家介紹了關(guān)于C++如何調(diào)用opencv完成運(yùn)動(dòng)目標(biāo)捕捉的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05C++實(shí)現(xiàn)LeetCode(29.兩數(shù)相除)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(29.兩數(shù)相除),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07