【游戏】基于GUI四子棋【Matlab 302期】

一、简介

四子棋是一款优秀的CONNECT 4游戏体验,玩家现在可以用全新方式玩这款老少皆宜的经典游戏。和对手见招拆招,第一个连起4个棋子的人即为获胜。

二、源代码

function connectfour(varargin)
%
% An implementation of the classic Connect Four by Milton Bradley.  Your
% goad is to arrange 4 chips in a row either vertically, horizontally, or
% diagonally before your opponent does.
%
% To play, simply click on the column in which you want to drop a peice.
% The computer will then automatically make its move.
%
% THE COMPUTER IS BEATABLE!  This is NOT the perfect Connect Four algorithm 
% that you may see on some websites.  The algorithm is good enough to win
% its fair number of matches.
%
% You can also click the "Demo" button to watch the computer play itself.

if nargin==0,
    handles.fig=openfig('connectfour.fig');
    micintro
    set(handles.fig,'WindowButtonDownFcn',[mfilename '(''boardclick'',guidata(gcbo))'])
    set(get(handles.fig,'children'),'visible','on')
    handles=guihandles(handles.fig);
    handles.turn=1;
    handles.demomode=0;
    handles.board=zeros(6,7);
    handles.snapshot=handles.board;
    drawboard(handles)
    guidata(handles.ax,handles)
else
    feval(varargin{
    
    :});
end

function demofcn(handles)
set(handles.Demo,'enable','off')
set(handles.message,'string','自动演示游戏')%Demo Game.
handles.demomode=1;
handles.board=zeros(6,7);
handles.snapshot=handles.board;
handles.turn=ceil(2*rand);
guidata(handles.fig,handles)
set(handles.fig,'WindowButtonDownFcn','')
set(handles.message,'string','自动演示游戏')%Demo Game.
drawboard(handles)
handles.board(end,ceil(7*rand))=handles.turn;
handles=addpeice(handles);
handles.turn=2/handles.turn;
test=handles;
try
    while ~checkifend(handles) & test.demomode,
        handles=halzmove(handles);
        handles=addpeice(handles);
        handles.turn=2/handles.turn;
        test=guidata(handles.fig);
    end
catch
end

function w=checkifend(handles)
[r,c]=size(handles.board);
[w,ind]=check4win(handles.board);
if w,
    if w<3,
        set(handles.message,'string',sprintf('玩家 %d 赢!',w))
        plot(ind(2,:)-0.5,r-ind(1,:)+0.5,'b','linewidth',3)
        untitled;
    else
        set(handles.message,'string',sprintf('It''s a draw!',w))
    end
    set(handles.fig,'windowbuttondownfcn','')
end

function resetfcn(handles)
set(handles.Demo,'enable','on')
handles.board=zeros(6,7);
handles.snapshot=handles.board;
handles.turn=1;
handles.demomode=0;
set(handles.fig,'WindowButtonDownFcn',[mfilename '(''boardclick'',guidata(gcbo))'])
set(handles.message,'string','玩家1选定一列')%Player 1 choose a column.
drawboard(handles)
guidata(handles.fig,handles)

function boardclick(handles)
set(handles.fig,'WindowButtonDownFcn','')
point=get(handles.ax,'currentpoint');
point=point(1,1:2);
board=handles.board;
[r,c]=size(board);
try
    if point(1)>0 & point(1)<c & point(2)>0 & point(2)<r,
        point=floor(point)+1;
        handles.turn=1;
        [handles.board,result]=makemove(handles.board,1,point(1));
        if result,
            handles=addpeice(handles);
            handles.turn=2;
            if ~checkifend(handles),
                handles=halzmove(handles);
                handles=addpeice(handles);
                checkifend(handles);
            end
        end
    end
    guidata(handles.fig,handles)
    set(handles.fig,'WindowButtonDownFcn',[mfilename '(''boardclick'',guidata(gcbo))'])
catch
end

function handles=halzmove(handles)
board=handles.board;
[r,c]=size(board);

oponent=2/handles.turn;
for j=1:c,
    [possibleBoard,result]=makemove(handles.board,handles.turn,j);
    w=check4win(possibleBoard);
    if w==handles.turn,
        handles.board=possibleBoard;
        return
    end
end
for j=1:c,
    [possibleBoard,result]=makemove(handles.board,oponent,j);
    w=check4win(possibleBoard);
    if w==oponent,
        handles.board=makemove(handles.board,handles.turn,j);;
        return
    end
end
if length(find(handles.board==handles.turn))<3,
    offense=0;
else
    if handles.demomode,
        offense=round(1-rand^3);
    else
        offense=round(rand);
    end
end
for j=1:c,
    [nextBoard,result(j)]=makemove(handles.board,handles.turn,j);        
    score(j)=scoreboard(nextBoard,handles.turn,offense);
end
score(~result)=-inf;
moves=find(score==max(score));
nextMove=ceil(length(moves)*rand);
handles.board=makemove(handles.board,handles.turn,moves(nextMove));


function score=scoreboard(board,turn,offense)
oponent=2/turn;
if offense,
    score=maxinarow(board,turn);
else
    score=1/maxinarow(board,oponent);
end
for j=1:size(board,2),
    nextBoard=makemove(board,oponent,j);
    w=check4win(nextBoard);
    if w==oponent,score=-1;end
end

三、运行结果

在这里插入图片描述

四、备注

完整代码或者代写添加QQ 912100926

猜你喜欢

转载自blog.csdn.net/m0_54742769/article/details/114365276