Difference between revisions of "Add a feature to randomly scatter rocks over the field"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==Where to put the code== The CreateNewField method sets each field element to soil: <syntaxhighlight lang=csharp line> static void CreateNewField(char[,] Field)...")
 
(The new code)
Line 28: Line 28:
 
             int Row = 0;
 
             int Row = 0;
 
             int Column = 0;
 
             int Column = 0;
 +
            Random RandomInt = new Random();
 
             for (Row = 0; Row < FIELDLENGTH; Row++)
 
             for (Row = 0; Row < FIELDLENGTH; Row++)
 
             {
 
             {
 
                 for (Column = 0; Column < FIELDWIDTH; Column++)
 
                 for (Column = 0; Column < FIELDWIDTH; Column++)
 
                 {
 
                 {
                    Random RandomInt = new Random();
 
 
                     if (RandomInt.Next(0, 20) == 1)
 
                     if (RandomInt.Next(0, 20) == 1)
 
                     {
 
                     {
Line 48: Line 48:
 
         }
 
         }
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
I started by copying the RandomInt and the if statement from SimulateSpring. I have changed the range of the random number to better reflect the probability of a rock, ie 1 in 20.
 +
 +
if the random number is a 1 then it adds a rock.
 +
if the random number is not a 1 then it adds soil.

Revision as of 14:24, 10 March 2017

Where to put the code

The CreateNewField method sets each field element to soil:

 1         static void CreateNewField(char[,] Field)
 2         {
 3             int Row = 0;
 4             int Column = 0;
 5             for (Row = 0; Row < FIELDLENGTH; Row++)
 6             {
 7                 for (Column = 0; Column < FIELDWIDTH; Column++)
 8                 {
 9                     Field[Row, Column] = SOIL;
10                 }
11             }
12             Row = FIELDLENGTH / 2;
13             Column = FIELDWIDTH / 2;
14             Field[Row, Column] = SEED;
15         }

We could generate a random number and if it is within a certain range set ROCK instead of SOIL.

The new code

 1 static void CreateNewField(char[,] Field)
 2         {
 3             int Row = 0;
 4             int Column = 0;
 5             Random RandomInt = new Random();
 6             for (Row = 0; Row < FIELDLENGTH; Row++)
 7             {
 8                 for (Column = 0; Column < FIELDWIDTH; Column++)
 9                 {
10                     if (RandomInt.Next(0, 20) == 1)
11                     {
12                         Field[Row, Column] = ROCKS;
13                     }
14                     else
15                     {
16                         Field[Row, Column] = SOIL;
17                     }
18                 }
19             }
20             Row = FIELDLENGTH / 2;
21             Column = FIELDWIDTH / 2;
22             Field[Row, Column] = SEED;
23         }

I started by copying the RandomInt and the if statement from SimulateSpring. I have changed the range of the random number to better reflect the probability of a rock, ie 1 in 20.

if the random number is a 1 then it adds a rock. if the random number is not a 1 then it adds soil.