by SamProf
@page "/" <PageTitle>Penalty Shootout</PageTitle> <p>@($"p_Diff1:\t{_pDiff1:P}")</p> <p>@($"p_Diff2:\t{_pDiff2:P}")</p> <p>@($"p_Diff3:\t{_pDiff3:P}")</p> @code { private double _pDiff1 = 0; private double _pDiff2 = 0; private double _pDiff3 = 0; protected override void OnInitialized() { base.OnInitialized(); // create an array with number from 0 to 1023 in binary format // {00000000, 00000001, 00000010, 00000011, ...} var binaryArray = Enumerable.Range(0, 1024).Select(x => Convert.ToString(x, 2).PadLeft(10, '0')).ToArray(); // for each element in the array, take the binary number and convert it to a bool array // {00000000} -> {false, false, false, false, false, false, false, false, false, false} // {00000001} -> {false, false, false, false, false, false, false, false, false, true} // ... var boolArray = binaryArray.Select(x => x.Select(y => y == '1').ToArray()).ToArray(); // split each bool array into 2 parts of 5 elements each and write them to a new 2D array // {false, false, false, false, false, false, false, false, false, false} -> {{false, false, false, false, false}, {false, false, false, false, false}} // {false, false, false, false, false, false, false, false, false, true} -> {{false, false, false, false, false}, {false, false, false, false, true}} // ... var splitArray = boolArray.Select(x => new bool[][] { x.Take(5).ToArray(), x.Skip(5).ToArray() }).ToArray(); // number of diff1, diff2, diff3 var nDiff1 = 0; var nDiff2 = 0; var nDiff3 = 0; // for each element in the split array foreach (var item in splitArray) { var goalsTeam1 = 0; var goalsTeam2 = 0; // for each round for (var i = 0; i < 5; i++) { // if team 1 can still win if (Math.Abs(goalsTeam2 - goalsTeam1) <= 5 - i) { // if team 1 scores if (item[0][i]) goalsTeam1++; } // if team 2 can still win if (Math.Abs(goalsTeam1 - goalsTeam2) <= 5 - i) { // if team 2 scores if (item[1][i]) goalsTeam2++; } } // add the difference to the respective counter switch (Math.Abs(goalsTeam1 - goalsTeam2)) { case 0: case 1: nDiff1++; break; case 2: nDiff2++; break; case 3: nDiff3++; break; default: throw new Exception("This cannot be and should not happen."); break; } } // calculate the probabilities of diff1, diff2, diff3 _pDiff1 = nDiff1 / 1024d; _pDiff2 = nDiff2 / 1024d; _pDiff3 = nDiff3 / 1024d; } }
namespace BlazorFiddleProject { using Microsoft.Extensions.DependencyInjection; public class Startup { public void ConfigureServices(IServiceCollection services) { } // Warning: IComponentsApplicationBuilder - not the original .NET 6.0 interface. This was done to make old saves work. public void Configure(IComponentsApplicationBuilder app) { app.AddComponent<App>("app"); } } }
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width"> <title>BlazorFiddleProject</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link href="_content/MatBlazor/dist/matBlazor.css" rel="stylesheet" /> <script src="_content/MatBlazor/dist/matBlazor.js"></script> <style> app { } </style> <script type="text/javascript"> </script> </head> <body> <app>Loading...</app> <script src="_framework/blazor.webassembly.js"></script> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> </body> </html>

Add component

BlazorFiddle was updated from Blazor 0.7 to .NET 6.0. Your old source code could not work. You need to upgrade to latest.