利用Matlab制作三子棋游戲的示例代碼
效果:


注: 由于用uifigure和uiaxes寫(xiě)的會(huì)比較慢,改成常規(guī)的axes和figure會(huì)快很多。
完整代碼:
function OX_chess
fig=uifigure('units','pixels',...
'position',[350 100 500 500],...
'Numbertitle','off',...
'name','OX_chess',...
'Color',[1 1 1],...
'resize','off',...
'menubar','none');
ax=uiaxes('Units','pixels',...
'parent',fig,...
'Color',[0.9106 0.9773 1],...
'Position',[-22 -20 540 540],...
'XLim',[0-20 400+20],...
'YLim',[0-20 400+20],...
'XColor',[0.8706 0.9373 0.9686],...
'YColor',[0.8706 0.9373 0.9686]);
ax.Toolbar.Visible='off';
hold(ax,'on')
drawCheckerboard()
%==========================================================================
set(fig,'WindowButtonDownFcn',@buttondown)
function buttondown(~,~)
xy=get(ax,'CurrentPoint');
xp=xy(1,2);yp=xy(1,1);
Pos=[xp,yp];
Pos=round((Pos-50)./150)+1;
if map(Pos(2),Pos(1))==0
switch turn
case 1,drawO(Pos(2),Pos(1)),turn=-1;map(Pos(2),Pos(1))=1;
case -1,drawX(Pos(2),Pos(1)),turn=1;map(Pos(2),Pos(1))=-1;
end
end
judge();
end
function judge(~,~)
judge1=sum(map,1);
judge2=sum(map,2);
judge3=sum(map(eye(3)==1));
judge4=sum(map([3,5,7]));
winner=[];
switch 1
case any(judge1==3)||any(judge2==3)||judge3==3||judge4==3,winner='甜甜圈';
case any(judge1==-3)||any(judge2==-3)||judge3==-3||judge4==-3,winner='手指餅干';
end
if ~isempty(winner)
buttonName1=uiconfirm(fig,[winner,'獲得勝利'],[winner,'獲得勝利'],'Options',{'關(guān)閉','重新開(kāi)始'},'Icon','success');
if isempty(buttonName1),buttonName1='end';end
if strcmp(buttonName1,'重新開(kāi)始'),restart();
elseif strcmp(buttonName1,'關(guān)閉');delete(fig);
end
end
end
function restart(~,~)
turn=1;
map=zeros(3,3);
delete(findobj(ax,'type','patch'))
delete(findobj(ax,'type','line'))
delete(findobj(ax,'type','scatter'))
drawCheckerboard()
end
turn=1;
map=zeros(3,3);
%==========================================================================
function drawX(i,j)
x=50+150*(i-1);
y=50+150*(j-1);
Xb=[-55:1:+55,+55:-1:-55];
Xs=[-50:1:+50,+50:-1:-50];
Yb=[(-15).*ones(1,length(Xb)/2),(+15).*ones(1,length(Xb)/2)];
Ys=[(-12).*ones(1,length(Xs)/2),(+12).*ones(1,length(Xs)/2)];
Xsin=-48:0.1:48;
Ysin=sin(Xsin./2).*5;
Xp=-47:15:45;
Yp=0.*ones(size(Xp));
theta=pi/6;
fill(ax,x+Xb.*cos(theta)-Yb.*sin(theta),y+Yb.*cos(theta)+Xb.*sin(theta),[1.0000 0.9216 0.6588]);
fill(ax,x+Xs.*cos(theta)-Ys.*sin(theta),y+Ys.*cos(theta)+Xs.*sin(theta),[0.6627 0.6431 0.2745],'EdgeColor','none');
plot(ax,x+Xsin.*cos(theta)-Ysin.*sin(theta),y+Ysin.*cos(theta)+Xsin.*sin(theta),'LineWidth',2,'Color',[0.9451 0.9843 0.8471])
scatter(ax,x+Xp.*cos(theta)-Yp.*sin(theta),y+Yp.*cos(theta)+Xp.*sin(theta),10,'filled','CData',[0.9216 0.4000 0.3725])
theta=3*pi/6;
fill(ax,x+Xb.*cos(theta)-Yb.*sin(theta),y+Yb.*cos(theta)+Xb.*sin(theta),[1.0000 0.9216 0.6588]);
fill(ax,x+Xs.*cos(theta)-Ys.*sin(theta),y+Ys.*cos(theta)+Xs.*sin(theta),[0.6627 0.6431 0.2745],'EdgeColor','none');
plot(ax,x+Xsin.*cos(theta)-Ysin.*sin(theta),y+Ysin.*cos(theta)+Xsin.*sin(theta),'LineWidth',2,'Color',[0.9451 0.9843 0.8471])
scatter(ax,x+Xp.*cos(theta)-Yp.*sin(theta),y+Yp.*cos(theta)+Xp.*sin(theta),10,'filled','CData',[0.9216 0.4000 0.3725])
end
function drawO(i,j)
x=50+150*(i-1);
y=50+150*(j-1);
R=55;
t=0:0.01:2*pi;
XR=x+cos(t).*R;
YR=y+sin(t).*R;
Xr=x+cos(t).*R.*0.4;
Yr=y+sin(t).*R.*0.4;
fill(ax,[XR,Xr],[YR,Yr],[0.90 0.73 0.45],'EdgeColor',[0.67 0.42 0.15],'LineWidth',1)
[t,rL,RL]=createRandomLine_O(R);
T=[t,t(end:-1:1)];
Rr=[RL,rL];
X=x+cos(T).*Rr;
Y=y+sin(T).*Rr;
fill(ax,X,Y,[0.33 0.18 0.12],'EdgeColor','none')
candiColor=[ 0.9765 0.8353 0.4902
0.9647 0.9647 0.8314
0.1490 0.4235 0.6980
0.2431 0.4510 0.3490
0.9490 0.9647 0.9686
0.7647 0.1059 0.1569
0.2784 0.1843 0.5216
0.8824 0.6471 0.7490];
for i=1:35
t1=rand(1)*2*pi;
t2=rand(1)*2*pi;
r1=(R*0.6-5).*rand(1)+R*0.4;
x1=x+cos(t1)*r1;
y1=y+sin(t1)*r1;
x2=x1+cos(t2)*7;
y2=y1+sin(t2)*7;
plot(ax,[x1,x2],[y1,y2],'Color',candiColor(randi(size(candiColor,1)),:),'LineWidth',2);
end
end
function [t,r,R]=createRandomLine_O(RR)
t=0:0.2:2*pi;
R=(RR-3)+5.*rand(size(t));
R=interp1(t,R,0:0.01:2*pi,'spline');
r=(RR*0.4+7)-6.*rand(size(t));
r=interp1(t,r,0:0.01:2*pi,'spline');
t=0:0.01:2*pi;
end
function drawCheckerboard
[Xq,Yq]=createRandomLine_CB(440,10,5,26);
fill(ax,Xq-20,Yq+112,[0.96 0.80 0.52],'EdgeColor',[0.45 0.11 0.05].*0.8,'LineWidth',1)
[Xq,Yq]=createRandomLine_CB(440,10,5,26);
fill(ax,Xq-20,Yq+262,[0.96 0.80 0.52],'EdgeColor',[0.45 0.11 0.05].*0.8,'LineWidth',1)
[Xq,Yq]=createRandomLine_CB(440,10,5,26);
fill(ax,Yq+112,Xq-20,[0.96 0.80 0.52],'EdgeColor',[0.45 0.11 0.05].*0.8,'LineWidth',1)
[Xq,Yq]=createRandomLine_CB(440,10,5,26);
fill(ax,Yq+262,Xq-20,[0.96 0.80 0.52],'EdgeColor',[0.45 0.11 0.05].*0.8,'LineWidth',1)
[Xq,Yq]=createRandomLine_CB(440,10,2,10);
fill(ax,Xq-20,Yq+120,[0.97 0.91 0.65],'EdgeColor',[0.5,0.3 0.3],'LineWidth',1)
[Xq,Yq]=createRandomLine_CB(440,10,2,10);
fill(ax,Xq-20,Yq+270,[0.97 0.91 0.65],'EdgeColor',[0.5,0.3 0.3],'LineWidth',1)
[Xq,Yq]=createRandomLine_CB(440,10,5,10);
fill(ax,Yq+120,Xq-20,[0.97 0.91 0.65],'EdgeColor',[0.5,0.3 0.3],'LineWidth',1)
[Xq,Yq]=createRandomLine_CB(440,10,5,10);
fill(ax,Yq+270,Xq-20,[0.97 0.91 0.65],'EdgeColor',[0.5,0.3 0.3],'LineWidth',1)
end
function [Xq,Yq]=createRandomLine_CB(Lim,N,randMax,h)
X1=linspace(0,Lim,N);
X2=X1(end:-1:1);
Y1=-randMax.*rand(size(X1));
Y2=randMax.*rand(size(X2));
Xq1=0:0.1:Lim;
Yq1=interp1(X1,Y1,Xq1,'spline');
Xq2=Lim:-0.1:0;
Yq2=interp1(X2,Y2,Xq2,'spline')+h;
Xq=[Xq1,Xq2];
Yq=[Yq1,Yq2];
end
end
到此這篇關(guān)于利用Matlab制作三子棋游戲的示例代碼的文章就介紹到這了,更多相關(guān)Matlab三子棋游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言驅(qū)動(dòng)開(kāi)發(fā)之內(nèi)核解鎖與強(qiáng)刪文件
在某些時(shí)候我們的系統(tǒng)中會(huì)出現(xiàn)一些無(wú)法被正常刪除的文件,如果想要強(qiáng)制刪除則需要在驅(qū)動(dòng)層面對(duì)其進(jìn)行解鎖后才可刪掉,本文為大家介紹了內(nèi)核解鎖與強(qiáng)刪文件的方法,希望對(duì)大家有所幫助2023-06-06
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易三子棋
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易三子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
VS2019添加引用出錯(cuò):對(duì)COM組件的調(diào)用返回了錯(cuò)誤HRESULT E_FAIL(未能完成操作未指定的錯(cuò)誤)
這篇文章主要介紹了VS2019添加引用出錯(cuò):對(duì)COM組件的調(diào)用返回了錯(cuò)誤HRESULT E_FAIL(未能完成操作。未指定的錯(cuò)誤),需要的朋友可以參考下2020-07-07
C語(yǔ)言回調(diào)函數(shù)的簡(jiǎn)單運(yùn)用
回調(diào)函數(shù)就是函數(shù)指針變量作為另外一個(gè)函數(shù)的參數(shù)而使用的一種應(yīng)用情形。本文就詳細(xì)的介紹一下C語(yǔ)言回調(diào)函數(shù)的簡(jiǎn)單運(yùn)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
C++中類(lèi)型推斷(auto和decltype)的使用
在C++11之前,每個(gè)數(shù)據(jù)類(lèi)型都需要在編譯時(shí)顯示聲明,在運(yùn)行時(shí)限制表達(dá)式的值,但在C++的新版本之后,引入了 auto 和 decltype等關(guān)鍵字,本文就來(lái)介紹一下C++中類(lèi)型推斷(auto和decltype)的使用,感興趣的可以了解一下2023-12-12
C++項(xiàng)目求Fibonacci數(shù)列的參考解答
今天小編就為大家分享一篇關(guān)于C++項(xiàng)目求Fibonacci數(shù)列的參考解答,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
C++詳細(xì)分析講解函數(shù)參數(shù)的擴(kuò)展
在C++中,定義函數(shù)時(shí)可以給形參指定一個(gè)默認(rèn)的值,這樣調(diào)用函數(shù)時(shí)如果沒(méi)有給這個(gè)形參賦值(沒(méi)有對(duì)應(yīng)的實(shí)參),那么就使用這個(gè)默認(rèn)的值。也就是說(shuō),調(diào)用函數(shù)時(shí)可以省略有默認(rèn)值的參數(shù)2022-04-04
C++實(shí)現(xiàn)LeetCode(91.解碼方法)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(91.解碼方法),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

