# Pitting two Blackjack bots against each other

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;

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

}
}
}


## migrated from programmers.stackexchange.comApr 2 '15 at 20:05

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

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!");
}


• @DunderMifflin11 I think looking at the docs for String.Format and the ternary conditional will clear up a lot of your questions. – RubberDuck Apr 2 '15 at 20:34