diff --git a/PuzzlePlayer/BlackJack.cs b/PuzzlePlayer/BlackJack.cs index cc471e25e68d0c7f170c30ad8a0d20bb4eb82f5a..36cd1263d950ebad308df6b32f764c5a93e8247c 100644 --- a/PuzzlePlayer/BlackJack.cs +++ b/PuzzlePlayer/BlackJack.cs @@ -1,13 +1,6 @@ -using Microsoft.VisualBasic.Devices; -using System; +using System; using System.Collections.Generic; -using System.Diagnostics; using System.Drawing; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; namespace PuzzlePlayer_Namespace @@ -23,13 +16,11 @@ namespace PuzzlePlayer_Namespace { public string name; // for drawing the card, needs to be in format num_of_kind public int value; // value of the card - public int value2; // some cards have two values - public Card(string n, int val, int val2) + public Card(string n, int val) { name = n; value = val; - value2 = val2; } } @@ -39,6 +30,11 @@ namespace PuzzlePlayer_Namespace int money; int deployedMoney = 0; readonly FontFamily BJFont = FontFamily.GenericSansSerif; + readonly int defaultFontSize; + const double cardMultply = 1.4; //double to go from the width of the card to the height + const int aceValueMax = 11; //the two values of a ace + const int aceValueMin = 1; + const int BLACKJACK = 21; //the value to get blackjack Size screen = Screen.PrimaryScreen.WorkingArea.Size; @@ -48,8 +44,8 @@ namespace PuzzlePlayer_Namespace Panel ResultPanel; List<Card> cardsLeft; - List<Card> playerCards; - List<(Card,bool)> dealerCards; // boolean is to keep track if the card is shown + List<Card> playerCards = new(); + List<(Card,bool)> dealerCards = new(); // boolean is to keep track if the card is visable (int,string,Color)[] chipInfo = { (5,"5",Color.Gray), (25, "25", Color.Blue), (50, "50", Color.Green), (100, "100", Color.DarkCyan), (500, "500", Color.Purple), (1000, "1K", Color.Red), (5000, "5K", Color.Orange), (10000, "10K", Color.Gold) }; @@ -57,7 +53,11 @@ namespace PuzzlePlayer_Namespace { state = BJSTATE.Setup; WindowState = FormWindowState.Maximized; + DoubleBuffered = true; BackColor = Color.DarkGreen; + + defaultFontSize = screen.Width / 50; + Paint += BlackJack_Paint; money = ReadMoney(); @@ -70,23 +70,22 @@ namespace PuzzlePlayer_Namespace { List<Card> cards = new List<Card>(); string[] numbers = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "ace", "jack", "king", "queen" }; - string[] kinds = { "clubs", "diamonds", "hearths", "spades" }; + string[] kinds = { "clubs", "diamonds", "hearts", "spades" }; foreach (string kind in kinds) { foreach (string num in numbers) { - int val, val2 = 0; + int val; if (num == "jack" || num == "king" || num == "queen") val = 10; else if (num == "ace") // if it is an ace the player can chose between 1 or 11 { val = 11; - val2 = 1; } else val = int.Parse(num); - cards.Add(new Card(num + "_of_" + kind,val,val2)); + cards.Add(new Card(num + "_of_" + kind,val)); } } @@ -150,10 +149,33 @@ namespace PuzzlePlayer_Namespace GamePanel.Paint += Game_Paint; GamePanel.Size = new Size((int)(screen.Width / 1.5), screen.Height); GamePanel.Location = new Point((screen.Width - GamePanel.Size.Width)/2, 0); - GamePanel.BackColor = Color.Red; //DEBUG - - + //GamePanel.BackColor = Color.Red; //DEBUG + //buttons + Button HitButton = new Button(); + HitButton.Text = "HIT"; + HitButton.Click += (object sender, EventArgs e) => + { + playerCards.Add(PopCard()); + Invalidate(true); + }; + HitButton.BackColor = Color.Red; + HitButton.Size = new Size(GamePanel.Width/5, GamePanel.Height/5); + HitButton.Location = new Point(0, GamePanel.Height / 2 - HitButton.Size.Height / 2); + HitButton.Font = new Font(BJFont, HitButton.Size.Width/3, FontStyle.Bold); + + GamePanel.Controls.Add(HitButton); + + Button StandButton = new Button(); + StandButton.Text = "STAND"; + StandButton.Click += standButtonClick; + StandButton.BackColor = Color.Blue; + StandButton.Size = new Size(GamePanel.Width / 5, GamePanel.Height / 5); + StandButton.Location = new Point(GamePanel.Width-StandButton.Width, GamePanel.Height / 2 - StandButton.Size.Height / 2); + StandButton.Font = new Font(BJFont, StandButton.Size.Width / 6,FontStyle.Bold); + + GamePanel.Controls.Add(StandButton); + GamePanel.Hide(); //Hide until the game state is reached Controls.Add(GamePanel); #endregion @@ -185,44 +207,34 @@ namespace PuzzlePlayer_Namespace return; SetupPanel.Hide(); - playerCards.Add((Card)PopCard()); // give both dealer and player cards - dealerCards.Add(((Card)PopCard(), false)); //hided card - playerCards.Add((Card)PopCard()); - dealerCards.Add(((Card)PopCard(), true)); + playerCards.Add(PopCard()); // give both dealer and player cards + dealerCards.Add((PopCard(), false)); //hided card + playerCards.Add(PopCard()); + dealerCards.Add((PopCard(), true)); //visable card GamePanel.Show(); state = BJSTATE.Game; } - private void BlackJack_Paint(object sender, PaintEventArgs e) + private void standButtonClick(object sender, EventArgs e) { - Graphics g = e.Graphics; - //default draw - Font moneyFont = new Font(BJFont, screen.Width/50, FontStyle.Bold); - g.DrawString("Money: " + money.ToString(), moneyFont, Brushes.Black, new PointF(0,0)); + } - /* - switch(state) // paint according to the current state - { - case BJSTATE.Setup: - //Setup_Paint(g); - break; - case BJSTATE.Game: - //Game_Paint(g); - break; - case BJSTATE.Result: - //Result_Paint(g); - break; - }*/ + private void BlackJack_Paint(object sender, PaintEventArgs e) //this will always get drawn + { + Graphics g = e.Graphics; + + Font moneyFont = new Font(BJFont, defaultFontSize, FontStyle.Bold); + g.DrawString("Money: $" + money.ToString(), moneyFont, Brushes.Black, new PointF(0,0)); } private void Setup_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; // draw deployd money - // Might update this to butiful chipss - string s = "Deployd Money: " + deployedMoney.ToString(); + // Might update this to butiful chipss TODO + string s = "Deployd Money: $" + deployedMoney.ToString(); Font deplMoneyFont = new Font(BJFont, SetupPanel.Width / 20, FontStyle.Bold); SizeF pf = g.MeasureString(s, deplMoneyFont); PointF p = new PointF(SetupPanel.Width / 2 - pf.Width / 2, SetupPanel.Height / 2); @@ -232,19 +244,47 @@ namespace PuzzlePlayer_Namespace private void Game_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; - + int cardWidth = 250; + int cardHeight = (int)(cardWidth * cardMultply); + // draw cards for player and dealer for(int i = 0; i < playerCards.Count; i++) { - int ba = 200; - Point p = new Point(i*200); - DrawCard(g, p, 200, playerCards[i].name); - } + int centerOffsetX = GamePanel.Width/2 - playerCards.Count * cardWidth/2; //to get the cards perfectly in the middle - foreach(Card c in playerCards) + Point p = new Point(centerOffsetX + i * cardWidth, screen.Height - cardHeight); + DrawCard(g, p, cardWidth, playerCards[i].name); + } + for (int i = 0; i < dealerCards.Count; i++) { - Point p = new Point(); - DrawCard(g, p, 200, c.name); + int centerOffsetX = GamePanel.Width / 2 - dealerCards.Count * cardWidth / 2; + Point p = new Point(centerOffsetX + i * cardWidth, 0); + if (dealerCards[i].Item2) + DrawCard(g, p, cardWidth, dealerCards[i].Item1.name); + else + DrawCard(g, p, cardWidth, "back"); //if not visable draw backside of card } + + //draw player and dealer points + int playerValue = getValue(playerCards); + int dealerValue = getValue(getVisableDealerCards()); + string playerString = $"Player: {playerValue} points"; + string dealerString = $"Dealer: {dealerValue} points"; + Font f = new Font(BJFont, defaultFontSize,FontStyle.Bold); + + SizeF playerSF = g.MeasureString(playerString, f); + PointF playerStrPos = new PointF(GamePanel.Width / 2 - playerSF.Width / 2, GamePanel.Height - cardHeight - playerSF.Height); + g.DrawString(playerString, f, Brushes.Black, playerStrPos); + + SizeF dealerSF = g.MeasureString(dealerString, f); + PointF dealerStrPos = new PointF(GamePanel.Width / 2 - dealerSF.Width / 2, cardHeight); + g.DrawString(dealerString, f, Brushes.Black, dealerStrPos); + + //draw deployed money + string deployedString = $"${deployedMoney}"; + SizeF deployedSF = g.MeasureString(deployedString, f); + PointF deployedStrPos = new PointF(GamePanel.Width/2 - deployedSF.Width / 2,GamePanel.Height/2 - deployedSF.Height/2); + g.DrawString(deployedString,f, Brushes.Black, deployedStrPos); + } private void Result_Paint(Graphics g) @@ -280,16 +320,54 @@ namespace PuzzlePlayer_Namespace private void DrawCard(Graphics g, Point pos, int width, string cardName) //cardName needs to be in format: num_of_kind { Image img = SettingForm.GetEmbeddedImage("BlackJack.cards." + cardName + ".png"); - g.DrawImage(img, pos.X, pos.Y, width, (float)(width * 1.4)); //img is 500x726 + g.DrawImage(img, pos.X, pos.Y, width, (float)(width * cardMultply)); //img is 500x726 } - private Card? PopCard() + private Card PopCard() { if (cardsLeft.Count == 0) - return null; + cardsLeft = getAllCards(); //reshuffle, In the real game the reshuffle is done after the round has ended but i am lazy :) Card c = cardsLeft[cardsLeft.Count - 1]; cardsLeft.RemoveAt(cardsLeft.Count - 1); return c; } + + private int getValue(List<Card> cardList) + { + int result = 0; + int aceCount = 0; + + foreach(Card c in cardList) + { + if (c.name.StartsWith("ace")) + aceCount++; + else + result += c.value; + } + + if(aceCount > 0) + { + for (int i = 0; i < aceCount; i++) //check for each ace if adding 11 will make them go over 21. if not then we can add 21 else we will add 1 + { + if (result + aceValueMax > BLACKJACK) + result += aceValueMin; + else + result += aceValueMax; + } + } + + return result; + } + + private List<Card> getVisableDealerCards() + { + List<Card> cards = new List<Card>(); + + foreach ((Card c,bool b) in dealerCards) + if(b) + cards.Add(c); + + return cards; + } } } diff --git a/PuzzlePlayer/PuzzlePlayer.csproj b/PuzzlePlayer/PuzzlePlayer.csproj index 761b306d36a073e000909f7e90e49e2e9c23ab57..ff26550e41b347c6dd217155ec82fde08d1972fe 100644 --- a/PuzzlePlayer/PuzzlePlayer.csproj +++ b/PuzzlePlayer/PuzzlePlayer.csproj @@ -53,6 +53,7 @@ <None Remove="Resources\BlackJack\cards\ace_of_diamonds.png" /> <None Remove="Resources\BlackJack\cards\ace_of_hearts.png" /> <None Remove="Resources\BlackJack\cards\ace_of_spades.png" /> + <None Remove="Resources\BlackJack\cards\back.png" /> <None Remove="Resources\BlackJack\cards\jack_of_clubs.png" /> <None Remove="Resources\BlackJack\cards\jack_of_diamonds.png" /> <None Remove="Resources\BlackJack\cards\jack_of_hearts.png" /> @@ -115,6 +116,7 @@ <EmbeddedResource Include="Resources\BlackJack\cards\ace_of_diamonds.png" /> <EmbeddedResource Include="Resources\BlackJack\cards\ace_of_hearts.png" /> <EmbeddedResource Include="Resources\BlackJack\cards\ace_of_spades.png" /> + <EmbeddedResource Include="Resources\BlackJack\cards\back.png" /> <EmbeddedResource Include="Resources\BlackJack\cards\jack_of_clubs.png" /> <EmbeddedResource Include="Resources\BlackJack\cards\jack_of_diamonds.png" /> <EmbeddedResource Include="Resources\BlackJack\cards\jack_of_hearts.png" /> diff --git a/PuzzlePlayer/Resources/BlackJack/cards/back.png b/PuzzlePlayer/Resources/BlackJack/cards/back.png new file mode 100644 index 0000000000000000000000000000000000000000..07fb311e50ec4b3110dfea36947a89eb90e19138 Binary files /dev/null and b/PuzzlePlayer/Resources/BlackJack/cards/back.png differ