棋游戏c语言,【C语言】3子棋游戏,

最近写了一个三子棋的游戏,然后我们来看一下。

主函数:int main()

{

int start = 1;

char lchess = 0;

char cchess = 0;

char chess[5][11];

while(start)

{

int n = 1;

int p = 1;

start = 1;

printf("---------------开始游戏-------------\n");

for(cchess = 0;cchess 

{

for(lchess = 0;lchess 

{

if(!(cchess % 2))

{

if(lchess == 3||lchess == 7)

chess[cchess][lchess] = '|';

else

chess[cchess][lchess] = ' ';

}

else

{

if(lchess == 3||lchess == 7)

chess[cchess][lchess] = '|';

else

chess[cchess][lchess] = '-';

}

}

}

while(n)

{

print(chess);

playChess(chess,&p);

if(p == 1)

computerChess(chess);

check(chess,&n);

}

print(chess);

printf("请问是否继续游戏:是1,否0\n");

scanf("%d",&start);

}

return 0;

}

玩家走棋:int playChess(char ch[5][11],int *p)

{

int i = 0;

int j = 0;

*p = 1;

printf("请走棋");

scanf("%d %d",&i,&j);

if(i <= 0||i > 3)

{

printf("输入错误\n");

*p = 0;

return 0;

}

if(j <= 0||j > 3)

{

printf("输入错误\n");

*p = 0;

return 0;

}

i = (i-1)*2;

switch(j)

{

case 1:

if(ch[i][1] != 'X'||ch[i][1] != '0')

ch[i][1] = 'X';

break;

case 2:

if(ch[i][5] != 'X'||ch[i][5] != '0')

ch[i][5] = 'X';

break;

case 3:

if(ch[i][9] != 'X'||ch[i][9] != '0')

ch[i][9] = 'X';

break;

default:

break;

}

return 0;

}

电脑走棋:如果想实现较高的AI的话,可以自己一一吧算法推导,然后添加进去,我就进行了简单的棋子填充:void computerChess(char ch[5][11])

{

int i = 0;

int j = 0;

for(i = 0;i 

{

for(j = 1;j 

{

if(ch[i][j] == 'X'||ch[i][j] == '0')

continue;

else

ch[i][j] = '0';

return;

}

}

}

胜率判断:int check(char ch[5][11],int *n)

{

int *p = n;

int i = 0;

int j = 0;

int count = 0;

for(i = 0;i 

{

for(j = 1;j 

{

if(ch[i][j] == 'X'||ch[i][j] == '0')

count++;

}

}

if(count == 9)

{

printf("棋盘已满平局,游戏结束");

*p = 0;

}

for(i = 0;i 

{

if(ch[i][1] == ch[i][5]&&ch[i][5] == ch[i][9]&&ch[i][1] != ' ')

{

if(ch[i][1] == 'X')

printf("玩家获胜\n");

else

printf("电脑获胜\n");

*p = 0;

}

}

for(j = 1;j 

{

if(ch[0][j] == ch[2][j]&&ch[2][j] ==ch[4][j]&&ch[0][j] != ' ')

{

if(ch[0][j] == 'X')

printf("玩家获胜\n");

else

printf("电脑获胜\n");

*p = 0;

}

}

if(ch[0][1] == ch[2][5]&& ch[2][5] == ch[4][9]&&ch[0][1] != ' ')

{

if(ch[0][1] == 'X')

printf("玩家获胜\n");

else

printf("电脑获胜\n");

*p = 0;

}

if(ch[4][1] == ch[2][5] &&ch[2][5]== ch[0][9]&&ch[0][9] != ' ')

{

if(ch[4][1] == 'X')

printf("玩家获胜\n");

else

printf("电脑获胜\n");

*p = 0;

}

return 0;

}

大概就这样,代码很简单,有兴趣可以自己复制试试,注意加上头文件还有函数的声明。也可以自己写一个头文件函数调用,这样调理比较清楚一点。

然后在附加一个strstr()函数的重写:#include 

char *mystrstr(const char *s1,const char *s2)

{

char *spd = (char *) s1;

if(*s1==0)//s1 = 0

{

if(*s2)//s2 = 0

return (char*)NULL;

return (char*)s1;

}

while(*spd)

{

int i=0;

while(1)

{

if(s2[i]==0)

return spd;

if(s2[i]!=spd[i])

break;//完全遍历,d e n den ,den den,输入前面。子字符大于父字符则寻找不到

i++;

}

spd++;

}

return (char*)NULL;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值