设置变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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); |
绘图
绘制棋盘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 |
绘制棋子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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 |
棋子移动规则
判断是否可以移动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
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 |
移动棋子
1
2
3
4
5
6
7
8
9
|
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 |
吃子
1
2
3
4
5
6
7
8
9
10
|
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实现中国象棋的示例代码的详细内容,更多关于Matlab中国象棋的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/abcwsp/article/details/122799521