关于c语言实现的#格游戏ai代码

游戏是在#格中哪边先连成3个一样的哪边获胜。这个游戏还是比较简单的,我写完代码也赢不了我的代码了,可能是自己赢不了自己的逻辑。这个试着加了注释,但是由于我的电脑比较卡,切换输入法会死机几分钟,影响效率,所以就用了自编式英语注释了。

#include<stdio.h>
int i_victory(char (*)[3]);

/*
 * name:printf
 * func:printf now game
 * return ''
 * */
void print(char (*s)[3]){
	printf("you for '&' and i for '@'\n");
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			printf("%c ",s[i][j]);
		}
		printf("\n");
	}

}

/*
 * name:Input
 * func:Input a new hang and lie
 * return ''
 */
void Input(int *hang,int *lie){
	while(1){
		scanf("%d,%d",hang,lie);
		while((getchar()!='\n'));
		if(*hang<1||*hang>3||*lie>3||*lie<1){
			printf("error!\ninput again:");
		}else{
			break;
		}
	}
}

/*
 * name:judge
 * func:judge this input have inputed
 * right return 0
 * error return -1
 */
int judge(int *hang,int *lie,char (*s)[3]){
	if(s[*hang-1][*lie-1]=='@'||s[*hang-1][*lie-1]=='&'){
		printf("error!\ninput again>");
		return -1;
	}
	return 0;
}

/*name:judge_how_2_input
 * func:judge my 2 how input
 * return 0
 */
int judge_how_2_input(char (*s)[3]){
	int i=0,j=0,k=0,y=2;
	static int z=0;
	if(z)	
		y=i_victory(s);
	z++;
	if(y){
		for(k=0;k<3;k++){
			if(s[k][j]==s[k][j+1]&&s[k][j]=='&'&&s[k][j+2]==' '){
				s[k][j+2]='@';
				return 0;
			}else if(s[k][j]==s[k][j+2]&&s[k][j]=='&'&&s[k][j+1]==' '){
				s[k][j+1]='@';
				return 0;
			}else if(s[k][j+1]==s[k][j+2]&&s[k][j+1]=='&'&&s[k][j]==' '){
				s[k][j]='@';
				return 0;
			}
			if(s[i][k]==s[i+1][k]&&s[i][k]=='&'&&s[i+2][k]==' '){
				s[i+2][k]='@';
				return 0;
			}else if(s[i][k]==s[i+2][k]&&s[i][k]=='&'&&s[i+1][k]==' '){
				s[i+1][k]='@';
				return 0;
			}else if(s[i+1][k]==s[i+2][k]&&s[i+1][k]=='&'&&s[i][k]==' '){
				s[i][k]='@';
				return 0;
			}
		}
		if(s[1][1]==s[0][2]&&s[1][1]=='&'&&s[2][0]==' '){
			s[2][0]='@';
		}else if(s[1][1]==s[2][0]&&s[1][1]=='&'&&s[0][2]==' '){
			s[0][2]='@';
		}else{
			if(z==2&&s[0][1]=='&'&&s[1][0]==' '){
				s[1][0]='@';
				return 0;
			}
			for(int i=0;i<3;i++){
				for(int j=0;j<3;j++){
					if(s[i][j]==' '){
						s[i][j]='@';
						return 0;
					}
				}
			}
		}
	}
}

/*name:input_1_n
 * func 1 after input
 * return ''
 */
void input_1_n(int *hang,int *lie,char (*s)[3]){
	printf("please input(x,x   1<=x<=3)>");
	while(1){
		Input(hang,lie);
		int ret=judge(hang,lie,s);
		if(ret==0){
			s[*hang-1][*lie-1]='&';
			break;
		}
	}
}

/*
 * name:victory
 * func:judge who victory
 * return 0
 */
