当前位置:首页 » 《随便一记》 » 正文

【游戏】五子棋【含Matlab源码 078期】_TIQCmatlab的博客

20 人参与  2022年02月20日 17:06  分类 : 《随便一记》  评论

点击全文阅读


一、获取代码方式

获取代码方式1:
完整代码已上传我的资源:【游戏】五子棋【含Matlab源码 078期】

获取代码方式2:
通过紫极神光博客主页开通CSDN会员,凭支付凭证,私信博主,可获得此代码。

获取代码方式3:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。

备注:开通CSDN会员,仅只能免费获得1份代码(有效期为开通日起,三天内有效);
订阅紫极神光博客付费专栏,可免费获得2份代码(有效期为订阅日起,三天内有效);

二、部分源代码

function varargout = wuziqi(varargin)



gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @wuziqi_OpeningFcn, ...
                   'gui_OutputFcn',  @wuziqi_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
               
if nargin == 0  % LAUNCH GUI

	fig = openfig(mfilename,'new');

	% Use system color scheme for figure:
	%set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));

	% Generate a structure of handles to pass to callbacks, and store it. 
	handles = guihandles(fig);
	guidata(fig, handles);

	if nargout > 0
		varargout{1} = fig;
	end


end

if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

%%
% --- Executes just before wuziqi is made visible.
function wuziqi_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to wuziqi (see VARARGIN)

% Choose default command line output for wuziqi
handles.output = hObject;


handles.ncol = 32;
handles.nrow = 22;
handles.grids = zeros(handles.ncol, handles.nrow);
handles.step = [];
handles.white = 1;
handles.start = 0;
handles.hint = 0;

axes(handles.axes2);
im = imread('title.jpg');
imshow(im,[]);

axes(handles.axes3);
im = imread('demo.jpg');
imshow(im,[]);

axes(handles.axes1);
hold on
plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','w','markersize',20,'MarkerEdgeColor','k');
line ([0.9,0.9,handles.ncol+0.1,handles.ncol+0.1,0.9],[0.9,handles.nrow+0.1,handles.nrow+0.1,0.9,0.9],'color','k');
for i=1:handles.ncol
line ([i,i],[1, handles.nrow],'color','k');
end

for i=1:handles.nrow
line ([1 handles.ncol],[i,i],'color','k');
end
axis off
%title('Wu Zi Qi');
%guidata (hObject, handles);
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes wuziqi wait for user response (see UIRESUME)
% uiwait(handles.figure1);

%%
function mutual_exclude(off)
set(off,'Value',0)

%%
% --- Outputs from this function are returned to the command line.
function varargout = wuziqi_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


%%
%function figure1_CreateFcn(hObject, eventdata, handles)



%%
% --- Executes on button press in start.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%%% Start
handles.start = 1;
set(handles.radiobutton2,'Enable','off');
set(handles.radiobutton1,'Enable','off');
handles.grids = zeros(handles.ncol, handles.nrow);

handles.step = [];

axes(handles.axes1); 
hold off

if handles.white ==1
    plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','w','markersize',20,'MarkerEdgeColor','k');
else
    plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','k','markersize',20,'MarkerEdgeColor','w');
end

hold on
line ([0.9,0.9,handles.ncol+0.1,handles.ncol+0.1,0.9],[0.9,handles.nrow+0.1,handles.nrow+0.1,0.9,0.9],'color','k');
hold on
for i=1:handles.ncol
line ([i,i],[1, handles.nrow],'color','k');
end

for i=1:handles.nrow
line ([1 handles.ncol],[i,i],'color','k');
end
axis off
guidata(hObject, handles);

%%
% --- Executes on button press in Undo.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if length(handles.step)>0
   i = abs(handles.step(length(handles.step)));
   [x,y]=ind2sub([handles.ncol,handles.nrow],i);
   handles.grids(x,y) = 0;
   if length(handles.step)>1
       handles.step = handles.step(1:length(handles.step)-1);
   else
       handles.step = [];
   end 
   if handles.white == 1
       handles.white = 0;
   else
       handles.white = 1;
   end
   Draw_Chess(handles);
