/
rezvich
/
BlazorMatchGame
Обзор
Документация
Войти
/
rezvich
/
BlazorMatchGame
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Components/Pages/Home.razor
130 строк
2 KB
ItsEnd
Debug blazor rendermode
27 дек 2025, 18:16
27 дек 2025, 18:16
c02bf99
Код
Авторство
О чём код?
@page "/" @rendermode InteractiveServer @using System.Timers; <style> .container { width: 400px; } button { width: 100px; height: 100px; font-size: 50px; } </style> <div class="container"> <div class="row"> @for (var animalNumber = 0; animalNumber < shuffledAnimals.Count; animalNumber++) { var animal = shuffledAnimals[animalNumber]; var uniqieDescription = $"Button #{animalNumber}"; <div class="col-3"> <button @onclick="@(() => ButtonClick(animal, uniqieDescription))" type="button" class="btn btn-outline-dark"> <h1>@animal</h1> </button> </div> } </div> <div class="row"> <h2>Mathches found: @matchesFound</h2> </div> <div class="row"> <h2>Time = @timeDisplay</h2> </div> </div> @code { List<string> animalEmoji = new List<string>() { "🐶","🐶", "🐱","🐱", "🐭","🐭", "🐹","🐹", "🐰","🐰", "🦊","🦊", "🐻","🐻", "🦁","🦁" }; List<string> shuffledAnimals = new List<string>(); int matchesFound = 0; Timer timer = new Timer(); int secodns = 0; string timeDisplay = string.Empty; protected override void OnInitialized() { timer = new Timer(100); timer.Elapsed += Timer_tick; SetUpGame(); } private void SetUpGame() { Random random = new Random(); shuffledAnimals = animalEmoji .OrderBy(item => random.Next()) .ToList(); matchesFound = 0; secodns = 0; } string lastAnimalEmojiFound = string.Empty; string lastDescription = string.Empty; private void ButtonClick(string animal, string animalDescription) { if (lastAnimalEmojiFound == string.Empty) { lastAnimalEmojiFound = animal; lastDescription = animalDescription; timer.Start(); } else if (lastAnimalEmojiFound == animal && (animalDescription != lastDescription)) { lastAnimalEmojiFound = string.Empty; shuffledAnimals = shuffledAnimals .Select(a => a.Replace(animal, string.Empty)) .ToList(); matchesFound++; if (matchesFound == 8) { timer.Stop(); timeDisplay += " - Play again?"; SetUpGame(); } } else { lastAnimalEmojiFound = string.Empty; } } private void Timer_tick(Object source, ElapsedEventArgs e) { InvokeAsync(() => { secodns++; timeDisplay = (secodns / 10F) .ToString("0.0s"); StateHasChanged(); } ); } }