C語言編寫掃雷小程序
更新時間:2022年09月09日 08:29:55 作者:Rae8023
這篇文章主要為大家詳細介紹了C語言編寫掃雷小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言實現(xiàn)掃雷小程序的具體代碼,供大家參考,具體內(nèi)容如下
首先創(chuàng)建一個項目,建立一個頭文件game.h,兩個源文件game.c和test.c
game.h代碼片:
#ifndef ?__GAME_H__ #define ?__GAME_H__ #include<stdio.h> #include<stdlib.h> #include<string.h> #define ROWS 10//行數(shù) #define COLS 10//列數(shù) #define MINE 20//雷數(shù) void Init_board(char arr[ROWS+2][COLS+2],int rows,int cols, char a); void Is_show(char arr[ROWS+2][COLS+2], int rows, int cols); void Set_mine(char arr[ROWS + 2][COLS + 2], int rows, int cols); void Over_board(char arr[ROWS + 2][COLS + 2], int rows, int cols); #endif
test.c代碼片:
#include"game.h" #include<time.h> void menu() ? //打印菜單欄 { ? ? int i = 0; ? ? int j = 0; ? ? for (i = 0; i < 5; i++) ? ? { ? ? ? ? if (i == 2) ? ? ? ? { ? ? ? ? ? ? printf(" ? 1.Play ? ? ? 0.Exit ? ?"); ? ? ? ? } ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? for (j = 0; j < COLS * 3; j++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? printf("%c", 3); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? printf("\n"); ? ? } } int ?get_mine(char arr[ROWS + 2][COLS + 2], int x, int y)//用于清空沒有雷區(qū)的地方 { ? ? return (arr[x - 1][y] - '0') ? ? ? ? + (arr[x - 1][y - 1] - '0') ? ? ? ? + (arr[x - 1][y + 1] - '0') ? ? ? ? + (arr[x][y - 1] - '0') ? ? ? ? + (arr[x][y + 1] - '0') ? ? ? ? + (arr[x + 1][y - 1] - '0') ? ? ? ? + (arr[x + 1][y + 1] - '0') ? ? ? ? + (arr[x + 1][y] - '0'); } void game() ?//玩游戲函數(shù) { ? ? char mine[ROWS+2][COLS+2] = { 0 }; ? ? char show[ROWS+2][COLS+2] = { 0 }; ? ? int win = 1; ? ? srand((unsigned int)time(NULL)); ? ? Init_board(mine, ROWS + 2, COLS + 2, '0'); ? ? Init_board(show, ROWS + 2, COLS + 2, '*'); ? ? /*Is_show(mine, ROWS + 2, COLS + 2); ? ? Is_show(show, ROWS + 2, COLS + 2);*/ ? ? //打印雷區(qū)棋盤 方便調(diào)試 ? ? Set_mine(mine, ROWS + 2, COLS + 2); ? ? /*Is_show(mine, ROWS + 2, COLS + 2);*/ ? ? Is_show(show, ROWS + 2, COLS + 2); ? ? while (win < ROWS*COLS - MINE) ? ? { ? ? ? ? int x = 0; ? ? ? ? int y = 0;? ? ? ? ? printf("請選擇>:"); ? ? ? ? scanf("%d%d", &x, &y); ? ? ? ? if ((x >= 1) && (x <= 10) && (y >= 1) && (y <= 10)) ? ? ? ? { ? ? ? ? ? ? if (mine[x][y] == '1') ? ? ? ? ? ? { ? ? ? ? ? ? ? ? printf("你輸了!\n"); ? ? ? ? ? ? ? ? Over_board(mine, ROWS + 2, COLS + 2); ? ? ? ? ? ? ? ? Is_show(mine, ROWS + 2, COLS + 2); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? int count = get_mine(mine, x, y); ? ? ? ? ? ? ? ? if (count == 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? win += 9; ? ? ? ? ? ? ? ? ? ? show[x][y] = ' '; ? ? ? ? ? ? ? ? ? ? show[x - 1][y - 1] = ' '; ? ? ? ? ? ? ? ? ? ? show[x - 1][y] = ' '; ? ? ? ? ? ? ? ? ? ? show[x - 1][y + 1] = ' '; ? ? ? ? ? ? ? ? ? ? show[x][y - 1] = ' '; ? ? ? ? ? ? ? ? ? ? show[x][y + 1] = ' '; ? ? ? ? ? ? ? ? ? ? show[x + 1][y - 1] = ' '; ? ? ? ? ? ? ? ? ? ? show[x + 1][y] = ' '; ? ? ? ? ? ? ? ? ? ? show[x + 1][y + 1] = ' '; ? ? ? ? ? ? ? ? ? ? Is_show(show, ROWS + 2, COLS + 2); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? win++; ? ? ? ? ? ? ? ? ? ? show[x][y] = count + '0'; ? ? ? ? ? ? ? ? ? ? Is_show(show, ROWS + 2, COLS + 2); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? printf("輸入有誤,請重新輸入%c", 1); ? ? ? ? } ? ? } ? ? if (win >=ROWS*COLS - MINE) ? ? { ? ? ? ? Over_board(show, ROWS + 2, COLS + 2); ? ? ? ? printf("你贏了!\n"); ? ? ? ? Is_show(show, ROWS + 2, COLS + 2); ? ? } } int main() { ? ? int input = 0; ? ? do ? ? { ? ? ? ? menu(); ? ? ? ? printf("請選擇>;"); ? ? ? ? scanf("%d", &input); ? ? ? ? switch (input) ? ? ? ? { ? ? ? ? case 1: ? ? ? ? ? ? game(); ? ? ? ? ? ? break; ? ? ? ? case 0: ? ? ? ? ? ? break; ? ? ? ? default: ? ? ? ? ? ? printf("選擇錯誤,請重新選擇\n"); ? ? ? ? ? ? break; ? ? ? ? } ? ? } while (input); ? ? return 0; }
game.c代碼片:
#include"game.h" void Init_board(char arr[ROWS+2][COLS+2], int rows, int cols, char a)//初始化棋盤 { ? ? memset(arr, a, rows*cols); } void Is_show(char arr[ROWS+2][COLS+2], int rows, int cols)//打印棋盤 { ? ? int i = 0; ? ? int j = 0; ? ? printf(" ? ? ?"); ? ? for (i = 0; i < rows - 2; i++) ? ? { ? ? ? ? printf("_%d__", i + 1); ? ? } ? ? printf("\n"); ? ? for (i = 0; i < rows - 2; i++) ? ? { ? ? ? ? printf("%2d ? ", i + 1); ? ? ? ? for (j = 0; j < cols - 2; j++) ? ? ? ? { ? ? ? ? ? ? printf("|_%c_", arr[i + 1][j + 1]); ? ? ? ? } ? ? ? ? printf("|\n"); ? ? } } void Set_mine(char arr[ROWS + 2][COLS + 2], int rows, int cols)//設置雷區(qū) { ? ? int x = 0; ? ? int y = 0; ? ? int count = MINE; ? ? while (count) ? ? { ? ? ? ? x = rand() % 10 + 1; ? ? ? ? y = rand() % 10 + 1; ? ? ? ? if (arr[x][y] != '1') ? ? ? ? { ? ? ? ? ? ? arr[x][y] = '1'; ? ? ? ? ? ? count--; ? ? ? ? } ? ? } } void Over_board(char arr[ROWS + 2][COLS + 2], int rows, int cols)//將所有不是雷區(qū)的位置清空 { ? ? int i = 0; ? ? int j = 0; ? ? for (i = 0; i < rows - 2; i++) ? ? { ? ? ? ? for (j = 0; j < cols - 2; j++) ? ? ? ? { ? ? ? ? ? ? if (arr[i + 1][j + 1] == '0') ? ? ? ? ? ? ? ? arr[i + 1][j + 1] = ' '; ? ? ? ? } ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VC創(chuàng)建進程CreateProcess的方法
這篇文章主要介紹了VC創(chuàng)建進程CreateProcess的方法,涉及VC操作進程的基本技巧,需要的朋友可以參考下2015-05-05C++實現(xiàn)簡單的希爾排序Shell Sort實例
這篇文章主要介紹了C++實現(xiàn)簡單的希爾排序Shell Sort實例,對于正在學習算法的朋友很有借鑒價值,需要的朋友可以參考下2014-07-07C++11中l(wèi)ambda、std::function和std:bind詳解
大家都知道C++11中增加了許多的新特性,下面在這篇文中我們就來聊一下lambda表達式,閉包,std::function以及std::bind。文中介紹的很詳細,相信對大家具有一定的參考價值,有需要的朋友們下面來一起看看吧。2017-01-01