Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dice Game #31

Merged
merged 16 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/Dice Game Build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Dice Game Build
on:
push:
paths:
- 'Projects/Dice Game/**'
pull_request:
paths:
- 'Projects/Dice Game/**'
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
- name: dotnet build
run: dotnet build "Projects\Dice Game\Dice Game.csproj" --configuration Release
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
"console": "externalTerminal",
"stopAtEntry": false,
},
{
"name": "Dice Game",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "Build Dice Game",
"program": "${workspaceFolder}/Projects/Dice Game/bin/Debug/Dice Game.dll",
"cwd": "${workspaceFolder}/Projects/Dice Game/bin/Debug",
"console": "externalTerminal",
"stopAtEntry": false,
},
{
"name": "Quick Draw",
"type": "coreclr",
Expand Down
13 changes: 13 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,19 @@
],
"problemMatcher": "$msCompile",
},
{
"label": "Build Dice Game",
"command": "dotnet",
"type": "process",
"args":
[
"build",
"${workspaceFolder}/Projects/Dice Game/Dice Game.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary",
],
"problemMatcher": "$msCompile",
},
{
"label": "Build Solution",
"command": "dotnet",
Expand Down
8 changes: 8 additions & 0 deletions Projects/Dice Game/Dice Game.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
64 changes: 64 additions & 0 deletions Projects/Dice Game/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;

int playerPoints = 0;
int rivalPoints = 0;

Random random = new();

Console.WriteLine("Dice Game");
Console.WriteLine();
Console.WriteLine("In this game you and a computer Rival will play 10 rounds");
Console.WriteLine("where you will each roll a 6-sided dice, and the player");
Console.WriteLine("with the highest dice value will win the round. The player");
Console.WriteLine("who wins the most rounds wins the game. Good luck!");
Console.WriteLine();
Console.Write("Press any key to start...");
Console.ReadKey(true);
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"Round {i + 1}");
int rivalRandomNum = random.Next(1, 7);
Console.WriteLine("Rival rolled a " + rivalRandomNum);
Console.Write("Press any key to roll the dice...");
Console.ReadKey(true);
Console.WriteLine();
int playerRandomNum = random.Next(1, 7);
Console.WriteLine("You rolled a " + playerRandomNum);
if (playerRandomNum > rivalRandomNum)
{
playerPoints++;
Console.WriteLine("You won this round.");
}
else if (playerRandomNum < rivalRandomNum)
{
rivalPoints++;
Console.WriteLine("The Rival won this round.");
}
else
{
Console.WriteLine("This round is a draw!");
}
Console.WriteLine($"The score is now - You : {playerPoints}. Rival : {rivalPoints}.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
Console.WriteLine();
Console.WriteLine();
}
Console.WriteLine("Game over.");
Console.WriteLine($"The score is now - You : {playerPoints}. Rival : {rivalPoints}.");
if (playerPoints > rivalPoints)
{
Console.WriteLine("You won!");
}
else if (playerPoints < rivalPoints)
{
Console.WriteLine("You lost!");
}
else
{
Console.WriteLine("This game is a draw.");
}
Console.Write("Press any key to exit...");
Console.ReadKey(true);
30 changes: 30 additions & 0 deletions Projects/Dice Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<h1 align="center">
Dice Game
</h1>

<p align="center">
<a href="https://github.com/ZacharyPatten/dotnet-console-games" alt="GitHub repo"><img alt="flat" src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/github-repo-black.svg"></a>
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/" alt="GitHub repo"><img alt="Language C#" src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/language-csharp.svg"></a>
<a href="https://dotnet.microsoft.com/download"><img src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/dotnet-badge.svg" title="Target Framework" alt="Target Framework"></a>
<a href="https://github.com/ZacharyPatten/dotnet-console-games/actions"><img src="https://github.com/ZacharyPatten/dotnet-console-games/workflows/Dice%20Game%20Build/badge.svg" title="Goto Build" alt="Build"></a>
<a href="https://discord.gg/4XbQbwF" alt="Discord"><img src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/discord-badge.svg" title="Go To Discord Server" alt="Discord"/></a>
<a href="https://github.com/ZacharyPatten/dotnet-console-games/blob/master/LICENSE" alt="license"><img src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/license-MIT-green.svg" /></a>
</p>

**[Source Code](Program.cs)**

In Dice Game you and a computer Rival will play 10 rounds where you will each roll a 6-sided dice, and the player with the highest dice value will win the round. The player who wins the most rounds wins the game. Good luck!

## Input

- any key press to confirm prompts

<p align="center">
You can play this game in your browser:
<br />
<a href="https://zacharypatten.github.io/dotnet-console-games/Dice Game" alt="Play Now">
<sub><img height="40"src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/play-badge.svg" title="Play Now" alt="Play Now"/></sub>
</a>
<br />
<sup>Hosted On GitHub Pages</sup>
</p>
9 changes: 9 additions & 0 deletions Projects/Website/BlazorConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ public void WriteNoRefresh(char c)
View[row, column] = View[row + 1, column];
}
}
for (int column = 0; column < View.GetLength(1); column++)
{
View[View.GetLength(0) - 1, column] = new()
{
Char = ' ',
BackgroundColor = BackgroundColor,
ForegroundColor = ForegroundColor
};
}
CursorTop--;
}
View[CursorTop, CursorLeft] = new()
Expand Down
78 changes: 78 additions & 0 deletions Projects/Website/Games/Dice Game/Dice Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Threading.Tasks;

