예제 #1
0
 public Card(Card card) : base(card.origin, new KeyValuePair <int, int>(card.X, card.Y), card.Width, card.Height, card.Sprite, card.Background)
 {
     Value     = card.Value;
     Flipped   = card.Flipped;
     Name      = card.Name;
     BlankCard = card.BlankCard;
 }
예제 #2
0
 public Card(int value, bool flipped, SpriteOrigin origin, KeyValuePair <int, int> position, int width = 0, int height = 0, KeyValuePair <ConsoleColor, char>[,] sprite = null, ConsoleColor background = ConsoleColor.Black) : base(origin, position, width, height, sprite, background)
 {
     Value     = value;
     Flipped   = flipped;
     BlankCard = new ConsoleSprite(origin, position, width, height);
     Program.GetSetSpriteFromFile("../blankcard.consolesprite", ref BlankCard);
 }
예제 #3
0
 public ConsoleSprite(ConsoleSprite sprite)
 {
     origin     = sprite.origin;
     X          = sprite.X;
     Y          = sprite.Y;
     Width      = sprite.Width;
     Height     = sprite.Height;
     Sprite     = sprite.Sprite;
     Background = sprite.Background;
 }
예제 #4
0
        public ConsoleSprite Clone()
        {
            ConsoleSprite sprite = new ConsoleSprite(this);

            return(sprite);
        }
