三子棋游戏

本文介绍了一个简单的三子棋游戏程序的设计与实现过程。主要内容包括棋盘初始化、打印、玩家及电脑下棋逻辑、胜负判断等功能。通过巧妙使用逗号表达式等技巧,实现了游戏流程的自动化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

</pre>一说到写个三子棋游戏,首先我们得想到有个棋盘初始化和棋盘打印函数。<p></p><p>接下来,玩游戏阶段,人玩游戏,电脑玩游戏,各写一个函数,每次下完棋都得判断有没有人赢了游戏,若有人赢了,游戏退出,否则继续游戏。在这过程中,还有可能棋盘满</p><p>了,所以写个判断棋盘是否满的的函数,棋盘满了,程序结束,没人赢,此时就平局。</p><p>当然了,还有一些辅助函数,比如game函数,print_manu函数,只是起到封装的作用,也防止main函数过长。</p><p>程序亮点:巧妙地运用了逗号表达式(在电脑下棋游戏中),但是,得用个计数器,while语句执行一次就好,不然电脑就耍赖了

#include"game.h"  
  
int chess_full(char chessBoard[3][3])//棋盘判满函数  
{  
    int i = 0;  
    int j = 0;  
    for (i = 0;i < 3;i++)  
    {  
        for (j = 0;j < 3;j++)  
        {  
            if (chessBoard[i][j] == ' ')  
                return 0;  
        }  
    }  
    return 1;  
}  
void init(char chessBoard[3][3])//棋盘初试化函数  
{  
    int i = 0;  
    int j = 0;  
    for (i = 0; i < 3; i++)  
    {  
        for (j = 0; j < 3; j++)  
        {  
            chessBoard[i][j] = ' ';  
        }  
    }  
  
    distchessBoard(chessBoard);  
}  
void distchessBoard(char chessBoard[3][3])//打印棋盘函数  
{  
    int i = 0;  
    for (i = 0;i < 3;i++)//打印棋盘  
    {  
        printf(" %c | %c | %c \n", chessBoard[i][0], chessBoard[i][1], chessBoard[i][2]);  
        if (i != 2)  
            printf("---|---|---\n");//pchessBoard  
    }  
}  
void manPlay(char chessBoard[3][3])//人下棋函数  
{  
    if (chess_full(chessBoard) == 1)  
    {  
        exit(0);  
    }  
    int line = 0;  
    int column = 0;  
    do  
    {  
        printf("请输入你的棋子的位置(注意最小下标是0哦):");  
        scanf("%d%d", &line, &column);  
        while (chessBoard[line][column] != ' ')  
            if ((line < 0) || (line > 2) || (column < 0) || (column > 2))  
            {  
                printf("对不起,您输入的是无效的位置!");  
            }  
        chessBoard[line][column] = 'X';  
    } while (chessBoard[line][column] == ' ');  
    distchessBoard(chessBoard);  
}  
void pcplay(char chessBoard[3][3])//电脑下棋函数  
{  
    if (chess_full(chessBoard) == 1)  
    {  
        exit(0);  
    }  
    printf("电脑下棋中...\n");  
    int line = 0;  
    int column = 0;  
    int count = 0;  
    while (line = rand() % 3, column = rand() % 3, chessBoard[line][column] == ' ')  
    {  
        count++;  
        chessBoard[line][column] = 'Y';  
        if (count == 1)  
        {  
            break;  
        }  
    }  
    distchessBoard(chessBoard);  
}  
int judge(char chessBoard[3][3])  
{  
    int i = 0;  
    if ((chessBoard[0][0] == chessBoard[1][1]) && (chessBoard[1][1] == chessBoard[2][2]))  
    {  
        if (chessBoard[1][1] == 'X')  
        {  
            printf("玩家赢了\n");  
            return 1;  
        }  
        if (chessBoard[1][1] == 'Y')  
        {  
            printf("对方赢了\n");  
            return 1;  
        }  
    }  
    if ((chessBoard[0][2] == chessBoard[1][1]) && (chessBoard[1][1] == chessBoard[2][0]))  
    {  
        if (chessBoard[1][1] == 'X')  
        {  
            printf("玩家赢了\n");  
            return 1;  
        }  
        if (chessBoard[1][1] == 'Y')  
        {  
            printf("对方赢了\n");  
            return 1;  
        }  
    }  
    for (i = 0;i < 3;i++)  
    {  
        if ((chessBoard[i][1] == chessBoard[i][0]) && (chessBoard[i][1] == chessBoard[i][2]))  
        {  
            if (chessBoard[i][1] == 'X')  
            {  
                printf("玩家赢了\n");  
                return 1;  
            }  
            if (chessBoard[i][1] == 'Y')  
            {  
                printf("对方赢了\n");  
                return 1;  
            }  
        }  
  
    }  
    for (i = 0;i < 3;i++)  
    {  
        if ((chessBoard[0][i] == chessBoard[1][i]) && (chessBoard[1][i] == chessBoard[2][i]))  
        {  
            if (chessBoard[0][i] == 'X')  
            {  
                printf("玩家赢了\n");  
                return 1;  
            }  
            if (chessBoard[0][i] == 'Y')  
            {  
                printf("对方赢了\n");  
                return 1;  
            }  
        }  
    }  
    return 0;  
}  
void game(char chessBoard[3][3])  
{  
    //char chessBoard[3][3];  
    int i = 0;  
    int ret = 0;  
    init(chessBoard);//调用初始化函数  
    while ((chess_full(chessBoard) == 0))  
    {  
        manPlay(chessBoard);  
        pcplay(chessBoard);  
        ret = judge(chessBoard);  
        if (ret == 1)  
            break;  
    }  
    if (ret == 0)  
    {  
        printf("平局\n");  
    }  
}  
  
void print_manu()  
{  
    printf("----------------欢迎进入三子棋系统-------------------\n");  
    printf("*****************************************************\n");  
    printf("**********************1.play*************************\n");  
    printf("**********************2.exit*************************\n");  
    printf("*****************************************************\n");  
[objc] view plain copy
}  
[objc] view plain copy
<pre name="code" class="objc"><span style="background-color: rgb(255, 102, 102);">game.h文件</span>  
[objc] view plain copy
</pre><pre name="code" class="objc">#define _CRT_SECURE_NO_WARNINGS 1  
#include<stdio.h>  
#include<malloc.h>  
#include<time.h>  
#include<stdlib.h>  
  
int chess_full(char chessBoard[3][3]);//棋盘判满函数  
void init(char chessBoard[3][3]);//棋盘初试化函数  
void distchessBoard(char chessBoard[3][3]);//打印棋盘函数  
void manPlay(char chessBoard[3][3]);//人下棋函数  
void pcplay(char chessBoard[3][3]);//电脑下棋函数  
int judge(char chessBoard[3][3]);//评判胜负函数  
void game(char chessBoard[3][3]);//游戏函数  
void print_manu();//打印菜单函数  

test.c文件
[objc] view plain copy
#define _CRT_SECURE_NO_WARNINGS 1  
#include"game.h"  
  
int main()  
{  
    print_manu();  
    char chessBoard[3][3];  
    printf("请输入你的选择>");  
    int choose = 0;  
    scanf("%d", &choose);  
    switch (choose)  
    {  
    case 1:  
        game(chessBoard);  
        break;  
    case 0:  
        exit(0);  
        break;  
    default:  
        printf("input error");  
    }  
    system("pause");  
    return 0;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值