资源简介
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
- 上一篇:眼底血管匹配滤波MATLAB源码
- 下一篇:三电平空间适量调制matlab仿真
相关资源
- 三电平空间适量调制matlab仿真
- 眼底血管匹配滤波MATLAB源码
- 最大熵模型
- gps精密单点定位后处理方法matlab代码
- 简单有效的LDPC码BP译码算法 MATLAB
- 在线SVR,Matlab版本。直接可以使用
- lorenz混沌系统matlab程序
- 均值漂移的MATLAB程序
- matlab运动视频跟踪及轨迹显示 值得看
- MATLAB卡尔曼滤波伪距单点定位
- 基于matlab的gui信号发生器
- MATLAB实现基于TCPIP的通信
- 0.618法和fibonacci法matlab算法
- 维纳滤波和约束最小二乘滤波图像复
- MATLAB密度聚类程序
- matlab编写6-SPS并联机器人工作空间的程
- matlab圆形提取
- 近邻传播聚类affinity propagation cluster
- 遗传算法解决非线性规划问题的Matl
- PSO优化RBFNN的MATLAB源代码
- 光纤激光器matlab数值模拟
- 风光柴储混合发电的matlab仿真模型
- ESPRIT算法MATLAB仿真程序
-
基于simuli
nk的s-function的PWM生成.rar - 经典MUSIC算法MATLAB仿真,带详细注释
- dft函数matlab代码
- matlab傅里叶级数展开程序
- 颜色CIEDE2000matlab程序
-
卡尔曼滤波器在simuli
nk 中的实现 - QPSK调制解调过程的MATLAB仿真
评论
共有 条评论