Difference between revisions of "Provide a method for the player to swap 5 random tiles from their hand"
Line 22: | Line 22: | ||
Then we need to actually add the logic for the command in the HaveTurn method with a new else-if/choice for 5. | Then we need to actually add the logic for the command in the HaveTurn method with a new else-if/choice for 5. | ||
+ | ==Simple Method== | ||
+ | |||
+ | <syntaxhighlight lang="C#"> | ||
+ | Random random = new Random(); | ||
+ | for (int i = 0; i < 5; i++) | ||
+ | { | ||
+ | PlayerTiles = PlayerTiles.Remove(random.Next(0,PlayerTiles.Length-1),1); | ||
+ | string newtile = TileQueue.Remove(); | ||
+ | PlayerTiles = PlayerTiles + newtile; | ||
+ | } | ||
+ | Console.WriteLine("New hand:" + PlayerTiles); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Advanced Method== | ||
<syntaxhighlight lang="C#"> | <syntaxhighlight lang="C#"> | ||
// Some stuff was here | // Some stuff was here |
Revision as of 11:18, 16 November 2017
To do this we must firstly add a new command to the GetChoice Method for this.
private static string GetChoice() {
string Choice;
Console.WriteLine();
Console.WriteLine("Either:");
Console.WriteLine(" enter the word you would like to play OR");
Console.WriteLine(" press 1 to display the letter values OR");
Console.WriteLine(" press 4 to view the tile queue OR");
Console.WriteLine(" press 7 to view your tiles again OR");
Console.WriteLine(" press 5 to swap 5 random tiles from hand"); // <- New Command here
Console.WriteLine(" press 0 to fill hand and stop the game.");
Console.Write("> ");
Choice = Console.ReadLine();
Console.WriteLine();
Choice = Choice.ToUpper();
return Choice;
}
Then we need to actually add the logic for the command in the HaveTurn method with a new else-if/choice for 5.
Simple Method
Random random = new Random();
for (int i = 0; i < 5; i++)
{
PlayerTiles = PlayerTiles.Remove(random.Next(0,PlayerTiles.Length-1),1);
string newtile = TileQueue.Remove();
PlayerTiles = PlayerTiles + newtile;
}
Console.WriteLine("New hand:" + PlayerTiles);
Advanced Method
// Some stuff was here
else if (Choice == "5") { // Swap 5 random tiles command
Random random = new Random(); // To select tiles to remove
foreach (int X in Enumerable.Range(0, 5)) {
String newTile = TileQueue.Remove(); // Get New Tile for hand
TileQueue.Add(); // Replace taken tile with random new one
int removalTileIndex = random.Next(0, PlayerName.Length-1);
PlayerTiles = PlayerTiles.Remove(removalTileIndex, 1).Insert(removalTileIndex, newTile);
}
Console.WriteLine("New Hand: {0}", PlayerTiles);
}
// Some stuff was here
The only part of the above method which I feel may be somewhat confusing is the assignment of the PlayerTiles variable. "String".Remove() and "String".Insert() both return a new string with the desired logic of the method executed. Meaning u can chain these commands as I've done above instead of perhaps doing this:
String removed = PlayerTiles.Remove(removalTileIndex, 1);
String inserted = removed.Insert(removalTileIndex, newTile);
PlayerTiles = inserted;
You cannot, NO MATTER WHAT, just call the instance method PlayerTiles.Remove() or PlayerTiles.Insert() and expect PlayerTiles variable to have that value stored automatically. Both these methods return a new string instead of changing the existing string upon which they are run.
Aside from that, I feel this method is quite simple.