namespace Website.Games.Dice_Game;

public class Dice_Game
{
public readonly BlazorConsole Console = new();

public async Task Run()
{
int playerPoints = 0;
int rivalPoints = 0;

Random random = new();

await Console.WriteLine("Dice Game");
await Console.WriteLine();
await Console.WriteLine("In this game you and a computer Rival will play 10 rounds");
await Console.WriteLine("where you will each roll a 6-sided dice, and the player");
await Console.WriteLine("with the highest dice value will win the round. The player");
await Console.WriteLine("who wins the most rounds wins the game. Good luck!");
await Console.WriteLine();
await Console.Write("Press any key to start...");
await Console.ReadKey(true);
await Console.WriteLine();
await Console.WriteLine();
for (int i = 0; i < 10; i++)
{
await Console.WriteLine($"Round {i + 1}");
int rivalRandomNum = random.Next(1, 7);
await Console.WriteLine("Rival rolled a " + rivalRandomNum);
await Console.Write("Press any key to roll the dice...");
await Console.ReadKey(true);
await Console.WriteLine();
int playerRandomNum = random.Next(1, 7);
await Console.WriteLine("You rolled a " + playerRandomNum);
if (playerRandomNum > rivalRandomNum)
{
playerPoints++;
await Console.WriteLine("You won this round.");
}
else if (playerRandomNum < rivalRandomNum)
{
rivalPoints++;
await Console.WriteLine("The Rival won this round.");
}
else
{
await Console.WriteLine("This round is a draw!");
}
await Console.WriteLine($"The score is now - You : {playerPoints}. Rival : {rivalPoints}.");
await Console.Write("Press any key to continue...");
await Console.ReadKey(true);
await Console.WriteLine();
await Console.WriteLine();
}
await Console.WriteLine("Game over.");
await Console.WriteLine($"The score is now - You : {playerPoints}. Rival : {rivalPoints}.");
if (playerPoints > rivalPoints)
{
await Console.WriteLine("You won!");
}
else if (playerPoints < rivalPoints)
{
await Console.WriteLine("You lost!");
}
else
{
await Console.WriteLine("This game is a draw.");
}
await Console.Write("Press any key to exit...");
await Console.ReadKey(true);
await Console.Clear();
await Console.Write("Dice Game was closed.");
await Console.Refresh();
}
}
47 changes: 47 additions & 0 deletions Projects/Website/Pages/Dice Game.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@using System

@page "/Dice Game"

<PageTitle>Dice Game</PageTitle>

<h1>Dice&nbsp;Game</h1>

<a href="https://github.com/ZacharyPatten/dotnet-console-games/tree/main/Projects/Dice%20Game" alt="Go To Readme">
<img title="Go To Readme" alt="Go To Readme" src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/readme-black.svg">
</a>

<div class="console-window text-center my-3" @onkeydown="@Console.OnKeyDown" tabindex="0">
<div class="d-inline-block bg-dark text-light border p-2 text-start shadow padding-0">
<pre class="console">
<code>@Console.State</code>
</pre>
</div>
<div>
<button class="btn btn-primary" @onclick="() => Console.EnqueueInput(ConsoleKey.Enter) ">enter</button>
</div>
</div>

<div class="alert alert-secondary" role="alert">
&#9000; Keyboard input is supported if you <strong>click</strong> on the game.
</div>

<div class="alert alert-secondary" role="alert">
&#8635; You can restart the game by <strong>refreshing</strong> the page.
</div>

