Ejemplo n.º 1
0
        private MaxScoreData GetMaxScorePositions(EBoardSelection [] boardData, EBoardSelection inputMark)
        {
            MaxScoreData playerMaxScore = new MaxScoreData();

            playerMaxScore.Reset();

            DebugPrint("GetMaxScorePositions inputMark = " + inputMark.ToString());

            List <int> scoreList = new List <int>();

            //First check if there is a potential winner with next move and calculate scores
            for (int i = 0; i < MAX_NUM_MARKS; i++)
            {
                if (boardData [i] == EBoardSelection.E_Selection_None)
                {
                    int score = CalculateScore(boardData, inputMark, i);
                    scoreList.Add(score);

                    if (score == WINNING_SCORE)
                    {
                        playerMaxScore.score     = score;
                        playerMaxScore.positions = new List <int> ();
                        playerMaxScore.positions.Add(i);
                        break;                         // We have a clear winner so break from loop
                    }
                    else if (playerMaxScore.score != WINNING_SCORE &&
                             playerMaxScore.score < score)
                    {
                        //If the score is less then remove the old scores
                        if (playerMaxScore.score < score)
                        {
                            playerMaxScore.positions = null;
                            playerMaxScore.positions = new List <int> ();
                        }
                        playerMaxScore.score = score;
                        playerMaxScore.positions.Add(i);
                    }
                    else
                    {
                        //ignore the position
                    }
                }
                else
                {
                    scoreList.Add(INVALID_SCORE);
                }
            }

            string debugScores = "";

            for (int i = 0; i < scoreList.Count; i++)
            {
                debugScores += scoreList[i];
                debugScores += "\t";
            }

            DebugPrint("GetMaxScorePositions Scores = " + debugScores);

            return(playerMaxScore);
        }
Ejemplo n.º 2
0
        public int GetNextBestMove(EBoardSelection [] boardData, EBoardSelection inputMark, bool bIsRandom = false)
        {
            UnityEngine.Debug.Assert(boardData.Length == MAX_NUM_MARKS, "Number of marks on board cant exceed max");

            DebugPrint("GetNextBestMove mark = " + inputMark.ToString());

            if (bIsRandom)
            {
                return(GenRandomMove(boardData));
            }

            MaxScoreData currPlayerMaxScore = GetMaxScorePositions(boardData, inputMark);

            //We have a winner, so return it
            if (currPlayerMaxScore.score == WINNING_SCORE)
            {
                DebugPrint("GetNextBestMove() winning move found ");
                return(currPlayerMaxScore.positions [0]);
            }

            DebugPrint("GetNextBestMove() no winning move found for given player");

            //We dont a clear winner, so lets try scoring the opposite player moves.
            MaxScoreData oppPlayerMaxScore = GetMaxScorePositions(boardData, GetOppPlayerMark(inputMark));

            //Opposite Player has a winner, so block it
            if (oppPlayerMaxScore.score == WINNING_SCORE)
            {
                DebugPrint("GetNextBestMove() block opponent win");
                return(oppPlayerMaxScore.positions [0]);
            }

            DebugPrint("GetNextBestMove() no blocking move found to opponent");

            if (oppPlayerMaxScore.score > currPlayerMaxScore.score)
            {
                DebugPrint("GetNextBestMove() Opposition got a better move");

                int randPosition = UnityEngine.Random.Range(0, oppPlayerMaxScore.positions.Count);
                return(oppPlayerMaxScore.positions [randPosition]);
            }

            //Lets find if there is any position which has high score for both players
            List <int> match = currPlayerMaxScore.positions.Intersect(oppPlayerMaxScore.positions).ToList <int>();

            if (match.Count > 0)
            {
                //get a random positions from list of match
                int randPosition = UnityEngine.Random.Range(0, match.Count);
                return(match [randPosition]);
            }
            else
            {
                //get a random positions from list of max score positions
                int randPosition = UnityEngine.Random.Range(0, currPlayerMaxScore.positions.Count);
                return(currPlayerMaxScore.positions [randPosition]);
            }
        }