Skip to content

Commit

Permalink
ez AI cardgame
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosux committed Jul 16, 2024
1 parent 7ec0ad7 commit 81c7be8
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 64 deletions.
1 change: 1 addition & 0 deletions Cards.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Optimize>true</Optimize>
</PropertyGroup>

</Project>
254 changes: 197 additions & 57 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,213 @@
class Program
using System.Text.RegularExpressions;

class Program
{
static void Main(string[] args)
{
Spore.Clear();
for(int i=0;i<CardType.Cards.Length;i++)
Console.Title = "Magic Card Trick";
Console.CursorVisible = false;
List<Card> deck = new List<Card>();
deck.Add(new Card(
$"<Red>♥<Reset>A",
[@" ", @" <Red>♥<Reset> ", @" "],
SporeTraceColor.White
));
for(int i=2;i<11;i++)
{
deck.Add(new Card(
$"<Red>♥<Reset>{i}",
[@" ", @" <Red>♥<Reset> ", @" "],
SporeTraceColor.White
));
}
deck.Add(new Card(
$"<Red>♥<Reset>J",
[@" ", @" <Red>♥<Reset> ", @" "],
SporeTraceColor.White
));
deck.Add(new Card(
$"<Red>♥<Reset>Q",
[@" ", @" <Red>♥<Reset> ", @" "],
SporeTraceColor.White
));
deck.Add(new Card(
$"<Red>♥<Reset>K",
[@" ", @" <Red>♥<Reset> ", @" "],
SporeTraceColor.White
));
string savedName = "";
int selectedCard = 0;
while(true)
{
Spore.Clear();
Spore.Color("Pick a card (using the Up/Down arrows and Enter to select):");
for(int i=0;i<deck.Count;i++)
{
Card c = deck[i];
if(i == selectedCard){c.RimColor = SporeTraceColor.Green;}else{c.RimColor = SporeTraceColor.White;}
c.DisplayCard(0, i+1);
}
ConsoleKey key = Console.ReadKey(true).Key;
if(key == ConsoleKey.UpArrow){
selectedCard--;
}else if(key == ConsoleKey.DownArrow){
selectedCard++;
}else if(key == ConsoleKey.Enter){
Spore.Clear();
savedName = deck[selectedCard].Name;
break;
}
selectedCard = Math.Clamp(selectedCard, 0, deck.Count-1);
}
Random rng = new Random();
for(int i=0;i<5;i++)
{
Spore.Clear();
Spore.Color("Shuffling the deck...");
Shuffle(deck);
for(int j=0;j<deck.Count;j++)
{
Card c = deck[j];
c.RimColor = SporeTraceColor.White;
c.DisplayCard(0, j+1);
}
Thread.Sleep(200);
}
for(int i=0;i<10;i++)
{
Spore.Clear();
Spore.Color("Finding your card...");
deck[rng.Next(deck.Count)].DisplayCard(0, 1);
Thread.Sleep(200);
}
bool selection = false;
while(true)
{
string cardType = CardType.Cards[i];
Spore.Color(
$"<Reset>┌{cardType}<Red>♥<Reset>{(cardType.Length == 1 ? '─' : "")}──┐ "
+
$"<Reset>┌{cardType}<White>♣<Reset>{(cardType.Length == 1 ? '─' : "")}──┐ "
+
$"<Reset>┌{cardType}<Red>♦<Reset>{(cardType.Length == 1 ? '─' : "")}──┐ "
+
$"<Reset>┌{cardType}<White>♠<Reset>{(cardType.Length == 1 ? '─' : "")}──┐\n"
);
if(i == CardType.Cards.Length-1)
Spore.Clear();
Spore.Color("Is this your card?");
foreach(Card c in deck)
{
Spore.Color(
"│ │ │ │ │ │ │ │ \n"+
"│ │ │ │ │ │ │ │ \n"+
"│ │ │ │ │ │ │ │ \n"+
$"└──{(cardType.Length == 1 ? '─' : "")}<Red>♥<Reset>{cardType}┘ "+
$"└──{(cardType.Length == 1 ? '─' : "")}<White>♣<Reset>{cardType}┘ "+
$"└──{(cardType.Length == 1 ? '─' : "")}<Red>♦<Reset>{cardType}┘ "+
$"└──{(cardType.Length == 1 ? '─' : "")}<White>♠<Reset>{cardType}\n"
);
if(c.Name == savedName)
{
c.DisplayCard(0, 1);
break;
}
}
Spore.Color($"{(!selection ? "<Red>" : "<Reset>")}>No {(selection ? "<Green>" : "<Reset>")}>Yes");
ConsoleKey key = Console.ReadKey(true).Key;
if(key == ConsoleKey.LeftArrow){selection = false;}
else if(key == ConsoleKey.RightArrow){selection = true;}
else if(key == ConsoleKey.Enter){break;}
}
Spore.Clear();
if(!selection)
{
Spore.Color("LIAR!!!");
}else{
Spore.Color("Thanks for playing!");
}
Console.ReadKey(true);
}

public static void Shuffle(List<Card> list)
{
Random rng = new Random();
int n = list.Count;
while(n > 1){
n--;
int k = rng.Next(n + 1);
Card value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}

public static class CardType
public struct Card
{
public static readonly string Ace = "A";
public static readonly string Two = "2";
public static readonly string Three = "3";
public static readonly string Four = "4";
public static readonly string Five = "5";
public static readonly string Six = "6";
public static readonly string Seven = "7";
public static readonly string Eight = "8";
public static readonly string Nine = "9";
public static readonly string Ten = "10";
public static readonly string Jack = "J";
public static readonly string Queen = "Q";
public static readonly string King = "K";
public static readonly string[] Cards = new string[]{
Ace,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
};
private static readonly string _colorSplitPattern = @"(<Black>|<DarkBlue>|<DarkGreen>|<DarkCyan>|<DarkRed>|<DarkMagenta>|<DarkYellow>|<Gray>|<DarkGray>|<Blue>|<Green>|<Cyan>|<Red>|<Magenta>|<Yellow>|<White>|<Previous>|<Reset>)";
public string Name = "a";
public SporeTraceColor RimColor = SporeTraceColor.White;
public string[] Art = [
" ",
" ",
" "
];

public Card(){}
public Card(string name, string[] art, SporeTraceColor rimColor=SporeTraceColor.White)
{
if(Regex.Replace(name, _colorSplitPattern, "").Length > 5){throw new Exception("Name parameter must be between 0-5 characters long excluding colors.");}
if(art.Length != 3 || Regex.Replace(art[0], _colorSplitPattern, "").Length != 5 || Regex.Replace(art[1], _colorSplitPattern, "").Length != 5 || Regex.Replace(art[2], _colorSplitPattern, "").Length != 5){throw new Exception("Art parameter must be 3 strings of 5 chars long");}
Name = name;
Art = art;
RimColor = rimColor;
}

public void DisplayCard(int x=0, int y=0)
{
PrintColoredText(
$"<{RimColor}>┌<Reset>{Format(Name, 5, '─')}<{RimColor}>┐<Reset>\n"+
$"<{RimColor}>│<Reset>{Center(Art[0], 5, ' ')}<{RimColor}>│<Reset>\n"+
$"<{RimColor}>│<Reset>{Center(Art[1], 5, ' ')}<{RimColor}>│<Reset>\n"+
$"<{RimColor}>│<Reset>{Center(Art[2], 5, ' ')}<{RimColor}>│<Reset>\n"+
$"<{RimColor}>└<Reset>{ReverseFormat(Name, 5, '─')}<{RimColor}>┘<Reset>\n",
x,
y
);
}

private void PrintColoredText(string text, int startX, int startY)
{
int currentY = startY;
foreach(string line in text.Split('\n'))
{
Console.SetCursorPosition(startX, currentY);
Spore.Color(line);
currentY++;
}
}

private string Format(string data, int totalWidth, char d=' ')
{
return $"{data}<{RimColor}>{new string(d, Math.Max(0, totalWidth-Regex.Replace(data, _colorSplitPattern, "").Length))}<Reset>";
}
private string ReverseFormat(string data, int totalWidth, char d=' ')
{
return $"<{RimColor}>{new string(d, Math.Max(0, totalWidth-Regex.Replace(data, _colorSplitPattern, "").Length))}<Reset>{data}";
}
private string Center(string data, int totalWidth, char filler=' ')
{
string m = data;
for(int i=Math.Max(0, totalWidth-Regex.Replace(data, _colorSplitPattern, "").Length);i>0;i--){
m = ((i % 2 == 1) ? filler : "") + m + ((i % 2 == 0) ? filler : "");
}
return m;
}
}

// ┌─
// │ │
// ├─┼┤
// └─┴┘
// ┌─┐
// │ │
// └─┘

// ♥
// ♦
// ♣
// ♠
// ♠

// ┌Q♠───┐
// │ │
// │ │
// │ │
// └───♥Q┘

// ┌♥1───┐
// ┌♥2───┐
// ┌♥3───┐
// ┌♥4───┐
// ┌♥5───┐
// ┌♥6───┐
// │ │
// │ ♥ │
// │ │
// └───♥6┘
1 change: 1 addition & 0 deletions Spore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static void Color(string value)

public static void Clear()
{
Console.CursorVisible = false;
Console.Clear();
}

Expand Down
Binary file modified bin/Debug/net8.0/Cards.dll
Binary file not shown.
Binary file modified bin/Debug/net8.0/Cards.exe
Binary file not shown.
Binary file modified bin/Debug/net8.0/Cards.pdb
Binary file not shown.
27 changes: 27 additions & 0 deletions obj/Cards.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"dependencies": {
"Microsoft.NET.ILLink.Tasks": {
"suppressParent": "All",
"target": "Package",
"version": "[8.0.1, )",
"autoReferenced": true
}
},
"imports": [
"net461",
"net462",
Expand All @@ -54,13 +62,32 @@
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Runtime.win-x64",
"version": "[8.0.1, 8.0.1]"
},
{
"name": "Microsoft.NETCore.App.Runtime.win-x64",
"version": "[8.0.1, 8.0.1]"
},
{
"name": "Microsoft.WindowsDesktop.App.Runtime.win-x64",
"version": "[8.0.1, 8.0.1]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.101/PortableRuntimeIdentifierGraph.json"
}
},
"runtimes": {
"win-x64": {
"#import": []
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions obj/Cards.csproj.nuget.g.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
<SourceRoot Include="C:\Users\Rosux\.nuget\packages\" />
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
</ItemGroup>
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="$(NuGetPackageRoot)microsoft.net.illink.tasks\8.0.1\build\Microsoft.NET.ILLink.Tasks.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.illink.tasks\8.0.1\build\Microsoft.NET.ILLink.Tasks.props')" />
</ImportGroup>
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<PkgMicrosoft_NET_ILLink_Tasks Condition=" '$(PkgMicrosoft_NET_ILLink_Tasks)' == '' ">C:\Users\Rosux\.nuget\packages\microsoft.net.illink.tasks\8.0.1</PkgMicrosoft_NET_ILLink_Tasks>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion obj/Debug/net8.0/Cards.AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[assembly: System.Reflection.AssemblyCompanyAttribute("Cards")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+696a608ac2b48e89aada628db311ee77c487c642")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+7ec0ad7b790794c54983d6fa353210e783058703")]
[assembly: System.Reflection.AssemblyProductAttribute("Cards")]
[assembly: System.Reflection.AssemblyTitleAttribute("Cards")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Expand Down
2 changes: 1 addition & 1 deletion obj/Debug/net8.0/Cards.AssemblyInfoInputs.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
39fd616edf72e83c9f67e219cfe69cfabee9df2b71f07218606a50410c727c10
f1b3e84d22961790211c12ea18b561b97fd7726d0033f39a47caa8fb9fa566a2
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
is_global = true
build_property.EnableAotAnalyzer =
build_property.EnableSingleFileAnalyzer =
build_property.EnableTrimAnalyzer =
build_property.IncludeAllContentForSelfExtract =
build_property.TargetFramework = net8.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
Expand Down
Binary file modified obj/Debug/net8.0/Cards.assets.cache
Binary file not shown.
Binary file modified obj/Debug/net8.0/Cards.dll
Binary file not shown.
Binary file modified obj/Debug/net8.0/Cards.pdb
Binary file not shown.
Binary file modified obj/Debug/net8.0/apphost.exe
Binary file not shown.
Binary file modified obj/Debug/net8.0/ref/Cards.dll
Binary file not shown.
Binary file modified obj/Debug/net8.0/refint/Cards.dll
Binary file not shown.
Loading

0 comments on commit 81c7be8

Please sign in to comment.