标题:[原创]自己寫的井字遊戲~~
取消只看楼主
soukaseng
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-3-13
 问题点数:0 回复次数:0 
[原创]自己寫的井字遊戲~~

#include<stdio.h>
void main()
{
char t[5][5];
int a[9]={0};
int ai(int a1[9]);
void table(char t1[5][5],int a1[9]);
int i,j,n,x;
table(t,a);
for (;;)
{
if (ai(a)<0) break;
printf("Please enter :");
for (;;)
{
scanf("%s",&n);
if ((n<'1') || (n>'9') || (a[n-'1']!=0)) printf("Wrong!!!Please enter again :"); else break;
}
a[n-'1']=1;
if (ai(a)>=0) a[ai(a)]=10;
table(t,a);
}
ai(a)==-1? printf("You win !\n"):ai(a)==-2? printf("You lose !\n"):printf("Draw!\n");
}

/* set & show table */
void table(char t1[5][5],int a1[9])
{
int i,j,k;
k=0;
for (i=0;i<=4;i++)
{
for (j=0;j<=4;j++)
{
if ((i%2)==0)
if ((j%2)==0)
{
t1[i][j]=(a1[k]==1)? 'O':a1[k]==10? 'X':k+'1';
k++;
}
else t1[i][j]='|';
else t1[i][j]=((j%2)==0)? '-':'+';
putchar(t1[i][j]);
}
putchar('\n');
}
}

/* ai */
int ai(int a1[9])
{
int c[9][5]={{0,3,6,-999,-999},{0,4,-999,-999,-999},{0,5,7,-999,-999},{1,3,-999,-999,-999},{1,4,6,7,-999},{1,5,-999,-999,-999},{2,3,7,-999,-999},{2,4,-999,-999,-999},{2,5,6,-999,-999}};
int b[8]={0};
int i,j,x,out,max;
x=0; out=-9;
for (i=0;i<=2;i++)
for (j=0;j<=2;j++)
{
b[i]+=a1[j+3*i];
b[3+i]+=a1[3*j+i];
}
b[6]=a1[0]+a1[4]+a1[8];
b[7]=a1[2]+a1[4]+a1[6];
for (j=0;j<=8;j++)
{
if (a1[j]==0)
{
c[j][4]=0;
for(i=0;i<=3;i++)
{
switch (b[c[j][i]])
{
case 20 : c[j][4]+=1000; break;
case 2 : c[j][4]+=100; break;
case 10 : c[j][4]+=20; break;
case 1 : a1[4]==1? (c[j][4]+=4):(c[j][4]-=4); break;
case 11 : c[j][4]+=0; break;
case 0 : c[j][4]+=2; break;
}
}
}
else x++;
if (b[j]==30) out=-2;
if (b[j]==3) out=-1;
}
if ((x==9) && (out==-9)) out=-3;
else if (out==-9)
{
max=c[0][4];
out=0;
for (i=1;i<=8;i++)
{
if (c[i][4]>max)
{
max=c[i][4];
out=i;
}
}
if (a1[4]==0) out=4;
}
return(out);
}

搜索更多相关主题的帖子: void include 
2007-05-21 20:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-141288-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053645 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved