【_Charloe原创】 2019年4月5日web

[前言]:

突发奇想,想写一个基于EGE简易五子棋的小程序。预期先写单机上的玩家对战,而后进一步写玩家联机对战,最后看看能不能写出人机大战。由于五子棋有黑棋必胜策略,因此想写一个在无禁手状况下的不可打败机器。这是也是忽然想写这个程序的初心,也是最终目的。

但基于目前的能力,只能一步一步实现。在写中学,在学中写。算法

[实现]:

先利用画矩形,画线等方法,打印棋盘。

考虑黑白棋子:这里没有用简单的画小圆做为一个棋子(太丑),而是想用打印图片的方法。但在网上没有找到合适的图片(那种没有背景,只有一个裸棋子图片才合适),因此选择本身用PS画一个(也是第一次用PS画图)。但又遇到问题:即便用PS选择透明画布,而后将棋子打印到EGE上,惊奇的发现画布居然依然被打印出来了!然而,无论用什么图片软件打开棋子图都是没有背景的。查了不少资料,想了不少方法,都没有成功解决。最后,采用了一种小聪明方法:将棋子图背景颜色设为和棋盘颜色同样,这样打印出的效果与没有棋子图背景干扰一致。(事实上,在程序被写完时发现了,一个函数能够忽略背景:putimage_transparent(); 但可能由于我第一次用PS,画出的图有些瑕疵,以前的那种方法反而更完美,因此没有改动。)

游戏胜利时,打印的图片是从王者荣耀里面扣出来的。(侵删)

这里说的都是在EGE图形化方面遇到的问题,至于算法方面,目前五子棋程序还不算很复杂,实现过程当中也没有遇到很大的困难,也就不同叙述。

最后,又遇到一些问题,如:如何将程序发布出来,在其余机器上也能运行。搞清楚怎么弄花费了很长的时间。由于按照网上的方法,设为运行库:MT,release模式,编译的时候个人VS直接报错,在MD模式又正常,我就很奇怪。又想打包成安装程序发布,又查了不少资料。结果在安装VS installer project 的时候出现安装错误,VS直接暴毙。打开VS的时候,提示缺失组件,请从新安装。(我头都要掉了)。最后修复了,决定再试一次安装VS installer project,居然就成功了。(我也不知道为啥以前错误此次成功,可能和项目路径有关)。结果又经历了重重困难(也解决了MT release 模式的问题),终于成功了。(由于个人程序要用到一些图片,因此打包成安装程序)小程序

[代码]:

由于我是分文件写的,把因此代码放上来不太方便,那就放算法的那一部分。(羞,由于感受放上来没啥用,你们都会。)

至于最终程序见最下面的连接。

【注意】:这段代码是不能直接运行的。数组

