Hi so I have completed a rock paper scissors game a and these are the winning rules
<pre lang="ASP.NET"><pre>
<pre>@inject Choices choices
<div class="container">
<div class="row mt-3">
<div class="col-6 text-center text-uppercase"><h1>You : @playerScore</h1></div>
<div class="col-6 text-center text-uppercase"><h1>Computer :@computerScore</h1></div>
</div>
<div class="row mt-3" style="height:200px;">
<div class="col-6 text-center choosen-image">
@playerChoiceImage
</div>
<div class="col-6 text-center choosen-image">
@computerChoiceImage
</div>
</div>
<div class="text-center mt-3" style="height:40px;">
<h3>@message</h3>
</div>
<div class="row mt-3">
@foreach(var i in Enumerable.Range(0,3))
{
var choice = i;
<div class="col-4 text-center image-to-choose" @onclick="(()=> OnPlayerMadeChoice(choice))">
@choices[i]
</div>
}
</div>
<div class="text-center mt-5">
<button class="btn btn-primary" @onclick="Reset">Reset</button>
</div>
</div>
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace RockPaperScissors.Components
{
public partial class Game
{
private int playerScore;
private int computerScore;
private string playerChoiceImage;
private string computerChoiceImage;
private string message;
private readonly Action<int> playerMadeChoice;
private readonly Action<int> computerMadeChoice;
private readonly Action<int, int> bothMadeChoice;
private readonly Random random;
public Game()
{
random = new Random();
playerMadeChoice = (playerChoice) =>
{
playerChoiceImage = choices[playerChoice];
computerMadeChoice(playerChoice);
};
computerMadeChoice = (playerChoice) =>
{
var computerChoice = random.Next(2);
computerChoiceImage = choices[computerChoice];
StateHasChanged();
bothMadeChoice(playerChoice, computerChoice);
};
bothMadeChoice = (playerChoice, computerChoice) =>
{
var (_, _, playerWon, computerWon, message) = WinningRules[playerChoice, computerChoice];
this.message = message;
playerScore = playerWon ? ++playerScore : playerScore;
computerScore = computerWon ? ++computerScore : computerScore;
StateHasChanged();
};
}
protected override void OnInitialized() => SetDefaultMessage();
private void SetDefaultMessage() => message = "Make a choice";
private void OnPlayerMadeChoice(int playerChoice) => playerMadeChoice(playerChoice);
private void Reset()
{
SetDefaultMessage();
playerScore = computerScore = 0;
playerChoiceImage = computerChoiceImage = string.Empty;
}
[Inject]
public WinningRules WinningRules { get; set; }
}
}
namespace RockPaperScissors.Components
{
public class Choices
{
private readonly Dictionary<int, string=""> choices =
new Dictionary<int, string="">
{
{0, "✊" },
{1, "✋" },
{2, "✌️" }
};
public string this[int choiceKey] => choices[choiceKey];
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace RockPaperScissors.Components
{
public class WinningRules
{
private readonly IList<(int, int, bool, bool, string)> winningRules =
new List<(int, int, bool, bool, string)>
{
(0,0, false, false, "You both select Rock, it's tie"),
(0, 1, false, true, "Paper beats Rock, you lose"),
(0, 2, true, false, "Rock beats Scissors, you win"),
(1, 0, true, false, "Paper beats Rock, you win"),
(1, 1, false, false, "You both select Paper, it's tie"),
(1, 2, false, true, "Scissors beats Paper, you lose"),
(2, 0, false, true, "Rock beats Scissors, you lose"),
(2, 1, true, false, "Scissors beats Paper, you win"),
(2, 2, false, false,"You both select Scissors, it's tie")
};
public (int, int, bool, bool, string) this[int playerChoice, int computerChoice]
=> winningRules.Single(w => w.Item1 == playerChoice && w.Item2 == computerChoice);
}
}
What I am looking to add is to allow the game to pick a winner after every 3 rounds, whoever wins 2 out of 3 matches gets to be the winner. Additionally, I am also hoping to add a feature where the game would inform users which were the most used moves to win the game.
What I have tried:
I have tried to look for ways of implementing the idea however I am not sure how I should. I have followed along with the tutorial and found the GitHub repository if the full project is needed
GitHub - estyval/RockPaperScissors: Rock Paper Scissors game with BlazorWebAssembly[
^]