一、简介

基于matlab中国象棋

二、源代码

function chess1()
% chess - Chinese chess for two players
%
% Jianjiang Feng
% 2008-01
clc
close all
h=zeros(2,16);%改进,储存棋子的句柄
% Constant values

% nSize = 60;

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_cid = 0;
hText = zeros(2,16);

figure(1);
hold on
axis([-offset_x nColNum+offset_x -offset_y nRowNum+offset_y])
s = title('红');
axis equal
set(1,'name','中国象棋',...
    'menubar','figure',...
    'toolbar','none',...
    'WindowButtonDownFcn',@OnWindowButtonDown);

set(s,'string','红','color','r','fontsize',20);
InitializeChessPosition;
DrawBoard;
DrawAllChess;

% 改进
set(gca,'xtick',[])
set(gca,'ytick',[])
set(gca,'xcolor','w','ycolor','w')

    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
   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);%如果在棋盘内,则给cc赋值
    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(h(cur_turn,cur_cid),'color','g')
%                 set(hText(cur_turn,cur_cid),'Color','k');
            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),'Color','none');
                cur_cid = cc;
                set(hText(cur_turn,cur_cid),'Color','k');
            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
                else
                    if ~(isempty(find(pos_chess(y+1,oldx+1+inc:inc:x+1-inc)~=0)))
                        flag = 0;
                        return
                    end

三、运行结果

【游戏】基于matlab中国象棋【含Matlab源码 489期】_赋值

四、备注

版本:2014a