Difference between revisions of "AS 2019 RandomPlayerStarts"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Where)
(Idea)
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:
  
 
=Idea=
 
=Idea=
create a random, then generate a random number, and then use it to decide if A or B goes first.
+
create a random, then generate a random number, and then use it to decide if A or B goes first.  
  
so:
+
So we need to just declare nextPlayer instead, and also add the following code:
 
<syntaxhighlight lang=c#>
 
<syntaxhighlight lang=c#>
 
string nextPlayer;
 
string nextPlayer;
 +
Random rnd = new Random();
 
if (rnd.Next(100) <= 50)
 
if (rnd.Next(100) <= 50)
 
   nextPlayer = "a";
 
   nextPlayer = "a";
Line 18: Line 19:
 
Console.WriteLine(nextPlayer + " goes first!!");
 
Console.WriteLine(nextPlayer + " goes first!!");
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
This generates a random number between 0 and 100, we therefore check if the random is less than or equal to 50. The position of the if statement could change depending on where you would want it to go, at the moment it decides who's turn before asking if you want to load a saved game.

Latest revision as of 14:31, 1 April 2019

Where

Find the Game method, and look for:

string nextPlayer = "a";

Idea

create a random, then generate a random number, and then use it to decide if A or B goes first.

So we need to just declare nextPlayer instead, and also add the following code:

string nextPlayer;
Random rnd = new Random();
if (rnd.Next(100) <= 50)
   nextPlayer = "a";
else
   nextPlayer = "b";
Console.WriteLine(nextPlayer + " goes first!!");

This generates a random number between 0 and 100, we therefore check if the random is less than or equal to 50. The position of the if statement could change depending on where you would want it to go, at the moment it decides who's turn before asking if you want to load a saved game.