end

guidata(hObject, handles);


%%
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
starts=get(gca,'CurrentPoint');
Clck=starts(2,:);
x = round(Clck(1));
y = round(Clck(2));
indicator = 0;

hold on;
if (x>0)&&(x<=handles.ncol)&&(y>0)&&(y<=handles.nrow)&&(handles.start==1)    
    if handles.grids(x,y)==0
        if handles.white ==1
            %plot(x,y,'o','MarkerFaceColor','w','markersize',10,'MarkerEdgeColor','k');
            %plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','k','markersize',20,'MarkerEdgeColor','w');
            handles.white = 0;
            s = 1;
            handles.grids(x,y) = 1;
        else
            %plot(x,y,'o','MarkerFaceColor','k','markersize',10,'MarkerEdgeColor','w');
            %plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','w','markersize',20,'MarkerEdgeColor','k');
            handles.white = 1;
            s = -1;
            handles.grids(x,y) = -1;
        end
        handles.step = [handles.step, s*sub2ind([handles.ncol,handles.nrow],x,y)];
        indicator = Draw_Chess(handles);
    end    
end

if indicator ~=0
    handles.start = 0;
    set(handles.radiobutton2,'Enable','on');
    set(handles.radiobutton1,'Enable','on');
    handles.grids = zeros(handles.ncol, handles.nrow);
    handles.step = [];
end
    
guidata(hObject, handles);

%%
% --- Executes on button press in black first.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton2
handles.white=0;
plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','k','markersize',20,'MarkerEdgeColor','w');
off = [handles.radiobutton1];
mutual_exclude(off);
guidata(hObject,handles);


%%
% --- Executes on button press in white first.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1
handles.white=1;
plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','w','markersize',20,'MarkerEdgeColor','k');
off = [handles.radiobutton2];
mutual_exclude(off);
guidata(hObject,handles);


%%
% --- Executes on button press in Clear.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.start = 0;
set(handles.radiobutton2,'Enable','on');
set(handles.radiobutton1,'Enable','on');
handles.grids = zeros(handles.ncol, handles.nrow);

handles.step = [];

axes(handles.axes1); 
hold off

if handles.white ==1
    plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','w','markersize',20,'MarkerEdgeColor','k');
else
    plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','k','markersize',20,'MarkerEdgeColor','w');
end

hold on
line ([0.9,0.9,handles.ncol+0.1,handles.ncol+0.1,0.9],[0.9,handles.nrow+0.1,handles.nrow+0.1,0.9,0.9],'color','k');
%hold on
for i=1:handles.ncol
line ([i,i],[1, handles.nrow],'color','k');
end

for i=1:handles.nrow
line ([1 handles.ncol],[i,i],'color','k');
end
axis off
guidata(hObject,handles);

%%
function indicator = Draw_Chess(handles)

axes(handles.axes1);
hold off

if handles.white ==1
    plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','w','markersize',20,'MarkerEdgeColor','k');
else
    plot(handles.ncol+3.5,handles.nrow,'o','MarkerFaceColor','k','markersize',20,'MarkerEdgeColor','w');
end

hold on
line ([0.9,0.9,handles.ncol+0.1,handles.ncol+0.1,0.9],[0.9,handles.nrow+0.1,handles.nrow+0.1,0.9,0.9],'color','k');
%hold on
for i=1:handles.ncol
    line ([i,i],[1, handles.nrow],'color','k');
end

for i=1:handles.nrow
    line ([1 handles.ncol],[i,i],'color','k');
end
axis off

for j = 1:length(handles.step)
    [x,y]=ind2sub([handles.ncol,handles.nrow],abs(handles.step(j)));
    if handles.step(j)>0
        plot(x,y,'o','MarkerFaceColor','w','markersize',10,'MarkerEdgeColor','k');
    else
        plot(x,y,'o','MarkerFaceColor','k','markersize',10,'MarkerEdgeColor','w');
    end
end

三、运行结果

在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016.
[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.


点击全文阅读


本文链接:http://zhangshiyu.com/post/35050.html

代码  获取  方式  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1