您好,登錄后才能下訂單哦!
這篇文章主要講解了“C語言實現掃雷小游戲代碼”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“C語言實現掃雷小游戲代碼”吧!
本文將介紹如何用C語言多文件編程實現掃雷該示例掃雷程序可實現以下幾個功能:
自定義雷數 踩雷后會將所有雷顯示出來 地圖大小易修改
Mine_clearance.h #pragma once#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<time.h>#define ROW 11#define COL 11#define 踩雷 0#define 玩家勝利 1int g_number_mine = (ROW - 1)*(COL - 1); //雷的數量上限char map[ROW][COL]; //玩家看到的地圖char mine_map[ROW + 2][COL + 2]; //雷陣void Init_map(); //初始化玩家看到的地圖void Init_mine_map(int input_count); //初始化雷陣void Print_map(); //打印地圖void Print_mine_map(); //如果踩雷需要把雷陣打印出來void Player_move(int row, int col); //提示玩家輸入坐標開始游戲 1.合法性校驗(是否越界)2.是否重復輸入,(已經輸入過的)int Judge_mine_end(int row, int col); //判斷是否踩雷char Count_number_mine(int row, int col); //統計周圍八個方塊雷的個數void Print_mine_number(int row, int col, int n);//將周圍八個方塊雷的數量顯示在當前方塊上int Judge_win(int input_count); //判斷是否獲勝 Mine_clearance.c #include "Mine_clearance.h"//初始化玩家看到的地圖void Init_map(){ for (int row = 0; row <= ROW; ++row) { for (int col = 0; col <= COL; ++col) { map[row][col] = ' '; } }}//初始化雷陣void Init_mine_map(int input_count){ for (int row = 0; row < ROW; ++row) { for (int col = 0; col < COL; ++col) { mine_map[row][col] = ' '; } } srand((unsigned)time(NULL)); for (int i = 0; i < input_count; ++i) { int row = rand() % (ROW - 1) + 1; int col = rand() % (COL - 1) + 1; while (1) { if (mine_map[row][col] == 'x' && row == (ROW - 1) && col == (COL - 1)) { row = 1; col = 1; } else if (mine_map[row][col] == 'x' && (col == COL - 1)) { row += 1; col = 1; } else if (mine_map[row][col] == 'x' && (col < COL - 1)) { col++; } else { mine_map[row][col] = 'x'; break; } } }}//打印地圖void Print_map(){ for (int i = 1; i < ROW; ++i) { printf(" %d ", i); } printf("\n|"); for (int j = 1; j < ROW; ++j) { printf("-----|"); } printf("\n"); int i = 1; for (int row = 1; row < ROW; ++row) { for (int col = 1; col < COL; ++col) { printf("| %c ", map[row][col]); } printf("|%d\n", i); for (int b = 1; b < ROW; ++b) { printf("|-----"); } printf("|"); printf("\n"); i = i + 1; }}//如果踩雷需要把雷陣打印出來void Print_mine_map(){ for (int i = 1; i < ROW; ++i) { printf(" %d ", i); } printf("\n|"); for (int j = 1; j < ROW; ++j) { printf("-----|"); } printf("\n"); int i = 1; for (int row = 1; row < ROW; ++row) { for (int col = 1; col < COL; ++col) { printf("| %c ", mine_map[row][col]); } printf("|%d\n", i); for (int b = 1; b < ROW; ++b) { printf("|-----"); } printf("|"); printf("\n"); i = i + 1; }}//提示玩家輸入坐標開始游戲// 1.合法性校驗(是否越界// 2.是否重復輸入,(已經輸入過的)void Player_move(int row, int col){ if (row < 1 || row >= ROW || col < 1 || col >= COL) { printf("非法輸入!請重新輸入\n"); } else if (map[row][col] == 'o') { printf("重復輸入無效,請重新輸入!\n"); } else { map[row][col] = 'o'; }}//判斷是否踩雷int Judge_mine_end(int row, int col){ if (map[row][col] == 'o'&&mine_map[row][col] == 'x') { return 踩雷; } return 1;}//統計周圍八個方塊雷的個數char Count_number_mine(int row, int col){ int n = 0; for (int i = -1; i < 2; ++i) { for (int j = -1; j < 2; ++j) { if (mine_map[row + i][col + j] == 'x') { n += 1; } } } return n;}//將周圍八個方塊雷的數量顯示在當前方塊上void Print_mine_number(int row, int col, int n){ char num_mine[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8' }; switch (n){ case 0: map[row][col] = num_mine[0]; break; case 1: map[row][col] = num_mine[1]; break; case 2: map[row][col] = num_mine[2]; break; case 3: map[row][col] = num_mine[3]; break; case 4: map[row][col] = num_mine[4]; break; case 5: map[row][col] = num_mine[5]; break; case 6: map[row][col] = num_mine[6]; break; case 7: map[row][col] = num_mine[7]; break; case 8: map[row][col] = num_mine[8]; break; default: printf("雷數非法\n"); break; }}int Judge_win(int input_count){ int num_mine = 0; for (int row = 1; row < ROW; ++row) { for (int col = 1; col < COL; ++col) { if (map[row][col] != ' ') { num_mine += 1; } } } if (num_mine == (ROW - 1)*(COL - 1) - input_count) { return 玩家勝利; } return 2;} main.c #include "Mine_clearance.h"int main(){ while (1) { int input_count = 0; int count_mine = 0; int row = 0; int col = 0; printf("自定義雷的個數:\n"); scanf("%d", &input_count); if (input_count < 1 || input_count >= g_number_mine) { printf("非法輸入,請重新輸入!\n"); } else { Init_mine_map(input_count); Init_map(); Print_map(); while (1) { printf("請輸入坐標:(中間用空格隔開)\n"); scanf("%d %d", &row, &col); Player_move(row, col); if (Judge_mine_end(row, col) == 踩雷) { printf("踩雷了,游戲結束\n"); Print_mine_map(); break; } else { count_mine = Count_number_mine(row, col); Print_mine_number(row, col, count_mine); Print_map(); if (Judge_win(input_count) == 玩家勝利) { printf("你贏了!\n"); break; } } } } } system("pause"); return 0;}
感謝各位的閱讀,以上就是“C語言實現掃雷小游戲代碼”的內容了,經過本文的學習后,相信大家對C語言實現掃雷小游戲代碼這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。