欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

人人对战五子棋

程序员文章站 2024-03-18 13:57:40
...
C语言人人对战五子棋
 计算机小白经过了视频学习绘制图形库,全部内容纯手撸出来的C语言人人对战五子棋。
 虽然代码不长但是核心的判断输赢的算法是自己想出来的,图形界面是利用easyx库,播放音乐使用的也是库。(其中的所有图片和音乐均需要放在源文件中)。
  看起来五子棋很简单,但是对于其中有许多需要考虑的因素,所以前前后后大概花了有24小时左右的时间,找bug的时间应该花的是最多的,另外主要是自己的思考非常重要,不能忙碌的复制别人的代码,别人每一行代码都需要弄清楚,不是简单意义上的搬砖,那样对于自己是没有一点成长的。
  以下附上我的代码(c++中运行的c语言文件)
  原谅我的直男视角,界面有点丑,之后在我弄懂人机对战和网络对战,深度学习后,会继续更新完善我的五子棋。

  有好的想法或者看不懂的地方可以互相交流,感谢。

#include <graphics.h>      // 引用图形库头文件
#include <conio.h>         //鼠标落子
#include <mmsystem.h>
#include <windows.h>
#include <math.h>
# define N 27			//落子定位数组的大小
#pragma comment(lib,"winmm.lib")//音频库	
int chess[N][N];	
void messagebox ();
void playgame ();
void wingame ();
void gamebegin()

{
	IMAGE backgroundImage;//存照片
	loadimage (&backgroundImage,"background.jpg");
	int width = backgroundImage.getwidth();//定义长与宽
	int height = backgroundImage.getheight();
	initgraph(width = 508,height = 300);
	putimage( 0, 0, &backgroundImage);//输出照片
	PlaySound("1.wav", NULL, SND_ALIAS | SND_ASYNC);
	settextstyle (30,0,"楷体");
	while (!_kbhit())
	{
		settextcolor(WHITE);
		outtextxy( 0, 0,"来和老夫下盘棋吧!按下任意键开始");
		Sleep(500);
	}
	
}
void Gamebackground()//棋盘的背景
{
	IMAGE Gamebackground;
	loadimage (&Gamebackground,"Gamebackground.jpg");
	int width = Gamebackground.getwidth();
	int height = Gamebackground.getheight();
	initgraph(width = 600,height = 452);
	putimage( 0, 0, &Gamebackground);
	messagebox();
	playgame();
//	getchar();
	
		
	
	
}
void messagebox()//提示框
{
	setlinecolor(WHITE);
	setfillcolor(BLACK);
	fillrectangle(450, 0, 580, 60);
	settextstyle(20,0,"楷体");
	outtextxy(451 ,0 ,"黑方:玩家一");
	settextcolor(WHITE);
	outtextxy(451 ,30 ,"白方:玩家二");
	settextcolor(WHITE);
}
	
