基于Matlab实现中国象棋的示例代码
作者:abcwsp 时间:2022-09-06 23:47:54
设置变量
nRowNum = 8; % 画布行数
nColNum = 9; % 画布列数
offset_x = 0;% 红车坐标起点
offset_y = 0;% 红车坐标起点
chess_name = {{'帥','仕','相','马','車','炮','兵'},{'將','仕','象','马','車','炮','卒'}};
chess_type = [5 4 3 2 1 2 3 4 5 6 6 7 7 7 7 7]; % 存储下棋类型
colors = 'rk';
% global variables
chess_x = -ones(2,16);
chess_y = -ones(2,16);
pos_chess = zeros(nRowNum+1,nColNum+1);% 存储棋子位置
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','.');%改进
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
棋子移动规则
判断是否可以移动
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
移动棋子
function MoveChess(x,y)
set(h(cur_turn,cur_cid),'xdata',x,'ydata',y)%改进,移动棋子到新位置
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
来源:https://blog.csdn.net/abcwsp/article/details/122799521
标签:Matlab,象棋
0
投稿
猜你喜欢
c#窗体传值用法实例详解
2022-04-04 03:44:15
C语言与C++中关于字符串使用的比较
2022-01-22 01:30:37
Java常用JVM参数实战
2023-03-15 10:33:39
Android 自定义View实现多节点进度条功能
2022-05-04 17:13:37
Android Filterable实现Recyclerview筛选功能的示例代码
2023-08-30 16:52:47
Android树形控件绘制方法
2022-09-05 01:50:55
Java细数IO流底层原理到方法使用
2022-08-06 21:30:45
C#反射内存的处理分析
2022-04-30 00:56:22
Java线程生命周期的终止与复位
2022-04-22 01:57:01
C#多线程系列之进程同步Mutex类
2022-07-11 19:29:16
java构造方法的作用总结
2023-05-31 00:54:45
springboot 集成redission 以及分布式锁的使用详解
2023-06-20 06:48:43
c#获取相同概率随机数的算法代码
2022-09-07 21:18:13
java 中设计模式(值对象)的实例详解
2021-12-27 14:15:08
Java中ShardingSphere分库分表实战
2023-11-24 09:20:37
通过实例讲解springboot整合WebSocket
2023-03-07 07:02:03
android串口开发入门之搭建ndk开发环境及第一个jni调用程序
2023-03-14 16:54:55
简单了解java自定义和自然排序
2022-01-29 02:47:26
Java实现查找算法的示例代码(二分查找、插值查找、斐波那契查找)
2022-05-28 04:17:38
java对象转换String类型的三种方法
2023-11-09 15:50:59