本篇文章為大家展示了使用C語言編寫一個掃雷游戲,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
game.h
#pragma once #include <stdio.h> #include <string.h> #include <time.h> #include <windows.h> #define ROW 12 #define COL 12 #define NUMS 20 #pragma warning(disable:4996) void Menu(); void Game();
game.c
#include "game.h" void Menu() { printf("###########################\n"); printf("## 1.Play 2. Exit ##\n"); printf("###########################\n"); printf("請輸入# "); } void SetMines(char board[][COL], int row, int col) { int num = NUMS; while (num) { int x = rand() % 10 + 1; int y = rand() % 10 + 1; if (board[x][y] == '0') { board[x][y] = '1'; num--; } } } int GetNums(char board[][COL], int row, int col, int x, int y) { return board[x - 1][y - 1] + board[x - 1][y] + \ board[x - 1][y + 1] + board[x][y + 1] + \ board[x + 1][y + 1] + board[x + 1][y] + \ board[x + 1][y - 1] + board[x][y - 1] - 8 * '0'; } void ShowBoard(char board[][COL], int row, int col) { printf(" "); for (int i = 1; i < col - 1; i++) { printf(" %2d ", i); } printf("\n"); printf("-------------------------------------------\n"); for (int i = 1; i < row - 1; i++) { printf("%2d|", i); for (int j = 1; j < col - 1; j++) { printf(" %c |", board[i][j]); } printf("\n"); printf("-------------------------------------------\n"); } } void Game() { system("cls"); srand((unsigned long)time(NULL)); char show_board[ROW][COL]; char mine_board[ROW][COL]; memset(show_board, '*', sizeof(show_board)); memset(mine_board, '0', sizeof(mine_board)); SetMines(mine_board, ROW, COL); int count = (ROW - 2) * (COL - 2) - NUMS; int x = 0; int y = 0; do { ShowBoard(show_board, ROW, COL); printf("請輸入坐標(biāo)# "); scanf("%d %d", &x, &y); if (x < 1 || x > ROW - 2 || y < 1 || y > COL - 2) { printf("輸入位置越界,請重新輸入!\n"); continue; } if (show_board[x][y] != '*') { printf("該位置已經(jīng)被排除!\n"); continue; } if (mine_board[x][y] == '1') { break; } int num = GetNums(mine_board, ROW, COL, x, y); show_board[x][y] = num + '0'; count--; system("cls"); } while (count > 0); if (count > 0) { printf("你被炸死了!\n"); ShowBoard(mine_board, ROW, COL); } else { printf("恭喜,你通過游戲!\n"); } }
main.c
#include "game.h" void Menu() { printf("###########################\n"); printf("## 1.Play 2. Exit ##\n"); printf("###########################\n"); printf("請輸入# "); } void SetMines(char board[][COL], int row, int col) { int num = NUMS; while (num) { int x = rand() % 10 + 1; int y = rand() % 10 + 1; if (board[x][y] == '0') { board[x][y] = '1'; num--; } } } int GetNums(char board[][COL], int row, int col, int x, int y) { return board[x - 1][y - 1] + board[x - 1][y] + \ board[x - 1][y + 1] + board[x][y + 1] + \ board[x + 1][y + 1] + board[x + 1][y] + \ board[x + 1][y - 1] + board[x][y - 1] - 8 * '0'; } void ShowBoard(char board[][COL], int row, int col) { printf(" "); for (int i = 1; i < col - 1; i++) { printf(" %2d ", i); } printf("\n"); printf("-------------------------------------------\n"); for (int i = 1; i < row - 1; i++) { printf("%2d|", i); for (int j = 1; j < col - 1; j++) { printf(" %c |", board[i][j]); } printf("\n"); printf("-------------------------------------------\n"); } } void Game() { system("cls"); srand((unsigned long)time(NULL)); char show_board[ROW][COL]; char mine_board[ROW][COL]; memset(show_board, '*', sizeof(show_board)); memset(mine_board, '0', sizeof(mine_board)); SetMines(mine_board, ROW, COL); int count = (ROW - 2) * (COL - 2) - NUMS; int x = 0; int y = 0; do { ShowBoard(show_board, ROW, COL); printf("請輸入坐標(biāo)# "); scanf("%d %d", &x, &y); if (x < 1 || x > ROW - 2 || y < 1 || y > COL - 2) { printf("輸入位置越界,請重新輸入!\n"); continue; } if (show_board[x][y] != '*') { printf("該位置已經(jīng)被排除!\n"); continue; } if (mine_board[x][y] == '1') { break; } int num = GetNums(mine_board, ROW, COL, x, y); show_board[x][y] = num + '0'; count--; system("cls"); } while (count > 0); if (count > 0) { printf("你被炸死了!\n"); ShowBoard(mine_board, ROW, COL); } else { printf("恭喜,你通過游戲!\n"); } }
上述內(nèi)容就是使用C語言編寫一個掃雷游戲,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:使用C語言編寫一個掃雷游戲-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://jinyejixie.com/article16/dpoodg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、外貿(mào)建站、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司、網(wǎng)站維護(hù)、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容