2016-10-22 91 views
0

一切可能的行动,我有4个可能为每个玩家移动(上,下,左,右) 在一个简单的游戏 所有玩家

public struct Point 
{ 
    public int x; 
    public int y; 
} 
Point[] directions = new Point[] 
{ 
    new Point() {x=0,y=1 }, 
    new Point() {x=0,y=-1 }, 
    new Point() {x=1,y=0 }, 
    new Point() {x=-1,y=0 } 
}; 

我有n个的球员,比如3,但这个数字不是恒定的。

我需要列举所有可能的移动到数组中。 例如:

player1: up, player2:up, player3:up 
    player1: up, player2:up, player3:left 
    player1: up, player2:up, player3:right 
    player1: up, player2:up, player3:down 
    player1: up, player2:left, player3:up 
    .... 
    .... 

什么是枚举所有可能的所有玩家移动到一个数组的最佳方式?

结果数组必须是:

item[0] = {up,up,up}; 
item[1] = {up,up,down}; 
item[2] = {up,up,left}; 
item[3] = {up,up,right}; 
item[4] = {up,down,up}; 
.... 

,能不能请你帮我吗?

+0

在编写代码之前,它与数学有关。找到公式,然后尝试编码。 – Nofuzy

+0

你的代码在哪里!你试过什么了? – Marusyk

回答

1

比方说,你有一个像

enum Move 
{ 
    Up, 
    Down, 
    Left, 
    Right 
} 

的方法是迭代的整数和转换这个整数枚举在底座4的结果将是:

0000 - > 0001 - >技术 - > 0003 - > 0010 - > ... - > 3333

你可以采取数字的每一个数字并投下它o枚举值。这里是产生的方法:

private static List<Move[]> GetPossibleMoves(int NumberOfPlayers) 
{ 
    int Combination = 0; 
    List<Move[]> PossibleMoves = new List<Move[]>(); 
    while (true) 
    { 
     List<int> Digits = IntToString(Combination, new char[] { '0', '1', '2', '3' }) 
      .PadLeft(NumberOfPlayers, '0') 
      .Select(D => Convert.ToInt32(D.ToString())) 
      .ToList(); 

     if (Digits.Count() > NumberOfPlayers) 
     { 
      break; 
     } 
     else 
     { 
      Move[] Moves = new Move[NumberOfPlayers]; 
      for (int Player = 0; Player < NumberOfPlayers; Player++) 
      { 
       Moves[Player] = (Move)Digits[Player]; 
      } 
      PossibleMoves.Add(Moves);     
     } 
     Combination++; 
    } 
    return PossibleMoves; 
} 

至于帮手转换为基4您可以从德克福尔马尔该方法在this question

private static string IntToString(int value, char[] baseChars) 
{ 
    string result = string.Empty; 
    int targetBase = baseChars.Length; 

    do 
    { 
     result = baseChars[value % targetBase] + result; 
     value = value/targetBase; 
    } 
    while (value > 0); 

    return result; 
} 

结果将是:

向上,向上,向上

向上,向上,向上,向下

向上,向上,向上,左

向上,向上,向上,向右

向上,向上,向下,向上

...

右,右,右右

1

假设你有4名选手,每名选手有4个可能的动作。共有256个组合。 n球员,4次移动=> 4^n 将您的移动映射到数字 Up - 0, Down - 1, Left - 2, Right - 3然后将256(基数为10)转换为4的基数,将获得10000.因此,从0到3333(以4为底)进行迭代,并为每个玩家提取每个数字的位置并进行相应的映射。 在例如一些2330将意味着 - 第一的球员 - 左,第二的球员 - 右边,3 - 右,第4的球员 - 高达

1

使用此枚举:

public enum Direction 
{ 
    Up, 
    Down, 
    Right, 
    Left 
} 

尝试遍历枚举值使用Enum.GetValues Method和使用this LINQ的超负荷的SelectMany(它可以让你创建了两次IEnumerables项目的每个组合)创建的所有组合:

var options = Enum.GetValues(typeof(Direction)).Cast<Direction>() 
       .SelectMany(firstValue => Enum.GetValues(typeof(Direction)).Cast<Direction>(), 
       (firstValue, secondValue) => new { firstValue, secondValue}) 
       .SelecyMany(firstValues => Enum.GetValues(typeof(Direction)).Cast<Direction>(), 
       (firstValues, lastValue) => new {firstValues.firstValue, firstValues.secondValue, lastValue}).ToList(); 

您也可以替换最后的SelectMany创建3 lengt^h数组列表项目,而不是annonymous类型,只需更换到最后一行:

(firstValues, lastValue) => new Direction[] {firstValues.firstValue, firstValues.secondValue, lastValue}).ToList();