回复 18楼 beyondyf
我想你要求的是写一个对战平台,像QQ游戏大厅那样的平台,游戏只有一个(井字棋),双方可以写自己的算法程序在这个平台上运行,比如一个人的程序中走了这一步,另一个人的程序做出对应的动作走一步认为对自己有利的那一步,直到有一方胜利或平局结束。[ 本帖最后由 小赵q1 于 2012-6-26 15:35 编辑 ]
程序代码:#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//用循环遍历行/列/斜
int checkH(int pos, char* c, char ch) //如果存在toWin的情况,返回剩余位置i,否则返回9让程序选择随机空白位置
{
int getASpace = 0; //找到了一个空白位
int getAFriend = 0;
int count,spacePos=9;
int oldH;
for (count = 0; count!=2 ;count++ )
{
oldH = pos/3; //保存旧行数
pos++;
if (pos/3>oldH)
{
pos-=3;
}
if (c[pos]==' ')
getASpace = 1, spacePos = pos;
else if (c[pos]==ch)
getAFriend = 1;
}
if(getAFriend && getASpace)
return spacePos;
else
return 9;
}
int checkV(int pos, char* c,char ch)
{
int getASpace = 0;
int getAFriend = 0;
int count=0,spacePos=9;
for (; count!=2 ;count++ )
{
pos+=3;
if (pos/3==3)
{
pos-=9;
}
if (c[pos]==' ')
getASpace = 1, spacePos = pos;
else if (c[pos]==ch)
getAFriend = 1;
}
if(getAFriend && getASpace)
return spacePos;
else
return 9;
}
int checkX(int pos, char* c, char ch) //如果i在对角线上,需checkX() if (0==i%2),则checkX
{
int getASpace = 0;
int getAFriend = 0;
int count,spacePos=9;
if (pos%4) //check for pos 0,4,8
{
for (count = 0; count != 2 ; count++)
{
pos+=4;
if (pos == 12)
{
pos-=12;
}
if (c[pos]==' ')
getASpace = 1, spacePos = pos;
else if (c[pos]==ch)
getAFriend = 1;
}
}
else
{
for (count = 0; count != 2 ; count++)
{
pos+=2;
if (pos == 8)
{
pos-=6;
}
if (c[pos]==' ')
getASpace = 1, spacePos = pos;
else if (c[pos]==ch)
getAFriend = 1;
}
}
if(getAFriend && getASpace)
return spacePos;
else
return 9;
}
int main()
{
char c[16];
int i,pos;
printf("Beyond.Game.Tic-tac-toe.1.0\n"); //输出游戏标识
printf("My Test Bot 02\n"); //输出BOT名字
fflush(NULL); //记得每次输出后清空缓冲区,否则平台可能因为不能及时接受输出信息而判超时
gets(c); //获取自身棋子
srand((unsigned int)time(NULL));
char enemy = c[0];
// char me = (enemy=='X')?'O':'X';
for(;gets(c);)
{
for (i=0; i!=9;i++ ) //用遍历判断局势
{
if (c[i]!=enemy) //没找到敌方棋子则继续
continue;
// 找到敌方棋子了,开始判局
if (0==i%2) //对角线上的数字,需判断斜行
{
if ((pos=checkH(i,c,enemy))!= 9)
printf("%d\n",pos);
else if ((pos=checkV(i,c,enemy))!= 9)
printf("%d\n",pos);
else if ((pos=checkX(i,c,enemy))!= 9)
printf("%d\n",pos);
}
else
{
if ((pos=checkH(i,c,enemy))!= 9)
printf("%d\n",pos);
else if ((pos=checkV(i,c,enemy))!= 9)
printf("%d\n",pos);
}
fflush(0);
}
if (i==9)
{
while(c[i = rand() % 9] != ' '); //判断所选位是否为空
printf("%d\n",i); //输出你的行棋位置
fflush(0);
}
}
return 0;
}
0 1 2
。这个井字棋就可以遍历完所有的状态,我做这个游戏的意义并不在下棋的结果,而在于练习AI的编写。