c# blackjack

posted in phil67rpg's Blog
Published January 26, 2019
C#
Advertisement

I am starting on a blackjack game using c#.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        enum suit { C=1, D, H, S};
        enum face { J=11, Q, K, A};
        
        static void Main(string[] args)
        {
            Random rand = new Random();
            int card = rand.Next(2, 15);
            int card_suit = rand.Next(1, 5);

            if (card >= 2 && card <=10)
            {
                Console.Write(card);
            }
            else if(card==11)
            {
                Console.Write(face.J);
            }
            else if (card == 12)
            {
                Console.Write(face.Q);
            }
            else if (card == 13)
            {
                Console.Write(face.K);
            }
            else if (card == 14)
            {
                Console.Write(face.A);
            }
            if (card_suit == 1)
            {
                Console.WriteLine(suit.C);
            }
            if (card_suit == 2)
            {
                Console.WriteLine(suit.D);
            }
            if (card_suit == 3)
            {
                Console.WriteLine(suit.H);
            }
            if (card_suit == 4)
            {
                Console.WriteLine(suit.S);
            }




        }
    }
}

 

Previous Entry c# tic tac toe game
Next Entry c# snake game
0 likes 10 comments

Comments

phil67rpg

you got a point I will update my code.

January 26, 2019 11:45 PM
phil67rpg

I have made some progress, 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace blackjack
{
    public static class Program
    {
        enum suit { C=1, D, H, S};
        enum face { J=11, Q, K, A};

        private static Random rng = new Random();

        public static void Shuffle<T>(this IList<T> list)
        {
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }

        static void Main(string[] args)
        {
            Random rand = new Random();

            List<int> cards = new List<int>(13);

            for (int i = 0; i < 13; i++) 
            {
                cards.Add(i+2);
            }

            cards.Shuffle();

            List<int> suits = new List<int>(4);

            for (int i = 0; i < 4; i++)
            {
                suits.Add(i + 1);
            }

            suits.Shuffle();

            int count = 0;

            while (count < 2)
            {
                int card = cards[0];
                int card_suit = suits[0];

                if (card >= 2 && card <= 10)
                {
                    Console.Write(card);
                }
                else if (card == 11)
                {
                    Console.Write(face.J);
                }
                else if (card == 12)
                {
                    Console.Write(face.Q);
                }
                else if (card == 13)
                {
                    Console.Write(face.K);
                }
                else if (card == 14)
                {
                    Console.Write(face.A);
                }
                if (card_suit == 1)
                {
                    Console.WriteLine(suit.C);
                }
                if (card_suit == 2)
                {
                    Console.WriteLine(suit.D);
                }
                if (card_suit == 3)
                {
                    Console.WriteLine(suit.H);
                }
                if (card_suit == 4)
                {
                    Console.WriteLine(suit.S);
                }
                count++;
            }

        }
    }
}

 

January 28, 2019 01:54 AM
8Observer8

You can add OpenTK.dll to the References: OpenTK.zip

It will allow you to create a window from your console application and draw textures with OpenGL API. If you are interesting, please, ask me any questions about OpenTK and OpenGL 3. I want to start a card game too in the future soon. But I know nothing about Blackjack. I think this is very complicated game like poker. Maybe I'm wrong. I will read about Blackjack. I want to start more simple game like "Durak" because I know how to play it. This was very popular game in Soviet States.

This program just create a window from a console application. It set a title, ESC button for closing the window and fill canvas with the dark green color. But you can see how it is simple to add Update(), Draw() and OnKeyDown() methods. I hope it will help you. I can show how to load images for cards and how to draw textures with modern OpenGL 3.

Durak_OpenTKOpenGL31CSharp_DarkGreen.png.3cf5191dc4665fcc20ab63c1572ab1ae.png

 

Program.cs



namespace Durak
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var window = new Window())
            {
                window.Run();
            }
        }
    }
}

Window.cs


using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System;
using OpenTK.Input;

namespace Durak
{
    class Window : GameWindow
    {
        // Initialize the game
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Set a window size and a title text
            Title = "Durak";
            Width = 300;
            Height = 300;

            // Set a clear color
            GL.ClearColor(Color4.DarkGreen);
        }

