Last active
November 13, 2017 14:37
-
-
Save winteryoung/02b90381c5bfbc616217677f0527e8f8 to your computer and use it in GitHub Desktop.
Black peach card probability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.winteryoung.lab; | |
import com.google.common.collect.*; | |
import org.springframework.boot.ApplicationArguments; | |
import org.springframework.boot.ApplicationRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
/** | |
* @author Winter Young | |
* @since 2017/11/9 | |
*/ | |
@SpringBootApplication | |
public class Application implements ApplicationRunner { | |
public static void main(String[] args) throws Exception { | |
SpringApplication application = new SpringApplication(Application.class); | |
application.run(args); | |
} | |
private enum Player { | |
EAST, | |
SOUTH, | |
WEST, | |
NORTH | |
} | |
private enum CardType { | |
BLACK_PEACH, | |
OTHER | |
} | |
private static class Card { | |
private CardType cardType; | |
private Card(CardType cardType) { | |
this.cardType = cardType; | |
} | |
} | |
private static class PlayerStat { | |
private Player player; | |
private Multiset<CardType> cardTypeMultiset; | |
private PlayerStat(Player player, Multiset<CardType> cardTypeMultiset) { | |
this.player = player; | |
this.cardTypeMultiset = cardTypeMultiset; | |
} | |
} | |
private static class PlayerBlackPeachStat { | |
private Player player; | |
private int count; | |
private PlayerBlackPeachStat(Player player, int count) { | |
this.player = player; | |
this.count = count; | |
} | |
} | |
private static class Counter { | |
private long baseCounter = 0; | |
private long eastHas3BlackPeachCounter = 0; | |
} | |
@Override | |
public void run(ApplicationArguments args) throws Exception { | |
List<Card> cards = generateCards(); | |
Counter counter = new Counter(); | |
for (int iteration = 0; iteration < 10_000; iteration++) { | |
doRound(cards, counter); | |
} | |
System.out.println((double) counter.eastHas3BlackPeachCounter / counter.baseCounter); | |
} | |
private void doRound(List<Card> cards, Counter counter) { | |
Collections.shuffle(cards); | |
Multimap<Player, Card> playerDecks = HashMultimap.create(); | |
List<List<Card>> partitions = Lists.partition(cards, 13); | |
for (int i = 0; i < 4; i++) { | |
Player player = Player.values()[i]; | |
playerDecks.putAll(player, partitions.get(i)); | |
} | |
List<PlayerStat> playerStats = statPlayers(playerDecks); | |
Map<Player, Integer> playerBlackPeachStats = | |
playerStats | |
.stream() | |
.filter(ps -> ps.cardTypeMultiset.count(CardType.BLACK_PEACH) > 0) | |
.map(this::countPlayerBlackBeach) | |
.collect(Collectors.toMap(s -> s.player, s -> s.count)); | |
int northCount = playerBlackPeachStats.getOrDefault(Player.NORTH, 0); | |
int southCount = playerBlackPeachStats.getOrDefault(Player.SOUTH, 0); | |
if (northCount + southCount == 8) { | |
counter.baseCounter += 1; | |
int eastCount = playerBlackPeachStats.getOrDefault(Player.EAST, 0); | |
if (eastCount == 3) { | |
counter.eastHas3BlackPeachCounter += 1; | |
} | |
} | |
} | |
private PlayerBlackPeachStat countPlayerBlackBeach(PlayerStat ps) { | |
int count = ps.cardTypeMultiset.count(CardType.BLACK_PEACH); | |
return new PlayerBlackPeachStat(ps.player, count); | |
} | |
private List<PlayerStat> statPlayers(Multimap<Player, Card> playerDecks) { | |
return playerDecks | |
.asMap() | |
.entrySet() | |
.stream() | |
.map(this::statPlayer) | |
.collect(Collectors.toList()); | |
} | |
private PlayerStat statPlayer(Map.Entry<Player, Collection<Card>> entry) { | |
Player player = entry.getKey(); | |
Collection<Card> cards = entry.getValue(); | |
Multiset<CardType> cardTypeMultiset = HashMultiset.create(); | |
PlayerStat stat = new PlayerStat(player, cardTypeMultiset); | |
for (Card card : cards) { | |
cardTypeMultiset.add(card.cardType); | |
} | |
return stat; | |
} | |
private List<Card> generateCards() { | |
List<Card> cards = Lists.newArrayListWithCapacity(52); | |
for (int i = 0; i < 13; i++) { | |
cards.add(new Card(CardType.BLACK_PEACH)); | |
} | |
for (int i = 0; i < 52 - 13; i++) { | |
cards.add(new Card(CardType.OTHER)); | |
} | |
return cards; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment