基于Matlab實(shí)現(xiàn)中國(guó)象棋的示例代碼
更新時(shí)間:2022年02月08日 10:30:43 作者:abcwsp
中國(guó)象棋是起源于中國(guó)的一種棋,屬于二人對(duì)抗性游戲的一種,在中國(guó)有著悠久的歷史。由于用具簡(jiǎn)單,趣味性強(qiáng),成為流行極為廣泛的棋藝活動(dòng)。本文將利用Matlab實(shí)現(xiàn)這一游戲,需要的可以參考一下
設(shè)置變量
nRowNum = 8; % 畫布行數(shù)
nColNum = 9; % 畫布列數(shù)
offset_x = 0;% 紅車坐標(biāo)起點(diǎn)
offset_y = 0;% 紅車坐標(biāo)起點(diǎn)
chess_name = {{'帥','仕','相','馬','車','炮','兵'},{'將','仕','象','馬','車','炮','卒'}};
chess_type = [5 4 3 2 1 2 3 4 5 6 6 7 7 7 7 7]; % 存儲(chǔ)下棋類型
colors = 'rk';
% global variables
chess_x = -ones(2,16);
chess_y = -ones(2,16);
pos_chess = zeros(nRowNum+1,nColNum+1);% 存儲(chǔ)棋子位置
cur_turn = 1; % cur_turn為紅色表示1
cur_cid = 0;
hText = zeros(2,16);
繪圖
繪制棋盤

function DrawBoard()
for k = 1:2
for r = 1:nRowNum+1
x = [(k-1)*5 4+(k-1)*5];
y = [(r-1) (r-1)];
plot(x,y,'b-')
end
for c = 1:nColNum+1
x = [(c-1) (c-1)];
y = [0 nRowNum];
plot(x,y,'b-')
end
x = [0 2] + (k-1)*7;
y = [3 5];
plot(x,y,'b-')
x = [0 2] + (k-1)*7;
y = [5 3];
plot(x,y,'b-')
end
text(4.5,1.2,'楚 河 漢 界','rotation',90)
end 繪制棋子

