2014-01-15 74 views
0

我需要在随机位置的二维数组中填充一组特定值。我已经将它设置在某些位置,但是我希望它在随机位置。这里是我的代码帮助在二维数组中填充随机数

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

因此,大家可以看到,而不是它有GameboardArray[2, 1] = "D";我只是它希望它在4,4阵列上的随机位置结束。

+0

搜索建议:类来生成随机数称为'Random' –

+2

你只需要产生EAC 2张随机数h迭代循环,并将这些随机数用作数组中的索引。 –

+0

不知道GameboardArray的实现,但是如果E,D,P和G值是唯一的(即不能在同一个位置),那么当生成随机数时你将不得不做一些检查。 – dursk

回答

0

这是假设您可以放置​​一些字母,您已经放置了以前的字母。如果没有,你应该明白我的意思:

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

是minvalue包含在内,并且包括maxValue是独家(如果你想知道为什么它是(0,4)

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

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