具有可变可能性的变化

时间:2015-11-25 17:38:14

标签: c#

我编写了一个玩家有30天建造城市的游戏。 我想知道玩家可以采取的所有可能的方法,以便我知道哪个是最好的。

基本上30天和9个项目可以购买和升级,数学公式将是n ^ k。在我们的情况下,这是9 ^ 30 = 42.391.158.275.216.203.514.294.433.201 但是因为它是一个游戏,玩家每轮可以购买的项目取决于变量,例如硬币。所以它的东西就像  4 * 1 * 5 * 3 * 2 ...

我的问题是,模拟游戏环境的GameStats在添加新的构建路径时不会被重置。

实施例

玩家以15个硬币开始,收入为5.第一天,他为10个硬币建造房屋

  • 第0天:buildpath {house};硬币15 - >买了房子-10硬币
  • 第1天:buildpath {house,empty};硬币5 - >没有钱买任何东西
  • 第2天:buildpath {house,empty,house};硬币10 - >玩家可以买房子,街道或者自行车道

现在对于下一个可能的构建路径,我们从第2天开始,也可以在街道或者自行车道上进行制作并拥有10个硬币,但拥有硬币的GameStats现在是15币。

如何保持GameStats对每个可能的构建路径都是唯一的?

public string[] GetPermutation(int days, List<BaseProject> projects)
{

    ArrayList output = new ArrayList();
    GameStats game = new GameStats();
    GetPermutationPerRef(days, projects, ref output, ref game);

    return output.ToArray(typeof(string)) as string[];
}


private void GetPermutationPerRef(int days, List<BaseProject> projects, ref ArrayList output, ref GameStats game, string outputPart = "")
{
    if (days == 0)
    {
        outputPart += " points: " ; 
        output.Add(outputPart);
    }
    else
    {
        if(projects.Count > 0)
        {

            foreach (BaseProject p in projects)
            {
                // construct the current project
                game.BuyProject(p);

                // move on to the next day
                game.nextDay();

                // find all the projects the player could buy the next day
                projects = game.GetBuyAbleBaseProjects();

                GetPermutationPerRef(days - 1,          
                    projects,                            
                    ref output,  
                    ref game,                               
                    outputPart + p.projectName +" "); 
            }
        }
        else
        {
            // dont buy or upgrade any project

            game.nextDay();

            projects = game.GetBuyAbleBaseProjects();

            GetPermutationPerRef(days - 1,
                projects,
                ref output,
                ref game,
                outputPart + "empty ");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

制作链接的天数列表。将项目添加到链接列表是一个不同的结果。

function Parent() {
    this.pool = [];
    this.idMap = new Map();
    this._idCount = 0;
 }

 Parent.prototype.addChild = function(instance) {
    this.idMap.set(this._idCount, instance);
    this._idCount++;
    this.pool.push(instance);

    return this;
 };

 Parent.prototype.removeChild = function(id) {
    var index = this.pool.indexOf(this.idMap.get(id));
    if(index !== -1) {
      this.idMap.delete(id);
      this.pool.splice(index, 1);
    }
 };

 function Child() {
      //something
 }