0


三子棋游戏

编写此游戏最好新建三个文件:两个源文件,一个头文件

即://test.c 测试游戏的逻辑
//game.c 游戏代码的实现
//game.h 游戏代码的声明(函数声明、符号定义)

先说下编译此游戏的关键步骤思路:

1、创建一个二维数组(棋盘),并进行初始化

2、打印棋盘形状

3、玩家下棋

4、判断输赢并打印棋盘

4、电脑下棋

5、判断输赢并打印棋盘

……(以此循环,直到分出胜负或平局)

其中有五个关键的自定义函数:

初始化棋盘的函数、打印棋盘的函数、玩家下棋的函数、电脑下棋的函数、判断输赢的函数

如下:

//初始化棋盘
void initialize_chess(char chess[row][col], int row1, int col1);
//打印棋盘
void print_chess(char chess[row][col], int row1, int col1);
//玩家下棋
void player_chess(char chess[row][col],int row1,int col1);
//判断输赢
char is_win(char chess[row][col], int row1, int col1);
//电脑下棋
void computer_chess(char chess[row][col], int row1, int col1);

下面进入正题:

首先写出主函数并打印菜单:

主函数内容:

int main()
{
    int input = 0;
    srand((unsigned)time(NULL));//设置随机数的生成起点
    do
    {
        menu();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("游戏已退出\n");
            break;
        default:
            printf("选择错误,请重新选择\n");
            break;
        }
    } while(input);
    return 0;
}

打印菜单:

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

函数game是自定义的三子棋游戏实现函数,也是整个三子棋游戏代码的主体,函数内的主要代码内容编写如下:

    char ret = 0;
    char chess[row][col] = { 0 };
    //初始化棋盘
    initialize_chess(chess, row, col);
    //打印棋盘
    print_chess(chess, row, col);

初始化棋盘函数:

void initialize_chess(char chess[row][col], int row1, int col1)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row1; i++)
    {
        for (j = 0; j < col1; j++)
        {
            chess[i][j] = ' ';
        }
    }
}

打印棋盘函数:

void print_chess(char chess[row][col], int row1, int col1)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i ++ )
    {
        //打印数据
        for (j = 0; j < col; j++)
        {
            printf(" %c ", chess[i][j]);
            if (j < col - 1)
                printf("|");
        }
        printf("\n");
        //打印分割线
        if (i < row - 1)
        {
            for (j = 0; j < col ; j++)
            {
                printf("---");
                if (j < col - 1)
                    printf("|");
            }
            printf("\n");
        }
    }
}

然后便开始编写玩家与电脑博弈的while循环:

    //玩家和电脑下棋博弈
    while (1)
    {
        //玩家下棋
            player_chess(chess, row, col);
            //判断输赢
            ret = is_win(chess, row, col);
            if (ret != 'C')
                break;
            print_chess(chess, row, col);

        //电脑下棋
            computer_chess(chess, row, col);
            //判断输赢
            ret = is_win(chess, row, col);
            if (ret != 'C')
                break;
            print_chess(chess, row, col);
    }

其中的玩家下棋函数:

void player_chess(char chess[row][col], int row1, int col1)
{
    int m = 0;
    int n = 0;
    printf("玩家下棋:>\n");
    while (1)
    {
        printf("请输入坐标:>");
        scanf("%d,%d", &m, &n);
        //判断输入坐标是否合法
        if (m >= 1 && m <= row && n >= 1 && n <= col)
        {
            if (chess[m - 1][n - 1] == ' ')
            {
                chess[m - 1][n - 1] = '*';
                break;
            }
            else
                printf("坐标已占用,请重新输入\n");
        }
        else
            printf("输入坐标非法,请重新输入\n");
    }
}

电脑下棋函数:

void computer_chess(char chess[row][col], int row1, int col1)
{
    printf("电脑下棋:>\n");
    int m = 0;
    int n = 0;
    while (1)
    {
        m = rand() % row;//0~2
        n = rand() % col;//0~2
        if (chess[m][n] == ' ')
        {
            chess[m][n] = '#';
            break;
        }
    }
}

判断输赢函数:

int draw(char chess[row][col], int row1, int col1)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (chess[i][j])//有空格即还未平局
                return 0;
        }
    }
    return 1;//满格了即平局
}
char is_win(char chess[row][col], int row1, int col1)
{
    //行方向棋下满
    int i = 0;
    for (i = 0; i < row; i++)
    {
        if (chess[i][0] == chess[i][1] && chess[i][1] == chess[i][2] && chess[i][0] != ' ')
            return chess[i][0];
    }
    //列方向棋下满
    int j = 0;
    for (j = 0; j < col; j++)
    {
        if (chess[0][j] == chess[1][j] && chess[1][j] == chess[2][j] && chess[0][j] != ' ')
            return chess[0][j];
    }
    //对角线方向棋下满
    if (chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[0][0] != ' ')
        return chess[0][0];
    if (chess[0][2] == chess[1][1] && chess[1][1] == chess[2][0] && chess[0][2] != ' ')
        return chess[0][2];
    //平局(棋下满还未出胜负)
    if (draw(chess, row, col))
        return 'D';
    //游戏继续
    return 'C';
}

