2014-01-15 110 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); 
    } 
}