Android Open Source - Toe Strategy Item Pattern Detector






From Project

Back to project page Toe.

License

The source code is released under:

GNU General Public License

If you think the Android project Toe 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

/**
 * @author John Piser developer@earthblood.com
 *//w w w  .jav a 2 s  . c o  m
 * Copyright (C) 2014 EARTHBLOOD, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**************************************************************************************************
 *   TRY TO DETECT IF YOU OR YOUR OPPONENT HAS TWO BOXES OF A WINNING PATTERN,
 *   IF THE THIRD WINNING BOX OF THAT PATTERN IS AVAILABLE, SELECT IT EITHER TO BLOCK OR WIN
 *
 *   THIS STRATEGY CAN BE USED OFFENSIVELY OR DEFENSIVELY
 *
 *   int numberOfPatternsToDetect: The number of game winning patterns to target.
 *                                 The higher the number, the greater the degree
 *                                 of offensive or defensive difficulty.
 *                                 Cannot be higher than total number of patterns
 *
 *   GameSymbol symbolToDetect:    The symbol whose selected boxes will be inspected
 *                                  for pattern matching.
 *                                  When initialized with the Android's symbol,
 *                                  this strategy will be offensive, else defensive
 *
 **************************************************************************************************
 */
package com.earthblood.tictactoe.strategy;

import com.earthblood.tictactoe.util.GameSymbol;
import com.earthblood.tictactoe.util.GameWinPattern;

public class StrategyItemPatternDetector extends StrategyItem {

    protected int numberOfPatternsToDetect;
    protected GameSymbol symbolToDetect;

    public StrategyItemPatternDetector(StrategyItem successor, int numberOfPatternsToDetect, GameSymbol symbolToDetect) {
        this.setSuccessor(successor);
        this.numberOfPatternsToDetect = numberOfPatternsToDetect;
        this.symbolToDetect = symbolToDetect;
    }

    public int getBoxId(int[] selectedXBoxIds, int[] selectedOBoxIds, GameSymbol androidSymbol) {

        int[] selectedBoxesToInspect = symbolToDetect == GameSymbol.X ? selectedXBoxIds : selectedOBoxIds;
        numberOfPatternsToDetect     = numberOfPatternsToDetect > GameWinPattern.values().length ?
                                                                  GameWinPattern.values().length : numberOfPatternsToDetect;

        GameWinPattern[] gameWinPatternsSubSet = GameWinPattern.randomBunchOfWinningPatterns(numberOfPatternsToDetect, getRandomObject());

        for (GameWinPattern gameWinPattern : gameWinPatternsSubSet) {
            int count = 0;
            for (int selectedOpponentPosition : selectedBoxesToInspect) {
                if (gameWinPattern.containsBoxId(selectedOpponentPosition)) {
                    count++;
                }
            }
            if (count == 2) {
                int found = gameWinPattern.returnFirstMatch(availableBoxes, NOT_FOUND);
                if (found > NOT_FOUND) {
                    return found;
                }
            }
        }
        return NOT_FOUND;
    }
}




Java Source Code List

com.earthblood.tictactoe.activity.GameActivity.java
com.earthblood.tictactoe.activity.MainActivity.java
com.earthblood.tictactoe.application.Toe.java
com.earthblood.tictactoe.contentprovider.GameContentProvider.java
com.earthblood.tictactoe.engine.ToeGame.java
com.earthblood.tictactoe.guice.ToeRoboModule.java
com.earthblood.tictactoe.helper.CoinTossHelper.java
com.earthblood.tictactoe.helper.GameDatabaseHelper.java
com.earthblood.tictactoe.helper.HapticFeedbackHelper.java
com.earthblood.tictactoe.helper.HtmlHelper.java
com.earthblood.tictactoe.helper.PreferenceHelperImpl.java
com.earthblood.tictactoe.helper.PreferenceHelper.java
com.earthblood.tictactoe.strategy.StrategyItemPatternDetector.java
com.earthblood.tictactoe.strategy.StrategyItemPickMiddleBox.java
com.earthblood.tictactoe.strategy.StrategyItemPickRandomBox.java
com.earthblood.tictactoe.strategy.StrategyItem.java
com.earthblood.tictactoe.strategy.ToeStrategyBase.java
com.earthblood.tictactoe.strategy.ToeStrategyEasy.java
com.earthblood.tictactoe.strategy.ToeStrategyExplicit.java
com.earthblood.tictactoe.strategy.ToeStrategyHard.java
com.earthblood.tictactoe.strategy.ToeStrategyNormal.java
com.earthblood.tictactoe.strategy.ToeStrategyVeryHard.java
com.earthblood.tictactoe.strategy.ToeStrategy.java
com.earthblood.tictactoe.util.GameBox.java
com.earthblood.tictactoe.util.GamePreference.java
com.earthblood.tictactoe.util.GameStatus.java
com.earthblood.tictactoe.util.GameSymbol.java
com.earthblood.tictactoe.util.GameWinPattern.java
com.earthblood.tictactoe.util.Skill.java