栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

学完c语言基础的小练习——井字棋

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学完c语言基础的小练习——井字棋

 这个项目适合大一上新生在完成新课以后尝试,大概时间是在12月份以后。

其中没有任何涉及的数据结构的用法,更多的是设计结构的考察。

本文的代码是使用的MVC设计模式(当然是极其简化的,作者水平不高也是大一新生)。

而对于这个项目本身,边界处理和特殊情况的考虑是比较重要的,具体细节在下面的代码里面有所体现。

肯定会有更好的胜利条件,即本文judge函数,这就需要读者自己思考了。(比如回朔法?)

这串代码在我的电脑上能够流畅运行。

#include
#define lone 1
#define two 0

void jingplacedraw(int a[])
{
	int hang,lie,flag = lone,i = 1;
	for(hang = 0;hang < 7;hang++)
	{
		for(lie = 0;lie < 5;lie++)
		{
			if( flag == lone)
			{
				printf("__");
			}
			else
			if(lie % 2 == 0)
			{
			if(a[i] >= 0)
			printf(" %d|",a[i]);
			else
			printf("  |");
			i++;
		    }
		}
		printf("n");
		(flag == lone) ? (flag = two):(flag = lone);
	}
}

int judge(int a[])
{
	int tag = -1;
	int place;
	for(place = 1;place < 10;place+=3)
	{
		if(a[place] == a[place + 1] && a[place] == a[place + 2])
		{
			if( a[place] == 0)
			tag = 0;
			else
			tag = 1;
		}
	}
	for(place = 1;place < 4;place+=1)
	{
		if(a[place] == a[place + 3] && a[place] == a[place + 6])
		{
			if( a[place] == 0)
			tag = 0;
			else
			tag = 1;
		}
	}
	
	place = 1;
	if(a[place] == a[place + 4] && a[place] == a[place + 8] )
	{		
			if( a[place] == 0)
			tag = 0;
			else
			tag = 1;
	}
	
	place = 1;
	if(a[place + 2] == a[place + 4] && a[place + 2] == a[place + 6])
	{
			if( a[place + 2] == 0)
			tag = 0;
			else
			tag = 1;
	}
	return tag;
}

void competation(int a[])
{
   	int time = -1;
   	int tern = 0;
   	int timenow = 0;
   	int qi;
   	while(time == -1 && timenow < 9)
   	{
	jingplacedraw(a);
	printf("按数字1~9则可在对应位点下棋(你不会不懂对应位子是什么意思吧?)n");
	printf("现在轮到%d选手下棋n",tern);
	scanf("%d",&qi);
	if(qi > 0 && qi < 10)
	{
	if(a[qi] < 0)
	{
	a[qi] = tern;
	(tern == 0)?(tern = 1):(tern = 0);
   	time = judge(a);
   	timenow++;
   }
   else
   printf("你下的位置已经有东西了,重新下一次n");
 }
 else
 printf("你输入的数字不在棋盘里面,重新下一次n");
}
    jingplacedraw(a);
    time = judge(a);
    if(time >= 0)
	printf("获胜的是%d号选手n",time);
	else
	printf("好遗憾哟,这局是平局n");
	
}

int main()
{
int a[10] = {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10};
int name;
printf("来一把井字棋?n先手是0号选手,后手是1号选手n");
competation(a);	
} 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664512.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号