用C编写扫雷游戏
程序员文章站
2024-03-18 13:27:28
...
扫雷
游戏规则:玩家输入想要扫的位置坐标,若扫到雷,挑战失败,游戏结束;反之,若没有扫到雷,此位置会显示出该位置周围雷的个数,直到扫到雷或者挑战成功。
界面设计:定义两个char类型二维数组,一个是棋盘,一个用来显示雷的个数
布雷:使电脑随机布雷(控制布雷的个数;'1’表示有雷,'0’表示安全)
下面使用C编写的代码
程序代码:
//mine.h
#ifndef MINE_H
#define MINE_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#pragma warning (disable:4996)
#define ROW 12
#define COL 12
#define NUM 20
void Menu();//'1':有雷,'0':安全
void InitBoard(char board[][COL], int row, int col, char elem);
void SetMine(char mine[][COL], int row, int col);//埋雷
void ShowBoard(char board[][COL], int row, int col);
char GetNum(char mine[][COL], int x, int y); //返回周围雷的个数
void Game();
#endif
//mine.c
#include "mine.h"
void Menu()
{
printf("###########################\n");
printf("### 欢迎挑战扫雷游戏! ###\n");
printf("###########################\n");
printf("### 1、Play 2、Exit ###\n");
printf("###########################\n");
printf("please select:");
}
void InitBoard(char board[][COL], int row, int col, char elem)
{
int i = 0;
for (; i < row; i++){
int j = 0;
for (; j < col; j++){
board[i][j] = elem;
}
}
}
void SetMine(char mine[][COL], int row, int col)
{
int n = NUM;
while (n){
int x = rand() % (row - 2) + 1;
int y = rand() % (col - 2) + 1;
if ('1' == mine[x][y]){
continue;
}
mine[x][y] = '1';
n--;
}
}
void ShowBoard(char board[][COL], int row, int col)
{
int i = 1;
printf(" ");
for (; i <= col - 2; i++){
printf(" %-2d|", i);
}
printf("\n");
for (i = 1; i <= col - 2; i++){
printf("%s", "----");
}
printf("----\n");
for (i = 1; i <= row - 2; i++){
printf(" %-2d|", i);
int j = 1;
for (; j <= col - 2; j++){
printf(" %-2c|", board[i][j]);
}
printf("\n");
for (j = 1; j <= col - 2; j++){
printf("%s", "----");
}
printf("----\n");
}
}
char GetNum(char mine[][COL], int x, int y)//'1'='0'+1
{
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] - 7 * '0';
}
void Game()
{
srand((unsigned long)time(NULL));
char board[ROW][COL];
char mine[ROW][COL];
InitBoard(board, ROW, COL, '*');
InitBoard(mine, ROW, COL, '0');
SetMine(mine, ROW, COL);
int count = (ROW - 2)*(COL - 2) - NUM;
while (count > 0){
system("cls");//刷屏
int x = 0;
int y = 0;
ShowBoard(board, ROW, COL);
printf("请输入你要扫的位置:");
scanf("%d %d", &x, &y);
if (x<1 || x>ROW - 2 || y<1 || y>COL - 2){
printf("你输入的位置有误,请重新输入!\n");
Sleep(100);
continue;
}
if (board[x][y] != '*'){
printf("你输入的位置已经被扫过了!\n");
Sleep(100);
continue;
}
if (mine[x][y] == '0'){
count--;
char num = GetNum(mine, x, y);
board[x][y] = num;
}
else{
printf("你输入的位置(%d,%d)有雷!\n", x, y);
break;
}
}
if (count > 0){
printf("##############################\n");
printf("## 你扫到雷了,挑战失败 ! ##\n");
printf("##############################\n");
}
else{
printf("##############################\n");
printf("## 恭喜你,挑战成功! ##\n");
printf("##############################\n");
}
ShowBoard(mine, ROW, COL);
}
//main.c
#include "mine.h"
int main()
{
int quit = 0;
while (!quit){
int select = 0;
Menu();
scanf("%d", &select);
switch (select){
case 1:
Game();
printf("游戏结束,要不要再来一局?\n");
break;
case 2:
quit = 1;
printf("你已选择退出游戏!\n");
break;
default:
break;
}
}
system("pause");
return 0;
}