Android Open Source - minecraft-connect-4 Easy Ai






From Project

Back to project page minecraft-connect-4.

License

The source code is released under:

MIT License

If you think the Android project minecraft-connect-4 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* COPYRIGHT (C) 2014 Aleksandr Belkin. All Rights Reserved. */
package sq.squ1rr.mcc4.ai;
//from   w w w .  j a v a2  s  .  c  om
import sq.squ1rr.mcc4.rules.Player;

/**
 * Quite basic AI. Tries to add a new chip to the longest combination,
 * overlooks if it's not possible to finish this combination though.
 * Overlooks gaped combinations too. Overlook upper diagonals too.
 * @author Aleksandr Belkin
 */
public class EasyAi extends PeacefulAi {
    /** number of columns */
    protected final int cols;
    
    /** number of rows */
    protected final int rows;
    
    /*
     * Indices offsets
     */
    protected static final int X_L = -1;
    protected static final int X_R = 1;
    protected static final int Y_T = -1;
    protected static final int Y_B = 1;
    
    /**
     * Create and initialise AI
     * @param grid
     */
    public EasyAi(int[][] grid) {
        super(grid);
        
        cols = grid.length;
        rows = grid[0].length;
    }

    /*
     * (non-Javadoc)
     * @see ai.PeacefulAi#run()
     */
    @Override
    public int run() {
        int col = -1;
        int com = -1;
        
        for(int i = 0; i < grid.length; ++i) {
            if(grid[i][0] != 0) continue;
            int j = findRow(i);
            int a = analyse(i, j, 0); // analyse every possible column
            if(a > com) {
                com = a;
                col = i;
            }
        }
        if(com < 1) return super.run();
        return col;
    }
    
    /**
     * Find empty row in a column
     * @param col
     * @return
     */
    protected int findRow(int col) {
        int row = 0;
        while(row < rows - 1 && grid[col][row + Y_B] == 0) row++;
        return row;
    }
    
    /**
     * Analyses the column, returns the number which represents the length
     * of the longest combination that will be created if the chip is placed
     * in this particular column.
     * @param col
     * @param row
     * @param com
     * @return
     */
    protected int analyse(int col, int row, int com) {
        return Math.max(com, countCombinations(col, row, Player.PLAYER2));
    }
    
    /**
     * Returns the longest combination that will be created if the chip is
     * placed in this particular column.
     * @param col
     * @param row
     * @param p
     * @return
     */
    protected int countCombinations(int col, int row, int p) {
        int com = 0;
        com = Math.max(com, combination(col+X_L,    row,        X_L, 0,   p));
        com = Math.max(com, combination(col+X_L,    row+Y_B,    X_L, Y_B, p));
        com = Math.max(com, combination(col,        row+Y_B,    0,   Y_B, p));
        com = Math.max(com, combination(col+X_R,    row+Y_B,    X_R, Y_B, p));
        com = Math.max(com, combination(col+X_R,    row,        X_R, 0,   p));
        return com;
    }
    
    /**
     * Calculates the combination length
     * @param x column
     * @param y row
     * @param i offset on X
     * @param j offset on Y
     * @return combination length.
     */
    protected int combination(int x, int y, int i, int j, int p) {
        if(x < 0 || x >= cols) return 0;
        if(y >= rows || y < 0) return 0;
        if(grid[x][y] == p) {
            return combination(x + i, y + j, i, j, p) + 1;
        }
        return 0;
    }
}




Java Source Code List

sq.squ1rr.mcc4.AboutMenu.java
sq.squ1rr.mcc4.BaseActivity.java
sq.squ1rr.mcc4.GameActivity.java
sq.squ1rr.mcc4.MainMenuActivity.java
sq.squ1rr.mcc4.MainMenu.java
sq.squ1rr.mcc4.MenuLayout.java
sq.squ1rr.mcc4.OptionsMenu.java
sq.squ1rr.mcc4.QuickGameMenu.java
sq.squ1rr.mcc4.StatsMenu.java
sq.squ1rr.mcc4.Stats.java
sq.squ1rr.mcc4.ai.Ai.java
sq.squ1rr.mcc4.ai.EasyAi.java
sq.squ1rr.mcc4.ai.HardAi.java
sq.squ1rr.mcc4.ai.NormalAi.java
sq.squ1rr.mcc4.ai.PeacefulAi.java
sq.squ1rr.mcc4.board.BoardDialogue.java
sq.squ1rr.mcc4.board.BoardLogic.java
sq.squ1rr.mcc4.board.BoardRenderer.java
sq.squ1rr.mcc4.board.BoardView.java
sq.squ1rr.mcc4.board.GameBoard.java
sq.squ1rr.mcc4.board.Texture.java
sq.squ1rr.mcc4.gl.Rectangle.java
sq.squ1rr.mcc4.gl.SpriteBatch.java
sq.squ1rr.mcc4.gl.SpriteString.java
sq.squ1rr.mcc4.gl.Sprite.java
sq.squ1rr.mcc4.layout.LayoutManager.java
sq.squ1rr.mcc4.layout.McButton.java
sq.squ1rr.mcc4.layout.McGroup.java
sq.squ1rr.mcc4.layout.McSelector.java
sq.squ1rr.mcc4.layout.McStyle.java
sq.squ1rr.mcc4.layout.McText.java
sq.squ1rr.mcc4.layout.McToggler.java
sq.squ1rr.mcc4.layout.McToken.java
sq.squ1rr.mcc4.rules.GameRules.java
sq.squ1rr.mcc4.rules.Player.java
sq.squ1rr.mcc4.rules.Rule.java
sq.squ1rr.mcc4.util.GlobalConstants.java