分页: 1 / 1

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

发表于 : 2016-11-23 21:47
hanis_ghost

代码: 全选

#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;	
}









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

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


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