资源简介

MATLAB版本下实现的2048小游戏,代码简单,风格清晰,易学掌握!

资源截图

代码片段和文件信息

function direction = ai(board)
persistent score
if isempty(score)score = 0;end
board(isnan(board)) = 0;    %将矩阵中等于NaN的元置为0,便于计算
d = {‘up‘ ‘right‘ ‘down‘ ‘left‘};%方向的集合
s = sum(sum(board ~= 0));           %计算占据的方块数
% 第一种深度
depth = floor(s/2);                 %依据s赋予搜索深度的值
if depth>5 depth = 5;end           %若深度大于6,则置深度为6
% 第二种深度
% if s>12depth = 2;
% elseif s>10depth = 3;
% elseif s>8depth = 4;
% elseif s>4depth = 5;
% else
%     depth = 6;
% end
hint = findBestMove(board score depth);   %搜索最佳的方向
[newBoard newScore] = move(board score hint);%试移动一次
score = newScore;   %更新score
if isequal(newBoard board) %若为产生合并
    switch (hint)
        case 1
            newMat = [2 3 4];
        case 2
            newMat = [1 3 4];
        case 3
            newMat = [1 2 4];
        case 4
            newMat = [1 2 3];
    end
    hint = newMat(randi(3));    %随机赋予hint一个值
end

direction = d(hint);    %最终的方向
end


function direction = findBestMove(board score depth)
minVal = -2^32; %初始最小值
maxVal = 2^32;  %初始最大值
player = 1;     %表示用户,0表示对手
[direction ~] = alphaBeta(board score depth minVal maxVal player);
end


function [direction outScore] = alphaBeta(board score depth alpha beta player)
bestDirection = 1;  %初始最佳方向
if isGameTerminated(board)  %若游戏结束
    bestScore = min([score 1]);
elseif depth == 0   %若深度为0,计算得分
    bestScore = heuristicScore(score getNumberOfEmptyCells(board)...
        calculateClusteringScore(board));
else
    if player
        for dirIdx = 1 : 4
            newBoard = board;
            newScore = score;
            [newBoard newScore] = move(newBoard newScore dirIdx);
            if isequal(newBoard board)continue;end
            [~ currentScore] = alphaBeta(newBoard ...
                newScore depth-1 alpha beta 0);
            if currentScore > alpha
                alpha = currentScore;
                bestDirection = dirIdx;
            end
            if beta <= alphabreak;end
        end
        bestScore = alpha;
    else
        [row col] = getEmptyCellIds(board);
        possibleValues = [2 4];
%         flag = 0;
        len = length(row);
        for idx = 1 : len
            i = row(idx);
            j = col(idx);
            len2 = length(possibleValues);
            for idx2 = 1 : len2
                value = possibleValues(idx2);
                newBoard = board;
                newScore = score;
                newBoard(i j) = value;
                [~ currentScore] = alphaBeta(newBoard ...
                    newScore depth-1 alpha beta 1);%User
                if currentScore < betabeta = currentScore;end
                if beta <= alpha break;end
            end
            if beta <= alpha break;end
        end
        bestScore = beta;
        if len == 0bestScore = 0;end
    end
end
outScore = bestScore;
direction = bestDirection;
end


function hScore = heuri

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-11-26 15:56  Matlab实现游戏(2048)\
     目录           0  2018-11-26 15:55  Matlab实现游戏(2048)\2048\
     文件        6504  2015-05-31 14:18  Matlab实现游戏(2048)\2048\ai.m
     文件       27795  2015-06-13 14:21  Matlab实现游戏(2048)\2048\play2048game.m
     文件         362  2015-05-31 12:57  Matlab实现游戏(2048)\2048\sendm.p
     文件          13  2018-11-26 15:56  Matlab实现游戏(2048)\readme.txt

评论

共有 条评论