您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用C語言寫一個(gè)掃雷游戲”,在日常操作中,相信很多人在怎么用C語言寫一個(gè)掃雷游戲問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用C語言寫一個(gè)掃雷游戲”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
掃雷游戲
1.菜單
2.初始化棋盤
3.打印棋盤
4.布置雷
5.排查雷
6.整體效果展示
7.代碼分享
7.1 test.c
7.2 game.c
7.3 game.h
今天我們就要設(shè)計(jì)一款這樣的游戲,首先需要設(shè)置棋盤,然后對(duì)棋盤進(jìn)行隨機(jī)布雷,最后在排雷。
首先制作一個(gè)簡(jiǎn)易菜單
int main() { int input; srand((unsigned int)time(NULL)); do { menu(); printf("請(qǐng)選擇:\n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戲\n"); break; default: printf("選擇錯(cuò)誤\n"); break; } } while(input); return 0; }
效果如下:
首先需要設(shè)置兩個(gè)二維數(shù)組,分別存放雷的信息和排查出雷的信息
char mine[ROWS][COLS] = { 0 };//存放雷的信息 char show[ROWS][COLS] = { 0 };//存放排查出雷的信息
然后對(duì)其進(jìn)行初始化
//初始化棋盤 void InitBoard(char board[ROWS][COLS], int rows, int cols,char set) { for (int i = 0;i < rows;i++) { for (int j = 0;j < cols;j++) { board[i][j] = set; } } }
棋盤的打印可以直接遍歷二維數(shù)組,并加上一些分割線
void PrintBoard(char board[ROWS][COLS], int row, int col) { printf("--------掃雷--------\n"); for (int i = 0;i <= 9;i++) { printf("%d ", i); } printf("\n"); for (int i = 1;i < row+1 ;i++) { printf("%d ", i); for (int j = 1;j < col+1;j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("--------------------\n"); }
打印出來的棋盤如下
棋盤打印出來了,接下來就是在棋盤上隨機(jī)布雷
void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % 9 + 1; int y = rand() % 9 + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } }
排查雷采用玩家輸入坐標(biāo),對(duì)比觀察該坐標(biāo)是否有雷并給出提示,提示顯示該坐標(biāo)周圍有幾個(gè)雷或者是否被炸死
//排查雷 int GetMineCount(char board[ROWS][COLS],int x,int y) { int count=0; for (int i = x - 1;i <= x + 1;i++) { for (int j = y - 1;j <= y + 1;j++) { count += (board[i][j]-'0'); } } return count; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x, y,win=0; while (win<row*col-EASY_COUNT) { printf("請(qǐng)輸入坐標(biāo)\n"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] != '1') { int count = GetMineCount(mine,x,y); show[x][y] = count + '0'; PrintBoard(show, ROW, COL); win++; } else { printf("很遺憾,你被炸死了\n"); PrintBoard(mine, ROW, COL); break; } } else { printf("坐標(biāo)非法,請(qǐng)重新輸入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); PrintBoard(mine, ROW, COL); } }
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("*********************************\n"); printf("********* 1. play **********\n"); printf("********* 0. exit **********\n"); printf("*********************************\n"); } void game() { char mine[ROWS][COLS] = { 0 };//存放雷的信息 char show[ROWS][COLS] = { 0 };//存放排查出雷的信息 //初始化棋盤 InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS,'*'); //打印棋盤 //PrintBoard(mine, ROW, COL); PrintBoard(show, ROW, COL); //布置雷 SetMine(mine, ROW, COL); //PrintBoard(mine, ROW, COL); //排查雷 FindMine(mine, show, ROW, COL); } int main() { int input; srand((unsigned int)time(NULL)); do { menu(); printf("請(qǐng)選擇:\n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戲\n"); break; default: printf("選擇錯(cuò)誤\n"); break; } } while(input); return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" //初始化棋盤 void InitBoard(char board[ROWS][COLS], int rows, int cols,char set) { for (int i = 0;i < rows;i++) { for (int j = 0;j < cols;j++) { board[i][j] = set; } } } //打印棋盤 void PrintBoard(char board[ROWS][COLS], int row, int col) { printf("--------掃雷--------\n"); for (int i = 0;i <= 9;i++) { printf("%d ", i); } printf("\n"); for (int i = 1;i < row+1 ;i++) { printf("%d ", i); for (int j = 1;j < col+1;j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("--------------------\n"); } //設(shè)置雷 void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % 9 + 1; int y = rand() % 9 + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } } //排查雷 int GetMineCount(char board[ROWS][COLS],int x,int y) { int count=0; for (int i = x - 1;i <= x + 1;i++) { for (int j = y - 1;j <= y + 1;j++) { count += (board[i][j]-'0'); } } return count; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x, y,win=0; while (win<row*col-EASY_COUNT) { printf("請(qǐng)輸入坐標(biāo)\n"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] != '1') { int count = GetMineCount(mine,x,y); show[x][y] = count + '0'; PrintBoard(show, ROW, COL); win++; } else { printf("很遺憾,你被炸死了\n"); PrintBoard(mine, ROW, COL); break; } } else { printf("坐標(biāo)非法,請(qǐng)重新輸入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); PrintBoard(mine, ROW, COL); } }
#pragma once #include<stdio.h> #include<time.h> #include<stdlib.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 //初始化棋盤 void InitBoard(char board[ROWS][COLS], int rows, int cols,char set); //打印棋盤 void PrintBoard(char board[ROWS][COLS], int row, int col); //設(shè)置雷 void SetMine(char board[ROWS][COLS], int row, int col); //排查雷 void FindMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col);
到此,關(guān)于“怎么用C語言寫一個(gè)掃雷游戲”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。