您当前的位置: 首页 >  matlab

slandarer

暂无认证

  • 1浏览

    0关注

    248博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

MATLAB 人机对弈黑白棋

slandarer 发布时间:2020-07-15 09:45:11 ,浏览量:1

目录
          • 界面展示
          • 人机实现思路
          • 完整代码
          • 代码使用方式

界面展示

在这里插入图片描述

人机实现思路

这个机器输出接口只有短短的80行,并没有用人工智能的库,而只是给予其简单的规则,令其取满足规则的最好的点罢了。两条规则如下:

  • 优先占取四角,并且有多个可占角时,选择占领后使棋盘上白子最多的一个角。
  • 假设黑子在接下来一步会做出局部最好的选择(最贪婪的选择),尽量使棋盘上仍能保留尽可能多的白子。
完整代码

主体部分(运行时要运行这个函数)

function reversi_Man_Machine
%图形界面初始化:
    axis equal
    axis([-0.2 9.2,-0.2 9.2])
    set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')
    set(gca,'color',[0.6353 0.5451 0.3333])
    hold on
    
   [0.2235    0.4902    0.2667
    0.3843    0.1569    0.0078
    0.7882    0.7647    0.4196
    0.6353    0.5451    0.3333
    0.1373    0.2902    0.1686];
%按键函数初始化设置:
    set(gcf,'KeyPressFcn',@key,'tag','keyset')
    set(gcf,'WindowButtonDownFcn',@buttondown)
%全局变量:
global winner;
global turn;
global checher_board
global black;
global white;

global plotblack;
global plotwhite;

global postion;
global arrivable;
global plotpostion;
global plotarrivable;
init()
    function init()
        %初始化前清除原有图像:
        delete(findobj('tag','piece'));
        delete(findobj('tag','gc'));
        delete(findobj('tag','rx'));
        delete(findobj('type','line'));
        delete(findobj('type','patch'));
        
        %棋盘绘制:
        fill([-0.1;9.1;9.1;-0.1;-0.1],[-0.1;-0.1;9.1;9.1;-0.1],[0.3843 0.1569 0.0078])
        fill([0.5;8.5;8.5;0.5;0.5],[0.5;0.5;8.5;8.5;0.5],[0.2235 0.4902 0.2667])
        plot([1.5:1:7.5;1.5:1:7.5],[ones(1,7).*0.5;ones(1,7).*8.5],'color',[0.1373 0.2902 0.1686],'linewidth',0.75)
        plot([ones(1,7).*0.5;ones(1,7).*8.5],[1.5:1:7.5;1.5:1:7.5],'color',[0.1373 0.2902 0.1686],'linewidth',0.75)
        scatter([2.5,6.5,2.5,6.5],[2.5,2.5,6.5,6.5],30,'filled','CData',[0.1373 0.2902 0.1686]);
        numberset={'A','B','C','D','E','F','G','H'};
        for i=1:8
            text(0.2,9-i,num2str(i),...
                'HorizontalAlignment','center',...
                'color',[0.6353 0.5451 0.3333],...
                'FontWeight','bold',...
                'FontSize',12)
            text(i,8.83,numberset{i},...
                'HorizontalAlignment','center',...
                'color',[0.6353 0.5451 0.3333],...
                'FontWeight','bold',...
                'FontSize',12)
        end
        
        %棋子棋盘数值初始化:
        winner=0;turn=1;
        black=[4 4;5 5];white=[4 5;5 4];
        checher_board=zeros(8,8);
        checher_board(black(:,1)+(black(:,2)-1).*8)=1;
        checher_board(white(:,1)+(white(:,2)-1).*8)=-1;
        postion=[0 0];
        postion(1,:)=[];
        arrivable=[5 3;6 4;3 5;4 6];
        
        %绘制函数初始化:
        plotblack=scatter(gca,black(:,1),black(:,2),450,'o','filled','CData',[0.1 0.1 0.1],'tag','piece');
        plotwhite=scatter(gca,white(:,1),white(:,2),450,'o','filled','CData',[0.9 0.9 0.9],'tag','piece');
        plotpostion=scatter(gca,postion(:,1),postion(:,2),50,'o','CData',[0.5059 0.6078 0.3529],'LineWidth',1.5,'tag','gc'); 
        plotarrivable=scatter(gca,arrivable(:,1),arrivable(:,2),150,'x',...
            'CData',[0.7843 0.3412 0.3098].*0.9,'LineWidth',1.5,'tag','rx'); 
        
    end
    function buttondown(~,~)
        xy=get(gca,'CurrentPoint');
        xp=xy(1,2);yp=xy(1,1);
        pos=[yp,xp];
        pos=round(pos);
        if all(abs(pos)8|temp_set(:,1)8|temp_set(:,2)size(white,1))||isempty(white)
                winner=1;
            case (all(all(abs(checher_board)))&&size(white,1)>size(black,1))||isempty(black)
                winner=-1;
            case (all(all(abs(checher_board)))&&size(white,1)==size(black,1))
                winner=3;
        end
        if winner~=0
            redraw()
            switch winner
            case 1
                buttonName1=questdlg('黑棋胜利','black win','关闭','重新开始','关闭');
                if isempty(buttonName1),buttonName1='end';end
                if strcmp(buttonName1,'重新开始'),init();
                elseif strcmp(buttonName1,'关闭'),close;
                end
            case -1
                buttonName1=questdlg('白棋胜利','white win','关闭','重新开始','关闭');
                if isempty(buttonName1),buttonName1='end';end
                if strcmp(buttonName1,'重新开始'),init();
                elseif strcmp(buttonName1,'关闭'),close;
                end
            case 3
                buttonName1=questdlg('平局','tie','关闭','重新开始','关闭');
                if isempty(buttonName1),buttonName1='end';end
                if strcmp(buttonName1,'重新开始'),init();
                elseif strcmp(buttonName1,'关闭'),close;
                end
            end
        end
    end

end

功能函数(计算可行点)

function outcome=get_arrivable(board,t)
outcome=[0 0];
outcome(1,:)=[];
switch t
    case 1,t=1;
    case 0,t=-1;  
end
[x,y]=find(board==t);
collection=[x,y];
dir=[1 0;-1 0;0 1;0 -1;1 1;-1 -1;1 -1;-1 1];
if ~isempty(collection)
for i=1:size(collection,1)
    for j=1:8
        temp_set=collection(i,:)+((1:7)')*dir(j,:);
        temp_set(temp_set(:,1)>8|temp_set(:,1)8|temp_set(:,2)8|temp_set(:,1)8|temp_set(:,2)            
关注
打赏
1664692598
查看更多评论
0.0410s