• 大小: 274KB
    文件类型: .zip
    金币: 2
    下载: 2 次
    发布日期: 2021-06-17
  • 语言: Java
  • 标签:

资源简介

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 < MAX_CONSIDERING_TIME);
        
        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.xml
     文件         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.xml
     文件         375  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle.xml
     文件         379  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_1024.xml
     文件         374  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_128.xml
     文件         374  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_16.xml
     文件         374  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_2.xml
     文件         379  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_2048.xml
     文件         375  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_256.xml
     文件         374  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_32.xml
     文件         374  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_4.xml
     文件         379  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_512.xml
     文件         374  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_64.xml
     文件         374  2014-04-20 02:11  2048-master\res\drawable-mdpi\cell_rectangle_8.xml
............此处省略48个文件信息

评论

共有 条评论