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

Tug Of War #38

Merged
merged 4 commits into from
Jun 1, 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/Tug Of War Build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Tug Of War Build
on:
push:
paths:
- 'Projects/Tug Of War/**'
pull_request:
paths:
- 'Projects/Tug Of War/**'
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\Tug Of War\Tug Of War.csproj" --configuration Release
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
"console": "externalTerminal",
"stopAtEntry": false,
},
{
"name": "Tug Of War",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "Build Tug Of War",
"program": "${workspaceFolder}/Projects/Tug Of War/bin/Debug/Tug Of War.dll",
"cwd": "${workspaceFolder}/Projects/Tug Of War/bin/Debug",
"console": "externalTerminal",
"stopAtEntry": false,
},
{
"name": "Whack A Mole",
"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 @@ -2,6 +2,19 @@
"version": "2.0.0",
"tasks":
[
{
"label": "Build Tug Of War",
"command": "dotnet",
"type": "process",
"args":
[
"build",
"${workspaceFolder}/Projects/Tug Of War/Tug Of War.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary",
],
"problemMatcher": "$msCompile",
},
{
"label": "Build Tower Of Hanoi",
"command": "dotnet",
Expand Down
153 changes: 153 additions & 0 deletions Projects/Tug Of War/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using System.Threading;

try
{
while (true)
{
int position = 0;
const int displacement = 10;
string L() => new(' ', displacement + position + 4);
string R() => new(' ', displacement - position + 4);
string Ground =
new string(' ', 2) +
new string('-', displacement + (15 - displacement) + 2) +
new string('=', displacement * 2 + 2) +
new string('-', displacement + (15 - displacement) + 2) +
new string(' ', 2);
bool frame_a = false;
Console.Clear();
Console.WriteLine(@"
Tug Of War

Out pull your opponent in a rope pulling
competition. Mash the [left]+[right] arrow
keys and/or the [A]+[D] keys to pull on the
rope. First player to pull the center of the
rope into their boundary wins.

Choose Your Opponent:
[1] Easy.......2 mashes per second
[2] Medium.....4 mashes per second
[3] Hard.......8 mashes per second
[4] Harder....16 mashes per second
[escape] give up");
int? requiredMash = null;
while (requiredMash is null)
{
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.D1 or ConsoleKey.NumPad1: requiredMash = 02; break;
case ConsoleKey.D2 or ConsoleKey.NumPad2: requiredMash = 04; break;
case ConsoleKey.D3 or ConsoleKey.NumPad3: requiredMash = 08; break;
case ConsoleKey.D4 or ConsoleKey.NumPad4: requiredMash = 16; break;
case ConsoleKey.Escape: return;
}
}
Console.Clear();
int mash = 0;
int presses = 0;
int sleeps = 0;
ConsoleKey lastKey = default;
DateTime start = DateTime.Now;
while (true)
{
while (Console.KeyAvailable)
{
ConsoleKey key = Console.ReadKey(true).Key;
if (key is ConsoleKey.Escape)
{
return;
}
else if (lastKey is not default(ConsoleKey) &&
key is ConsoleKey.A or ConsoleKey.D or ConsoleKey.LeftArrow or ConsoleKey.RightArrow &&
key != lastKey)
{
presses++;
mash++;
lastKey = default;
}
else if (key is ConsoleKey.A or ConsoleKey.D or ConsoleKey.LeftArrow or ConsoleKey.RightArrow)
{
lastKey = key;
}
}
if (sleeps is 2)
{
position = mash < requiredMash.Value
? position + 1
: position - 1;
sleeps = 0;
mash = 0;
if (Math.Abs(position) >= displacement)
{
break;
}
}
Console.CursorVisible = false;
Console.SetCursorPosition(0, 0);
Console.WriteLine();
Console.WriteLine(" Tug Of War");
Console.WriteLine();
Console.Write(frame_a
?
$@"{L()}o o {R()}{"\n"}" +
$@"{L()}LL-------------+-------------JJ\{R()}{"\n"}" +
$@"{L()}\\ //{R()}{"\n"}" +
$@"{L()}| \ / |{R()}{"\n"}"
:
$@"{L()} o o{R()}{"\n"}" +
$@"{L()}/LL-------------+-------------JJ{R()}{"\n"}" +
$@"{L()}\\ //{R()}{"\n"}" +
$@"{L()}| \ / |{R()}{"\n"}");
Console.WriteLine(Ground);
Console.WriteLine();
Console.WriteLine(frame_a
? " *** Mash [A]+[D] or [Left]+[Right] ***"
: " ''' Mash [A]+[D] or [Left]+[Right] '''");
Thread.Sleep(500);
sleeps++;
frame_a = !frame_a;
}
bool win = position < 0;
double seconds = (DateTime.Now - start).TotalSeconds;
double average = presses / seconds;
Console.Clear();
Console.WriteLine();
Console.WriteLine(" Tug Of War");
Console.WriteLine();
Console.Write(win
?
$@"{L()}o {R()}{"\n"}" +
$@"{L()}LL------------+------. o___ {R()}{"\n"}" +
$@"{L()}\\ \// \\__{R()}{"\n"}" +
$@"{L()}| \ \_____\ {R()}{"\n"}"
:
$@"{L()} o{R()}{"\n"}" +
$@"{L()} ___o .------+------------JJ{R()}{"\n"}" +
$@"{L()}__// \\/ //{R()}{"\n"}" +
$@"{L()} /_____/ / |{R()}{"\n"}");
Console.WriteLine(Ground);
Console.WriteLine();
Console.WriteLine(" You " + (win ? "Win!" : "Lose!"));
Console.WriteLine($" Average: {average:0.##} mashes per second");
Console.WriteLine(" [enter] return to menu");
Console.WriteLine(" [escape] exit game");
bool enterPressed = false;
while (!enterPressed)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter: enterPressed = true; break;
case ConsoleKey.Escape: return;
}
}
}
}
finally
{
Console.CursorVisible = true;
Console.Clear();
Console.Write("Tug Of War was closed.");
}
45 changes: 45 additions & 0 deletions Projects/Tug Of War/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<h1 align="center">
Tug Of War
</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/Tug%20Of%20War%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)**

Tug Of War is a rope pulling competition. The first person to pull the center of the rope into their territory wins. To pull, mash the keys `A`+`D` or `←`+`→` as fast as you can.

```
Tug Of War

o o
LL-------------+-------------JJ\
\\ //
| \ / |
-----------------======================-----------------

*** Mash [A]+[D] or [Left]+[Right] ***
```

## Input

- `A`, `D`, `←`, `→`: pull rope (mash)
- `1`, `2`, `3`, `4`: choose opponent
- `enter` confirm
- `escape` exit game

<p align="center">
You can play this game in your browser:
<br />
<a href="https://zacharypatten.github.io/dotnet-console-games/Tug%20Of%20War" 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/Tug Of War/Tug Of War.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Tug_Of_War</RootNamespace>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Loading