Difference between revisions of "Provide a method for the player to swap 5 random tiles from their hand"
(→Simple Method) |
(→Simple Method) |
||
Line 34: | Line 34: | ||
Console.WriteLine("New hand:" + PlayerTiles); | Console.WriteLine("New hand:" + PlayerTiles); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | This method creates a random number, and then a for loop is made for 5 tiles. | ||
+ | Then the code removes 1 random tile from the list of player tiles (the reason it is PlayerTiles.Length-1 is because the list starts at one, whilst the position starts at 0). | ||
+ | It then adds 1 new tile to the end and repeats the process 5 times. | ||
==Advanced Method== | ==Advanced Method== |
Latest revision as of 11:27, 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);
This method creates a random number, and then a for loop is made for 5 tiles. Then the code removes 1 random tile from the list of player tiles (the reason it is PlayerTiles.Length-1 is because the list starts at one, whilst the position starts at 0). It then adds 1 new tile to the end and repeats the process 5 times.
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.