前面已经说明了步骤思路,这里奉上源代码更直观清晰:

test.c源文件:

#include"game.h"
void menu()
{
    printf("*****************************\n");
    printf("************1.enter**********\n");
    printf("************0.exit **********\n");
    printf("*****************************\n");
}
void game()
{
    char ret = 0;
    char chess[row][col] = { 0 };
    //初始化棋盘
    initialize_chess(chess, row, col);
    //打印棋盘
    print_chess(chess, row, col);
    //玩家和电脑下棋博弈
    while (1)
    {
        //玩家下棋
            player_chess(chess, row, col);
            //判断输赢
            ret = is_win(chess, row, col);
            if (ret != 'C')
                break;
            print_chess(chess, row, col);

        //电脑下棋
            computer_chess(chess, row, col);
            //判断输赢
            ret = is_win(chess, row, col);
            if (ret != 'C')
                break;
            print_chess(chess, row, col);
    }
    if (ret == '*')
        printf("玩家赢\n");
    else if (ret == '#')
        printf("电脑赢\n");
    else
        printf("平局\n");
    print_chess(chess, row, col);
}
int main()
{
    int input = 0;
    srand((unsigned)time(NULL));//设置随机数的生成起点
    do
    {
        menu();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("游戏已退出\n");
            break;
        default:
            printf("选择错误,请重新选择\n");
            break;
        }
    } while(input);
    return 0;
}

game.c源文件:

#include"game.h"
void initialize_chess(char chess[row][col], int row1, int col1)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row1; i++)
    {
        for (j = 0; j < col1; j++)
        {
            chess[i][j] = ' ';
        }
    }
}
void print_chess(char chess[row][col], int row1, int col1)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i ++ )
    {
        //打印数据
        for (j = 0; j < col; j++)
        {
            printf(" %c ", chess[i][j]);
            if (j < col - 1)
                printf("|");
        }
        printf("\n");
        //打印分割线
        if (i < row - 1)
        {
            for (j = 0; j < col ; j++)
            {
                printf("---");
                if (j < col - 1)
                    printf("|");
            }
            printf("\n");
        }
    }
}
void player_chess(char chess[row][col], int row1, int col1)
{
    int m = 0;
    int n = 0;
    printf("玩家下棋:>\n");
    while (1)
    {
        printf("请输入坐标:>");
        scanf("%d,%d", &m, &n);
        //判断输入坐标是否合法
        if (m >= 1 && m <= row && n >= 1 && n <= col)
        {
            if (chess[m - 1][n - 1] == ' ')
            {
                chess[m - 1][n - 1] = '*';
                break;
            }
            else
                printf("坐标已占用,请重新输入\n");
        }
        else
            printf("输入坐标非法,请重新输入\n");
    }
}
int draw(char chess[row][col], int row1, int col1)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (chess[i][j])//有空格即还未平局
                return 0;
        }
    }
    return 1;//满格了即平局
}
char is_win(char chess[row][col], int row1, int col1)
{
    //行方向棋下满
    int i = 0;
    for (i = 0; i < row; i++)
    {
        if (chess[i][0] == chess[i][1] && chess[i][1] == chess[i][2] && chess[i][0] != ' ')
            return chess[i][0];
    }
    //列方向棋下满
    int j = 0;
    for (j = 0; j < col; j++)
    {
        if (chess[0][j] == chess[1][j] && chess[1][j] == chess[2][j] && chess[0][j] != ' ')
            return chess[0][j];
    }
    //对角线方向棋下满
    if (chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[0][0] != ' ')
        return chess[0][0];
    if (chess[0][2] == chess[1][1] && chess[1][1] == chess[2][0] && chess[0][2] != ' ')
        return chess[0][2];
    //平局(棋下满还未出胜负)
    if (draw(chess, row, col))
        return 'D';
    //游戏继续
    return 'C';
}
void computer_chess(char chess[row][col], int row1, int col1)
{
    printf("电脑下棋:>\n");
    int m = 0;
    int n = 0;
    while (1)
    {
        m = rand() % row;//0~2
        n = rand() % col;//0~2
        if (chess[m][n] == ' ')
        {
            chess[m][n] = '#';
            break;
        }
    }
}

game.h头文件:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define row 3
#define col 3
//初始化棋盘
void initialize_chess(char chess[row][col], int row1, int col1);
//打印棋盘
void print_chess(char chess[row][col], int row1, int col1);
//玩家下棋
void player_chess(char chess[row][col],int row1,int col1);
//判断输赢
char is_win(char chess[row][col], int row1, int col1);
//电脑下棋
void computer_chess(char chess[row][col], int row1, int col1);

细节说明:

1、直接在源文件中引用#include"game.h"会更方便

2、电脑下棋是随机下棋,srand((unsigned)time(NULL))语句是用来生成随机数的起始位置的,要知道rand的用法

标签: 游戏程序 c语言

本文转载自: https://blog.csdn.net/lang_965/article/details/125547167
版权归原作者 冥界战士 所有, 如有侵权,请联系我们删除。

“三子棋游戏”的评论:

还没有评论