Skip to content

Instantly share code, notes, and snippets.

@KevinTyrrell
Created September 14, 2016 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinTyrrell/4d2db1b855c080276204efb2f7769c8b to your computer and use it in GitHub Desktop.
Save KevinTyrrell/4d2db1b855c080276204efb2f7769c8b to your computer and use it in GitHub Desktop.
import java.util.Collections;
import java.util.LinkedList;
import java.util.Random;
import java.util.Stack;
class Toast
{
public static void main(String[] args)
{
final long SIMULATION_ATTEMPTS = 10000000L;
final int DECK_SIZE = 30, SHUFFLES = 3, FIRST_STARTING_CARDS = 3,
SECOND_STARTING_CARDS = 4;
final byte barnes = 1, yshaarj = -1;
Random generator = new Random();
long victories = 0;
for (long i = 0; i < SIMULATION_ATTEMPTS; i++)
{
LinkedList<Byte> deck = new LinkedList<>(), hand = new LinkedList<>();
deck.push(barnes);
deck.push(yshaarj);
for (int h = 0; h < DECK_SIZE - 2; h++)
deck.push((byte)0); // Non-important card.
for (int h = 0; h < SHUFFLES; h++)
Collections.shuffle(deck);
// HS Deck is now shuffled well.
// Different amount of cards depending on who goes first and second.
boolean goFirst = generator.nextBoolean();
for (int h = 0; h < (goFirst ? FIRST_STARTING_CARDS : SECOND_STARTING_CARDS); h++)
hand.push(deck.pop());
// Muligan for barnes, but get rid of Y'shaarj.
boolean have_yshaarj = hand.contains(yshaarj),
have_barnes = hand.contains(barnes);
Stack<Byte> temp = new Stack<>();
if (!have_barnes)
while (!hand.isEmpty())
temp.push(hand.pop());
else if (have_yshaarj)
{
// I can't use remove() because it think yshaarj is an index.
hand.removeFirstOccurrence(yshaarj);
temp.push(yshaarj);
}
for (int h = 0; h < temp.size(); h++)
hand.push(deck.pop());
// Put muliganned cards back into the deck.
deck.addAll(temp);
Collections.shuffle(deck);
// It's possible we just got barnes from a mulligan.
have_barnes = hand.contains(barnes);
for (Byte turn = 1, drawnCard = null; deck.size() > 0; turn++)
{
drawnCard = deck.pop();
// RIP in pepperonis.
if (drawnCard == yshaarj)
break;
else if (drawnCard == barnes)
have_barnes = true;
// Check for GOURGE YOUR HATRED. EMBRACE YOUR RAGE.
if (have_barnes && (turn >= 4 || (turn >= 3 && !goFirst)))
{
victories++;
break;
}
}
if ((i & 4095) == 0)
System.out.printf("%.1f%%: Toast has drawn Barnes before Y'sharaaj %.2f%% of the time.\r", 100 * (double)i/SIMULATION_ATTEMPTS, 100 * (double)victories/i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment