diff --git a/BlackJ/App.config b/BlackJ/App.config new file mode 100644 index 0000000..56efbc7 --- /dev/null +++ b/BlackJ/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BlackJ/Art.cs b/BlackJ/Art.cs new file mode 100644 index 0000000..d051fe6 --- /dev/null +++ b/BlackJ/Art.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using BlackJack; + +namespace BlackJack +{ + public class Art + { + private Dictionary artCollection = new Dictionary() + { + { "boat", new string[] + { + " .", + " ___ | _:______", + " : _`&_'&___| [] [] [] |_&______", + " | o o o o o o o o /", + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + } + }, + }; + public void Print(string name) + { + if (artCollection.TryGetValue(name.ToLower(), out string[] artLines)) + { + foreach (var line in artLines) + { + Console.WriteLine(line); + } + } + else + { + Console.WriteLine($"No art found for '{name}'"); + } + } + public string[] GetArt(string name) + { + if (artCollection.TryGetValue(name.ToLower(), out string[] artLines)) + { + return artLines; + } + else + { + return null; + } + } + } +} diff --git a/BlackJ/BlackJack.csproj b/BlackJ/BlackJack.csproj new file mode 100644 index 0000000..2af9718 --- /dev/null +++ b/BlackJ/BlackJack.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {10631FE8-36EE-4692-BEF8-26F9A0D01D59} + Exe + BlackJ + BlackJ + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/BlackJ/Card.cs b/BlackJ/Card.cs new file mode 100644 index 0000000..ca1b32d --- /dev/null +++ b/BlackJ/Card.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Eventing.Reader; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using BlackJack; + +namespace BlackJack +{ + public class Card + { + public Suits Suit; + public int Number; + + public Card(Suits suit, int number) + { + + Suit = suit; + if (number < 10) + { + Number = number; + } + else + { + Number = 10; + } + + + } + } +} diff --git a/BlackJ/Dealer.cs b/BlackJ/Dealer.cs new file mode 100644 index 0000000..ed7e231 --- /dev/null +++ b/BlackJ/Dealer.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BlackJack; + +namespace BlackJack +{ + public class Dealer + { + public List Hand = new List(); + public void RemoveHand() + { + Hand = new List(); + } + public int GetHandValue() + { + int total = 0; + int n = Hand.Count; + for (int i = 0; i < n; i++) + { + Card card = Hand[i]; + + int cardValue = card.Number; + total += cardValue; + + } + return total; + } + public Card DrawCard(Deck deck) + { + Card card = deck.Cards[0]; + Hand.Add(card); + deck.Cards.Remove(card); + + return card; + } + } +} diff --git a/BlackJ/Deck.cs b/BlackJ/Deck.cs new file mode 100644 index 0000000..28f7688 --- /dev/null +++ b/BlackJ/Deck.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; +using BlackJack; + + +namespace BlackJack +{ + public enum Suits + { + Diamonds, + Hearts, + Spade, + Clubs, + } + public class Deck + { + public List Cards = new List(); + private readonly Random _rnd = new Random(); + public Deck() + { + for (int i = 1; i <= 13; i++) { + foreach (Suits suit in Enum.GetValues(typeof(Suits))) + { + Card card = new Card(suit, i); + AddCard(card); + } + } + + } + + public void AddCard(Card card) + { + Cards.Add(card); + } + + public void Shuffle() + { + int n = Cards.Count; + for (int i = 0; i < n; i++) + { + int j = _rnd.Next(0, n-1); + + Card temp = Cards[i]; + Cards[i] = Cards[j]; + Cards[j] = temp; + } + } + + public void PrintDeck() + { + for (int i = 0; i < Cards.Count; i++) + { + Card card = Cards[i]; + Console.WriteLine(card.Suit.ToString() + " " + card.Number.ToString()); + } + } + + } +} diff --git a/BlackJ/Game.cs b/BlackJ/Game.cs new file mode 100644 index 0000000..25088c4 --- /dev/null +++ b/BlackJ/Game.cs @@ -0,0 +1,252 @@ +using BlackJack; +using System; +using System.Collections.Generic; +using System.Diagnostics.Eventing.Reader; +using System.Linq; +using System.Runtime.Remoting.Messaging; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace BlackJack +{ + public class Game + { + + readonly Player player = new Player(); + readonly Dealer dealer = new Dealer(); + readonly Shop shop = new Shop(); + readonly Art art = new Art(); + void DisplayBalance() + { + Console.WriteLine("Balance: " + player.cash + "$"); + Console.WriteLine("_____________________________"); + Console.WriteLine(); + } + public void Initialize() + { + Console.Clear(); + Console.WriteLine("Welcome to BlackJack)"); + Console.WriteLine("You start with " + player.cash + "$"); + Console.WriteLine(); + Console.WriteLine("'Enter' to continue"); + Console.ReadLine(); + Menu(); + } + + + public void Menu() + { + Console.Clear(); + DisplayBalance(); + Console.WriteLine("> Enter round (1)"); + Console.WriteLine("> Shop (2)"); + Console.WriteLine("> Inventory (3)"); + string input = Console.ReadLine(); + if (input == "1") + { + NewRound(); + } + else if (input == "2") + { + + } + else if (input == "3") + { + + } + else + { + Console.WriteLine("Invalid input. Press 'Enter' to try again"); + Console.ReadLine(); + Console.Clear(); + Initialize(); + } + + } + + public void NewRound() + { + Console.Clear(); + + if (player.cash <= 0) + { + Console.WriteLine("You went bankrupt!"); + Environment.Exit(0); + } + + Deck deck = new Deck(); + player.RemoveHand(); + dealer.RemoveHand(); + + double bet; + deck.Shuffle(); + + player.DrawCard(deck); + player.DrawCard(deck); + + dealer.DrawCard(deck); + + void Bet() + { + Console.WriteLine("You have " + player.cash + "$"); + Console.WriteLine("How much do you want to bet?"); + string input = Console.ReadLine(); + + if (double.TryParse(input, out bet)) + { + if (bet <= player.cash && bet > 0) + { + player.cash -= bet; + } + else if (bet == 676767) + { + double cheat; + Console.WriteLine("Enter Cheat"); + input = Console.ReadLine(); + if (double.TryParse(input, out cheat)) + { + player.cash += cheat; + } + Console.Clear(); + Bet(); + } + else + { + Console.WriteLine("Invalid amount. Press 'Enter' to try again"); + Console.ReadLine(); + Console.Clear(); + Bet(); + } + + } + else + { + Console.WriteLine("Invalid amount. Press 'Enter' to try again"); + Console.ReadLine(); + Console.Clear(); + Bet(); + } + } + + Bet(); + + + Console.Clear(); + + void DisplayStats() + { + Console.WriteLine("Balance: " + player.cash + "$"); + Console.WriteLine("Bet: " + bet + "$"); + Console.WriteLine("_____________________________"); + Console.WriteLine(); + } + + + void PlayersTurn() + { + DisplayStats(); + + Console.WriteLine("Hand: " + player.GetHandValue()); + Console.WriteLine(); + Console.WriteLine("Dealer's first card: " + dealer.GetHandValue()); + Console.WriteLine(); + Console.WriteLine("Hit (h) or Stand (s)?"); + + string input = Console.ReadLine(); + Console.Clear(); + DisplayStats(); + + if (input == "h") + { + Card card = player.DrawCard(deck); + Console.WriteLine("You received " + card.Number + " " + card.Suit); + if (player.GetHandValue() > 21) + { + Bust(); + } + Console.WriteLine("Press 'Enter' to continue."); + Console.ReadLine(); + Console.Clear(); + PlayersTurn(); + + } + else if (input == "s") + { + Console.WriteLine("You chose to stand."); + DealersTurn(); + + } + else + { + Console.WriteLine("Invalid input. Press 'Enter' to try again"); + Console.ReadLine(); + Console.Clear(); + PlayersTurn(); + } + } + + PlayersTurn(); + + void InputNewRound() + { + Console.WriteLine(); + Console.WriteLine("Press 'Enter' to enter menu."); + Console.ReadLine(); + Menu(); + } + + void DealersTurn() + { + while (dealer.GetHandValue() < 17) + { + dealer.DrawCard(deck); + } + + + int playerHand = player.GetHandValue(); + int dealerHand = dealer.GetHandValue(); + + + + if (playerHand > dealerHand || dealerHand > 21) + { + Console.WriteLine("You won."); + Console.WriteLine("Your hand: " + playerHand); + Console.WriteLine("Dealer's hand: " + dealerHand); + + Console.WriteLine("+" + bet * 2 + "$"); + player.cash += bet * 2; + + InputNewRound(); + } + else if (playerHand < dealerHand) + { + Console.WriteLine("You lost."); + Console.WriteLine("Your hand: " + playerHand); + Console.WriteLine("Dealer's hand: " + dealerHand); + + InputNewRound(); + } + else + { + Console.WriteLine("It's a tie!"); + Console.WriteLine("Your hand: " + playerHand); + Console.WriteLine("Dealer's hand: " + dealerHand); + + Console.WriteLine("+" + bet + "$"); + player.cash += bet; + + InputNewRound(); + } + } + + void Bust() + { + Console.WriteLine("You bust! Hand: " + player.GetHandValue()); + InputNewRound(); + } + } + } +} + \ No newline at end of file diff --git a/BlackJ/Player.cs b/BlackJ/Player.cs new file mode 100644 index 0000000..3b0c801 --- /dev/null +++ b/BlackJ/Player.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BlackJack; + +namespace BlackJack +{ + public class Player + { + public double cash = 100; + public List Hand = new List(); + public List Inventory = new List(); + + public void RemoveHand() + { + Hand = new List(); + } + + public int GetHandValue() + { + int total = 0; + int n = Hand.Count; + for(int i = 0; i < n; i++) + { + Card card = Hand[i]; + + int cardValue = card.Number; + total += cardValue; + + } + return total; + } + + public Card DrawCard(Deck deck) + { + Card card = deck.Cards[0]; + Hand.Add(card); + deck.Cards.Remove(card); + + return card; + } + } +} diff --git a/BlackJ/Program.cs b/BlackJ/Program.cs new file mode 100644 index 0000000..5534541 --- /dev/null +++ b/BlackJ/Program.cs @@ -0,0 +1,18 @@ +using BlackJack; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlackJack +{ + internal class Program + { + static void Main(string[] args) + { + Game game = new Game(); + game.Initialize(); + } + } +} diff --git a/BlackJ/Properties/AssemblyInfo.cs b/BlackJ/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..6237b34 --- /dev/null +++ b/BlackJ/Properties/AssemblyInfo.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("BlackJ")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Gislaveds kommun")] +[assembly: AssemblyProduct("BlackJ")] +[assembly: AssemblyCopyright("Copyright © Gislaveds kommun 2025")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("10631fe8-36ee-4692-bef8-26f9a0d01d59")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/BlackJ/Shop.cs b/BlackJ/Shop.cs new file mode 100644 index 0000000..333cd1a --- /dev/null +++ b/BlackJ/Shop.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BlackJack; + +namespace BlackJack +{ + public class Shop + { + readonly Art art = new Art(); + public Shop() + { + } + } +} diff --git a/BlackJack.sln b/BlackJack.sln new file mode 100644 index 0000000..6c113b0 --- /dev/null +++ b/BlackJack.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36408.4 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlackJack", "BlackJ\BlackJack.csproj", "{10631FE8-36EE-4692-BEF8-26F9A0D01D59}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {10631FE8-36EE-4692-BEF8-26F9A0D01D59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10631FE8-36EE-4692-BEF8-26F9A0D01D59}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10631FE8-36EE-4692-BEF8-26F9A0D01D59}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10631FE8-36EE-4692-BEF8-26F9A0D01D59}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B58E74AA-816D-4A3F-8119-7B6CB941E4B5} + EndGlobalSection +EndGlobal