写了个井字游戏,大家可以给些意见。

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
hanis_ghost
帖子: 41
注册时间: 2015-06-19 21:16

写了个井字游戏,大家可以给些意见。

#1

帖子 hanis_ghost » 2016-11-23 21:47

代码: 全选

#include<stdio.h>

int main()
{
	char chess[3][3]={'1','2','3','4','5','6','7','8','9',};
	int player1=0,player2=0,winner=0,column,line,turn=0;
	int check(char chess[3][3]);
	
	printf("Game start!\nplayer1 *\nplayer2 O\n\n");
	
	printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]);
		printf("---+---+---\n");
		printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]);
		printf("---+---+---\n");
		printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]);
	while(1)
	{
		
		
		printf("player1's turn:");
		scanf("%d",&player1);
		if(!(player1>=1)&&(player1<=9))
		{
			printf("input error\n");
			return 0;
		}
		turn++;
		column=(int)(player1-1)/3;
		line=(int)(player1-1)%3;
		chess[column][line]='*';
		printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]);
		printf("---+---+---\n");
		printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]);
		printf("---+---+---\n");
		printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]);
		if(check(chess)==1)
		{
			printf("player1 is the winner\n");
			return 1;
		}
		if(turn==9)
		{
			printf("onbody win\n");
			return 1;
		}
			printf("player2's turn:");
		scanf("%d",&player2);
		if(!(player2>=1)&&(player2<=9))
		{
			printf("input error\n");
			return 0;
		}
		column=(int)(player2-1)/3;
		line=(int)(player2-1)%3;
		chess[column][line]='O';
		turn++;
		printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]);
		printf("---+---+---\n");
		printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]);
		printf("---+---+---\n");
		printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]);
		if(check(chess)==1)
		{
			printf("player2 is the winner\n");
			return 1;
		}
		
		
		
	}
}

int check(char chess[3][3])
{
	for(int i=0;i<=2;i++)
		if(chess[i][0]==chess[i][1]&&chess[i][1]==chess[i][2])
			return 1;
		else if(chess[0][i]==chess[1][i]&&chess[1][i]==chess[2][i])
			return 1;
	 if(chess[0][0]==chess[1][1]&&chess[1][1]==chess[2][2])
			return 1;
		else if(chess[0][2]==chess[1][1]&&chess[1][1]==chess[2][0])
			return 1;
		else 
			return 0;	
}








头像
astolia
论坛版主
帖子: 6541
注册时间: 2008-09-18 13:11

Re: 写了个井字游戏,大家可以给些意见。

#2

帖子 astolia » 2016-11-24 0:23

1、上次我跟你说的同一位置重复落子的问题你还是没改
2、处理玩家输入错误应该是让其重新输入,而不是直接退出程序
3、既然你会用函数,那么就不要把同一段代码到处复制粘贴
4、
printf("onbody win\n");
onbody是谁?


如果这是个大学计算机课程作业的话,你这个代码100分里能得50分吧
回复