Skip to content

Commit 43d9c5f

Browse files
committed
Dice Game ported to blazor
1 parent 7c5f6eb commit 43d9c5f

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace Website.Games.Dice_Game;
5+
6+
public class Dice_Game
7+
{
8+
public readonly BlazorConsole Console = new();
9+
10+
public async Task Run()
11+
{
12+
int playerPoints = 0;
13+
int rivalPoints = 0;
14+
15+
Random random = new();
16+
17+
await Console.WriteLine("Dice Game");
18+
await Console.WriteLine();
19+
await Console.WriteLine("In this game you and a computer Rival will play 10 rounds");
20+
await Console.WriteLine("where you will each roll a 6-sided dice, and the player");
21+
await Console.WriteLine("with the highest dice value will win the round. The player");
22+
await Console.WriteLine("who wins the most rounds wins the game. Good luck!");
23+
await Console.WriteLine();
24+
await Console.Write("Press any key to start...");
25+
await Console.ReadKey(true);
26+
await Console.WriteLine();
27+
await Console.WriteLine();
28+
for (int i = 0; i < 10; i++)
29+
{
30+
await Console.WriteLine($"Round {i + 1}");
31+
int rivalRandomNum = random.Next(1, 7);
32+
await Console.WriteLine("Rival rolled a " + rivalRandomNum);
33+
await Console.Write("Press any key to roll the dice...");
34+
await Console.ReadKey(true);
35+
await Console.WriteLine();
36+
int playerRandomNum = random.Next(1, 7);
37+
await Console.WriteLine("You rolled a " + playerRandomNum);
38+
if (playerRandomNum > rivalRandomNum)
39+
{
40+
playerPoints++;
41+
await Console.WriteLine("You won this round.");
42+
}
43+
else if (playerRandomNum < rivalRandomNum)
44+
{
45+
rivalPoints++;
46+
await Console.WriteLine("The Rival won this round.");
47+
}
48+
else
49+
{
50+
await Console.WriteLine("This round is a draw!");
51+
}
52+
await Console.WriteLine($"The score is now - You : {playerPoints}. Rival : {rivalPoints}.");
53+
await Console.Write("Press any key to continue...");
54+
await Console.ReadKey(true);
55+
await Console.WriteLine();
56+
await Console.WriteLine();
57+
}
58+
await Console.WriteLine("Game over.");
59+
await Console.WriteLine($"The score is now - You : {playerPoints}. Rival : {rivalPoints}.");
60+
if (playerPoints > rivalPoints)
61+
{
62+
await Console.WriteLine("You won!");
63+
}
64+
else if (playerPoints < rivalPoints)
65+
{
66+
await Console.WriteLine("You lost!");
67+
}
68+
else
69+
{
70+
await Console.WriteLine("This game is a draw.");
71+
}
72+
await Console.Write("Press any key to exit...");
73+
await Console.ReadKey(true);
74+
await Console.Clear();
75+
await Console.Write("Dice Game was closed.");
76+
await Console.Refresh();
77+
}
78+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@using System
2+
3+
@page "/Dice Game"
4+
5+
<PageTitle>Dice Game</PageTitle>
6+
7+
<h1>Dice&nbsp;Game</h1>
8+
9+
<a href="https://github.com/ZacharyPatten/dotnet-console-games/tree/main/Projects/Dice%20Game" alt="Go To Readme">
10+
<img title="Go To Readme" alt="Go To Readme" src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/readme-black.svg">
11+
</a>
12+
13+
<div class="console-window text-center my-3" @onkeydown="@Console.OnKeyDown" tabindex="0">
14+
<div class="d-inline-block bg-dark text-light border p-2 text-start shadow padding-0">
15+
<pre class="console">
16+
<code>@Console.State</code>
17+
</pre>
18+
</div>
19+
<div>
20+
<button class="btn btn-primary" @onclick="() => Console.EnqueueInput(ConsoleKey.Enter) ">enter</button>
21+
</div>
22+
</div>
23+
24+
<div class="alert alert-secondary" role="alert">
25+
&#9000; Keyboard input is supported if you <strong>click</strong> on the game.
26+
</div>
27+
28+
<div class="alert alert-secondary" role="alert">
29+
&#8635; You can restart the game by <strong>refreshing</strong> the page.
30+
</div>
31+
32+
@code
33+
{
34+
Games.Dice_Game.Dice_Game Game;
35+
BlazorConsole Console;
36+
37+
public Dice_Game()
38+
{
39+
Game = new();
40+
Console = Game.Console;
41+
Console.WindowWidth = 60;
42+
//Console.WindowHeight = PLACEHOLDER;
43+
Console.StateHasChanged = StateHasChanged;
44+
}
45+
46+
protected override void OnInitialized() => InvokeAsync(Game.Run);
47+
}

Projects/Website/Shared/NavMenu.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<span class="oi oi-play-circle" aria-hidden="true"></span> Rock Paper Scissors
1919
</NavLink>
2020
</div>
21+
<div class="nav-item px-3">
22+
<NavLink class="nav-link" href="Dice Game">
23+
<span class="oi oi-play-circle" aria-hidden="true"></span> Dice Game
24+
</NavLink>
25+
</div>
2126
<div class="nav-item px-3">
2227
<NavLink class="nav-link" href="Quick Draw">
2328
<span class="oi oi-play-circle" aria-hidden="true"></span> Quick Draw

0 commit comments

Comments
 (0)