@code
{
Games.Dice_Game.Dice_Game Game;
BlazorConsole Console;

public Dice_Game()
{
Game = new();
Console = Game.Console;
Console.WindowWidth = 60;
//Console.WindowHeight = PLACEHOLDER;
Console.StateHasChanged = StateHasChanged;
}

protected override void OnInitialized() => InvokeAsync(Game.Run);
}
5 changes: 5 additions & 0 deletions Projects/Website/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<span class="oi oi-play-circle" aria-hidden="true"></span> Rock Paper Scissors
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="Dice Game">
<span class="oi oi-play-circle" aria-hidden="true"></span> Dice Game
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="Quick Draw">
<span class="oi oi-play-circle" aria-hidden="true"></span> Quick Draw
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
|:-|:-|:-|
|[Guess&nbsp;A&nbsp;Number](https://github.com/ZacharyPatten/dotnet-console-games/tree/main/Projects/Guess%20A%20Number)|0|<sub><a href="https://zacharypatten.github.io/dotnet-console-games/Guess%20A%20Number" alt="Play Now"><img src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/play-badge.svg" title="Play Now" alt="Play Now"/></a>&nbsp;<a href="https://github.com/ZacharyPatten/dotnet-console-games/actions"><img src="https://github.com/ZacharyPatten/dotnet-console-games/workflows/Guess%20A%20Number%20Build/badge.svg" title="Go to Action" alt="Go to Action"></a></sub>|
|[Rock&nbsp;Paper&nbsp;Scissors](https://github.com/ZacharyPatten/dotnet-console-games/tree/main/Projects/Rock%20Paper%20Scissors)|0|<sub><a href="https://zacharypatten.github.io/dotnet-console-games/Rock%20Paper%20Scissors" alt="Play Now"><img src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/play-badge.svg" title="Play Now" alt="Play Now"/></a>&nbsp;<a href="https://github.com/ZacharyPatten/dotnet-console-games/actions"><img src="https://github.com/ZacharyPatten/dotnet-console-games/workflows/Rock%20Paper%20Scissors%20Build/badge.svg" title="Go to Action" alt="Go to Action"></a></sub>|
|[Dice&nbsp;Game](https://github.com/ZacharyPatten/dotnet-console-games/tree/main/Projects/Dice%20Game)|0|<sub><a href="https://zacharypatten.github.io/dotnet-console-games/Dice%20Game" alt="Play Now"><img src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/play-badge.svg" title="Play Now" alt="Play Now"/></a>&nbsp;<a href="https://github.com/ZacharyPatten/dotnet-console-games/actions"><img src="https://github.com/ZacharyPatten/dotnet-console-games/workflows/Dice%20Game%20Build/badge.svg" title="Go to Action" alt="Go to Action"></a></sub>|
|[Quick&nbsp;Draw](https://github.com/ZacharyPatten/dotnet-console-games/tree/main/Projects/Quick%20Draw)|0|<sub><a href="https://zacharypatten.github.io/dotnet-console-games/Quick%20Draw" alt="Play Now"><img src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/play-badge.svg" title="Play Now" alt="Play Now"/></a>&nbsp;<a href="https://github.com/ZacharyPatten/dotnet-console-games/actions"><img src="https://github.com/ZacharyPatten/dotnet-console-games/workflows/Quick%20Draw%20Build/badge.svg" title="Go to Action" alt="Go to Action"></a></sub>|
|[Whack&nbsp;A&nbsp;Mole](https://github.com/ZacharyPatten/dotnet-console-games/tree/main/Projects/Whack%20A%20Mole)|1|<sub><a href="https://zacharypatten.github.io/dotnet-console-games/Whack%20A%20Mole" alt="Play Now"><img src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/play-badge.svg" title="Play Now" alt="Play Now"/></a>&nbsp;<a href="https://github.com/ZacharyPatten/dotnet-console-games/actions"><img src="https://github.com/ZacharyPatten/dotnet-console-games/workflows/Whack%20A%20Mole%20Build/badge.svg" title="Go to Action" alt="Go to Action"></a></sub>|
|[Tic&nbsp;Tac&nbsp;Toe](https://github.com/ZacharyPatten/dotnet-console-games/tree/main/Projects/Tic%20Tac%20Toe)|1|<sub><a href="https://zacharypatten.github.io/dotnet-console-games/Tic%20Tac%20Toe" alt="Play Now"><img src="https://raw.githubusercontent.com/ZacharyPatten/dotnet-console-games/main/.github/resources/play-badge.svg" title="Play Now" alt="Play Now"/></a>&nbsp;<a href="https://github.com/ZacharyPatten/dotnet-console-games/actions"><img src="https://github.com/ZacharyPatten/dotnet-console-games/workflows/Tic%20Tac%20Toe%20Build/badge.svg" title="Go to Action" alt="Go to Action"></a></sub>|
Expand Down
6 changes: 6 additions & 0 deletions dotnet-console-games-and-website.sln
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tents", "Projects\Tents\Ten
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Guess A Number", "Projects\Guess A Number\Guess A Number.csproj", "{63300EAA-2013-43F4-8628-9FA979C83E44}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiceGame", "Projects\Dice Game\Dice Game.csproj", "{89EBF300-0A28-4054-A807-D6A67544CE4B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -225,6 +227,10 @@ Global
{63300EAA-2013-43F4-8628-9FA979C83E44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63300EAA-2013-43F4-8628-9FA979C83E44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63300EAA-2013-43F4-8628-9FA979C83E44}.Release|Any CPU.Build.0 = Release|Any CPU
{89EBF300-0A28-4054-A807-D6A67544CE4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89EBF300-0A28-4054-A807-D6A67544CE4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89EBF300-0A28-4054-A807-D6A67544CE4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89EBF300-0A28-4054-A807-D6A67544CE4B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions dotnet-console-games.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Projects\\Beep Pad\\Beep Pad.csproj",
"Projects\\Bound\\Bound.csproj",
"Projects\\Connect 4\\Connect 4.csproj",
"Projects\\Dice Game\\Dice Game.csproj",
"Projects\\Draw\\Draw.csproj",
"Projects\\Drive\\Drive.csproj",
"Projects\\Fighter\\Fighter.csproj",
Expand Down