Esempio n. 1
0
        public void Given_MiniMax_When_WinningMoveAvailable_Then_GetBestMove()
        {
            var ttt = TicTacToe.FromString(new string[]
            {
                "O O",
                "XX ",
                "   ",
            }, Player.One);

            var mcts = new MiniMax();
            var move = mcts.GetBestMove(ttt, 9);

            move.BestMove.X.Should().Be(2);
            move.BestMove.Y.Should().Be(1);
        }
Esempio n. 2
0
        public void Given_MiniMax_When_OpponentAboutToWinInEarlyGame_Then_BlockOpponent()
        {
            var ttt = TicTacToe.FromString(new string[]
            {
                "  O",
                " XX",
                "   ",
            }, Player.Two);

            var mcts = new MiniMax();
            var move = mcts.GetBestMove(ttt, 9);

            move.BestMove.X.Should().Be(0);
            move.BestMove.Y.Should().Be(1);
        }
Esempio n. 3
0
    public Vector2 GetMove()
    {
        // Clear out any debug spheres used for visualizing the options
        GameObject[] gos = GameObject.FindGameObjectsWithTag("debugSphere");
        foreach (GameObject go in gos)
        {
            Destroy(go);
        }

        // Start a timer for measuring the minimax duration
        var watch = System.Diagnostics.Stopwatch.StartNew();

        // Get the best move using the minimax algorithm
        Vector2 move = MiniMax.GetBestMove(player1, gs, sh, sf);

        watch.Stop();

        // Get all the options, used for debugging
        List <Vector2> options = MiniMax.GetOptions();

        Debug.Log("Minimax time: " + watch.ElapsedMilliseconds + "ms with " + options.Count + " move options");

        // For each option, add a sphere to visualize the options
        foreach (Vector2 option in options)
        {
            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            go.transform.position   = new Vector3(option.x, 1, option.y);
            go.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
            go.tag = "debugSphere";
            if (option.Equals(move))
            {
                go.name = "Move";
                go.GetComponent <Renderer>().material.shader = Shader.Find("Unlit/Color");
                go.GetComponent <Renderer>().material.color  = Color.red;
            }
        }

        //Debug.Log("Options = " + System.String.Join(", ", new List<Vector2>(options).ConvertAll(o => o.ToString()).ToArray()));

        Debug.Log("Ai is making move: " + move);
        return(move);
    }
 public IMove GetMove(IState state)
 {
     return(_minimax.GetBestMove(state));
 }