3
\$\begingroup\$

I'm taking part in this Daily Challenge on http://www.reddit.com/r/dailyprogrammer/comments/29zut0/772014_challenge_170_easy_blackjack_checker/

Basically, you have to develop a program that runs two blackjack players and outputs the winner, the user has no input on the player's choices - basically it's all AI based. I'm using the CASE statements to check for the card that the player receives, but I find it really inefficient, there's too much code for such a small program - especially when I compare it with others on the post.

Could anyone help me shorten the code, maybe suggest an alternative to the CASES?

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

namespace BlackJack_Winner
{
    class Program
    {
        //Variables
        static Random randomRunner = new Random();
        static int randOut, player1Count, player2count;


        //Player 2's method, contains algorithm to calculate their hand
        public static void player2Cases()
        {
            //Loop to give the player two cards
            for (int i = 0; i <= 1; i++)
            {

                randOut = randomRunner.Next(1, 11);
                switch (randOut)
                {
                    case 1:
                        Console.WriteLine("Player 2 has an ACE");
                        if (player2count <= 10) //If player 2's hand is less than or equal to 10, then the player classes the ACE as an 11
                        {
                            player2count = player2count + 11;
                        }
                        else //If player2's hand is higher than 10, then they class the ACE as a 1
                        {
                            player2count = player2count + 1;
                        }
                        break;
                    case 2: //From here on out, the cases each add the amount, from the card that the player drew.
                        Console.WriteLine("Player 2 has a 2");
                        player2count = player2count + 2;
                        break;
                    case 3:
                        Console.WriteLine("Player 2 has a 3");
                        player2count = player2count + 3;
                        break;
                    case 4:
                        Console.WriteLine("Player 2 has a 4");
                        player2count = player2count + 4;
                        break;
                    case 5:
                        Console.WriteLine("Player 2 has a 5");
                        player2count = player2count + 5;
                        break;
                    case 6:
                        Console.WriteLine("Player 2 has a 6");
                        player2count = player2count + 6;
                        break;
                    case 7:
                        Console.WriteLine("Player 2 has a 7");
                        player2count = player2count + 7;
                        break;
                    case 8:
                        Console.WriteLine("Player 2 has an 8");
                        player2count = player2count + 8;
                        break;
                    case 9:
                        Console.WriteLine("Player 2 has a 9");
                        player2count = player2count + 9;
                        break;
                    case 10:
                        Console.WriteLine("Player 2 has a 10");
                        player2count = player2count + 10;
                        break;
                    default:
                        Console.WriteLine("");
                        break;

                }
                if (i == 1) //Prints out the player's final hand when both cards have been drawn
                {
                    Console.WriteLine(player2count);
                }

            }


        }


        //Player 1's method, contains algorithm to calculate their hand
        public static void player1Cases()
        {
            //Loop to draw the player just two cards, as per standard BlackJack game
            for (int i = 0; i <= 1; i++ )
            {

                randOut = randomRunner.Next(1, 11);//random number between 1 and 11
                switch (randOut)
                {
                    case 1:
                        Console.WriteLine("Player 1 has an ACE");
                        if(player1Count <= 10)
                        {
                            player1Count = player1Count + 11;
                        }
                        else
                        {
                            player1Count = player1Count + 1;
                        }
                        break;
                    case 2:
                        Console.WriteLine("Player 1 has a 2");
                        player1Count = player1Count + 2;
                        break;
                    case 3:
                        Console.WriteLine("Player 1 has a 3");
                        player1Count = player1Count + 3;
                        break;
                    case 4:
                        Console.WriteLine("Player 1 has a 4");
                        player1Count = player1Count + 4;
                        break;
                    case 5:
                        Console.WriteLine("Player 1 has a 5");
                        player1Count = player1Count + 5;
                        break;
                    case 6:
                        Console.WriteLine("Player 1 has a 6");
                        player1Count = player1Count + 6;
                        break;
                    case 7:
                        Console.WriteLine("Player 1 has a 7");
                        player1Count = player1Count + 7;
                        break;
                    case 8:
                        Console.WriteLine("Player 1 has an 8");
                        player1Count = player1Count + 8;
                        break;
                    case 9:
                        Console.WriteLine("Player 1 has a 9");
                        player1Count = player1Count + 9;
                        break;
                    case 10:
                        Console.WriteLine("Player 1 has a 10");
                        player1Count = player1Count + 10;
                        break;
                    default:
                        Console.WriteLine("");
                        break;

                }
                if(i == 1)
                {
                    Console.WriteLine(player1Count);
                }


            }

        }

        //Main method
        static void Main(string[] args)
        {
            //Calls methods
            player1Cases();
            player2Cases();

            if(player1Count < player2count)
            {
                Console.WriteLine("Player 1 wins!");
            }
            else
            {
                Console.WriteLine("Player 2 wins!");
            }

            //Waits for user input
            Console.ReadLine();

        }
    }
}
\$\endgroup\$

migrated from programmers.stackexchange.com Apr 2 '15 at 20:05

This question came from our site for professionals, academics, and students working within the systems development life cycle.

2
\$\begingroup\$

You can shorten switch to

            randOut = randomRunner.Next(1, 11);
            if (randOut == 1 && player2count <= 10)
            {
                Console.WriteLine("Player 2 has ACE");
                player2count += 11;
            }
            else
            {
                Console.WriteLine("Player 2 has {0}", randOut == 1? "ACE" : randOut.ToString());
                player2count += randOut;
            }

edit: you can also remove duplicated methods (whole class)

class Program
{
    private static readonly Random randomRunner = new Random();

    public static int drawCard(string playerName)
    {
        var playerCount = 0;
        for (int i = 0; i <= 1; i++)
        {
            var randOut = randomRunner.Next(1, 11);
            if (randOut == 1 && playerCount <= 10)
            {
                Console.WriteLine("{0} has ACE", playerName);
                playerCount += 11;
            }
            else
            {
                Console.WriteLine("{0} has {1}", playerName, randOut == 1? "ACE" : randOut.ToString());
                playerCount += randOut;
            }

        }
        Console.WriteLine(playerCount);
        return playerCount;
    }

    static void Main()
    {
        var player1Count = drawCard("Player 1");
        var player2Count = drawCard("Player 2");

        if (player1Count < player2Count)
        {
            Console.WriteLine("Player 1 wins!");
        }
        else
        {
            Console.WriteLine("Player 2 wins!");
        }

        Console.ReadLine();
    }
}

}

\$\endgroup\$
  • 2
    \$\begingroup\$ So rid of all of the CASE statements and replace them with IF-ELSE? \$\endgroup\$ – DunderMifflin11 Apr 2 '15 at 19:59
  • \$\begingroup\$ I could have thought of that, I'm looking for ways to shorten the code, that of which won't do much. \$\endgroup\$ – DunderMifflin11 Apr 2 '15 at 20:02
  • 2
    \$\begingroup\$ Then why didn't you think of that? It shortens it greatly. \$\endgroup\$ – eidsonator Apr 2 '15 at 20:04
  • \$\begingroup\$ Could you please annotate some parts of this, a lot of the procedures are new to me. \$\endgroup\$ – DunderMifflin11 Apr 2 '15 at 20:11
  • 1
    \$\begingroup\$ @DunderMifflin11 I think looking at the docs for String.Format and the ternary conditional will clear up a lot of your questions. \$\endgroup\$ – RubberDuck Apr 2 '15 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.