Ejemplo n.º 1
0
        private int[,] SelectNode(Grid board, DataSet data, int[,] normalBoard)
        {
            // Calculate totalSimulations
            long totalSimulations = 0;

            for (int i = 0; i < data.Tables[0].Rows.Count; i++)
            {
                totalSimulations += (long)data.Tables[0].Rows[i].ItemArray[5];
            }


            var neverTested     = new List <int>();
            var monteCarloRatio = new List <double>();


            for (int index = 0; index < data.Tables[0].Rows.Count; index++)
            {
                if ((long)data.Tables[0].Rows[index].ItemArray[5] == 0)
                {
                    neverTested.Add(index);
                    monteCarloRatio.Add(0.0);
                }
                else
                {
                    monteCarloRatio.Add(CalculateMonteCarloRatio(totalSimulations, data.Tables[0].Rows[index]));
                }
            }

            int bestIndex;

            if (neverTested.Count > 0)
            {
                bestIndex = neverTested[_rnd.Next(neverTested.Count)];
            }
            else
            {
                int[] biggestIndexList = Baccaro.NetCore.Math.General.GetAllIndexesOfTheHighestValue <double>(monteCarloRatio.ToArray());
                bestIndex = biggestIndexList[_rnd.Next(biggestIndexList.Length)];
            }

            //for testing its random for now
            //bestIndex = _rnd.Next(data.Tables[0].Rows.Count);
            //end simulating


            int[,] bestNode = IAPlayerDB.ConvertNodeToInt2D((string)data.Tables[0].Rows[bestIndex].ItemArray[2]);

            // log before some randomization
            Log(data.Tables[0].Rows[bestIndex]);

            //do some randomization if applicable
            bestNode = SelectMove(board, normalBoard, bestNode);

            return(bestNode);
        }
Ejemplo n.º 2
0
        public override void UpdateScore(int score, string strResult)
        {
            base.UpdateScore(score);

            if (Learn)
            {
                var databaseIA = new IAPlayerDB(_dataBaseFileName);

                foreach (DataRow log in _historicRows)
                {
                    databaseIA.UpdateNode(log, strResult.ToUpper());
                }
            }

            _historicRows.Clear();
        }
Ejemplo n.º 3
0
        private int[] SearchBestMove(Grid board, ref int[,] normalBoard)
        {
            int[,] bestBoard = new int[3, 3];

            // 01 Searching best move.
            var     databaseIA = new IAPlayerDB(_dataBaseFileName);
            DataSet moveNodes  = databaseIA.SearchNode(normalBoard);

            if (moveNodes.Tables[0].Rows.Count == 0)
            {
                CreateNode(board, normalBoard, ref databaseIA);
                moveNodes = databaseIA.SearchNode(normalBoard);
            }
            bestBoard = SelectNode(board, moveNodes, normalBoard);

            //Achar as coordenadas (retirando rot end mirror) da jogada achada em 01
            return(DetermineCoords(board, normalBoard, bestBoard));
        }
Ejemplo n.º 4
0
        private void CreateNode(Grid board, int[,] currentBoard, ref IAPlayerDB databaseIA)
        {
            List <int[, ]> listOfPossibleNodes = new List <int[, ]>();
            int            count = board.FilledCellCounter;

            if (count == 0)
            {
                int[,] temp = new int[3, 3] {
                    { 1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }
                };
                listOfPossibleNodes.Add(temp);
                temp = new int[3, 3] {
                    { 0, 1, 0 }, { 0, 0, 0 }, { 0, 0, 0 }
                };
                listOfPossibleNodes.Add(temp);
                temp = new int[3, 3] {
                    { 0, 0, 0 }, { 0, 1, 0 }, { 0, 0, 0 }
                };
                listOfPossibleNodes.Add(temp);
            }
            else if ((count == 1) && (currentBoard[1, 1] != 0))
            {
                int[,] temp = new int[3, 3] {
                    { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 0 }
                };
                listOfPossibleNodes.Add(temp);
                temp = new int[3, 3] {
                    { 0, 1, 0 }, { 0, 2, 0 }, { 0, 0, 0 }
                };
                listOfPossibleNodes.Add(temp);
            }
            else if (board.FlagMirror == false)
            {
                if (currentBoard[0, 0] != 0)
                {
                    for (int row = 0; row < 3; row++)
                    {
                        for (int col = 1; col < 3; col++)
                        {
                            if ((col >= row) && (currentBoard[row, col] == 0))
                            {
                                int[,] temp    = (int[, ])currentBoard.Clone();
                                temp[row, col] = 1;
                                listOfPossibleNodes.Add(temp);
                            }
                        }
                    }
                }
                else
                {
                    for (int row = 0; row < 3; row++)
                    {
                        for (int col = 0; col < 2; col++)
                        {
                            if (currentBoard[row, col] == 0)
                            {
                                int[,] temp    = (int[, ])currentBoard.Clone();
                                temp[row, col] = 1;
                                listOfPossibleNodes.Add(temp);
                            }
                        }
                    }
                }
            }
            else
            {
                int[,] temp = (int[, ])currentBoard.Clone();
                if (checkWinDefeatSituation(ref temp))
                {
                    listOfPossibleNodes.Add(temp);
                }
                else
                {
                    for (int row = 0; row < 3; row++)
                    {
                        for (int col = 0; col < 3; col++)
                        {
                            if (currentBoard[row, col] == 0)
                            {
                                temp           = (int[, ])currentBoard.Clone();
                                temp[row, col] = 1;
                                listOfPossibleNodes.Add(temp);
                            }
                        }
                    }
                }
            }
            databaseIA.AddNodes(currentBoard, listOfPossibleNodes);
        }