void playgame()
{
	//int chess[N][N]={0};
	
	while(true)
	{
		int q=20;
		int p=30;
		int a=0;
		int b=0;
		MOUSEMSG m;
		m=GetMouseMsg();//获取鼠标信息
		switch(m.uMsg)
		{
		int i,j;
		case WM_LBUTTONDOWN://左鼠标落子
			if(m.x>=0&&m.x<=450+17&&m.y>=0&&m.y<=450+17)
			{
				for(a=0;a<15;a++)
				{
					for(b=0;b<15;b++)
					{
						if(m.x>=abs(17+p*a-q)&&m.x<=abs(17+p*a+q)&&m.y>=abs(17+p*b-q)&&m.y<=abs(17+p*b+q))
						{
							m.x = 17+p*a;
							m.y = 17+p*b;
							i = a;
							j = b;
						}
					}
				}	
				if(chess[i][j]!=0)
					continue;
				setlinecolor(BLACK);
				setfillcolor(BLACK);
				fillcircle(m.x,m.y,8);
				chess[i][j]=1;
				wingame();
			}break;
		case WM_RBUTTONDOWN://右鼠标落子
			if(m.x>=5&&m.x<=450+17&&m.y>=5&&m.y<=450+17)		
			{	
				for(a=0;a<15;a++)
				{	
					for(b=0;b<15;b++)
					{
						if(m.x>=abs(17+p*a-q)&&m.x<=abs(17+p*a+q)&&m.y>=abs(17+p*b-q)&&m.y<=abs(17+p*b+q))
						{
							m.x = 17+p*a;
							m.y = 17+p*b;
							i = a;
							j = b;
						}
					}
				}
				if(chess[i][j]!=0)
					continue;
				setlinecolor(WHITE);
				setfillcolor(WHITE);
 				fillcircle(m.x,m.y,8);
			    chess[i][j]=2;
				wingame();
			}break;
		}
    	//wingame();
	}

}
void wingame()//判断输赢
{	
	int i=0;
	int j=0;
	//MOUSEMSG m;
	//m=GetMouseMsg();
	//if(m.x>=0&&m.x<=450+17&&m.y>=0&&m.y<=450+17)
	{
		for(i=0;i<15;i++)
			{
			for(j=0;j<15;j++)
				{
					if(chess[i][j]==1&&chess[i+1][j]==1&&chess[i+2][j]==1&&chess[i+3][j]==1&&chess[i+4][j]==1)
					{
						settextcolor(WHITE);
						outtextxy(200,200 ,"the black win");
					}
					else if(chess[i][j]==1&&chess[i+1][j+1]==1&&chess[i+2][j+2]==1&&chess[i+3][j+3]==1&&chess[i+4][j+4]==1)
					{
						outtextxy(200 ,200 ,"the black win");
	                    settextcolor(WHITE);
					}
					else if(chess[i][j+1]==1&&chess[i][j+2]==1&&chess[i][j+3]==1&&chess[i][j]==1&&chess[i][j+4]==1)
					{
						outtextxy(200 ,200,"the black win");
						settextcolor(WHITE);
					}
					
					else if(i>3&&chess[i][j]==1&&chess[i+1][j-1]==1&&chess[i+2][j-2]==1&&chess[i+3][j-3]==1&&chess[i+4][j-4]==1)
					{
						outtextxy(200 ,200 ,"the black win");
						settextcolor(WHITE);
					}
					else if(j>3&&chess[i][j]==1&&chess[i+1][j-1]==1&&chess[i+2][j-2]==1&&chess[i+3][j-3]==1&&chess[i+4][j-4]==1)
					{
						outtextxy(200 ,200 ,"the black win");
						settextcolor(WHITE);
					}
					else if(chess[i][j]==2&&chess[i+1][j]==2&&chess[i+2][j]==2&&chess[i+3][j]==2&&chess[i+4][j]==2)
					{
						settextcolor(WHITE);
						outtextxy(200,200 ,"the  white win");
					}
					else if(chess[i][j]==2&&chess[i+1][j+1]==2&&chess[i+2][j+2]==2&&chess[i+3][j+3]==2&&chess[i+4][j+4]==2)
					{
						outtextxy(200 ,200 ,"the white win");
	                    settextcolor(WHITE);
					}
					else if(chess[i][j+1]==2&&chess[i][j+2]==2&&chess[i][j+3]==2&&chess[i][j]==2&&chess[i][j+4]==2)
					{
						outtextxy(200 ,200,"the white win");
						settextcolor(WHITE);
					}
					
					else if(i>3&&j>3&&chess[i][j]==2&&chess[i+1][j-1]==2&&chess[i+2][j-2]==2&&chess[i+3][j-3]==2&&chess[i+4][j-4]==2)
					{
						outtextxy(200 ,200 ,"the white win");
						settextcolor(WHITE);
					}
					else if(j>3&&chess[i][j]==2&&chess[i+1][j-1]==2&&chess[i+2][j-2]==2&&chess[i+3][j-3]==2&&chess[i+4][j-4]==2)
					{
						outtextxy(200 ,200 ,"the white win");
						settextcolor(WHITE);
					}
			}
		}	


	}
}

int main()
{
	gamebegin();
	Gamebackground();
    return 0;
}
人人对战五子棋人人对战五子棋