int victory(char (*s)[3]){
	int j=0,i=0;
	for(i=0;i<3;i++){
		if(s[i][j]==s[i][j+1]&&s[i][j]==s[i][j+2]){
			if(s[i][j]=='&'){
				printf("you win!\n");
				return 0;
			}else{
				printf("you lose!\n");
				return 0;
			}
		}
	}
	for(i=0;i<3;i++){
		if(s[j][i]==s[j+1][i]&&s[j][i]==s[j+2][i]){
			if(s[j][i]=='&'){
				printf("you win!\n");
				return 0;
			}else{
				printf("you lose!\n");
				return 0;
			}
		}
	}
	if((s[0][0]==s[1][1]&&s[0][0]==s[2][2])||(s[1][1]==s[0][2]&&s[1][1]==s[2][0])){
		if(s[0][0]=='&'){
			printf("you win!\n");
			return 0;
		}else{
			printf("you lose!\n");
			return 0;
		}
	}
	return 1;
}

/*name:i_victory
 * func:i goto victory
 * return 0
 */
int i_victory(char (*s)[3]){
	int i=0,j=0,k=0;
	for(k=0;k<3;k++){
		if(s[k][j]==s[k][j+1]&&s[k][j]=='@'&&s[k][j+2]==' '){
			s[k][j+2]='@';
			return 0;
		}else if(s[k][j]==s[k][j+2]&&s[k][j]=='@'&&s[k][j+1]==' '){
			s[k][j+1]='@';
			return 0;
		}else if(s[k][j+1]==s[k][j+2]&&s[k][j+1]=='@'&&s[k][j]==' '){
			s[k][j]='@';
			return 0;
		}
		if(s[i][k]==s[i+1][k]&&s[i][k]=='@'&&s[i+2][k]==' '){
			s[i+2][k]='@';
			return 0;
		}else if(s[i][k]==s[i+2][k]&&s[i][k]=='@'&&s[i+1][k]==' '){
			s[i+1][k]='@';
			return 0;
		}else if(s[i+1][k]==s[i+2][k]&&s[i+1][k]=='@'&&s[i][k]==' '){
			s[i][k]='@';
			return 0;
		}
	}
	if(s[1][1]==s[0][2]&&s[1][1]=='&'&&s[2][0]==' '){
		s[2][0]='@';
		return 0;
	}else if(s[1][1]==s[2][0]&&s[1][1]=='&'&&s[0][2]==' '){
		s[0][2]='@';
		return 0;
	}	
	return 1;
}
int main(int argc, const char *argv[])
{
	int hang=0,lie=0;
	char s[3][3]={' ',' ',' ',' ',' ',' ',' ',' ',' '};
	int chose=0,outs=2;
	printf("please chose (1 for you 2 for me)>");
	while(1){
		scanf("%d",&chose);
		while((getchar()!='\n'));
		if(chose==1){
			printf("please input(x,x   1<=x<=3)>");
			Input(&hang,&lie);
			s[hang-1][lie-1]='&';
			if(hang!=2||lie!=2){
				s[1][1]='@';
			}else{
				s[0][0]='@';
			}
			print(s);
			input_1_n(&hang,&lie,s);
			judge_how_2_input(s);
			print(s);
			input_1_n(&hang,&lie,s);
			judge_how_2_input(s);
			print(s);
			outs=victory(s);
			if(outs==0) return 0;
			input_1_n(&hang,&lie,s);
			judge_how_2_input(s);
			print(s);
			outs=victory(s);
			if(outs==0) return 0;
			input_1_n(&hang,&lie,s);
			print(s);
			outs=victory(s);
			if(outs) printf("ping\n");
			return 0;
		}else if(chose==2){
			s[1][1]='@';
			print(s);
			input_1_n(&hang,&lie,s);
			judge_how_2_input(s);
			print(s);
			input_1_n(&hang,&lie,s);
			judge_how_2_input(s);
			print(s);
			outs=victory(s);
			if(outs==0) return 0;
			input_1_n(&hang,&lie,s);
			judge_how_2_input(s);
			print(s);
			outs=victory(s);
			if(outs==0) return 0;
			input_1_n(&hang,&lie,s);
			print(s);
			outs=victory(s);
			if(outs) printf("ping\n");
			return 0;
		}else{
			printf("error!\nchose again>");
		}
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值