HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Pitting two Blackjack bots against each other

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pittingeachagainstbotstwootherblackjack

Problem

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.

Solution

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


}

Code Snippets

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

Context

StackExchange Code Review Q#85723, answer score: 2

Revisions (0)

No revisions yet.