Esempio n. 1
0
    public void UpdateAI()
    {
        if (grid == null)
        {
            grid = GameObject.FindObjectOfType <Grid>();
        }

        if (myTurn && !didTurn)
        {
            didTurn = true;

            State boardState = GetBoardState();

            Debug.Log("State visited counter: " + boardState.TimesVisited);

            if (brain == null)
            {
                brain = new StateAgent(boardState);
            }
            else
            {
                brain.SetState(boardState);
            }

            //get action from brain, execute.

            StateAction action = brain.GetChosenActionForCurrentState();

            string[] moves = Regex.Split(action.ActionString, "to");
            string[] from  = Regex.Split(moves[0], "-");
            string[] to    = Regex.Split(moves[1], "-");

            Debug.Log(action.ActionString + ", Quality: " + action.GetDeepEvaluation() + " (" + action.ActionEvaluation + ") --- " + brain.LearnedStates + "///" + brain.EvaluatedActions);

            if (action.GetDeepEvaluation() != action.ActionEvaluation)
            {
                Debug.Log("///////////////////////////////////////////////////////////////");
            }

            foreach (Node n in grid.grid)
            {
                n.UnhighlightEat();
                n.UnhighlightMove();
            }

            Node fromNode = grid.GetNodeAt(int.Parse(from[1]), int.Parse(from[0]));
            Node toNode   = grid.GetNodeAt(int.Parse(to[1]), int.Parse(to[0]));

            fromNode.HighlightMove();
            toNode.HighlightEat();

            piece = fromNode.Piece;
            piece.Pickup();
            GameManager.Instance.GameState.Grab();
            int reward = 0;

            Piece tPiece = toNode.Piece;
            if (tPiece == null)
            {
                if (piece.IsPossibleMove(toNode))
                {
                    if (Rules.IsCheckMove(this, piece, toNode, true))
                    {
                        Debug.Log("Move checked, not allowed"); // do nothing

                        brain.EvaluateLastAction(-10000);
                        GameManager.Instance.GameState.Checkmate();
                        GameManager.Instance.GameOver(GameManager.Instance.PlayerOponent, GameOverType.CHECKMATE);
                    }
                    else
                    {
                        piece.MoveToXZ(toNode, Drop);
                        GameManager.Instance.GameState.Place();
                    }
                }
            }
            else
            {
                if (piece.IsPossibleEat(toNode))
                {
                    if (Rules.IsCheckEat(this, piece, toNode, true))
                    {
                        Debug.Log("Eat checked"); // do nothing

                        brain.EvaluateLastAction(-10000);
                        GameManager.Instance.GameState.Checkmate();
                        GameManager.Instance.GameOver(GameManager.Instance.PlayerOponent, GameOverType.CHECKMATE);
                    }
                    else
                    {
                        GCPlayer oppPlayer = GameManager.Instance.Opponent(this);

                        oppPlayer.brain.EvaluateLastAction(-tPiece.GetPieceValue());
                        reward = tPiece.GetPieceValue();

                        oppPlayer.RemovePiece(tPiece);
                        AddEatenPieces(tPiece);
                        tPiece.ScaleOut(0.2f, 1.5f);
                        piece.MoveToXZ(toNode, Drop);
                        GameManager.Instance.GameState.Place();
                    }
                }
            }

            State newState = GetBoardState();

            brain.PerformStateAction(action, newState);
            brain.EvaluateLastAction(reward);
        }
    }