        // Draw game objects
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            // Clear a canvas with the clear color
            GL.Clear(ClearBufferMask.ColorBufferBit);

            // Swap back and front screen buffers
            SwapBuffers();
        }

        // Update game
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            // Update a game state here
            // ...
        }

        // Key handlers
        protected override void OnKeyDown(KeyboardKeyEventArgs e)
        {
            base.OnKeyDown(e);

            switch (e.Key)
            {
                // Close the window by the escape key
                case Key.Escape:
                    Close();
                    break;
            }
        }
    }
}

 

January 28, 2019 10:20 AM
phil67rpg

I am trying to learn c# although durak looks interesting I don't want to learn another library. I have also taken a break from learning opengl

January 28, 2019 06:11 PM
phil67rpg

I have got my game to deal one hand.let me  know if I got the right code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace blackjack
{
    public static class Program
    {
        enum suit { C=1, D, H, S};
        enum face { J=11, Q, K, A};

        private static Random rng = new Random();

        public static void Shuffle<T>(this IList<T> list)
        {
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }

        static void Main(string[] args)
        {
            Random rand = new Random();

            List<int> cards = new List<int>(13);

            for (int i = 0; i < 13; i++) 
            {
                cards.Add(i+2);
            }

            cards.Shuffle();

            List<int> suits = new List<int>(4);

            for (int i = 0; i < 4; i++)
            {
                suits.Add(i + 1);
            }

            suits.Shuffle();

            int count = 0;

            while (count < 2)
            {
                int card = cards[count];
                int card_suit = suits[count];

                if (card >= 2 && card <= 10)
                {
                    Console.Write(card);
                }
                else if (card == 11)
                {
                    Console.Write(face.J);
                }
                else if (card == 12)
                {
                    Console.Write(face.Q);
                }
                else if (card == 13)
                {
                    Console.Write(face.K);
                }
                else if (card == 14)
                {
                    Console.Write(face.A);
                }
                if (card_suit == 1)
                {
                    Console.WriteLine(suit.C);
                }
                if (card_suit == 2)
                {
                    Console.WriteLine(suit.D);
                }
                if (card_suit == 3)
                {
                    Console.WriteLine(suit.H);
                }
                if (card_suit == 4)
                {
                    Console.WriteLine(suit.S);
                }
                count++;
            }

        }
    }
}

 

January 29, 2019 12:24 AM
phil67rpg

I have decided to do my blackjack game using graphics.


        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Bitmap bitmap = new Bitmap("c01.bmp", true);
            g.DrawImage(bitmap, 20, 20);
        }

 

January 30, 2019 01:38 AM
8Observer8

I will make a network versions of the card games using build-in System.Net.Sockets of .NET because it will be more simple then to write AI. I think AI is the next step after the network version. But at the first (before a card game) I will make Tic-Tac-Toe with a local network connection. I will place the OpenTK.GLControl element on a Form in WinForms or WPF project (I did not decide what is better to learn at the first: WinForms or WPF. I think WPF is better to start if you know HTML, because HTML is similar to XAMP.). It will allow me to draw images by OpenGL 3 and allow me to make some effects with shaders and math in the future.

January 30, 2019 10:34 AM
phil67rpg

