【c语言】扫雷

头文件

#ifndef  __GAME_H__
#define  __GAME_H__

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


extern int leicount;
extern int spacenumber;


#define  ROW 9
#define  COL 9
#define  ROWS  ROW+2
#define  COLS  COL+2

void InitBoard(char Board[ROWS][COLS], int  rows, int cols, char  set); 
void PrintBoard(char Board[ROWS][COLS], int row, int  col);
void SetMine(char Board[ROWS][COLS], int row, int  col);
int  GetMineCount(char  mine[ROWS][COLS], int  x, int  y);
void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);
void FineMine(char mine[ROWS][COLS], char  show[ROWS][COLS], int row, int  col);




#endif 

game.c

#include "game.h"
int leicounte;
int spacenumber;
void InitBoard(char Board[ROWS][COLS], int rows, int  cols, char  set)
{
    memset(Board, set, rows*cols*sizeof(Board[0][0]));
}

void PrintBoard(char Board[ROWS][COLS], int row, int  col)
{
    int i = 0;
    int j = 0;
    for (j = 0; j <= row; j++)
    {
        printf("%-3d", j);
    }
    printf("\n");
    for (i = 0; i < row; i++)
    {
        printf("%-2d", i+1);
        for (j = 0; j < col; j++)
        {
            printf(" %c ", Board[i][j]);

        }
        printf("\n");
    }
    printf("\n");


}


void SetMine(char Board[ROWS][COLS], int row, int  col)
{
    int  leicount = 9;
    int x = 0;
    int y = 0;
    while (leicount)
    {
        x = rand() % ROW + 1;
        y = rand() % COL + 1;
        if ( '0' == Board[x][y])
        {
            Board[x][y] = '1' ;
            leicount--;

        }

    }
}

static  int  GetMineCount(char  mine[ROWS][COLS], int  x, int  y)
{
    int minecount = 0;
    if (mine[x - 1][y] == '1')
        minecount++;
    if (mine[x - 1][y + 1] == '1')
        minecount++;
    if (mine[x][y + 1] == '1')
        minecount++;
    if (mine[x + 1][y + 1] == '1')
        minecount++;
    if (mine[x + 1][y] == '1')
        minecount++;
    if (mine[x + 1][y - 1] == '1')
        minecount++;
    if (mine[x][y - 1] == '1')
        minecount++;
    if (mine[x - 1][y - 1] == '1')
        minecount++;
    return minecount;
}

void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{

    if (0 == GetMineCount(mine, x, y))
    {

        show[x][y] = ' ';
        if (show[x][y - 1] == '*')
        {
            expand(mine, show, row, col, x, (y - 1));
        }

        if (show[x][y + 1] == '*')
        {
            expand(mine, show, row, col, x, (y + 1));
        }

        if (show[x - 1][y] == '*')
        {
            expand(mine, show, row, col, (x - 1), y);
        }

        if (show[x + 1][y] == '*')
        {
            expand(mine, show, row, col, (x + 1), y);
        }
    }
    else
    {
        show[x][y] = GetMineCount(mine, x, y) + '0';
    }

}


void FineMine(char mine[ROWS][COLS], char  show[ROWS][COLS], int row, int  col)
{
    int x = 0;
    int y = 0;
    int  leicount = 9;
    int  ret = 0;
    while (leicount)
    {
        printf("请输入你的坐标;");
        scanf_s("%d%d", &x, &y);
        printf("\n");
        ret++;

        while (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            x--;
            y--;
            if ('1' == mine[x][y])
            {
                if (1 == ret)
                {
                    printf("再给你一次机会!\n");
                    printf("\n");
                    break;
                }
                else
                {
                    printf("GAME OVER!\n");
                    printf("\n");
                    return 0;
                }
            }
            else
            {
                expand(mine, show, row, col, x, y);
                PrintBoard(show, ROW, COL);
                break;
            }

            if (0 == (x >= 1 && x <= row && y >= 1 && y <= col))
            {
                printf("输入非法!请重新输入!\n");
                printf("\n");
            }
        }
    }
    if (0 == leicount)
    {
        printf("恭喜你,扫雷成功!\n");
        printf("\n");
    }

}

test.c

#include "game.h"

int leicount;
int Spacenumber;

void  menu()
{
    printf("**************************************\n");
    printf("************** 1.play ****************\n");
    printf("************** 0.exit ****************\n");
    printf("**************************************\n");
}

void  game()
{
    char mine[ROWS][COLS] = { '0' };
    char show[ROWS][COLS] = { '0' };


    InitBoard(mine, ROW, COL, '0');
    InitBoard(show, ROWS, COLS, '*');


    PrintBoard(show, ROW, COL);

    SetMine(mine, ROW, COL);

    FineMine(mine,show,ROW,COL );
}

int  main()
{
    int  input = 0;
    srand((unsigned)time(NULL));
    do
    {
        menu();
        printf("请选择>;");
        scanf_s("%d", &input);
        switch (input)
        {
        case 1:
                game();
                break;
        case 0:
                break;
        default:
            printf("选择错误,请重新输入!");
            break;
        }
    } while (input);



    system("pause");
    return 0;



}

猜你喜欢

转载自blog.csdn.net/weixin_41892460/article/details/82711934
今日推荐