资源简介
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_OFDM调制解调(来自剑桥大学)
- Matlab路面裂缝识别69319
- 高灵敏度GPS接收机MATLAB仿真,附捕获
- 基于MATLAB的质点弹道计算与外弹道优
- 阵列天线的matlab仿真
- MATLAB 经典程序源代码大全
- MATLAB小波软阈值去噪代码33473
- 天线阵的波束形成在MATLAB仿真程序及
- 非线性SVM算法-matlab实现
- 《MATLAB 智能算法超级学习手册》-程序
- 组合导航matlab程序
- 读取txt文件内容matlab代码实现
- Matlab实现基于相关的模板匹配程序
- matlab优化工具箱讲解
- 基于MATLAB的快速傅里叶变换
- 光纤传输中的分布傅立叶算法matlab实
- 基于matlab的图像处理源程序
- matlab 椭圆拟合程序
- 算术编码解码matlab源代码
- optical_flow 光流法 matlab 实现程序
- 引导图像滤波器 Matlab实现
- 分形几何中一些经典图形的Matlab画法
- OFDM系统MATLAB仿真代码
- SVM工具箱(matlab中运行)
- 图像小波变换MatLab源代码
- LU分解的MATLAB实现
- 冈萨雷斯数字图像处理matlab版(第三
- 替代数据法的matlab程序
- 用matlab实现的多站定位系统性能仿真
- 通过不同方法进行粗糙集属性约简m
评论
共有 条评论