Beispiel #1
0
    public void MovePiece()
    {
        List <Move> moves = new List <Move>();

        mPieceManager.setBoard(mBoard);
        string[,] boarddraught = mBoard.getDraughtAsStrings();
        BoardDraught b = new BoardDraught(boarddraught, currentPlayer, mBoard.sizeX, mBoard.sizeY);
        float        test;
        Move         currentMove = new Move();

        test = AIManager.Minimax(b, currentPlayer, maxDepth, 0, ref currentMove);

        //  Debug.Log("Moves Again: " + m.mPiece.name + " CurrentCell: " + m.mPiece.getCurrentCell().name + " TargetCell: " + mBoard.mAllCells[m.x, m.y].name);//.mPiece.getTargetCell().name);
        // StartCoroutine(ExecuteMoveAfterDelay(1));
        List <Piece> blist = mPieceManager.getBPieces();

        for (int i = 0; i < blist.ToArray().Length; i++)
        {
            if (blist.ToArray()[i].name == currentMove.mPieceName)
            {
                blist.ToArray()[i].CheckPath(currentMove);
                blist.ToArray()[i].ShowCells();
                //StartCoroutine(ExecuteHighlightAfterDelay(1, i, currentMove));

                StartCoroutine(ExecuteMoveAfterDelay((float)1.5, i, currentMove));

                break;
            }
        }
    }
Beispiel #2
0
    public static float Minimax(
        BoardDraught board,
        int player,
        int maxDepth,
        int currentDepth,

        ref Move bestMove)
    {
        if (board.IsGameOver() || currentDepth == maxDepth)
        {
            board.SetCurrentMove(bestMove);
            return(board.Evaluate(player));//board.Evaluate(player);
        }

        bestMove = null;
        float bestScore = Mathf.Infinity;

        if (board.GetCurrentPlayer() == player)
        {
            bestScore = Mathf.NegativeInfinity;
        }
        List <Move> allMoves   = new List <Move>();
        int         nextPlayer = 0;

        if (player == 2)
        {
            allMoves   = board.GetMoves(player);
            nextPlayer = 1;
        }
        else if (player == 1)
        {
            allMoves   = board.GetMoves(player);
            nextPlayer = 2;
        }
        BoardDraught bTest = board;
        Move         currentMove;

        foreach (Move m in allMoves)//board.GetMoves())
        {
            bTest = board.MakeMove(m);
            float currentScore;
            //Evaluate Moves
            currentMove = m;// null;//= m;//m; ??
            if (m.attacked)
            {
                if (nextPlayer == 2)
                {
                    nextPlayer = 1;
                }
                else
                {
                    nextPlayer = 2;
                }
            }
            currentScore = Minimax(bTest, nextPlayer, maxDepth, currentDepth + 1, ref currentMove);
            board.SetCurrentMove(m);
            float newScore = board.Evaluate(player);
            //Evaluierung aktueller Move

            //currentScore += newScore;
            if (board.GetCurrentPlayer() == player)
            {
                currentScore += newScore;

                if (currentScore > bestScore)
                {
                    bestScore = currentScore;
                    bestMove  = m;
                    m.mScore  = bestScore;
                }
            }
            else
            {
                currentScore -= newScore;

                if (currentScore < bestScore)
                {
                    bestScore = currentScore;
                    bestMove  = m;
                    m.mScore  = bestScore;
                }
            }
            bTest.StepBack();
        }
        //   bestMove.mScore = bestScore;
        return(bestScore);
    }
 // Start is called before the first frame update
 public PieceManagerDraught(PieceManager pieceManager)
 {
     this.board = pieceManager.getBoardDraught();
 }