0

I need to fill a set of specific values in a 2d array in random positions. I already got it set in certain positions but I want it in random positions instead. Here is my Code

    private void GenerateRandomBoard()
    {
       //Put a dot in every spot
        int row;
        int col;
        for (row = 0; row < 4; row++)
        {
            for (col = 0; col < 4; col++)
            {

                Console.Write(GameboardArray[row, col] = ".");
            }
            //Console.WriteLine();
        }

      //Randomly Places the entrance, dragon, pit and gold.


        GameboardArray[0, 0] = "E";
        GameboardArray[2, 1] = "D";
        GameboardArray[1, 1] = "P";
        GameboardArray[1, 3] = "P";
        GameboardArray[2, 2] = "P";
        GameboardArray[1, 2] = "G";
    }

So as you can see instead of where it has GameboardArray[2, 1] = "D"; I just it want it to end up in a random place on the 4, 4 array.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Search suggestion: class to generate random numbers called `Random` – Alexei Levenkov Jan 15 '14 at 02:29
  • 2
    You just need to generate 2 random numbers in each iteration of a loop and use those random numbers as indexes into your array. – Matthew Lock Jan 15 '14 at 02:33
  • Not sure about the implementation of `GameboardArray`, but if E, D, P, and G values are exclusive (i.e., cannot be in the same position), you're going to have to do some checking when generating random numbers. – dursk Jan 15 '14 at 02:57

3 Answers3

0

This is assuming that you can place some letter where you have already placed a previous letter. If not, you should get the idea:

private void GenerateRandomBoard()
{
    int row;
    int col;
    Random r = new Random();

    //Put a dot in every spot  
    for (row = 0; row < 4; row++)
    {
        for (col = 0; col < 4; col++)
            Console.Write(GameboardArray[row, col] = ".");
    }

    //Randomly Places the entrance, dragon, pit and gold.      

    GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "E";
    GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "D";
    GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "P";
    GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "P";
    GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "P";
    GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "G";     
}

The minValue is inclusive and the maxValue is exclusive (in case you are wondering why it is (0, 4).

See: http://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx And: How do I generate a random int number in C#?

Community
  • 1
  • 1
dursk
  • 4,435
  • 2
  • 19
  • 30
0
List<string> values = new List<string>() { "E", "D", "P", "P", "P", "G" };
Random rand = new Random();
foreach (string value in values)
{
  int row = rand.Next(0, 4);
  int col = rand.Next(0, 4);
  if (GameboardArray[row, col] == ".")
  {
    GameboardArray[row, col] = value;
  }
}
Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
0
class RandomGameBoard
{
    struct Position
    {
        public int Column;
        public int Row;

        public Position(int column, int row)
        {
            Column = column;
            Row = row;
        }
    }

    private const int ROWS = 4, COLUMNS = 4;

    private static char[,] GenerateRandomBoard(int columns, int rows)
    {
        char[,] board = new char[rows, columns];
        List<Position> positions = new List<Position>((columns + 1) * (rows + 1));

        for (int row = 0; row < rows; row++)
        {
            for (int column = 0; column < columns; column++)
            {
                board[column, row] = '.';
                positions.Add(new Position(column, row));
            }
        }

        Random random = new Random();
        foreach (char item in "EDPPPG")
        {
            int index = random.Next(positions.Count);
            Position position = positions[index];
            board[position.Column, position.Row] = item;
            positions.RemoveAt(index);
        }

        return board;
    }

    public static void Execute()
    {
        char[,] board;
        board = GenerateRandomBoard(COLUMNS, ROWS);
        board = GenerateRandomBoard(COLUMNS, ROWS);
        board = GenerateRandomBoard(COLUMNS, ROWS);
    }
}
Thejaka Maldeniya
  • 1,076
  • 1
  • 10
  • 20