91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

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?????????????????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

評論

共有 條評論