主要思路:

1.采用acllib图形库的画图工具创建棋盘和棋子,并定义鼠标点击事件,用数组储存棋子坐标。

2.设置整型变量n=1,每次点击会使n+1,n为奇数时生成蓝棋,n为偶数时生成黑棋,从而控制实现了蓝棋走一步、黑棋走一步的游戏规则。

3.判胜的四种情况:横、竖、左斜、右斜。

游戏规则:

国际五子棋规则

光标控制,两颜色棋子的持有者一人一步。

使用函数:

void paintchessboard(); 作用是在可视化窗口生成了一个37列 27行的棋盘
void paintchess(); 画出棋子
void mouse(int x,int y,int button,int e);  监视光标点击事件
void win(); 用四个if语句判胜(横、竖、左斜、右斜。) 

Setup();  在acllib中相当于main函数,initWindow函数用于生成可视化窗口。

效果展示:

源码很简单只有100行,如下:

#include "acllib.h"
#include <xpolymorphic_allocator.h>
int winwidth = 800;
int winheight = 580;
int x00 = 20, y00 = 20;
int x11 = 760, y11 = 20;
int x22 = 20, y22 = 540;
int x33 = 760,y33 = 540;
void paintchessboard();
void paintchess();
void mouse(int x,int y,int button,int e);
void win();
int n = 1;
const int maxnum = 30;
int f[maxnum][maxnum];
int g[maxnum][maxnum];
 /*for (int i = 0; i < maxnum; i++)
{
	 for (int j = 0; j < maxnum; j++) { f[i][j] = 0; }
}*/
int Setup()
{
	initWindow("chessgame", DEFAULT, DEFAULT, winwidth, winheight);
	
	paintchessboard();
	//paintchess();
	
	registerMouseEvent(mouse);
	return 0;

}
void paintchessboard()//棋盘及格子坐标初始化
{
	beginPaint();
	moveTo(0, 0);
	line(x00, y00, x11 , y11);
	line(x00, y00, x22,  y22);
	line(x11, y11, x33, y33);
	line(x22, y22, x33, y33);
	for (int x = 20; x <= 760; x = x + 37)
	{	
		line(x, y00, x, y22);
	}
	for (int y = 20; y <= 540; y = y + 26)
	{
		
		
		line(x00, y, x11, y);
	}
	endPaint();

}
void paintchess() 
{
	beginPaint();
	setBrushColor(BLACK);
	ellipse(43, 33, 71, 61);
	endPaint();//外切正方形边长18
}
void mouse(int x, int y, int button, int e)
{
	if (e != BUTTON_DOWN) return;
	if (button == LEFT_BUTTON )
	{
		beginPaint();
		
		for (int i = 20; i <= 760; i = i + 37)
		{
			for (int j = 20; j <= 540; j = j + 26)
				if (x >= i - 4 && x<=i + 4 && y>=j - 4 && y <= j + 4)
				{   if(n%2==1)
					setBrushColor(BLUE);
				    else
					setBrushColor(BLACK);

					ellipse(i - 13, j - 13, i + 13, j + 13);

					int qx = (i - 20) / 37;
					int qy = (j - 20) / 26;
					if (n % 2 == 1)
					f[qx][qy] = 1;
					else
					g[qx][qy] = 1;
					//cout<<qx<<qy<<endl;
					/*char score1[20];
					char score2[20];
					setTextSize(50);
					sprintf_s(score1, "%d", qx);
					sprintf_s(score2, "%d", qy);
					paintText(400, 300, score1);
					paintText(500, 300, score2);*/
					win();
					n++;
				}
		}
	}
		endPaint();
	
	
	}
void win()
{//遍历整个棋盘
for(int i=0;i<maxnum;i++)
{
	for (int j = 0; j < maxnum; j++)
	{
		if ((f[i][j] == 1 && f[i - 1][j] == 1 && f[i - 2][j] == 1 && f[i + 1][j] == 1 && f[i + 2][j] == 1)|| (g[i][j] == 1 && g[i - 1][j] == 1 && g[i - 2][j] == 1 && g[i + 1][j] == 1 && g[i + 2][j] == 1))

		{
			setTextSize(60);
			setTextBkColor(GREEN);
			setTextColor(RED);
			paintText(500, 460, "胜负已分");
		}
		if ((f[i][j] == 1 && f[i][j-1] == 1 && f[i][j-2] == 1 && f[i][j+1] == 1 && f[i][j+2] == 1)|| (g[i][j] == 1 && g[i][j - 1] == 1 && g[i][j - 2] == 1 && g[i][j + 1] == 1 && g[i][j + 2] == 1))

		{
			setTextSize(60);
			setTextBkColor(GREEN);
			setTextColor(RED);
			paintText(500, 460, "胜负已分");
		}
	
	     if ((f[i][j] == 1 && f[i+1][j - 1] == 1 && f[i+2][j - 2] == 1 && f[i-1][j + 1] == 1 && f[i-2][j + 2] == 1) || (g[i][j] == 1 && g[i+1][j - 1] == 1 && g[i+2][j - 2] == 1 && g[i-1][j + 1] == 1 && g[i-2][j + 2] == 1))

	     {
		 setTextSize(60);
		 setTextBkColor(GREEN);
		 setTextColor(RED);
		 paintText(500, 460, "胜负已分");
	      }//左斜
		 if ((f[i][j] == 1 && f[i - 1][j - 1] == 1 && f[i - 2][j - 2] == 1 && f[i + 1][j + 1] == 1 && f[i + 2][j + 2] == 1) || (g[i][j] == 1 && g[i- 1][j - 1] == 1 && g[i -2][j - 2] == 1 && g[i + 1][j + 1] == 1 && g[i + 2][j + 2] == 1))

		 {
			 setTextSize(60);
			 setTextBkColor(GREEN);
			 setTextColor(RED);
			 paintText(500, 460, "胜负已分");
		 }//右斜
		    

	}

	}

}








五子棋游戏基本实现,但目前只能同一台设备操作,且不存在人机对战,有待开发。


版权声明:本文为qq_52506111原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_52506111/article/details/127647535