示例#1
0
文件: Bot.cs 项目: ommzi-dev/5-Games
    /// <summary>
    /// Return the bigger mean in a tree of a Given depth.
    /// This function make a recursive call.
    /// </summary>
    private float MeanAdaptation(string configuration, float sum, int depth)
    {
        BoardConfiguration bc = FindBoardConfiguration(configuration);

        // Stop when the configuration doesn't exists or reached the max depth.
        if (bc == null)
        {
            return(sum / depth);
        }
        if (depth > MAX_DEPTH)
        {
            return(sum / (depth - 1));
        }

        // Select the best adaptation of the possible movements ever tried.
        float betterAdaptation = addap;

        foreach (MovementConfiguration mc in bc.GetMovementsConfigurations())
        {
            // if this configuration do not end up with another, return the mean.
            float confAdaptation = mc.GetAdaptation();
            if (mc.GetResults().Count == 0 &&
                betterAdaptation < (sum + confAdaptation) / depth)
            {
                betterAdaptation = (sum + confAdaptation) / depth;
            }

            Debug.Log("adaptation: " + confAdaptation);

            // Get the best mean adaptation of each result.
            foreach (string result in mc.GetResults())
            {
                float adaptation = MeanAdaptation(result, sum + confAdaptation, depth + 1);
                if (adaptation > betterAdaptation)
                {
                    betterAdaptation = adaptation;
                }
            }
        }

        return(betterAdaptation);
    }
示例#2
0
文件: Bot.cs 项目: ommzi-dev/5-Games
    /// <summary>
    /// Return the best movement by the historic.
    /// </summary>
    private Movement ChoseMovementFromHistoric(string configuration)
    {
        float              adaptation   = addap;
        Movement           bestMovement = null;
        BoardConfiguration bc           = FindBoardConfiguration(configuration);

        // Return null if the configuration doesn't exists.
        if (bc == null)
        {
            return(bestMovement);
        }

        foreach (MovementConfiguration moveConf in bc.GetMovementsConfigurations())
        {
            // if this configuration do not end up with another, return its own adaptation.
            if (moveConf.GetResults().Count == 0)
            {
                adaptation = moveConf.GetAdaptation();
                if (adaptation > biggestAdaptation)
                {
                    biggestAdaptation = adaptation;
                    bestMovement      = moveConf.GetMove();
                }
            }
            // Get the best mean adaptation of each result.
            foreach (string result in moveConf.GetResults())
            {
                adaptation = MeanAdaptation(result, 0f, 1);
                if (adaptation > biggestAdaptation)
                {
                    biggestAdaptation = adaptation;
                    bestMovement      = moveConf.GetMove();
                }
            }
        }
        //Debug.Log("Best Adaptation: " + biggestAdaptation);
        return(bestMovement);
    }
示例#3
0
    /// <summary>
    /// Random function to write some persitance tests in it.
    /// </summary>
    /// <remarks>
    /// Only used for Debug Purpose.
    /// </remarks>
    public void Test()
    {
        if (CheckersMultiplayer.Instance.IsTableTen)
        {
            BoardConfiguration foo =

                new BoardConfiguration("b#b###################w######w####B###b#########W########w###w######################################");
            Movement movement = new Movement(new IntVector2(7, 1),
                                             new IntVector2(3, 5), new IntVector2(5, 3));
            foo.AddMovement(movement, 5f);

            BinaryFormatter bf   = new BinaryFormatter();
            FileStream      file = File.Open(Application.persistentDataPath + "/gameStorage.dat", FileMode.Open);
            historic = (List <BoardConfiguration>)bf.Deserialize(file);
            file.Close();

            bool result  = false;
            bool result2 = false;
            foreach (BoardConfiguration bc in historic)
            {
                if (bc.Equals(foo))
                {
                    result = true;
                    Debug.Log("find: " + bc.ToString());

                    Debug.Log("movement contains: " +
                              bc.HasMovementConfiguration(foo.GetMovementsConfigurations()[0]));
                }
            }

            result2 = historic.Contains(foo);

            Debug.Log("result for: " + result + "\nresult contain: " + result2);
        }
        else
        {
            BoardConfiguration foo =
                new BoardConfiguration("b#b###################w######w####B###b#########W########w###w##");
            Movement movement = new Movement(new IntVector2(7, 1),
                                             new IntVector2(3, 5), new IntVector2(5, 3));
            foo.AddMovement(movement, 5f);

            BinaryFormatter bf   = new BinaryFormatter();
            FileStream      file = File.Open(Application.persistentDataPath + "/gameStorage.dat", FileMode.Open);
            historic = (List <BoardConfiguration>)bf.Deserialize(file);
            file.Close();

            bool result  = false;
            bool result2 = false;
            foreach (BoardConfiguration bc in historic)
            {
                if (bc.Equals(foo))
                {
                    result = true;
                    Debug.Log("find: " + bc.ToString());

                    Debug.Log("movement contains: " +
                              bc.HasMovementConfiguration(foo.GetMovementsConfigurations()[0]));
                }
            }

            result2 = historic.Contains(foo);

            Debug.Log("result for: " + result + "\nresult contain: " + result2);
        }
    }
