private void PlanMove()
    {
        Debug.Log("Planning Move");
        var         watch         = System.Diagnostics.Stopwatch.StartNew();
        Move        bestMove      = null;
        int         bestValue     = -9999;
        List <Move> possibleMoves = BoardStateUtils.EnumerateAvailableMoves(_moves);
        List <Move> shuffledMoves = Shuffle.FisherYatesCardDeckShuffle <Move>(possibleMoves);

        foreach (Move move in shuffledMoves)
        {
            List <Move> moves = new List <Move>(_moves);
            moves.Add(move);
            int value = MiniMax.minimax(minimaxDepth, moves, -1000, 1000, false);
            if (value > bestValue)
            {
                bestMove  = move;
                bestValue = value;
            }
        }
        watch.Stop();
        Debug.Log("Time Taken: " + watch.ElapsedMilliseconds);
        if (watch.Elapsed.Milliseconds < 1000)
        {
            int delay = 1000 - watch.Elapsed.Milliseconds;
            Thread.Sleep(delay);
        }
        plannedMove = bestMove;
        planning    = false;
    }
Exemple #2
0
    public void CheckMatches(int playerNum)
    {
        Piece[][][] b = BoardStateUtils.GenerateBoardState(_moves);

        Match[] matches = BoardChecker.FindMatches(b);
        Messenger <Match[]> .Broadcast(GameEvent.PIECES_MATCHED, matches);

        Match[] currentPlayerMatches = matches.Where(match => match.playerNum == playerNum).ToArray();
        if (currentPlayerMatches.Length == 3)
        {
            Debug.Log("All Pieces Matched!");
            gameOver = true;
            Messenger <int> .Broadcast(GameEvent.GAME_OVER, playerNum);
        }
    }
    private void PlanRandomMove()
    {
        Debug.Log("Planning Random Move");
        var         watch         = System.Diagnostics.Stopwatch.StartNew();
        List <Move> possibleMoves = BoardStateUtils.EnumerateAvailableMoves(_moves);
        List <Move> shuffledMoves = Shuffle.FisherYatesCardDeckShuffle <Move>(possibleMoves);

        plannedMove = shuffledMoves[0];
        watch.Stop();
        Debug.Log("Time Taken: " + watch.Elapsed.Milliseconds);
        if (watch.Elapsed.Milliseconds < 1000)
        {
            int delay = 1000 - watch.Elapsed.Milliseconds;
            Thread.Sleep(delay);
        }
        planning = false;
        return;
    }
Exemple #4
0
    public static int minimax(int depth, List <Move> movesPlayed, int alpha, int beta, bool isMaximisingPlayer)
    {
        if (depth == 0 || IsTerminalState(BoardStateUtils.GenerateBoardState(movesPlayed)))
        {
            return(-GetBoardValue(BoardStateUtils.GenerateBoardState(movesPlayed)));
        }
        List <Move> shuffledMoves = BoardStateUtils.EnumerateAvailableMoves(movesPlayed);

        if (isMaximisingPlayer)
        {
            int bestMove = -9999;
            foreach (Move move in shuffledMoves)
            {
                List <Move> newMoves = new List <Move>(movesPlayed);
                newMoves.Add(move);
                bestMove = Math.Max(bestMove, minimax(depth - 1, newMoves, alpha, beta, !isMaximisingPlayer));
                alpha    = Math.Max(alpha, bestMove);
                if (beta <= alpha)
                {
                    return(bestMove);
                }
            }
            return(bestMove);
        }
        else
        {
            int bestMove = 9999;
            foreach (Move move in shuffledMoves)
            {
                List <Move> newMoves = new List <Move>(movesPlayed);
                newMoves.Add(move);
                bestMove = Math.Min(bestMove, minimax(depth - 1, newMoves, alpha, beta, !isMaximisingPlayer));
                beta     = Math.Min(beta, bestMove);
                if (beta <= alpha)
                {
                    return(bestMove);
                }
            }
            return(bestMove);
        }
    }