예제 #1
0
    public override List <float> CollectState()
    {
        List <float> state = new List <float>( );


        TicTacToe game = TicTacToe.Instance;

        List <int> currentState = game.GetBoard();

        int curPlayer = game.getCurPlayerVal();


        bool nearingWin = false;
        int  winIndex   = -1;
        int  sign       = 0;

        for (int i = 0; i < currentState.Count; i++)
        {
            if (currentState [i] == TicTacToe.DefVal)
            {
                int col = -1; int row = -1;
                BoardGame.GetRowColFromIndex(i, out col, out row);



                int sum = 0;
                if (col == row)
                {
                    sum = game.sumDiagLR(false);
                    if (Mathf.Abs(sum) == game.Dim - 1)
                    {
                        sign       = (int)Mathf.Sign(sum);
                        nearingWin = true;
                        winIndex   = i;
                    }
                }

                if (col + row == game.Dim - 1)
                {
                    sum = game.sumDiagRL(false);
                    if (Mathf.Abs(sum) == game.Dim - 1)
                    {
                        if (nearingWin && sign == curPlayer)
                        {
                            // Do nothing
                        }
                        else
                        {
                            sign       = (int)Mathf.Sign(sum);
                            nearingWin = true;
                            winIndex   = i;
                        }
                    }
                }

                sum = game.sumCol(col, false);
                if (Mathf.Abs(sum) == game.Dim - 1)
                {
                    if (nearingWin && sign == curPlayer)
                    {
                        // Do nothing
                    }
                    else
                    {
                        sign       = (int)Mathf.Sign(sum);
                        nearingWin = true;
                        winIndex   = i;
                    }
                }
                sum = game.sumRow(row, false);
                if (Mathf.Abs(sum) == game.Dim - 1)
                {
                    if (nearingWin && sign == curPlayer)
                    {
                        // Do nothing
                    }
                    else
                    {
                        sign       = (int)Mathf.Sign(sum);
                        nearingWin = true;
                        winIndex   = i;
                    }
                }
            }
        }



        int maxVal = currentState.Count;


        for (int i = 0; i < currentState.Count; i++)
        {
            if (winIndex == i)
            {
                int val = maxVal * 100;
                state.Add(val);
            }
            else
            {
                if (currentState [i] == TicTacToe.DefVal)
                {
                    int val = maxVal * 10;
                    state.Add(val);
                }
                else
                {
                    state.Add(currentState [i] * 10);
                }
            }
        }
        state.Add((float)(curPlayer));



        if (!game.IsLearning)
        {
            //drawStates (state);
        }


        return(state);
    }
예제 #2
0
    public override void AgentStep(float[] act)
    {
        TicTacToe game = TicTacToe.Instance;


        if (!game.isPlayable() || game.getWinner() != TicTacToe.DefVal)
        {
            pubInstance.NotifyListeners("AgentReset");
            return;
        }

        if (!isMyMove())
        {
            return;
        }



        bool isValid = false;
        int  index   = act.Length > 0 ? Mathf.RoundToInt(act [0]) : -1;


        if (index >= 0 && index < game.Dim * game.Dim)
        {
            if (game.GetVal(index) == TicTacToe.DefVal)
            {
                int row = -1;
                int col = -1;

                BoardGame.GetRowColFromIndex(index, out col, out row);

                if (game.makeMove(col, row, game.getCurPlayerVal()))
                {
                    isValid  = true;
                    lastStep = stepCounter;
                    // Each time a valid value occurs, increment maxStep while learning
                    if (game.IsLearning)
                    {
                        maxStep = stepCounter + maxStepInit;
                    }
                    postStep();

                    return;
                }
            }
        }

        string status = "Action: " + act [0] + " Step " + stepCounter + " " + isValid;

        if (game.IsLearning)
        {
            Status.text = status;
        }
        else
        {
            Debug.Log(status);
        }



        if (!isValid)
        {
            reward = -1;
            if (!game.IsLearning)
            {
                randomMove();
                int winner = game.getWinner();
                if (winner != TicTacToe.DefVal)
                {
                    if (game.IsLearning)
                    {
                        Status.text = "Yay! " + myVal + " won!";
                        if (myVal == TicTacToe.CrossVal)
                        {
                            scoreCross++;
                        }
                        else
                        {
                            scoreNot++;
                        }
                    }
                }
            }
            else
            {
                pubInstance.NotifyListeners("InvalidValue");
            }
        }
    }