C语言五子棋源代码

本文分享了一段使用C语言编写的五子棋游戏源代码,详细解析了游戏逻辑和实现过程,适合C语言初学者和游戏编程爱好者学习参考。

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

#include "stdio.h"
#include "math.h"
#include "windows.h"
#include "time.h"//使用当前时钟做种子

#define N 15       //定义棋盘大小  1黑2白
#define M N+8      //定义数据棋盘大小

int  chess[N][N];//棋盘,用于储存下子位置
int  count[M][M];//数据棋盘,用于储存下子位置,和盘外数据
char buffer[N*2-1][N*4-3];//缓冲器,用于储存下子位置,盘外数据,和棋盘符号
long sum[M][M] = {
  
  {0}};//得分棋盘,储存得分情况
int  p[20][5];//存储该位置的所有可能的五连珠情况

int  player = 1, ai = 2, error = -1;
int  num;//双方回合总数
int  now;//设置下棋标志
int  flag0;//确定己方已下子
int  gs;//游戏结束标志

//基础函数
void RunGame();//进行整个对局
int  Menu();//设置开始菜单
void Initialize();//初始化棋盘,数据棋盘,和缓冲器
void Print(int x, int y);//将数据写入数据棋盘,和缓冲器
void Display();//将缓冲器数据输出到屏幕
void Buf_Display();//利用双缓冲将缓冲器数据输出到屏幕
int  Judge_Display();//输出胜负情况
//双方对局函数
void PGame();//玩家对局函数
void CGame();//电脑对局函数
//输入函数
void Input(int *x, int *y);//键盘方向键控制,空格键输入
void Mark(int x0, int y0, int x, int y);//标记下子位置
//电脑判断函数
int  Basic_condition(int x, int y);//判断下子位置是否出界
void Find_point(int *pp, int *qq);//找到最佳性最优的下子位置
int  JUDGE(int x, int y);//判断胜负
void Grade();//计算所有位置的得分
int  Base(int i, int j);//坐标为(i-4,j-4)的所有五连珠情况
long Assessment(int num1, int num2);//评分标准

//主程序
int main()
{
    system("title 简易五子棋");//设置标题
    system("mode con cols=58 lines=29");//设置窗口大小
    system("color E0");//设置颜色

    Initialize();//初始化棋盘,数据棋盘,和缓冲器
    RunGame();//进行整个对局*/

    return 0;
}

/***********************************************************************************************************************************************************/
void RunGame()//进行整个对局
{
    switch(Menu())
    {
        case 1:
            while(1)
            {
                PGame();//玩家对局函数
                if(Judge_Display())
                {
                    break;//对局结束
                }
                CGame();//电脑对局函数
                if(Judge_Display())
                {
                    break;//对局结束
                }
            }//while
            break;
        case 2:
            now=ai;
            count[N/2+4][N/2+4] = now; chess[N/2][N/2] = now;
            Print(N/2, N/2);//将数据写入数据棋盘,和缓冲器
            num++;
            while(1)
            {
                PGame();//玩家对局函数
                if(Judge_Display())//玩家对局函数
                {
                    break;//对局结束
                }
                CGame();//电脑对局函数
                if(Judge_Display())
                {
                    break;//对局结束
                }
            }//while
            break;
        case 3:
            now=1;
            count[N/2+4][N/2+4] = now; chess[N/2][N/2] = now;
            Print(N/2, N/2);//将数据写入数据棋盘,和缓冲器
            num++;
            while(1)
            {
                CGame();//电脑对局函数
                if(Judge_Display())
                {
                    break;//对局结束
                }
            }
            break;
        default:;
    }
}
/***********************************************************************************************************************************************************/

int Menu()//设置开始菜单
{
    int x;
    printf("1、玩家先手\n");
    printf("2、玩家后手\n");
    printf("3、电脑自对弈\n");
    scanf("%d", &x);
    return x;
}

void Initialize()//初始化棋盘,数据棋盘,和缓冲器
{
    int i, j;

    //初始化数据棋盘
    for(i = 0; i < M; i++)
    {
        for(j = 0; j < M; j++)
        {
            if((i < 4 || i >= M - 4)&&(j < 4 || j>= M - 4))
            {
                count[i][j] = error;
            }
        }
    }
    //初始化缓冲器
    for(i = 0; i < N*2-1; i++)
  
一个很好的五子棋c语言源程序代码,最重要的是能运行正确!!! #include #include #include #include #include #define CROSSRU 0xbf /*右上角点*/ #define CROSSLU 0xda /*左上角点*/ #define CROSSLD 0xc0 /*左下角点*/ #define CROSSRD 0xd9 /*右下角点*/ #define CROSSL 0xc3 /*左边*/ #define CROSSR 0xb4 /*右边*/ #define CROSSU 0xc2 /*上边*/ #define CROSSD 0xc1 /*下边*/ #define CROSS 0xc5 /*十字交叉点*/ /*定义棋盘左上角点在屏幕上的位置*/ #define MAPXOFT 5 #define MAPYOFT 2 /*定义1号玩家的操作键键码*/ #define PLAY1UP 0x1157/*上移--'W'*/ #define PLAY1DOWN 0x1f53/*下移--'S'*/ #define PLAY1LEFT 0x1e41/*左移--'A'*/ #define PLAY1RIGHT 0x2044/*右移--'D'*/ #define PLAY1DO 0x3920/*落子--空格键*/ /*定义2号玩家的操作键键码*/ #define PLAY2UP 0x4800/*上移--方向键up*/ #define PLAY2DOWN 0x5000/*下移--方向键down*/ #define PLAY2LEFT 0x4b00/*左移--方向键left*/ #define PLAY2RIGHT 0x4d00/*右移--方向键right*/ #define PLAY2DO 0x1c0d/*落子--回车键Enter*/ /*若想在游戏中途退出, 可按 Esc 键*/ #define ESCAPE 0x011b /*定义棋盘上交叉点的状态, 即该点有无棋子 */ /*若有棋子, 还应能指出是哪个玩家的棋子 */ #define CHESSNULL 0 /*没有棋子*/ #define CHESS1 'O'/*一号玩家的棋子*/ #define CHESS2 'X'/*二号玩家的棋子*/ /*定义按键类别*/ #define KEYEXIT 0/*退出键*/ #define KEYFALLCHESS 1/*落子键*/ #define KEYMOVECURSOR 2/*光标移动键*/ #define KEYINVALID 3/*无效键*/ 下载可看到完整的...
评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值