C语言基础编程实战(井字棋-待完善)

默认分类 | 2020-03-29 21:51:13 | 阅读 1003 次 | 评论(0)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//ASCII码
#define O 79
#define X 88


void Introduction(){
	printf("***************************井字棋小程序*****************************\n");
	printf("\t\t1 2 3\n\t\t4 5 6  ->玩家输入内容为数组编号\n\t\t7 8 9\n\t\t玩家player_2定义符号:O,玩家player_1定义符号:X");
	printf("\n***************************操 作 简  介*****************************\n");
	printf("\n");
}
//获胜机制
void win(char c[3][3])
{
	int i,j,count_1=0,count_2=0,count_3=0,count_4=0,count_5=0,count_6=0;
	int count_a=0,count_b=0,count_c=0,count_d=0,count_e=0,count_f=0;
	int XX=0,OO=0;

    //l,r检查(检查操作可另写函数,这里困了,不再继续研究……)
	for (i=0,j=0;i<3,j<3;i++,j++){
		//判别是否为player_2获胜
		if (c[i][0]==79 ){count_1++;}
		if (c[0][j]==79 ){count_2++;}
		if (c[i][1]==79 ){count_3++;}
		if (c[1][j]==79 ){count_4++;}
		if (c[i][2]==79 ){count_5++;}
		if (c[2][j]==79 ){count_6++;}

		//判别是否为player_1获胜
		if (c[i][0]==88 ){count_a++;}
		if (c[0][j]==88 ){count_b++;}
		if (c[i][1]==88 ){count_c++;}
		if (c[1][j]==88 ){count_d++;}
		if (c[i][2]==88 ){count_e++;}
		if (c[2][j]==88 ){count_f++;}
		
	   	
	}
	//对角线元素检查
	for (i=0,j=0;i<3,j<3;i++,j++){
		c[i][j]==79?XX++:OO++;//左斜线
	}
	//右斜线
	if(c[0][2]==79){if(c[1][1]==79){if(c[2][0]==79){printf("恭喜!player_2获胜\n");goto end;}}}
	if(c[0][2]==88){if(c[1][1]==88){if(c[2][0]==88){printf("恭喜!player_1获胜\n");goto end;}}}
	if(XX==3){printf("恭喜!player_2获胜\n");goto end;}
	if(OO==3){printf("恭喜!player_1获胜\n");goto end;}

	if(count_1==3 ||count_2==3 ||count_3==3 ||count_4==3 ||count_5==3 ||count_6==3 ){printf("恭喜!player_2获胜\n");goto end;}
	if(count_a==3 ||count_b==3 ||count_c==3 ||count_d==3 ||count_e==3 ||count_f==3 ){printf("恭喜!player_1获胜\n");goto end;}
	
	else {printf("平局!\n");}


//游戏结束
end:
	;

}

//操作模块
void games(char b[3][3])
{
	int name=1,mark,i,j;
	//int go_out;

	while(true){
		if(name%2==0){
			printf("请player_2操作!\n");
		scanf("%d",&mark);

		switch (mark){
		case(1):b[0][0]=O;break;
		case(2):b[0][1]=O;break;
		case(3):b[0][2]=O;break;
		case(4):b[1][0]=O;break;
		case(5):b[1][1]=O;break;
		case(6):b[1][2]=O;break;
		case(7):b[2][0]=O;break;
		case(8):b[2][1]=O;break;
		case(9):b[2][2]=O;break;
		default:break;
		}
		}

		else if(name%2==1){
		printf("请player_1操作!\n");
		scanf("%d",&mark);

		switch (mark){
		case(1):b[0][0]=X;break;
		case(2):b[0][1]=X;break;
		case(3):b[0][2]=X;break;
		case(4):b[1][0]=X;break;
		case(5):b[1][1]=X;break;
		case(6):b[1][2]=X;break;
		case(7):b[2][0]=X;break;
		case(8):b[2][1]=X;break;
		case(9):b[2][2]=X;break;
		default:break;
		} 
		}

		
		//打印数组
		for (i=0;i<3;i++){
			for(j=0;j<3;j++){
				printf("%c ",b[i][j]);}

		printf("\n");}
        name++;//name为操作次数
		if (name>9){printf("\n游戏结束,以下进行游戏结算!\n");goto var;}
	}
    
var:
	printf("*****************************\n");
	//获胜机制
	win(b);
	printf("本次对弈的操作次数为%d \n",name-1);

	
}

int main() { 

	char a[3][3]={{'_','_','_'},{'_','_','_'},{'_','_','_'}};
	clock_t start,end;

	start=clock();//开始计时
	Introduction();//操作介绍
	games(a);//功能函数
	end=clock();//结束计时

	printf("本次游戏的时长为: %.2f S\n",(float)(end-start)/CLOCKS_PER_SEC);
	printf("*****************************\n");
    return 0;
}




文章评论,共0条
游客请输入验证码
文章分类
最新评论