예제 #5
0
        static void Main(string[] args)
        {
            // Top text box center is 121, 4

            // Initialize Console
            const int screenWidth = 240, screenHeight = 132;

            Console.SetBufferSize(screenWidth, screenHeight);
            Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
            Console.SetWindowPosition(0, 0);
            Console.CursorVisible = false;

            // Initialize Sprites
            ConsoleSprite GUI    = new ConsoleSprite(SpriteOrigin.TopLeft, new KeyValuePair <int, int>(0, 0));
            ConsoleSprite msgBox = new ConsoleSprite(SpriteOrigin.Center, new KeyValuePair <int, int>(screenWidth / 2, screenHeight / 2));
            Card          card   = new Card(0, false, SpriteOrigin.Center, new KeyValuePair <int, int>(15, 10));

            GetSetSpriteFromFile("../GUI.consolesprite", ref GUI);
            GetSetSpriteFromFile("../Msg Box.consolesprite", ref msgBox);

            // Initialize Game
            Random        rng      = new Random();
            List <string> CardPoll = new List <string>()
            {
                "Clubs/Ace",
                "Clubs/2",
                "Clubs/3",
                "Clubs/4",
                "Clubs/5",
                "Clubs/6",
                "Clubs/7",
                "Clubs/8",
                "Clubs/9",
                "Clubs/10",
                "Clubs/Jack",
                "Clubs/Queen",
                "Clubs/King",

                "Spades/Ace",
                "Spades/2",
                "Spades/3",
                "Spades/4",
                "Spades/5",
                "Spades/6",
                "Spades/7",
                "Spades/8",
                "Spades/9",
                "Spades/10",
                "Spades/Jack",
                "Spades/Queen",
                "Spades/King",

                "Diamonds/Ace",
                "Diamonds/2",
                "Diamonds/3",
                "Diamonds/4",
                "Diamonds/5",
                "Diamonds/6",
                "Diamonds/7",
                "Diamonds/8",
                "Diamonds/9",
                "Diamonds/10",
                "Diamonds/Jack",
                "Diamonds/Queen",
                "Diamonds/King",

                "Hearts/Ace",
                "Hearts/2",
                "Hearts/3",
                "Hearts/4",
                "Hearts/5",
                "Hearts/6",
                "Hearts/7",
                "Hearts/8",
                "Hearts/9",
                "Hearts/10",
                "Hearts/Jack",
                "Hearts/Queen",
                "Hearts/King",
            };

            List <Card> DealerHand = new List <Card>();
            List <Card> PlayerHand = new List <Card>();

            // Startup
            GUI.RenderSprite();
            WriteText("Blackjack", 121, 4);

            msgBox.RenderSprite();
            WriteText("Press any key to start", screenWidth / 2, screenHeight / 2);
            while (!Keyboard.IsAnyKeyDown())
            {
            }

            msgBox.RemoveRender();

            // Draw first cards
            card = DrawCard(ref rng, ref CardPoll, DealerHand.Count, 23);
            card.RenderSprite();
            DealerHand.Add(card.Clone());
            Thread.Sleep(200);

            card = DrawCard(ref rng, ref CardPoll, PlayerHand.Count, 129);
            card.RenderSprite();
            PlayerHand.Add(card.Clone());
            Thread.Sleep(200);

            card         = DrawCard(ref rng, ref CardPoll, DealerHand.Count, 23);
            card.Flipped = true;
            card.RenderSprite();
            DealerHand.Add(card.Clone());
            card.Flipped = false;
            Thread.Sleep(200);

            card = DrawCard(ref rng, ref CardPoll, PlayerHand.Count, 129);
            card.RenderSprite();
            PlayerHand.Add(card.Clone());
            Thread.Sleep(200);

            // Check for a Blackjack
            if (PlayerHand[0].Value + PlayerHand[1].Value == 21)
            {
                msgBox.RenderSprite();
                WriteText("Blackjack!", screenWidth / 2, screenHeight / 2 - 1);
                WriteText("You win!", screenWidth / 2, screenHeight / 2);

                Console.SetCursorPosition(0, 0);
                Environment.Exit(0);
            }

            // Player loop
            while (true)
            {
Hit:
                int temp = 0;

                foreach (Card drawn in PlayerHand)
                {
                    temp += drawn.Value;
                }

                if (temp > 21)
                {
                    msgBox.RenderSprite();
                    WriteText("You lose!", screenWidth / 2, screenHeight / 2);

                    Console.SetCursorPosition(0, 0);
                    Environment.Exit(0);
                }

                msgBox.RenderSprite();
                WriteText("Hit(1) or Stay(2)?", screenWidth / 2, screenHeight / 2);

                while (true)
                {
                    if (Keyboard.IsKeyPressed(ConsoleKey.D1))
                    {
                        msgBox.RemoveRender();

                        card = DrawCard(ref rng, ref CardPoll, PlayerHand.Count, 129);
                        card.RenderSprite();
                        PlayerHand.Add(card.Clone());
                        Thread.Sleep(200);

                        goto Hit;
                    }
                    else if (Keyboard.IsKeyPressed(ConsoleKey.D2))
                    {
                        msgBox.RemoveRender();
                        break;
                    }
                }

                break;
            }

            int PlayerSum = 0;

            foreach (Card drawn in PlayerHand)
            {
                PlayerSum += drawn.Value;
            }

            DealerHand[1].Flipped = false;
            DealerHand[1].RenderSprite();

            // Dealer loop
            while (true)
            {
Hit:
                int temp = 0;

                foreach (Card drawn in DealerHand)
                {
                    temp += drawn.Value;
                }

                if (temp >= 17)
                {
                    break;
                }

                card = DrawCard(ref rng, ref CardPoll, DealerHand.Count, 23);
                card.RenderSprite();
                DealerHand.Add(card.Clone());
                Thread.Sleep(500 + rng.Next(1, 500));
            }

            int DealerSum = 0;

            foreach (Card drawn in DealerHand)
            {
                DealerSum += drawn.Value;
            }

            int PlayerState = PlayerSum == 21 ? 1 : 0;
            int DealerState = DealerSum == 21 ? 1 : 0;

            if (DealerSum > 21)
            {
                DealerState = 2;
            }

            // Decide winner
            switch (DealerState)
            {
            case 0:     // Less than 21
                switch (PlayerState)
                {
                case 0:
                    if (PlayerSum > DealerSum)
                    {
                        msgBox.RenderSprite();
                        WriteText("You win!", screenWidth / 2, screenHeight / 2);
                    }
                    else
                    {
                        msgBox.RenderSprite();
                        WriteText("You lose!", screenWidth / 2, screenHeight / 2);
                    }
                    break;

                case 1:
                    msgBox.RenderSprite();
                    WriteText("You win!", screenWidth / 2, screenHeight / 2);
                    break;
                }
                break;

            case 1:     // 21
                switch (PlayerState)
                {
                case 0:
                    msgBox.RenderSprite();
                    WriteText("You lose!", screenWidth / 2, screenHeight / 2);
                    break;

                case 1:
                    msgBox.RenderSprite();
                    WriteText("You win!", screenWidth / 2, screenHeight / 2);
                    break;
                }
                break;

            case 2:     // More than 21
                msgBox.RenderSprite();
                WriteText("You win!", screenWidth / 2, screenHeight / 2);
                break;
            }

            Console.SetCursorPosition(0, 0);
            Environment.Exit(0);
        }