Skip to content
Snippets Groups Projects
BlackJack.cs 7.81 KiB
using Microsoft.VisualBasic.Devices;
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
{
    enum BJSTATE // the different states the game can be in
    {
        Setup,  // choose how much money the player deploys
        Game,   // play the game
        Result, // see te result and then restart
    }

    struct Card
    {
        string name;    // for drawing the card, needs to be in format num_of_kind
        int value;      // value of the card
        int value2;     // some cards have two values
    }

    internal class BlackJack : Form
    {
        BJSTATE state;
        int money;
        int deployedMoney = 0;
        readonly FontFamily BJFont = FontFamily.GenericSansSerif;

        Size screen = Screen.PrimaryScreen.WorkingArea.Size;
        
        // visibility depends on game state
        Panel SetupPanel;
        Panel GamePanel;
        Panel ResultPanel;

        List<Card> cards;

        (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) };

        public object Int { get; private set; }

        public BlackJack()
        {
            state = BJSTATE.Setup;
            WindowState = FormWindowState.Maximized;
            BackColor = Color.DarkGreen;
            Paint += BlackJack_Paint;

            money = ReadMoney();

            SetupUI();
        }

        private int ReadMoney() //TODO read money from local file
        {
            return 1000;
        }

        private void SetupUI()
        {
            //Setup
            #region SetupPanel
            SetupPanel = new Panel();
            SetupPanel.Size = new Size(screen.Width/2,screen.Height);
            SetupPanel.Location = new Point(screen.Width / 4, 0);
            //DefaultPanel.BackColor = Color.Blue; //DEBUG

            SetupPanel.Paint += Setup_Paint;

            //money buttons
            int goofyIndex = 0; //dw
            for(int j = 1; j <= 2; j++)
                for(int i = 0; i < 4; i++)
                {
                    Button b = new Button();
                    b.Size = new Size(SetupPanel.Width / 4, SetupPanel.Height / 6); // /2
                    b.Location = new Point(i * b.Size.Width, SetupPanel.Height - b.Size.Height*j);
                    b.Name = goofyIndex.ToString(); // for getting the index for chipInfo later

                    (int, string, Color) chip = chipInfo[goofyIndex];
                    b.Text = chip.Item2;
                    b.BackColor = chip.Item3;
                    b.Font = new Font(BJFont, b.Size.Width / 5);

                    b.Click += moneyButtonClick;

                    SetupPanel.Controls.Add(b);
                    goofyIndex++;
                }

            // Deal button
            Button dealButton = new Button();
            dealButton.Text = "DEAL";
            dealButton.Size = new Size(SetupPanel.Width/4, SetupPanel.Height / 6);
            dealButton.Location = new Point(SetupPanel.Width /2 - dealButton.Width/2, SetupPanel.Height /4);
            dealButton.Font = new Font(BJFont, dealButton.Size.Width / 5);
            dealButton.BackColor = Color.DodgerBlue;
            dealButton.Click += (object o, EventArgs e) =>
            {
                if (deployedMoney == 0)
                    return;
                SetupPanel.Hide();
                GamePanel.Show();
                state = BJSTATE.Game;
            };
            SetupPanel.Controls.Add(dealButton);

            Controls.Add(SetupPanel);
            #endregion

            //Game
            #region GamePanel
            GamePanel = new Panel();
            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.Hide(); //Hide until the game state is reached
            Controls.Add(GamePanel);
            #endregion

            //Result
            #region ResultPanel
            ResultPanel = new Panel();
            ResultPanel.Hide(); //Hide until the result state is reached
            Controls.Add(ResultPanel);
            #endregion
        }


        private void moneyButtonClick(object sender, EventArgs e)
        {
            Button b = (Button) sender;
            (int, string, Color) chip = chipInfo[int.Parse(b.Name)];

            if(money >= chip.Item1)
            {
                deployedMoney += chip.Item1;
                money -= chip.Item1;
            }
            Invalidate(true);
        }

        private void BlackJack_Paint(object sender, PaintEventArgs 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 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();
            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);
            g.DrawString(s, deplMoneyFont, Brushes.Black, p);
        }

        private void Game_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            DrawCard(g, new Point(0, 0), 200, "9_of_spades");
        }

        private void Result_Paint(Graphics g)
        {

        }

        private void DrawChip(Graphics g, Color c, int r, Point pos, string value)
        {
            // Chip
            Brush brush = new SolidBrush(c);
            g.FillEllipse(brush, pos.X - r, pos.Y - r, r * 2, r * 2);

            // White Squares
            int squareSize = r / 2;

            Rectangle rect = new Rectangle(pos.X - squareSize/2, pos.Y - r, squareSize, squareSize);
            g.FillRectangle(Brushes.White, rect);
            rect.Offset(new Point(0, r*2-squareSize));
            g.FillRectangle(Brushes.White, rect);

            rect = new Rectangle(pos.X - r, pos.Y - squareSize/2, squareSize, squareSize);
            g.FillRectangle(Brushes.White, rect);
            rect.Offset(new Point(r * 2 - squareSize, 0));
            g.FillRectangle(Brushes.White, rect);

            //text
            Font f = new Font(BJFont, r/2);
            SizeF fontSize = g.MeasureString(value.ToString(), f);
            g.DrawString(value.ToString(), f, Brushes.Black, new PointF(pos.X-fontSize.Width/2, pos.Y-fontSize.Height/2));
        }

        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
        }
    }
}