資源簡介
2048
This is a port of Gabriele Cirulli's 2048 game to Android.
All credit for game design goes to him.
Logo redrawn by Margaret Lu.
Android Code original written by https://github.com/tpcstld/2048
Web Original code: https://github.com/gabrielecirulli/2048
Used under MIT License.

代碼片段和文件信息
package?us.shandian.game.twozero;
import?java.util.ArrayList;
import?java.util.Date;
/*
?*
?*?This?is?a?simple?AI?for?the?2048?game
?*?based?on?alpha-beta?method
?*?Credits?to:?Matt?Overlan
?*
?*/
public?class?AI
{
????enum?Player?{
????????DOCTOR
????????DALEKS
????}
????
????static?final?long?MAX_CONSIDERING_TIME?=?100;
????
????static?final?float?WEIGHT_SMOOTH?=?0.1f?WEIGHT_MONO?=?1.0f
???????????????????????WEIGHT_EMPTY?=?2.7f?WEIGHT_MAX?=?1.0f
???????????????????????WEIGHT_ISLANDS?=?0.5f?WEIGHT_TWOANDFOUR?=?2.5f;
????
????MainGame?mGame;
????
????public?AI(MainGame?game)?{
????????mGame?=?game;
????}
????
????public?int?getBestMove()?{
????????
????????int?bestMove?=?0;
????????int?depth?=?0;
????????long?start?=?new?Date().getTime();
????????
????????do?{
????????????int?move?=?search(mGame.clone()?depth?-10000?10000?Player.DOCTOR)[0];
????????????if?(move?==?-1)?{
????????????????break;
????????????}?else?{
????????????????bestMove?=?move;
????????????????depth++;
????????????}
????????}?while?(new?Date().getTime()?-?start?????????
????????return?bestMove;
????}
????
????/*
?????*
?????*?Search?for?the?best?move
?????*?based?on?alpha-beta?search?method
?????*?Simulates?two?players‘?game
?????*?The?Doctor?V.S.?The?Daleks
?????*
?????*/
????private?object[]?search(MainGame?game?int?depth?int?alpha?int?beta?Player?player)?{
????????int?bestMove?=?-1;
????????int?bestScore?=?0;
????????
????????if?(player?==?Player.DOCTOR)?{
????????????//?The?Doctoe‘s?turn
????????????//?Doctor?wants?to?defeat?the?Daleks
????????????bestScore?=?alpha;
????????????
????????????for?(int?i?=?0;?i?<=?3;?i++)?{
????????????????MainGame?g?=?game.clone();
????????????????????
????????????????if?(!g.move(i))?{
????????????????????continue;
????????????????}
????????????????
????????????????if?(game.won)?{
????????????????????//?If?won?just?do?it
????????????????????return?new?object[]{i?10000};
????????????????}
????????????????
????????????????int?score?=?0;
????????????????
????????????????if?(depth?==?0)?{
????????????????????//?Just?eval?if?this?is?at?the?bottom
????????????????????score?=?evaluate(g);
????????????????}?else?{
????????????????????//?Pass?the?game?to?the?Daleks
????????????????????score?=?search(g?depth?-?1?bestScore?beta?Player.DALEKS)[1];
????????????????????
????????????????????//?Don‘t?search?any?further?if?won
????????????????????if?(score?>?9900)?{
????????????????????????score--;
????????????????????}
????????????????}
????????????????
????????????????if?(score?>?bestScore)?{
????????????????????bestScore?=?score;
????????????????????bestMove?=?i;
????????????????}
????????????????
????????????????//?We?have?found?a?much?much?better?move
????????????????//?So?cutoff
????????????????if?(bestScore?>?beta)?{
????????????????????return?new?object[]{bestMove?beta};
????????????????}
????????????}
????????}?else?if?(player?==?Player.DALEKS)?{
????????????//?The?Daleks‘?turn
????????????//?“EXTETMI
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2014-04-20?02:11??2048-master\
?????文件?????????342??2014-04-20?02:11??2048-master\.classpath
?????文件?????????483??2014-04-20?02:11??2048-master\.gitattributes
?????文件??????????26??2014-04-20?02:11??2048-master\.gitignore
?????文件????????4981??2014-04-20?02:11??2048-master\2048-2048.iml
?????文件????????1244??2014-04-20?02:11??2048-master\AndroidManifest.xm
?????文件?????????292??2014-04-20?02:11??2048-master\README.md
?????目錄???????????0??2014-04-20?02:11??2048-master\assets\
?????文件??????269792??2014-04-20?02:11??2048-master\assets\ClearSans-Bold.ttf
?????文件?????????465??2014-04-20?02:11??2048-master\build.gradle
?????文件???????47065??2014-04-20?02:11??2048-master\ic_launcher-web.png
?????文件???????11431??2014-04-20?02:11??2048-master\logo_2048.png
?????文件?????????695??2014-04-20?02:11??2048-master\proguard-rules.txt
?????目錄???????????0??2014-04-20?02:11??2048-master\res\
?????目錄???????????0??2014-04-20?02:11??2048-master\res\drawable-hdpi\
?????文件????????7721??2014-04-20?02:11??2048-master\res\drawable-hdpi\ic_launcher.png
?????文件????????2922??2014-04-20?02:11??2048-master\res\drawable-hdpi\logo_2048.png
?????目錄???????????0??2014-04-20?02:11??2048-master\res\drawable-mdpi\
?????文件?????????378??2014-04-20?02:11??2048-master\res\drawable-mdpi\background_rectangle.xm
?????文件?????????375??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle.xm
?????文件?????????379??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_1024.xm
?????文件?????????374??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_128.xm
?????文件?????????374??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_16.xm
?????文件?????????374??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_2.xm
?????文件?????????379??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_2048.xm
?????文件?????????375??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_256.xm
?????文件?????????374??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_32.xm
?????文件?????????374??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_4.xm
?????文件?????????379??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_512.xm
?????文件?????????374??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_64.xm
?????文件?????????374??2014-04-20?02:11??2048-master\res\drawable-mdpi\cell_rectangle_8.xm
............此處省略48個文件信息
評論
共有 條評論