function chess()
% chess - Chinese chess for two players
%
% Jianjiang Feng
% 2008-01
close all
% Constant values
nSize = 60;
nRowNum = 8; %8行
nColNum = 9; %9列
offset_x = 0.5;
offset_y = 0.5;
chess_name = {{'帅','士','相','马','车','炮','兵'},{'将','士','象','马','车','炮','卒'}};
chess_type = [5 4 3 2 1 2 3 4 5 6 6 7 7 7 7 7]; %兵(卒)有5个,类型号为7;
%炮有2个,类型号为6;
%车马相士各有2个,类型号依次为5,4,3,2;
%帅有1个,类型号位为1.
% global variables
chess_x = -ones(2,16); %创建一个2行16列的全1数组chess_x,表示双方开始各有16枚棋子
chess_y = -ones(2,16); %创建一个2行16列的全1数组chess_y
pos_chess = zeros(nRowNum+1,nColNum+1); %创建一个9行10列的全0数组pos_chess
cur_turn = 1;
cur_cid = 0;
hText = zeros(2,16); %创建一个2行16列的全0数组
figure(1),hold on,axis([-offset_x nColNum+offset_x -offset_y nRowNum+offset_y])
axis equal
% axis off
set(1,'WindowButtonDownFcn',@OnWindowButtonDown);
set(1,'name','红');
InitializeChessPosition;
DrawBoard;
DrawAllChess;
%-------------------
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
end
%-------------------
function DrawAllChess()
colors = 'rk';
for k = 1:2
for i = 1:16
hText(k,i) = text(chess_x(k,i)-0.4,chess_y(k,i),['\fontsize{30}' chess_name{k}{chess_type(i)}],'color',colors(k));
end
end
end
%-------------------
function InitializeChessPosition()
chess_x(:,1:9) = [zeros(1,9);9*ones(1,9)];
chess_x(:,10:11) = [2 2;7 7];
chess_x(:,12:16) = [3*ones(1,5); 6*ones(1,5)];
chess_y(:,1:9) = [0:8;0:8];
chess_y(:,10) = [1;1];
chess_y(:,11) = [7;7];
chess_y(:,12:16) = [0:2:8;0:2:8];
pos_chess = zeros(nRowNum+1,nColNum+1);
pos_chess(:,1) = [1:9]';
pos_chess(:,nColNum+1) = [1:9]'+16;
pos_chess(2,3) = 10;
pos_chess(2,nColNum-1) = 10+16;
pos_chess(8,3) = 11;
pos_chess(8,nColNum-1) = 11+16;
pos_chess(1:2:9,4) = [12:16];
pos_chess(1:2:9,nColNum-2) = [12:16]+16;
end
%-------------------
function OnWindowButtonDown(src,evnt)
pt = get(gca,'CurrentPoint');
x = round(pt(1,1));
y = round(pt(1,2));
if x<0 || x>nColNum || y<0 || y>nRowNum
return
end
cc = pos_chess(y+1,x+1);
if cc~=0
ct = ceil(cc/16);
cc = mod(cc,16);
if cc == 0
cc = 16;
end
end
if cur_cid==0
if cc~=0% chess clicked
if ct==cur_turn
cur_cid = cc;
set(hText(cur_turn,cur_cid),'BackgroundColor',[.3 .5 .1]);
end
end
else% have current chess
if cc~=0% chess clicked
if cc==cur_cid && ct==cur_turn% no change
return
end
if ct==cur_turn% change chess
set(hText(cur_turn,cur_cid),'BackgroundColor','none');
cur_cid = cc;
set(hText(cur_turn,cur_cid),'BackgroundColor',[.3 .5 .1]);
else
% kill
if CanMove(x,y)==1
KillChess(ct,cc);
end
end
else% no chess clicked, go there
if CanMove(x,y)==1
MoveChess(x,y);
ChangeTurn();
end
end
end
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