@inject GameState State
@using System.Drawing
@WinnerMessage
@ErrorMessage
@CurrentTurn
@for (var i = 0; i < 42; i++)
{
}
@for (var i = 0; i < 42; i++)
{
}
@code {
private string[] Pieces = new string[42];
private string WinnerMessage = string.Empty;
private string ErrorMessage = string.Empty;
private string CurrentTurn => (WinnerMessage == string.Empty) ? $"Player {State.PlayerTurn}'s Turn" : "";
private string ResetStyle => (WinnerMessage == string.Empty) ? "display: none;" : "";
[Parameter()]
public Color BoardColor { get; set; }
= ColorTranslator.FromHtml("yellow");
[Parameter()]
public Color Player1Color { get; set; }
= ColorTranslator.FromHtml("red");
[Parameter()]
public Color Player2Color { get; set; }
= ColorTranslator.FromHtml("blue");
protected override void OnInitialized()
{
State.ResetBoard();
}
private void PlayPiece(byte col)
{
ErrorMessage = string.Empty;
try
{
var landingRow = State.PlayPiece(col);
var cssClass = $"player{State.PlayerTurn} col{col} drop{landingRow}";
Pieces[State.CurrentTurn - 1] = cssClass;
}
catch (ArgumentException ex)
{
ErrorMessage = ex.Message;
}
WinnerMessage = State.CheckForWin() switch
{
GameState.WinState.Player1_Wins => "Player 1 Wins!",
GameState.WinState.Player2_Wins => "Player 2 Wins!",
GameState.WinState.Tie => "It's a tie!",
_ => ""
};
if (WinnerMessage != string.Empty) {
ResetStyle = string.Empty;
}
}
void ResetGame()
{
State.ResetBoard();
ResetStyle = "display: none;";
WinnerMessage = string.Empty;
ErrorMessage = string.Empty;
Pieces = new string[42];
}
}