matlab小游戏,MATLAB实现五子棋游戏(双人对战、可悔棋)

本文实例为大家分享了MATLAB实现五子棋游戏的具体代码,供大家参考,具体内容如下

程序介绍:

1、此游戏只可用于双人对战。

2、棋盘颜色、棋盘格数、棋子颜色等参数均可自由设置

3、鼠标点击非棋盘区域可悔棋。

一、游戏界面

06604727eb1f5010475fccbc33baadde.png

二、主程序及函数

1、主程序

%Author:LeiZhen

%Date:2018-03-12

%此程序只下五子棋,并判断胜负关系

clear all;

clc

%定义颜色

Color_QiPanBack=[135,206,255];

Color_ChessLine=[100,100,100];

%获胜连子数

Num_Victory=5;

Dpixel=33;

NumCell=14;%棋盘格的行或列数

Wid_edge=18;

[M_QiPan,xcol_ChessLine,yrow_ChessLine]=QiPan(NumCell,Dpixel,Wid_edge,Color_QiPanBack,Color_ChessLine);

imshow(M_QiPan);

set (gcf,'Position',[600,45,625,625]);

set (gca,'Position',[0,0,1,1]);

hold on,

%棋半径

radius_Chess=15;

M_LuoZi=zeros(NumCell+1,NumCell+1,2);

VictoryB=0;

VictoryW=0;

StateB=1;

StateW=2;

NumChess=0;

for i=1:(NumCell+1)^2

[x_col_Chess, y_row_Chess]=ginput_pointer(1);

%获得距离鼠标点击点最近的下棋点的坐标,并保证点击的下棋点在棋盘内

if x_col_Chessmin(xcol_ChessLine)-Dpixel/2&&y_row_Chessmin(yrow_ChessLine)-Dpixel/2

for x_i=xcol_ChessLine

if abs(x_col_Chess-x_i)

x_col_Chess=x_i;

end

end

for y_i=yrow_ChessLine

if abs(y_row_Chess-y_i)

y_row_Chess=y_i;

end

end

%点击悔棋区(棋盘外的区域)悔棋

else

[x_col_LuoZi_old,y_row_LuoZi_old]=find(M_LuoZi(:,:,2)==max(max(M_LuoZi(:,:,2))));

x_col_Chess_old=(x_col_LuoZi_old-1)*Dpixel+Wid_edge+1;

y_row_Chess_old=(y_row_LuoZi_old-1)*Dpixel+Wid_edge+1;

if NumChess>=1

M_QiPan=Chess(M_QiPan,x_col_Chess_old,y_row_Chess_old,radius_Chess,3,Wid_edge,Dpixel,Color_QiPanBack,Color_ChessLine);

imshow(M_QiPan);

NumChess=NumChess-1;

M_LuoZi(x_col_LuoZi_old,y_row_LuoZi_old,1)=0;

M_LuoZi(x_col_LuoZi_old,y_row_LuoZi_old,2)=0;

end

continue;

end

%落子并防止重复在同一个下棋点落子

x_col_LuoZi=(x_col_Chess-Wid_edge-1)/Dpixel+1;

y_row_LuoZi=(y_row_Chess-Wid_edge-1)/Dpixel+1;

if M_LuoZi(x_col_LuoZi,y_row_LuoZi,1)==0

NumChess=NumChess+1;

M_LuoZi(x_col_LuoZi,y_row_LuoZi,2)=NumChess;

if mod(NumChess,2)==1

M_QiPan=Chess(M_QiPan,x_col_Chess,y_row_Chess,radius_Chess,1,Wid_edge,Dpixel,Color_QiPanBack,Color_ChessLine);

imshow(M_QiPan);

M_LuoZi(x_col_LuoZi,y_row_LuoZi,1)=StateB; %落子为黑棋

VictoryB=Victory_Judge(M_LuoZi,x_col_LuoZi,y_row_LuoZi,StateB);

elseif mod(NumChess,2)==0

M_QiPan=Chess(M_QiPan,x_col_Chess,y_row_Chess,radius_Chess,2,Wid_edge,Dpixel,Color_QiPanBack,Color_ChessLine);

imshow(M_QiPan);

M_LuoZi(x_col_LuoZi,y_row_LuoZi,1)=StateW; %落子为白棋

VictoryW=Victory_Judge(M_LuoZi,x_col_LuoZi,y_row_LuoZi,StateW);

end

end

%显示获胜信息

if VictoryB==1

%普通对话框

h=dialog('name','对局结束','position',[500 350 250 100]);

uicontrol('parent',h,'style','text','string','黑棋获胜!','position',[35 35 200 50],'fontsize',30);

uicontrol('parent',h,'style','pushbutton','position',[150 5 80 30],'fontsize',20,'string','确定','callback','delete(gcbf)');

break;

elseif VictoryW==1

%普通对话框

h=dialog('name','对局结束','position',[500 350 250 100]);

uicontrol('parent',h,'style','text','string','白棋获胜!','position',[35 35 200 50],'fontsize',30);

uicontrol('parent',h,'style','pushbutton','position',[150 5 80 3

  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值