C語言模擬實現(xiàn)簡單掃雷游戲
更新時間:2019年10月21日 16:52:52 作者:ChaseRaod
這篇文章主要為大家詳細介紹了C語言模擬實現(xiàn)簡單掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文指的掃雷是簡單模擬電腦中的掃雷游戲,但以我目前的水平,也就只能在黑框中實現(xiàn)
test.c
#include<stdio.h> #include<stdlib.h> #include<time.h> #include "game2.h" void menu() { printf("********* welcome ********\n"); printf("**********1.play**********\n"); printf("**********0.exit**********\n"); } enum Option { EXIT, PLAY }; void game() { int x = 0; int y = 0; int i = 0; int win = 0; char mine[ROWS + 2][COLS + 2] = { 0 }; char show[ROWS + 2][ROWS + 2] = { 0 }; init_board(mine, ROWS + 2, COLS + 2, '0'); init_board(show, ROWS + 2, COLS + 2, '*'); //display(mine, ROWS + 2, COLS + 2);#define _CRT_SECURE_NO_WARNINGS //display(show, ROWS + 2, COLS + 2); mine_set(mine, ROWS + 2, COLS + 2); display(mine, ROWS + 2, COLS + 2); while (win<ROWS*COLS - DEFAULT_COUNT) { for (i = 0; i <= win; i++) { printf("請輸入坐標:>"); scanf("%d%d", &x, &y); //合法性判斷 if ((x>0) && (x <= ROWS) && (y > 0) && (y <= COLS)) { if ((i == 0) && (mine[x][y] == '1')) { (mine[x][y] = '0') ; } if (mine[x][y] == '1') { printf("很遺憾,你被炸死了\n"); break; } else { int count = 0; win++; count = get_mine_num(mine, x, y); show[x][y] = count + '0'; display(show, ROWS + 2, COLS + 2); } } else { printf("輸入錯誤請重新輸入\n"); } } if (win >= ROWS*COLS - DEFAULT_COUNT) { printf("恭喜你,掃雷成功\n"); } } } int main() { int input = 0; srand((uint_t)time(NULL)); do { menu(); printf("請選擇:>"); scanf("%d", &input); switch (input) { case PLAY: game(); break; case EXIT: break; default: printf("輸入錯誤,請重新輸入\n"); break; } } while (input); system("pause\n"); return 0; }
game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "game2.h" #include<stdlib.h> #include<stdio.h> #include<string.h> void init_board(char arr[ROWS + 2][COLS + 2], int row, int col,char ch) { memset(arr, ch, sizeof(char) * row * col); } void display(char arr[ROWS + 2][COLS + 2], int row, int col) { int i = 0; int j = 0; printf(" "); for (i = 0; i < col - 2; i++) { printf("%d ", i + 1); } printf("\n"); for (i = 0; i < row - 2; i++) { printf("%2d ", i + 1); for (j = 0; j < col - 2; j++) { printf("%c ", arr[i + 1][j + 1]); } printf("\n"); } } void mine_set(char arr[ROWS + 2][COLS + 2], int row, int col) { int x = 0; int y = 0; int count = DEFAULT_COUNT; while (count) { x = rand() % 10 + 1; y = rand() % 10 + 1; if (arr[x][y] != '1') { arr[x][y] = '1'; count--; } } } int get_mine_num(char arr[ROWS + 2][COLS + 2], int x, int y) { return (arr[x][y - 1] - '0') + (arr[x - 1][y - 1]-'0')- + (arr[x - 1][y]-'0') + (arr[x - 1][y + 1]-'0') + (arr[x][y + 1]-'0') + (arr[x + 1][y + 1]-'0') + (arr[x + 1][y]-'0') + (arr[x + 1][y - 1]-'0');//返回周圍雷的個數(shù) }
game.h
#ifndef __GAME_H__ #define __GAME_H__ #define ROWS 10 #define COLS 10 #define DEFAULT_COUNT 20 typedef unsigned int uint_t;//類型重命名 #include<string.h> #include<stdio.h> #include<time.h> #include<stdlib.h> void init_board(char arr[ROWS + 2][COLS + 2], int row, int col,char ch);//初始化 void display(char arr[ROWS + 2][COLS + 2], int row, int col); void mine_set(char arr[ROWS + 2][COLS + 2], int row, int col);//放雷 int get_mine_num(char arr[ROWS + 2][COLS + 2], int row, int col);//統(tǒng)計坐標周圍雷的個數(shù) #endif //__GAME_H__
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
VsCode安裝和配置c/c++環(huán)境小白教程(圖文)
本文主要介紹了VsCode安裝和配置c/c++環(huán)境小白教程,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01詳解C++設計模式編程中策略模式的優(yōu)缺點及實現(xiàn)
這篇文章主要介紹了C++設計模式編程中策略模式的優(yōu)缺點及實現(xiàn),文中討論了策略模式中設計抽象接口的繼承和組合之間的區(qū)別,需要的朋友可以參考下2016-03-03windows上配置vscode?C/C++代碼跳轉的實現(xiàn)
C/C++官方的C/C++插件,必備的插件,是代碼跳轉、自動補全、代碼大綱顯示等功能的基礎,本文主要介紹了windows上配置vscode?C/C++代碼跳轉,感興趣的可以了解一下2023-09-09用c語言實現(xiàn)《狼人殺》游戲發(fā)牌系統(tǒng)
大家好,本篇文章主要講的是用c語言實現(xiàn)《狼人殺》游戲發(fā)牌系統(tǒng),感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01