0

in a simple game i have 4 possible moves (up, down, left, right) for each player

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 }
};

i have n players, for example 3 but this number is not constant.

i need enumerate all possibile moves into an array. for example:

    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
    ....
    ....

what is the best way to enumerate all possible moves of all players into an array?

the resulting array must be:

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};
....

please, can you help me?

danilonet
  • 1,757
  • 16
  • 33

3 Answers3

1

Let's say you have 4 players and each has a 4 possible moves. A total of 256 combinations. n players, 4 moves => 4^n Map your moves to numbers Up - 0, Down - 1, Left - 2, Right - 3 Then convert the 256(base of 10) to base of 4 and you will get 10000. So Iterate from 0 to 3333 (in base of 4) and extract each number place and map accordingly for each player. In example a number of 2330 would mean - 1st player - Left, 2nd player - right, 3rd - right, 4th player - up

Operatorius
  • 328
  • 2
  • 11
1

Using this enum:

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

Try to iterate over the enum values using Enum.GetValues Method and use this Linq SelectMany overload(which lets you to create every combination out of two IEnumerables items) to create all the combinations:

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();

You can also replace the last SelectMany to create a 3 length array as the list item instead the annonymous type, just replace the last line with:

(firstValues, lastValue) => new Direction[] {firstValues.firstValue, firstValues.secondValue, lastValue}).ToList();
YuvShap
  • 3,825
  • 2
  • 10
  • 24
1

Let's say you have an enum like

enum Move
{
    Up,
    Down,
    Left,
    Right
}  

A way is to iterate an integer and convert this integer to the base 4. The result will be:

0000 --> 0001 ---> 0002 ---> 0003 --> 0010 --> ... ---> 3333

You can take every digit of the numbers and cast it to the enum value. Here is the resulting method:

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;
}

As helper to convert to the base 4 you can take the method from Dirk Vollmar at 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;
}

The result will be:

Up, Up, Up, Up

Up, Up, Up, Down

Up, Up, Up, Left

Up, Up, Up, Right

Up, Up, Down, Up

...

Right, Right, Right Right

Community
  • 1
  • 1
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49