forked from dotnet/dotnet-console-games
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#38 from ZacharyPatten/tug-of-war
Tug Of War
- Loading branch information
Showing
12 changed files
with
484 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.