示例#4
0
文件: Bot.cs 项目: ommzi-dev/5-Games
    /// <summary>
    /// Sign the start of that player's turn.
    /// </summary>
    public override void Play()
    {
        if (PlayerPrefs.GetString("difficult") == "easy")
        {
            addap = -100;
        }
        else if (PlayerPrefs.GetString("difficult") == "medium")
        {
            addap = -1000;
        }
        else if (PlayerPrefs.GetString("difficult") == "hard")
        {
            addap = -10000;
        }


        biggestAdaptation = addap;
        string configuration = TranslateBoard();

        if (configList.Count >= 1 && !useHistoric)
        {
            configList[configList.Count - 1].GetMovementConfigurationWithMove(lastMovement)
            .AddResults(configuration);
        }

        Movement choseMove;


        // Select the same piece if it's playing again.
        if (base.currentPiece != null)
        {
            choseMove = (Movement)base.currentPiece.GetBestSucessiveCapture()[0];
        }
        // Select a new piece if it's the first play of this turn.
        else
        {
            choseMove = ChooseOneMovement();
            // Debug.LogError("choseMove " + choseMove);
            base.currentPiece = base.board.GetTile(choseMove.getOriginalPosition())
                                .GetChild().GetComponent <Piece>();
        }
        // Make a function call to find the best adaptation in the historic.
        Movement moveByHistoric = ChoseMovementFromHistoric(configuration);

        // Choose between use the historic choice or not.
        if (ga.AdaptationScore(choseMove) / 2 < biggestAdaptation)
        {
            Debug.Log("Select historic movement " + biggestAdaptation);
            choseMove   = moveByHistoric;
            useHistoric = true;
        }
        else
        {
            if (moveByHistoric != null)
            {
                Debug.Log("Historic is a bad choice\n historic:" + biggestAdaptation +
                          " newMove " + ga.AdaptationScore(choseMove));
                // Get possible moves for Bot's pieces.
                ArrayList botPieces     = base.board.GetEnemyPieces();
                ArrayList possibleMoves = ga.GenerateMutations(this, botPieces);
                // Get moves made that is saved in the historic
                BoardConfiguration bc         = FindBoardConfiguration(configuration);
                ArrayList          movesSaved = new ArrayList();
                foreach (MovementConfiguration moveConf in bc.GetMovementsConfigurations())
                {
                    movesSaved.Add(moveConf.GetMove());
                }

                // Remove from possible moves that which are in saved in historic
                ArrayList movesToBeRemoved = new ArrayList();
                foreach (Movement moveSaved in movesSaved)
                {
                    foreach (Movement move in possibleMoves)
                    {
                        if (move.Equals(moveSaved))
                        {
                            movesToBeRemoved.Add(move);
                        }
                    }
                }
                foreach (Movement moveToRemove in movesToBeRemoved)
                {
                    possibleMoves.Remove(moveToRemove);
                }

                // If doesn't left any movement, restore the movement and get one randomly.
                if (possibleMoves.Count == 0)
                {
                    possibleMoves = ga.GenerateMutations(this, botPieces);
                    int randomIndex = UnityEngine.Random.Range(0, possibleMoves.Count);


                    choseMove = (Movement)possibleMoves[randomIndex];
                }
                else
                {
                    Debug.Log("RANDOM MOVE");
                    // Choose the best one with the adaptation function.
                    choseMove = SelectBestMovement(possibleMoves);
                }
            }
            else
            {
                // Debug.Log("New configuration");

                useHistoric = false;
            }
        }

        // Add the current board configuration with the chose movement to the config list.
        BoardConfiguration bConfig = new BoardConfiguration(configuration);

        bConfig.AddMovement(choseMove, ga.AdaptationScore(choseMove));
        configList.Add(bConfig);

        // Make the movement.
        //   Debug.Log("Chosed Movement: " + choseMove.ToString());
        lastMovement = choseMove;
        base.board.MovePiece(choseMove);
    }