MATLAB实实现现五五子子棋棋游游戏戏 ((双双人人对对战战、、可可悔悔棋棋))
本文实例为大家分享了MATLAB实现五子棋游戏的具体代码, 大家参考,具体内容如下
程序介绍:
1、此游戏只可用于双人对战。
2、棋盘颜色、棋盘格数、棋子颜色等参数均可自由设置
3、鼠标点击非棋盘区域可悔棋。
一一、、游游戏戏界界面面
二二、、主主程程序序及及函函数数
1、主程序
%Author:LeiZhen
%Date:-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,C
olor_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,Dpi
xel,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