Example #1
0
        public static Vector2Int CalculateBestMove(ref int[,] matrix, int player, int depthLeft)
        {
            numNodes = 0;

            int alpha = int.MinValue;
            int beta = int.MaxValue;

            int dimensionWithWall = matrix.GetLength(0);
            int dimension = dimensionWithWall - 2;

            Vector2Int bestMove = new Vector2Int(-1, -1);
            for (int i = 1; i <= dimension; i ++)
            {
                for (int j = 1; j <= dimension; j ++)
                {
                    if (matrix[i, j] == 0)
                    {
                        matrix[i, j] = player;

                        int opponent = Constants.NUMPLAYERS - player + 1;
                        int score = AlphaBetaMin(alpha, beta, ref matrix, opponent, player, depthLeft - 1);
                        if (score > alpha)
                        {
                            alpha = score;
                            bestMove.x = i - 1;
                            bestMove.y = j - 1;
                        }
                        matrix[i, j] = 0;
                    }
                }
            }

            Debug.Log(numNodes);

            return bestMove;
        }
Example #2
0
 private static void CollectionLineWithoutEndings(ref int[,] matrix, Vector2Int start, Vector2Int step, int length, ref Dictionary<KeyValuePair<LinkType, int>, int>[] dictionary)
 {
     int len = 1;
     int currentPlayer = matrix[start.x , start.y];
     int previousPlayer = currentPlayer;
     for (int j = 1; j <= length; j++)
     {
         Vector2Int temp = start + step * j;
         int nextPlayer = matrix[temp.x, temp.y];
         if (nextPlayer != currentPlayer)
         {
             if (currentPlayer > 0 && len > 1)
             {
                 LinkType linkType = GetLinkType(previousPlayer, nextPlayer, currentPlayer);
                 KeyValuePair<LinkType, int> keyValuePair = new KeyValuePair<LinkType, int>(linkType, len);
                 if (dictionary[currentPlayer - 1].ContainsKey(keyValuePair))
                 {
                     dictionary[currentPlayer - 1][keyValuePair] += 1;
                 }
                 else
                 {
                     dictionary[currentPlayer - 1][keyValuePair] = 1;
                 }
             }
             len = 1;
             previousPlayer = currentPlayer;
             currentPlayer = nextPlayer;
         }
         else
         {
             len += 1;
         }
     }
 }