public Game(Random random, GameConfig gameConfig, IGameLog gameLog) { this.random = random; this.gameConfig = gameConfig; this.gameLog = gameLog; this.gameIndex = Game.RecycledGameIndices.GetInteger(); }
public static void FindBestStrategyForGame(GameConfig gameConfig) { var initialDescription = new PickByPriorityDescription(new CardAcceptanceDescription[] { new CardAcceptanceDescription( Cards.Province, new MatchDescription[] { new MatchDescription( null, CountSource.None, Comparison.None, 0)}), new CardAcceptanceDescription( Cards.Gold, new MatchDescription[] { new MatchDescription( null, CountSource.None, Comparison.None, 0)}), new CardAcceptanceDescription( Cards.Silver, new MatchDescription[] { new MatchDescription( null, CountSource.None, Comparison.None, 0)}) }); Random random = new Random(); Card[] supplyCards = gameConfig.GetSupplyPiles(2, random).Select(pile => pile.ProtoTypeCard).ToArray(); var initialPopulation = Enumerable.Range(0, 10).Select(index => initialDescription).ToArray(); var algorithm = new GeneticAlgorithm<PickByPriorityDescription, MutatePickByPriorityDescription, CompareStrategies>( initialPopulation, new MutatePickByPriorityDescription(random, supplyCards), new CompareStrategies(), new Random()); for (int i = 0; i < 1000; ++i) { System.Console.WriteLine("Generation {0}", i); System.Console.WriteLine("==============", i); for (int j = 0; j < 10; ++j) { algorithm.currentMembers[j].Write(System.Console.Out); System.Console.WriteLine(); } algorithm.RunOneGeneration(); System.Console.WriteLine(); } }
public GameState( IGameLog gameLog, IPlayerAction[] players, GameConfig gameConfig, Random random, IEnumerable<CardCountPair>[] startingDeckPerPlayer = null) { int playerCount = players.Length; this.gameLog = gameLog; this.cardGameSubset = gameConfig.cardGameSubset; this.supplyPiles = gameConfig.GetSupplyPiles(playerCount, random); this.nonSupplyPiles = gameConfig.GetNonSupplyPiles(); this.mapCardToPile = new MapOfCards<PileOfCards>(this.cardGameSubset); this.BuildMapOfCardToPile(); this.players = new PlayerCircle(playerCount, players, this.gameLog, random, this.cardGameSubset); this.hasPileEverBeenGained = new MapPileOfCardsToProperty<bool>(this.supplyPiles); this.pileEmbargoTokenCount = new MapPileOfCardsToProperty<int>(this.supplyPiles); this.trash = new BagOfCards(this.cardGameSubset); this.GainStartingCards(gameConfig); this.players.AllPlayersDrawInitialCards(gameConfig); foreach (PileOfCards cardPile in this.supplyPiles) { cardPile.ProtoTypeCard.DoSpecializedSetupIfInSupply(this); } }
public StrategyComparison( PlayerAction player1, PlayerAction player2, GameConfig gameConfig, bool rotateWhoStartsFirst, int numberOfGames) : this(new PlayerAction[] { player1, player2}, gameConfig, rotateWhoStartsFirst, numberOfGames) { }
public GameConfigBuilder(GameConfig gameConfig) { this.useShelters = gameConfig.useShelters; this.useColonyAndPlatinum = gameConfig.useColonyAndPlatinum; this.baneCard = gameConfig.baneCard; this.kingdomPiles = new List<Card>(gameConfig.kingdomPiles); this.events = new List<Card>(gameConfig.gameDescription.events); this.startingDeck = gameConfig.startingDeck; this.shuffleLuck = gameConfig.startingHand; }
public StrategyComparison( PlayerAction[] playerActions, GameConfig gameConfig, bool rotateWhoStartsFirst, int numberOfGames) { this.playerActions = playerActions; this.gameConfig = gameConfig; this.rotateWhoStartsFirst = rotateWhoStartsFirst; this.numberOfGames = numberOfGames; }
public double ComparePlayers( PlayerAction[] playerActions, GameConfig gameConfig, bool rotateWhoStartsFirst = true, bool shouldParallel = true, bool showVerboseScore = true, bool showCompactScore = false, bool showDistribution = false, bool createHtmlReport = true, bool createRankingReport = false, int numberOfGames = 1000, int logGameCount = 100, bool debugLogs = false, CreateGameLog createGameLog = null) { var strategyComparison = new StrategyComparison(playerActions, gameConfig, rotateWhoStartsFirst, numberOfGames); var results = strategyComparison.ComparePlayers( gameIndex => gameIndex < logGameCount ? GetGameLogWriterForIteration(playerActions, gameIndex) : null, gameIndex => debugLogs && gameIndex < logGameCount ? GetDebugLogWriterForIteration(playerActions, gameIndex) : null, shouldParallel: shouldParallel, gatherStats: createHtmlReport, createGameLog: createGameLog); if (showVerboseScore) { results.WriteVerboseScore(System.Console.Out); } if (showCompactScore) { results.WriteCompactScore(System.Console.Out); } if (showDistribution) { results.ShowDistribution(System.Console.Out); } if (createHtmlReport) { deferredHtmlGenerator.AddResults(results, GetOutputFilename); } if (createRankingReport) { this.resultRanker.AddResult(playerActions[0].PlayerName, playerActions[1].PlayerName, results.WinDifference); } return results.WinDifference; }
public static GameConfig CreateFromWithPlayPositionsSwapped(GameConfig gameConfig) { var result = new GameConfigBuilder(gameConfig); result.SwapPlayerOneAndTwo(); return result.ToGameConfig(); }
public GameConfigBuilder(GameConfig gameConfig) { this.useShelters = gameConfig.useShelters; this.useColonyAndPlatinum = gameConfig.useColonyAndPlatinum; this.kingdomPiles = gameConfig.kingdomPiles; this.startingDeck = gameConfig.startingDeck; this.startingHand = gameConfig.startingHand; }
public void AllPlayersDrawInitialCards(GameConfig gameConfig, GameState gameState) { foreach (PlayerState playerState in this.players) { IEnumerable<CardCountPair> shuffleLuck = gameConfig.ShuffleLuck(playerState.PlayerIndex); if (shuffleLuck != null) { playerState.shuffleLuck = GetCardsInOrder(shuffleLuck).GetEnumerator(); } playerState.DrawUntilCountInHand(5, gameState); } }
public void AllPlayersDrawInitialCards(GameConfig gameConfig) { for (int playerIndex = 0; playerIndex < this.players.Length; ++playerIndex) { PlayerState playerState = this.players[playerIndex]; IEnumerable<CardCountPair> startingHand = gameConfig.StartingHand(playerIndex); if (startingHand == null) { playerState.DrawUntilCountInHand(5); } else { playerState.DrawCardsIntoHand(startingHand); } } }
private static CardCountPair[] GetCardsForSplit(StartingCardSplit split, GameConfig gameConfig) { switch (split) { case StartingCardSplit.Random: return null; case StartingCardSplit.Split52: return Starting52Split; case StartingCardSplit.Split43: return gameConfig.useShelters ? Starting43SplitShelter : Starting43SplitEstate; case StartingCardSplit.Split25: return gameConfig.useShelters ? Starting25SplitShelter : Starting25Split; case StartingCardSplit.Split34: return gameConfig.useShelters ? Starting34SplitShelter : Starting34SplitEstate; default: throw new Exception(); } }
private void GainStartingCards(GameConfig gameConfig) { for (int playerIndex = 0; playerIndex < this.players.PlayerCount; ++playerIndex) { PlayerState player = this.players[playerIndex]; foreach (CardCountPair pair in gameConfig.StartingDeck(playerIndex)) { if (pair.Card.isShelter) { player.GainCard(this, pair.Card, DeckPlacement.Supply); } else { player.GainCardsFromSupply(this, pair.Card, pair.Count); } } } }
private void GainStartingCards(GameConfig gameConfig) { foreach (PlayerState player in this.players.AllPlayers) { foreach (CardCountPair pair in gameConfig.StartingDeck(player.PlayerIndex)) { if (pair.Card.isShelter) { player.GainCard(this, pair.Card, DeckPlacement.GameStart); } else { player.GainCardsFromSupply(this, pair.Card, pair.Count); } } } }
static IEnumerable<CardCountPair> GetDefaultStartingDeck(int playerPosition, GameConfig gameConfig) { return gameConfig.useShelters ? ShelterStartingDeck : EstateStartingDeck; }
static IEnumerable <CardCountPair> GetDefaultStartingDeck(int playerPosition, GameConfig gameConfig) { return(gameConfig.useShelters ? ShelterStartingDeck : EstateStartingDeck); }
static IEnumerable<CardCountPair> GetDefaultStartingHand(int playerPosition, GameConfig gameConfig) { return null; }
static IEnumerable <CardCountPair> GetDefaultStartingHand(int playerPosition, GameConfig gameConfig) { return(null); }
public void SimulateGameButtonClick() { if (!CanSimulateStrategies()) { return; } this.StrategyResultsAvailable.Value = false; var uiScheduler = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext(); Dominion.Strategy.Description.StrategyDescription player1Descr = this.player1Strategy.ConvertToDominionStrategy(); Dominion.Strategy.Description.StrategyDescription player2Descr = this.player2Strategy.ConvertToDominionStrategy(); Dominion.GameConfig gameConfig = this.GetGameConfig(); System.Diagnostics.Debug.WriteLine("Player 1: "); System.Diagnostics.Debug.WriteLine(player1Descr.ToString()); System.Diagnostics.Debug.WriteLine("Player 2: "); System.Diagnostics.Debug.WriteLine(player2Descr.ToString()); System.Threading.Tasks.Task <StrategyUIResults> .Factory.StartNew(() => { string player1nameAppend, player2nameAppend; GetStrategyNames(player1Descr, player2Descr, out player1nameAppend, out player2nameAppend); string player1Name = "Player 1 " + (!string.IsNullOrEmpty(player1nameAppend) ? "(" + player1nameAppend + ")" : ""); string player2Name = "Player 2 " + (!string.IsNullOrEmpty(player2nameAppend) ? "(" + player2nameAppend + ")" : ""); var playerActions = new Dominion.Strategy.PlayerAction[] { player1Descr.ToPlayerAction(player1Name), player2Descr.ToPlayerAction(player2Name) }; bool rotateWhoStartsFirst = true; int numberOfGames = 1000; var strategyComparison = new Dominion.Data.StrategyComparison(playerActions, gameConfig, rotateWhoStartsFirst, numberOfGames); Dominion.Data.StrategyComparisonResults strategyComparisonResults = strategyComparison.ComparePlayers(randomSeed: MainPage.random.Next(), shouldParallel: false); return(new StrategyUIResults() { strategyComparisonResults = strategyComparisonResults, Player1Name = player1Name, Player2Name = player2Name, Player1WinPercent = strategyComparisonResults.PlayerWinPercent(0), Player2WinPercent = strategyComparisonResults.PlayerWinPercent(1), TiePercent = strategyComparisonResults.TiePercent, }); }).ContinueWith(async(continuation) => { var results = (StrategyUIResults)continuation.Result; this.strategyReportDirty = true; this.strategyComparisonResults = results.strategyComparisonResults; this.Player1Name.Value = results.Player1Name; this.Player2Name.Value = results.Player2Name; this.Player1WinPercent.Value = results.Player1WinPercent; this.Player2WinPercent.Value = results.Player2WinPercent; this.TiePercent.Value = results.TiePercent; this.StrategyResultsAvailable.Value = true; }, uiScheduler); }
public double ComparePlayers( PlayerAction player1, PlayerAction player2, GameConfig gameConfig, bool rotateWhoStartsFirst = true, bool shouldParallel = true, bool showVerboseScore = true, bool showCompactScore = false, bool showDistribution = false, bool createHtmlReport = true, bool createRankingReport = false, int numberOfGames = 1000, int logGameCount = 100, bool debugLogs = false, CreateGameLog createGameLog = null) { return ComparePlayers(new PlayerAction[] { player1, player2 }, gameConfig, rotateWhoStartsFirst, shouldParallel, showVerboseScore, showCompactScore, showDistribution, createHtmlReport, createRankingReport, numberOfGames, logGameCount, debugLogs, createGameLog); }