简单扫雷游戏的实现

来源:互联网 发布:网络对青少年的好处 编辑:程序博客网 时间:2024/04/30 13:51

扫雷游戏的实现我采用多个源文件的编写方法,实现代码的分模块化编写,这样不仅代码清晰,且加强代码的理解性。
建议读者在实现代码时,不可心急求成,一次就要完成代码的所有函数逻辑部分,而是先编写大体代码框架,一步步思考代码的实现所需要的函数实现,进一步完善代码的函数主体。

代码头文件部分:game.h

#ifndef __GAME_H__#define __GAME_H__#define ROWS 3#define COLS 3enum GAME{    PLAY=1,    EXIT=0};void init_board(char board[ROWS][COLS]);//初始化棋盘void print_board(char board[ROWS][COLS]);//打印棋盘void player_move(char board[ROWS][COLS]);//玩家玩void computer_move(char board[ROWS][COLS]);//电脑玩char check_win(char board[ROWS][COLS]);//判断输赢int is_full(char board[ROWS][COLS]);//判断棋盘是否满void play_game(char board[ROWS][COLS]);//玩游戏void first_mine(char board[ROWS][COLS]);//第一次若踩雷,转移雷的位置#endif

游戏代码实现部分:game.c

#include "game.h"#include <stdio.h>void init_board(char board[ROWS][COLS]){    int i = 0;    int j = 0;    for (i = 0; i < ROWS; i++)    {        for (j = 0; j < COLS; j++)        {            board[i][j] = ' ';        }    }}void print_board(char board[ROWS][COLS]){    int  i = 0;    for (i = 0; i < ROWS; i++)    {        printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);        if (i<2)            printf("---|---|---\n");    }}void player_move(char board[ROWS][COLS]){    int x = 0;    int y = 0;    printf("玩家玩>:");    while (1)    {        scanf("%d%d", &x, &y);        if ((board[x - 1][y - 1] == ' ') && (x-1>=0) && (x-1<ROWS) && (y-1>=0) && (y-1<COLS))        {            board[x - 1][y - 1] = 'X';            break;        }        printf("继续输入\n>:");    }}void computer_move(char board[ROWS][COLS]){    int x = 0;    int y = 0;    printf("电脑玩\n");    while (1)    {        x = rand() % 3;        y = rand() % 3;        if (board[x][y] == ' ')        {            board[x][y] = '0';            break;        }    }}int is_full(char board[ROWS][COLS]){    int i = 0;    int j = 0;    for (i = 0; i < ROWS; i++)    {        for (j = 0; j < COLS; j++)        {            if (board[i][j] ==' ')                return 1;//棋盘未满        }    }    return 0;//棋盘已满}char check_win(char board[ROWS][COLS]){    int i = 0;    for (i = 0; i < ROWS; i++)    {        if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]))        {            return board[i][0];        }    }    for (i = 0; i < COLS; i++)    {        if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]))        {            return board[0][i];        }    }    if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))    {        return board[0][0];    }    if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]))    {        return board[1][1];    }    if (is_full(board) == 0)        return 'q';    else        return ' ';}void first_mine(char board[ROWS][COLS])//第一次若踩雷,转移雷的位置{    while (1)    {        int x = 0;        int y = 0;        x = rand() % 3;        y = rand() % 3;        if (board[x][y] == '0')        {            board[x][y] = '1';            break;        }    }}void play_game(char board[ROWS][COLS]){    char ret = 0;    init_board(board);    print_board(board);    while (1)    {        player_move(board);        first_mine(board);        print_board(board);        if (check_win(board) == 'X')        {            ret = 'X';            break;        }        if (check_win(board) == 'q')        {            ret = 'q';            break;        }        computer_move(board);        print_board(board);        if (check_win(board) == '0')        {            ret = '0';            break;        }        if (check_win(board) == 'q')        {            ret = 'q';            break;        }    }    if (ret = 'X')    {        printf("恭喜你,你赢了\n");    }    else if (ret = '0')    {        printf("电脑赢了\n");    }    else    {        printf("平局\n");    }}

测试函数代码:test.c

#include "game.h"#include <stdio.h>#include <stdlib.h>#include <time.h>void menu(){    printf("***********************************\n");    printf("**********     1.play    **********\n");    printf("**********     0.exit    **********\n");    printf("***********************************\n");}void game(){    char board[ROWS][COLS];    int input = 1;    srand((unsigned)time(NULL));    do    {        menu();        printf("请输入>:");        scanf("%d", &input);        switch (input)        {        case PLAY:            play_game(board);            break;        case EXIT:            break;        }    } while (input);}int main(){    game();    system("pause");    return 0;}

代码可以简单实现扫雷游戏,读者可根据具体游戏要求自行优化代码。

0 0