Difference between revisions of "Provide a method for the player to swap 5 random tiles from their hand"
(Created page with "To do this we must firstly add a new command to the GetChoice Method for this.") |
|||
Line 1: | Line 1: | ||
To do this we must firstly add a new command to the GetChoice Method for this. | To do this we must firstly add a new command to the GetChoice Method for this. | ||
+ | |||
+ | <syntaxhighlight lang="C#"> | ||
+ | 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; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Then we need to actually add the logic for the command in the HaveTurn method with a new else-if/choice for 5. | ||
+ | |||
+ | <syntaxhighlight lang="C#"> | ||
+ | // 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 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 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: | ||
+ | |||
+ | <syntaxhighlight lang="C#"> | ||
+ | String removed = PlayerTiles.Remove(removalTileIndex, 1); | ||
+ | String inserted = removed.Insert(removalTileIndex, newTile); | ||
+ | PlayerTiles = inserted; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 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. |
Revision as of 20:29, 14 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.
// 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.