function DrawAllChess()
for k = 1:2
for i = 1:16
if i>9
plot(chess_x(k,i),chess_y(k,i),'d','MarkerSize',15)
end
h(k,i)=plot(chess_x(k,i),chess_y(k,i),'MarkerSize',70,'Color',colors(k),'marker','.');%改進(jìn)
hText(k,i) = text(chess_x(k,i)-0.35,chess_y(k,i),['\fontsize{20}' chess_name{k}{chess_type(i)}],'color','w');
end
end
DrawStart
DrawEnd
DrawHelp
end
棋子移動(dòng)規(guī)則
判斷是否可以移動(dòng)
function flag = CanMove(x,y)
flag = 1;
oldx = chess_x(cur_turn,cur_cid);
oldy = chess_y(cur_turn,cur_cid);
switch chess_type(cur_cid)
case 1% 將
% move 1 step
if ~(x==oldx && abs(y-oldy)==1) && ~(y==oldy && abs(x-oldx)==1)
flag = 0;
return
end
% out area
if cur_turn==1
if ~(x>=0 && x<=2 && y>=3 && y<=5)
flag = 0;
return
end
else
if ~(x>=7 && x<=9 && y>=3 && y<=5)
flag = 0;
return
end
end
case 2% 士
% move 1 step
if ~(abs(x-oldx)==1 && abs(y-oldy)==1)
flag = 0;
return
end
% out area
if cur_turn==1
if ~(x>=0 && x<=2 && y>=3 && y<=5)
flag = 0;
return
end
else
if ~(x>=7 && x<=9 && y>=3 && y<=5)
flag = 0;
return
end
end
case 3% 象
% move 1 step
if ~(abs(x-oldx)==2 && abs(y-oldy)==2)
flag = 0;
return
end
% out area
if cur_turn==1
if ~(x>=0 && x<=4)
flag = 0;
return
end
else
if ~(x>=5 && x<=9)
flag = 0;
return
end
end
% in the way
mx = (x+oldx)/2;
my = (y+oldy)/2;
if pos_chess(my+1,mx+1)~=0
flag = 0;
return
end
case 4% 馬
% move 1 step
if ~(abs(x-oldx)==1 && abs(y-oldy)==2) && ~(abs(x-oldx)==2 && abs(y-oldy)==1)
flag = 0;
return
end
% in the way
if abs(y-oldy)==2
mx = oldx;
my = (y+oldy)/2;
else
mx = (x+oldx)/2;
my = oldy;
end
if pos_chess(my+1,mx+1)~=0
flag = 0;
return
end
case 5% 車
if ~(x==oldx && y~=oldy) && ~(x~=oldx && y==oldy)
flag = 0;
return
end
% no chess in the way
if x==oldx
inc = 1;
if oldy>y
inc = -1;
end
if ~isempty(find(pos_chess(oldy+1+inc:inc:y+1-inc,x+1)~=0))
flag = 0;
return
end
else
inc = 1;
if oldx>x
inc = -1;
end
if ~isempty(find(pos_chess(y+1,oldx+1+inc:inc:x+1-inc)~=0))
flag = 0;
return
end
end
case 6% 炮
if ~(x==oldx && y~=oldy) && ~(x~=oldx && y==oldy)
flag = 0;
return
end
% no chess in the way
if x==oldx
inc = 1;
if oldy>y
inc = -1;
end
if pos_chess(y+1,x+1)~=0
if ~(length(find(pos_chess(oldy+1+inc:inc:y+1-inc,x+1)~=0))==1)
flag = 0;
return
end
else
if ~(isempty(find(pos_chess(oldy+1+inc:inc:y+1-inc,x+1)~=0)))
flag = 0;
return
end
end
else
inc = 1;
if oldx>x
inc = -1;
end
if pos_chess(y+1,x+1)~=0
if ~(length(find(pos_chess(y+1,oldx+1+inc:inc:x+1-inc)~=0))==1)
flag = 0;
return
end
else
if ~(isempty(find(pos_chess(y+1,oldx+1+inc:inc:x+1-inc)~=0)))
flag = 0;
return
end
end
end
case 7% 兵
if cur_turn==1
if oldx<=4
if ~(y==oldy&&x-oldx==1)
flag = 0;
return
end
else% pass river
if ~(y==oldy&&x-oldx==1) && ~(abs(y-oldy)==1&&x==oldx)
flag = 0;
return
end
end
else
if oldx>=5
if ~(y==oldy&&x-oldx==-1)
flag = 0;
return
end
else% pass river
if ~(y==oldy&&x-oldx==-1) && ~(abs(y-oldy)==1&&x==oldx)
flag = 0;
return
end
end
end
end
end移動(dòng)棋子
function MoveChess(x,y)
set(h(cur_turn,cur_cid),'xdata',x,'ydata',y)%改進(jìn),移動(dòng)棋子到新位置
set(hText(cur_turn,cur_cid),'Position',[x-0.35 y]);
pos_chess(chess_y(cur_turn,cur_cid)+1,chess_x(cur_turn,cur_cid)+1) = 0;
pos_chess(y+1,x+1) = cur_cid+(cur_turn-1)*16;
chess_x(cur_turn,cur_cid) = x;
chess_y(cur_turn,cur_cid) = y;
end
吃子
function KillChess(kt,kc)
set(hText(kt,kc),'visible','off');%殺掉棋子
set(h(kt,kc),'visible','off');
MoveChess(chess_x(kt,kc),chess_y(kt,kc));
ChangeTurn();
sname = {'紅','黑'};
if kc==5
msgbox([sname{3-kt} '方獲勝!'], '象棋', 'modal');
end
end
以上就是基于Matlab實(shí)現(xiàn)中國(guó)象棋的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Matlab中國(guó)象棋的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++中為何推薦要把基類析構(gòu)函數(shù)設(shè)置成虛函數(shù)
這篇文章主要介紹了C++中為何推薦要把基類析構(gòu)函數(shù)設(shè)置成虛函數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
C語言文件操作 fopen, fclose, mkdir詳解
本文給大家詳細(xì)介紹了下C語言的文件操作函數(shù)fopen, fclose, mkdir的用法及示例,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2016-03-03
C語言利用UDP實(shí)現(xiàn)群聊聊天室的示例代碼
UDP是一個(gè)輕量級(jí)、不可靠、面向數(shù)據(jù)報(bào)的、無連接的傳輸層協(xié)議,多用于可靠性要求不嚴(yán)格,不是非常重要的傳輸,如直播、視頻會(huì)議等等。本文將利用UDP實(shí)現(xiàn)簡(jiǎn)單的群聊聊天室,感興趣的可以了解一下2022-08-08
Qt實(shí)現(xiàn)制作簡(jiǎn)單的計(jì)算器
計(jì)算器是我們生活中很常見的東西,它可以由多種語言多種方式來實(shí)現(xiàn)。本文主要介紹的是利用Qt實(shí)現(xiàn)的簡(jiǎn)易計(jì)算器的制作,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-12-12
C++實(shí)現(xiàn)LeetCode(137.單獨(dú)的數(shù)字之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(137.單獨(dú)的數(shù)字之二),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

