亲宝软件园·资讯

展开

Matlab中国象棋

abcwsp 人气:1

设置变量

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

加载全部内容

相关教程
猜你喜欢
用户评论