时间:2021-06-16 08:23:11 | 栏目:C代码 | 点击:次
本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下
扫雷(第一次多文件应用)
扫雷的思路
game.h
#ifndef _GAME_H_ #define _GAME_H_ #include<stdio.h> #include <time.h> #include<string.h> #include<windows.h> #pragma warning(disable:4996) #define ROW 12 #define COL 12 //定义20个雷 #define NUMS 20 void Menu(); void Game(); #endif
main.c
#include "game.h" int main(){ int quit = 0; int select = 0; while (!quit){ Menu(); scanf("%d", &select); switch (select){ case 1: Game(); break; case 2: quit = 1; break; default: printf("请重新输入"); break; } } system("pause"); return 0; }
game.c
#include "game.h" void Menu() { printf("##########################\n"); printf("## 1. Play 2. Exit ##\n"); printf("##########################\n"); printf("请输入# "); } //设置20个随机雷 void SetMines(char mine_board[][COL], int row, int col) { int count = NUMS; while (count){ int x = rand() % 10 + 1; int y = rand() % 10 + 1; if (mine_board[x][y] == '0'){ mine_board[x][y] = '1'; count--; } } } //判断周围有几个雷 int GetMines(char mine[][COL], int row, int col, int x, int y) { return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + \ mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + \ mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0'; } //设置界面的下划线 static void ShowLine(int nums) { printf("---"); for (int i = 0; i < nums; i++){ printf("-"); } printf("\n"); } //一个显示界面,传入界面数组显示扫雷界面,传入布雷数组显示雷区界面 void ShowBoard(char show_board[][COL], int row, int col) { printf(" "); for (int i = 1; i < row - 1; i++){ printf(" %d ", i); } printf("\n"); ShowLine(2 * col + col + 4); for (int i = 1; i < row - 1; i++){ printf("%2d|", i); for (int j = 1; j < col - 1; j++){ printf(" %c |", show_board[i][j]); } printf("\n"); ShowLine(2 * col + col + 4); } } void Game() { char show_board[ROW][COL]; char mine_board[ROW][COL]; memset(show_board, '*', sizeof(show_board)); memset(mine_board, '0', sizeof(mine_board)); srand((unsigned long)time(NULL)); 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("请输入位置# "); scanf("%d %d", &x, &y); if (x < 1 || x > 10 || y < 1 || y >10){ printf("输入越界,请重新输入!\n"); continue; } if (show_board[x][y] != '*'){ printf("该位置已经被排除,请重新输入!\n"); continue; } if (mine_board[x][y] == '1'){ break; } int num = GetMines(mine_board, ROW, COL, x, y); show_board[x][y] = num + '0'; count--; system("cls"); } while (count > 0); //count>0说明坐标是雷,break提前退出了 if (count > 0){ printf("你被炸死了!\n"); } else{ printf("你赢了!\n"); } printf("下面是雷区的排布!\n"); ShowBoard(mine_board, ROW, COL); }
更多有趣的经典小游戏实现专题,分享给大家: