示例#1
0
        private CellSign RunSingle(CellSign firstPlayerSign)
        {
            var firstPlayerKind  = firstPlayerSign == CellSign.X ? _firstBotKind : _secondBotKind;
            var secondPlayerKind = firstPlayerSign == CellSign.X ? _secondBotKind : _firstBotKind;

            try
            {
                var config = new GameConfiguration
                {
                    BotTurnLength = _botTurnLength,
                    FirstPlayer   = BotFactory.BuildBot(firstPlayerKind),
                    SecondPlayer  = BotFactory.BuildBot(secondPlayerKind),
                    Height        = _height,
                    Width         = _width
                };

                var game = Game.CreateNewGame(config);
                if (game == null)
                {
                    SetFailedToCreate();
                    return(CellSign.Empty);
                }

                var gameEndedEventArgs = game.RunSilently();
                return(GameStateToSign(gameEndedEventArgs.State));
            }
            catch (Exception ex)
            {
                LogException(LogLevel.Warning, ex);
                SetFailedToCreate();
                return(CellSign.Empty);
            }
        }
        private void FillBotDict()
        {
            var botEnumValues = Enum.GetValues(typeof(BotKind)).Cast <BotKind>().ToList();

            foreach (var botEnumValue in botEnumValues)
            {
                var bot  = BotFactory.BuildBot(botEnumValue);
                var name = bot.Name ?? botEnumValue.ToString();
                if (name != HumanPlayerName)
                {
                    _botKindToNameDict.Add(botEnumValue, name);
                }
            }
        }
        private void SecondPlayerComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SecondPlayerComboBox.SelectedItem == null)
            {
                return;
            }

            _secondPlayerString = (string)SecondPlayerComboBox.SelectedItem;
            if (_secondPlayerString != HumanPlayerName)
            {
                var botKind = _botKindToNameDict.First(kv => kv.Value == _secondPlayerString).Key;
                var bot     = BotFactory.BuildBot(botKind);
                SecondPlayerNameTextBox.IsEnabled = false;
                _secondPlayerCachedName           = SecondPlayerNameTextBox.Text;
                _secondPlayer = bot;
                SecondPlayerNameTextBox.Text = bot.Name;
            }
            else
            {
                SecondPlayerNameTextBox.Text = _secondPlayerCachedName;
                _secondPlayer = new HumanPlayer(_secondPlayerCachedName);
                SecondPlayerNameTextBox.IsEnabled = true;
            }
        }