good luck in  your project, what do you think of my  new game project I am  working on.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            Bitmap bitmap_ace_club = new Bitmap("c01.bmp", true);
            Bitmap bitmap_two_club = new Bitmap("c02.bmp", true);
            Bitmap bitmap_three_club = new Bitmap("c03.bmp", true);
            Bitmap bitmap_four_club = new Bitmap("c04.bmp", true);
            Bitmap bitmap_five_club = new Bitmap("c05.bmp", true);
            Bitmap bitmap_six_club = new Bitmap("c06.bmp", true);
            Bitmap bitmap_seven_club = new Bitmap("c07.bmp", true);
            Bitmap bitmap_eight_club = new Bitmap("c08.bmp", true);
            Bitmap bitmap_nine_club = new Bitmap("c09.bmp", true);
            Bitmap bitmap_ten_club = new Bitmap("c10.bmp", true);
            Bitmap bitmap_jack_club = new Bitmap("c11.bmp", true);
            Bitmap bitmap_queen_club = new Bitmap("c12.bmp", true);
            Bitmap bitmap_king_club = new Bitmap("c13.bmp", true);

            Bitmap bitmap_ace_diamond = new Bitmap("d01.bmp", true);
            Bitmap bitmap_two_diamond = new Bitmap("d02.bmp", true);
            Bitmap bitmap_three_diamond = new Bitmap("d03.bmp", true);
            Bitmap bitmap_four_diamond = new Bitmap("d04.bmp", true);
            Bitmap bitmap_five_diamond = new Bitmap("d05.bmp", true);
            Bitmap bitmap_six_diamond = new Bitmap("d06.bmp", true);
            Bitmap bitmap_seven_diamond = new Bitmap("d07.bmp", true);
            Bitmap bitmap_eight_diamond = new Bitmap("d08.bmp", true);
            Bitmap bitmap_nine_diamond = new Bitmap("d09.bmp", true);
            Bitmap bitmap_ten_diamond = new Bitmap("d10.bmp", true);
            Bitmap bitmap_jack_diamond = new Bitmap("d11.bmp", true);
            Bitmap bitmap_queen_diamond = new Bitmap("d12.bmp", true);
            Bitmap bitmap_king_diamond = new Bitmap("d13.bmp", true);

            Bitmap bitmap_ace_heart = new Bitmap("h01.bmp", true);
            Bitmap bitmap_two_heart = new Bitmap("h02.bmp", true);
            Bitmap bitmap_three_heart = new Bitmap("h03.bmp", true);
            Bitmap bitmap_four_heart = new Bitmap("h04.bmp", true);
            Bitmap bitmap_five_heart = new Bitmap("h05.bmp", true);
            Bitmap bitmap_six_heart = new Bitmap("h06.bmp", true);
            Bitmap bitmap_seven_heart = new Bitmap("h07.bmp", true);
            Bitmap bitmap_eight_heart = new Bitmap("h08.bmp", true);
            Bitmap bitmap_nine_heart = new Bitmap("h09.bmp", true);
            Bitmap bitmap_ten_heart = new Bitmap("h10.bmp", true);
            Bitmap bitmap_jack_heart = new Bitmap("h11.bmp", true);
            Bitmap bitmap_queen_heart = new Bitmap("h12.bmp", true);
            Bitmap bitmap_king_heart = new Bitmap("h13.bmp", true);

            Bitmap bitmap_ace_spade = new Bitmap("s01.bmp", true);
            Bitmap bitmap_two_spade = new Bitmap("s02.bmp", true);
            Bitmap bitmap_three_spade = new Bitmap("s03.bmp", true);
            Bitmap bitmap_four_spade = new Bitmap("s04.bmp", true);
            Bitmap bitmap_five_spade = new Bitmap("s05.bmp", true);
            Bitmap bitmap_six_spade = new Bitmap("s06.bmp", true);
            Bitmap bitmap_seven_spade = new Bitmap("s07.bmp", true);
            Bitmap bitmap_eight_spade = new Bitmap("s08.bmp", true);
            Bitmap bitmap_nine_spade = new Bitmap("s09.bmp", true);
            Bitmap bitmap_ten_spade = new Bitmap("s10.bmp", true);
            Bitmap bitmap_jack_spade = new Bitmap("s11.bmp", true);
            Bitmap bitmap_queen_spade = new Bitmap("s12.bmp", true);
            Bitmap bitmap_king_spade = new Bitmap("s13.bmp", true);

            if (e.Button == MouseButtons.Left)
            {
                Graphics g1 = this.CreateGraphics();
                g1.DrawImage(bitmap_king_spade, 360, 20);
                g1.Dispose();
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}

 

January 30, 2019 06:36 PM
phil67rpg

here is my screenshot

https://imgur.com/a/paS6hNl

January 30, 2019 10:47 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

asteroids wars

2661 views

plane game

3410 views

rgg platformer

2042 views

win 32 pong

2523 views

bug invaders

2190 views

c# book

2474 views

c# bug invaders

2131 views

c# console snake game

23812 views
Advertisement