void mouse_click() {

mouse_msg msg = { 0 };

int mouse_x=0, mouse_y=0;

int mouse_down=0;

bool over_flag=0;

int temp[2][2] = { {-1,-1},{-1,-1} };//temp[0]:黑棋;temp[1]:白棋

putimage(630, 30, img_B);

for (; is_run(); delay_fps(60))

{

while (mousemsg())//获取鼠标消息,这个函数会等待,等待到有消息为止

{

msg = getmouse();//将鼠标信息存入鼠标结构体

}

if ((int)msg.is_down() == 1)

{

mouse_x = msg.x;

mouse_y = msg.y;

msg = { 0 };

for(int x=0;x<15;x++) //打印棋子位置;

for (int y = 0; y < 15; y++)

{

if (mouse_x > (10+x*40) && mouse_x < (50+x*40) && mouse_y>(10+y*40) && mouse_y < (50+y*40))

{

if (whoflag == 0 && data[x][y] == -1)

{

putimage(10+x*40, 10+y*40, img_black);

putimage(630, 30, img_W);

data[x][y] = 0;

temp[0][0] = x;

temp[0][1] = y;

over_flag = whe_over(x, y);

if (over_flag == 1)

{

game_over(data[x][y]);

return;

}

whoflag = !whoflag;

break;

}

else if(whoflag == 1 && data[x][y] == -1)

{

putimage(10+x*40, 10+y*40, img_white);

putimage(630, 30, img_B);

data[x][y] = 1;

temp[1][0] = x;

temp[1][1] = y;

over_flag = whe_over(x, y);

if (over_flag == 1)

{

game_over(data[x][y]);

return;

}

whoflag = !whoflag;

break;

}

}

}

if (mouse_x > 670 && mouse_x < 770 && mouse_y>290 && mouse_y < 325)

{

for(int m=0;m<2;m++)

if(temp[m][0]>=0&&temp[m][0]<=14&&temp[m][1]>=0&&temp[m][1]<=14)//防止越界

{

data[temp[m][0]][temp[m][1]] = -1;//清除数据

bar(10 + temp[m][0] * 40, 10 + temp[m][1] * 40, 50 + temp[m][0] * 40, 50 + temp[m][1] * 40);//清除格子

if (temp[m][0] > 0 && temp[m][0] < 14 && temp[m][1] > 0 && temp[m][1] < 14)//内部格子处理

{

line(30 + temp[m][0] * 40, 10 + temp[m][1] * 40, 30 + temp[m][0] * 40, 50 + temp[m][1] * 40);//画竖

line(10 + temp[m][0] * 40, 30 + temp[m][1] * 40, 50 + temp[m][0] * 40, 30 + temp[m][1] * 40);//画横

}

else if (temp[m][0] == 0 )//左边

{

if (temp[m][1] > 0 && temp[m][1] < 14)//中部

{

line(30, 30 + temp[m][1] * 40, 30, 10+temp[m][1]*40);

line(30, 30 + temp[m][1] * 40, 50, 30 + temp[m][1]*40);

line(30, 30 + temp[m][1] * 40, 30, 50 + temp[m][1] * 40);

}

else if (temp[m][1] == 0)//左上角

{

line(30, 30, 50, 30);

line(30, 30, 30, 50);

}

else if (temp[m][1] == 14)//左下角

{

line(30, 590, 30, 570);

line(30, 590, 50, 590);

}

}

else if (temp[m][0] == 14 )//右边

{

if (temp[m][1] > 0 && temp[m][1] < 14)

{

line(590, 30 + temp[m][1] * 40, 590, 10 + temp[m][1] * 40);

line(590, 30 + temp[m][1] * 40, 570, 30 + temp[m][1] * 40);

line(590, 30 + temp[m][1] * 40, 590, 50 + temp[m][1] * 40);

}

else if (temp[m][1] == 0)//右上角

{

line(590, 30, 570, 30);

line(590, 30, 590, 50);

}

else if (temp[m][1] == 14)//右下角

{

line(590, 590, 590, 570);

line(590, 590, 570, 590);

}

}

else if (temp[m][1] == 0)//上边 中部

{

line(30 + temp[m][0] * 40, 30, 10 + temp[m][0] * 40, 30);

line(30 + temp[m][0] * 40, 30, 30 + temp[m][0] * 40, 50);

line(30 + temp[m][0] * 40, 30, 50 + temp[m][0] * 40, 30);

}

else if (temp[m][1] == 14)//下边 中部

{

line(30 + temp[m][0] * 40, 590, 10 + temp[m][0] * 40, 590);

line(30 + temp[m][0] * 40, 590, 30 + temp[m][0] * 40, 570);

line(30 + temp[m][0] * 40, 590, 50 + temp[m][0] * 40, 590);

}

temp[m][0] = 0; //复位

temp[m][1] = 0;

}

}

}

}

}

PS: 复制个代码怎么总是出现异常。整个博客都崩溃了(无反应)。

须要的话联系我(留下联系方式),我把源码发给你。svg

PS:[2019.4.7] 更好的实现悔棋操做:直接cleardevice(),在根据data数组值打印图片就行了;这样的代码比我以前那个判断要简洁的多。函数

2019.7.12

看到有人询问源码了,很抱歉好久没有上CSDN了。如今直接把源码连接放这里吧,有须要的自取。

另外,稍稍的看了一眼本身之前写的这个代码,感受有不少瑕疵,以后可能会修改一下,而后再分享出来。如下分享的是原代码。

这个五子棋只能在本地运行,以后会分享一个联机的五子棋源码,是基于此代码实现的。

2019.8.5

此次把以前的源码作了一下改动。悔棋操做再也不是利用我贴出来的代码实现,而是直接cleardevice(),再根据data数组值打印图片,代码简洁了一些。

上面是修改后的代码,安装文件也作了相应的修改。xml

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