扫雷游戏

猜数字游戏:
http://blog.csdn.net/romantic_c/article/details/78135924
三子棋:
http://blog.csdn.net/romantic_c/article/details/78311056
关机程序:
http://blog.csdn.net/romantic_c/article/details/78093507

这里写图片描述

头文件

#ifndef __GAME_H__
#define __GAME_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define ROWS 11
#define COLS 11
#define COUNT 10

void init_game(char mine[ROWS][COLS],char show[ROWS][COLS]);   //初始化扫雷界面
void set_mine(char mine[ROWS][COLS]);    //设置雷区
void display(char show[ROWS][COLS]);   //打印出界面
void sweep(char mine[ROWS][COLS], char show[ROWS][COLS]);  //开始扫雷
int get_mine(char mine[ROWS][COLS], int x, int y);   //附近雷的个数

#endif//__GAME_H__

源文件

#include "game.h"

void init_game(char mine[ROWS][COLS],char show[ROWS][COLS])//初始化扫雷界面
{
    int i=0;
    int j=0;
    for(i=0;i<ROWS;i++)
    {
        for(j=0;j<COLS;j++)
        {
            mine[i][j]='0';
            show[i][j]='*';
        }
    }
}

void display(char show[ROWS][COLS])  //打印出界面
{
    int i = 0;
    int j = 0;
    printf(" ");
    for (i = 1; i < COLS - 1; i++)
    {
        printf(" %d ", i);
    }
    printf("\n");
    for (i = 1; i < ROWS - 1; i++)
    {
        printf("%d", i);
        for (j = 1; j < COLS - 1; j++)
        {
            printf(" %c ", show[i][j]);
        }
        printf("\n");
    }
}


void set_mine(char mine[ROWS][COLS])   //设置雷区
{
    int count = COUNT;
    int x = 0;
    int y = 0;
    srand((unsigned)time(NULL));
    while (count)
    {
        x = rand()%9+1;
        y = rand()%9+1;
        if (mine[x][y] == '0')
        {
            mine[x][y] = '1';
            count--;
        }
    }
}


int get_mine(char mine[ROWS][COLS], int x, int y)   //附近雷的个数
{
    int count = 0;
    if (mine[x - 1][y - 1] == '1') count++; //坐标位置的上3个
    if (mine[x - 1][y] == '1') count++;
    if (mine[x - 1][y + 1] == '1') count++;

    if (mine[x][y - 1] == '1') count++;  //坐标位置的左右
    if (mine[x][y + 1] == '1') count++;

    if (mine[x + 1][y - 1] == '1') count++; //坐标位置的下3个
    if (mine[x + 1][y] == '1') count++;
    if (mine[x + 1][y + 1] == '1') count++;

    return count;
}

void sweep(char mine[ROWS][COLS], char show[ROWS][COLS])//开始扫雷
{
    int count = 0;
    int x = 0;
    int y = 0;
    while(count!=((ROWS-2)*(COLS-2)-COUNT))
    {
        printf("\n");
        printf("请输入坐标>:");
        scanf("%d%d", &x, &y);
        if (mine[x][y] == '1')
        {
            printf("踩雷了!\n");
            display(mine);
            return;
        }
        else
        {
            int ret = get_mine(mine, x, y);
            show[x][y] = ret + '0';
            printf("\n");
            display(show);
            count++;
        }
    }
    printf("恭喜你赢了!\n");
    display(mine);
}


#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"

void game()//游戏函数
{
    char mine[ROWS][COLS];
    char show[ROWS][COLS];
    srand((unsigned)time(NULL));
    init_game(mine, show);
    display(show);
    set_mine(mine);
    sweep(mine, show);
}


void menu()//菜单函数
{
    printf("-------------------------\n");
    printf("--------- 1.play --------\n");
    printf("--------- 0.exit --------\n");
    printf("-------------------------\n");
}

int main()
{
    int input=0;
    int i=0;
    do
    {
        if(i!=0)
        {
            Sleep(5000);//延迟
        }
        system("cls");//清屏
        i=1;
        menu();
        printf("请选择>:");
        scanf("%d",&input);
        switch(input)
        {
        case 1:game();
            break;
        case 0:
            break;
        default: printf("选择错误,请重新选择\n");
            i=0;
            break;
        }
    }
    while(input);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值