您好,登錄后才能下訂單哦!
本文實例為大家分享了C語言實現掃雷游戲的具體代碼,供大家參考,具體內容如下
主函數:main.c
#include "game.h" void Menu() { printf("##########################\n"); printf("##1.play 0.exit##########\n"); printf("##########################\n"); printf("## Please Enter select! ##\n"); } int main() { Menu(); srand((unsigned int)time(NULL)); int quit = 0; while (!quit) { int select = 0; printf("請輸入你的選擇:\n"); scanf("%d", &select); switch (select) { case 1: game(); break; case 2: quit = 1; break; default : printf("你輸入有誤,請重新輸入:\n"); break; } } printf("Bye Bye!\n"); system("pause"); return 0; }
子函數:game.c
#include "game.h" void game() { char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; memset(mine, '0', sizeof(mine));//初始化數組置為0 memset(show, '*', sizeof(show));//初始化數組置為* int no_y, no_x; set_mine(mine,ROWS,COLS,&no_x,&no_y);//布雷 ‘1'表示雷 int x = 0; int y = 0; int time = 100 - NUM; while (time > 0) { system("cls");//清屏 Show(show, ROWS, COLS);//打印 棋盤 printf("請輸入坐標:\n"); scanf("%d%d", &x, &y); if (x<1 || x>10 || y<1 || y>10) { printf("你輸入有誤,請重新輸入:\n"); continue; } if (show[x][y] != '*') { printf("你輸入有誤,請重新輸入:\n"); continue; } if (mine[x][y] == '1') { if (time == 80)//如果第一次有雷,用一個沒雷的與這個交換 { mine[x][y] = '0'; mine[no_y][no_y] = '1'; } else { printf("game over!\n"); Show(mine, ROWS, COLS); break; } } show[x][y] = get_mine_count(mine, x, y) + '0'; Expand(mine, show, x, y); time--; } } void set_mine(char mine[ROWS][COLS],int col,int row,int *no_x,int *no_y)//聲明布雷函數 { int count = NUM;//設置計數器,統計布雷的個數 while (count > 0) { int x = rand() % (col-2) + 1; int y = rand() % (col-2) + 1; if ((mine[x][y]) == '0') { mine[x][y] = '1'; count--; } } for (int i = 1; i <= 10; i++) { for (int j = 1; i <= 10; j++) { if (mine[i][j] == '0') { no_x = i; no_y = j; return; } } } } void Show(char mine[ROWS][COLS], int row, int col)//聲明打印棋盤函數 { int i = 0; int j = 0; printf(" "); for (i = 1; i <= 10 ; i++) { printf("%2d |", i); } printf("\n"); for (i = 1; i <= 11; i++) { printf("----"); } printf("\n"); for (i = 1; i <= 10 ; i++) { printf("%2d |", i); for (j = 1; j <= 10; j++) { printf("%2c |", mine[i][j]); } printf("\n"); for (int i = 1; i <= 11; i++) { printf("----"); } printf("\n"); } } int get_mine_count(char mine[ROWS][COLS], int x, int y)//雷數統計 { return 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]+ mine[x - 1][y] - 8 * '0'; } void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) { if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { if (get_mine_count(mine, x, y) + '0' == '0') //表示x,y周圍沒雷 { show[x][y] = '0'; if (show[x - 1][y - 1] == '*') { Expand(mine, show, x - 1, y - 1); } if (show[x - 1][y] == '*') { Expand(mine, show, x - 1, y ); } if (show[x - 1][y + 1] == '*') { Expand(mine, show, x - 1, y + 1); } if (show[x ][y - 1] == '*') { Expand(mine, show, x , y - 1); } if (show[x][y + 1] == '*') { Expand(mine, show, x , y + 1); } if (show[x + 1][y - 1] == '*') { Expand(mine, show, x + 1, y - 1); } if (show[x + 1][y] == '*') { Expand(mine, show, x + 1, y ); } if (show[x + 1][y + 1] == '*') { Expand(mine, show, x + 1, y + 1); } } } }
函數聲明:game.h
#ifndef _GAME_H_ #define _GAME_H_ #include<stdio.h> #include<Windows.h> #pragma warning(disable:4996) #include<time.h> #include<string.h> #define ROWS 12 #define COLS 12 #define NUM 20 //雷數 void game(); void set_mine(char mine[ROWS][COLS],int row, int col, int *no_x, int *no_y); void Show(char mine[ROWS][COLS], int row, int col); int get_mine_count(char mine[ROWS][COLS], int x, int y); void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y); #endif
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。