Negascout for java中的Tic-Tac-Toe

时间:2015-11-26 17:41:10

标签: java artificial-intelligence

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication7.ai;

import javaapplication7.model.Move;
import javaapplication7.model.TicTacToeGame;
/**
 *
 * @author Harshal
 */
public class NegaScout extends Strategy{

    @Override
    public Move makeMove(TicTacToeGame game) {
        System.out.println("Negascout");
        Move move = game.getAvaiableMoves().get(0);
        int best = Integer.MIN_VALUE;

        for(int i=0; i<game.getAvaiableMoves().size(); i++){
            System.out.print(game.getAvaiableMoves().get(i) + ",");
        }
        System.out.println();
        for (int i = 0; i < game.getAvaiableMoves().size(); i++) {
            //int result = negaMax(game);
            //TicTacToeGame tmp = game.clone();
            //tmp.playMove(game.getAvaiableMoves().get(i));

            int result = negaScout(game, Integer.MIN_VALUE, Integer.MAX_VALUE);
            //System.out.println("for i = " + i + " : " + game.getAvaiableMoves().get(i) + " result : " + result);
            System.out.printf("%d %d\n",best,result);
            if (result > best) {
                move = game.getAvaiableMoves().get(i);
                best = result;
            }
        }
        System.out.println();
        return move;
    }


    private int negaScout(TicTacToeGame game, int a, int b){
        if (Move.isWon(game.getCurrentPlayer().getMoves())) {
            return 1;
        }

        if (Move.isWon(game.getNonCurrentPlayer().getMoves())) {
            return -1;
        }
        //int score;
        int score = Integer.MIN_VALUE;
        for (int i = 0; i < game.getAvaiableMoves().size(); i++) {
            //Move move  = game.getAvaiableMoves().get(i);
            //TicTacToeGame tmp = game.clone();
                Move move  = game.getAvaiableMoves().get(i);
                TicTacToeGame tmp = game.clone();
                tmp.switchPlayers();
                tmp.playMove(move);

            if(i==0){               
                score = -negaScout(tmp,-b,-a);
            }else{

                score = -negaScout(tmp, ((-a)-1), -a);
                if(a<score && score<b)
                    score = -negaScout(tmp, -b, -score);
            }

            a = Math.max(a,score);
            if(a >= b){
                break;
            }

        }
        return a;
    }
}

我正在尝试对tic-tac-toe实施negascout,但是alglorithm没有做任何事情。 它只选择可用移动集中的第一步。 它返回值但我无法追踪它们。 一些帮助将不胜感激。

0 个答案:

没有答案