Difference between revisions of "Add the ability to set the player names"
(6 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | Somewhere near the top of | + | Somewhere near the top of PlayGame, while you're declaring variables, you'll want to add the following code: |
<syntaxhighlight lang=Csharp> | <syntaxhighlight lang=Csharp> | ||
// something here... | // something here... | ||
− | Console.WriteLine("Player One, input your name:"); | + | Console.WriteLine("Player One, input your name:"); |
string PlayerOneName = Convert.ToString(Console.ReadLine()); | string PlayerOneName = Convert.ToString(Console.ReadLine()); | ||
Console.WriteLine("Player Two, input you name:"); | Console.WriteLine("Player Two, input you name:"); | ||
string PlayerTwoName = Convert.ToString(Console.ReadLine()); | string PlayerTwoName = Convert.ToString(Console.ReadLine()); | ||
// something here... | // something here... | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Lower down, there's a while loop that looks like this: | ||
+ | |||
+ | <syntaxhighlight lang=Csharp> | ||
+ | while (PlayerOneTilesPlayed <= MaxTilesPlayed && PlayerTwoTilesPlayed <= MaxTilesPlayed && PlayerOneTiles.Length < MaxHandSize && PlayerTwoTiles.Length < MaxHandSize) | ||
+ | { | ||
+ | HaveTurn("Player One", ref PlayerOneTiles, ref PlayerOneTilesPlayed, ref PlayerOneScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles); | ||
+ | Console.WriteLine(); | ||
+ | Console.WriteLine("Press Enter to continue"); | ||
+ | Console.ReadLine(); | ||
+ | Console.WriteLine(); | ||
+ | HaveTurn("Player Two", ref PlayerTwoTiles, ref PlayerTwoTilesPlayed, ref PlayerTwoScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Just swap "Player One" and "Player Two" with your variables for the player's names, like so: | ||
+ | |||
+ | <syntaxhighlight lang=Csharp> | ||
+ | while (PlayerOneTilesPlayed <= MaxTilesPlayed && PlayerTwoTilesPlayed <= MaxTilesPlayed && PlayerOneTiles.Length < MaxHandSize && PlayerTwoTiles.Length < MaxHandSize) | ||
+ | { | ||
+ | HaveTurn(PlayerOneName, ref PlayerOneTiles, ref PlayerOneTilesPlayed, ref PlayerOneScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles); | ||
+ | Console.WriteLine(); | ||
+ | Console.WriteLine("Press Enter to continue"); | ||
+ | Console.ReadLine(); | ||
+ | Console.WriteLine(); | ||
+ | HaveTurn(PlayerTwoName, ref PlayerTwoTiles, ref PlayerTwoTilesPlayed, ref PlayerTwoScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | A few changes also need to be made to DisplayWinner. | ||
+ | |||
+ | <syntaxhighlight lang=Csharp> | ||
+ | private static void DisplayWinner(int PlayerOneScore, int PlayerTwoScore, string PlayerOneName, string PlayerTwoName) | ||
+ | { | ||
+ | Console.WriteLine(); | ||
+ | Console.WriteLine("**** GAME OVER! ****"); | ||
+ | Console.WriteLine(); | ||
+ | Console.WriteLine(PlayerOneName + ", your score is " + PlayerOneScore); | ||
+ | Console.WriteLine(PlayerTwoName + ", your score is " + PlayerTwoScore); | ||
+ | if (PlayerOneScore > PlayerTwoScore) | ||
+ | { | ||
+ | Console.WriteLine(PlayerOneName + " wins!"); | ||
+ | } | ||
+ | else if (PlayerTwoScore > PlayerOneScore) | ||
+ | { | ||
+ | Console.WriteLine(PlayerTwoName + " wins!"); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.WriteLine("It is a draw!"); | ||
+ | } | ||
+ | Console.WriteLine(); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | When DisplayWinner is called from HaveTurn, make sure to pass it the new variables: | ||
+ | |||
+ | <syntaxhighlight lang=Csharp> | ||
+ | DisplayWinner(PlayerOneScore, PlayerTwoScore, PlayerOneName, PlayerTwoName); | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 14:05, 20 November 2017
Somewhere near the top of PlayGame, while you're declaring variables, you'll want to add the following code:
// something here...
Console.WriteLine("Player One, input your name:");
string PlayerOneName = Convert.ToString(Console.ReadLine());
Console.WriteLine("Player Two, input you name:");
string PlayerTwoName = Convert.ToString(Console.ReadLine());
// something here...
Lower down, there's a while loop that looks like this:
while (PlayerOneTilesPlayed <= MaxTilesPlayed && PlayerTwoTilesPlayed <= MaxTilesPlayed && PlayerOneTiles.Length < MaxHandSize && PlayerTwoTiles.Length < MaxHandSize)
{
HaveTurn("Player One", ref PlayerOneTiles, ref PlayerOneTilesPlayed, ref PlayerOneScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles);
Console.WriteLine();
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
Console.WriteLine();
HaveTurn("Player Two", ref PlayerTwoTiles, ref PlayerTwoTilesPlayed, ref PlayerTwoScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles);
}
Just swap "Player One" and "Player Two" with your variables for the player's names, like so:
while (PlayerOneTilesPlayed <= MaxTilesPlayed && PlayerTwoTilesPlayed <= MaxTilesPlayed && PlayerOneTiles.Length < MaxHandSize && PlayerTwoTiles.Length < MaxHandSize)
{
HaveTurn(PlayerOneName, ref PlayerOneTiles, ref PlayerOneTilesPlayed, ref PlayerOneScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles);
Console.WriteLine();
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
Console.WriteLine();
HaveTurn(PlayerTwoName, ref PlayerTwoTiles, ref PlayerTwoTilesPlayed, ref PlayerTwoScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles);
}
A few changes also need to be made to DisplayWinner.
private static void DisplayWinner(int PlayerOneScore, int PlayerTwoScore, string PlayerOneName, string PlayerTwoName)
{
Console.WriteLine();
Console.WriteLine("**** GAME OVER! ****");
Console.WriteLine();
Console.WriteLine(PlayerOneName + ", your score is " + PlayerOneScore);
Console.WriteLine(PlayerTwoName + ", your score is " + PlayerTwoScore);
if (PlayerOneScore > PlayerTwoScore)
{
Console.WriteLine(PlayerOneName + " wins!");
}
else if (PlayerTwoScore > PlayerOneScore)
{
Console.WriteLine(PlayerTwoName + " wins!");
}
else
{
Console.WriteLine("It is a draw!");
}
Console.WriteLine();
}
When DisplayWinner is called from HaveTurn, make sure to pass it the new variables:
DisplayWinner(PlayerOneScore, PlayerTwoScore, PlayerOneName, PlayerTwoName);