Beispiel #1
0
        public bool Start()
        {
            if (IsRunning)
            {
                return(false);
            }
            else
            {
                System.Diagnostics.Debug.Assert(Player1 != null, "Player 1 should be assigned");
                System.Diagnostics.Debug.Assert(Player2 != null, "Player 2 should be assigned");
                System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(MapName), "MapName should be assigned");

                if (MaxTurns <= 0)
                {
                    MaxTurns = 200;
                }
                Aborted   = false;
                IsRunning = true;

                TurnsPlayed    = 0;
                PlayerWinnerId = 0;
                try
                {
                    engine = new Game(MapName, MaxTurns, 0, null);
                    engine.Init();
                    List <botDebugBase> players = new List <botDebugBase>();
                    players.Add(Player1);
                    players.Add(Player2);
                    IQueryable <botDebugBase> parallel = players.AsQueryable <botDebugBase>();
                    int turnsLeft = MaxTurns;

                    // I looked at the original engine, which had a lot of overhead because it was working with application instances
                    // and was capturing the output. Since I have a direct instance to the bot (wall, the debug wrapper) I can skip a lot of that
                    // and just use the bare essentials.
                    while (turnsLeft > 0 && (engine.Winner()) < 0)
                    {
                        if (!SilentGameEngine.CheckForAlliveOrDropPlayer(engine, players[0]))
                        {
                            PlayerWinnerId = 1;
                        }
                        if (!SilentGameEngine.CheckForAlliveOrDropPlayer(engine, players[1]))
                        {
                            PlayerWinnerId = 2;
                        }
                        if (PlayerWinnerId > 0)
                        {
                            break;
                        }


                        players.ForEach(player => player.GameBoardData = engine.PovRepresentation(player.Id));

                        foreach (botDebugBase player in parallel)
                        {
                            PushGameDataToPlayer(player);
                        }

                        turnsLeft--;
                        if (Aborted)
                        {
                            return(false);
                        }
                        else
                        {
                            TurnsPlayed++;
                            engine.FlushGamePlaybackString();
                            engine.DoTimeStep();

                            if (!SilentGameEngine.CheckForAlliveOrDropPlayer(engine, players[0]))
                            {
                                PlayerWinnerId = 1;
                            }
                            if (!SilentGameEngine.CheckForAlliveOrDropPlayer(engine, players[1]))
                            {
                                PlayerWinnerId = 2;
                            }
                            if (PlayerWinnerId > 0)
                            {
                                break;
                            }
                        }
                    }
                }
                finally
                {
                    IsRunning = false;
                }

                return(true);
            }
        }
        private void Start(string filename)
        {
            _NumberOfTurnsPlayed = 0;
            _CurrentGameEngine   = null;
            _Aborted             = false;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                //you forgot to chose a map, try again.
                MessageBox.Show("Please select a map on the \"Batch Run\" tavb first.");
                tabControlBotDebugger.SelectedTab = tabPageBatchRun;
                return;
            }
            InitializeColors();
            buttonPlay.Enabled = false;
            List <botDebugBase> players = new List <botDebugBase>();

            try
            {
                players.Add(GetBotInstance(cbOpponentOneOwnBot.Checked, comboBoxOpponent1.SelectedIndex, 1));
                players.Add(GetBotInstance(cbOpponentTwoOwnBot.Checked, comboBoxOpponent2.SelectedIndex, 2));

                int turnsLeft;

                if (!int.TryParse(textBoxTurns.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out turnsLeft))
                {
                    turnsLeft = 200;
                }


                _CurrentGameEngine = new Game(filename, turnsLeft, 0, null);
                _CurrentGameEngine.Init();

                //CalcTSM(cbOpponentOneOwnBot.Checked ? 1 : 2);
                ForceRender();
                IQueryable <botDebugBase> parallel = players.AsQueryable <botDebugBase>();
                // I looked at the original engine, which had a lot of overhead because it was working with application instances
                // and was capturing the output. Since I have a direct instance to the bot (wall, the debug wrapper) I can skip a lot of that
                // and just use the bare essentials.
                while (turnsLeft > 0 && _CurrentGameEngine.Winner() < 0)
                {
                    if (!SilentGameEngine.CheckForAlliveOrDropPlayer(_CurrentGameEngine, players[0]) &&
                        SilentGameEngine.CheckForAlliveOrDropPlayer(_CurrentGameEngine, players[1]))
                    {
                        return;
                    }

                    players.ForEach(player => player.GameBoardData = _CurrentGameEngine.PovRepresentation(player.Id));

                    if (checkBoxBreakpoint.Checked && _HitBreakPointOnTurnNumber == _NumberOfTurnsPlayed)
                    {
                        _PlayAtFullSpeed = false;
                        while (checkBoxBreakpoint.Checked && !_Aborted && !_ReleaseBreakpoint)
                        {
                            // Blink
                            labelBreakpoint.Visible = DateTime.Now.Millisecond % 300 < 100;
                            Application.DoEvents();
                        }
                        labelBreakpoint.Visible = false;
                    }
                    _ReleaseBreakpoint = false;

                    if (checkBoxParallel.Checked)
                    {
                        foreach (botDebugBase player in parallel)
                        {
                            PushGameDataToPlayer(player);
                        }
                    }
                    else
                    {
                        players.ForEach(PushGameDataToPlayer);
                    }

                    if (!SilentGameEngine.CheckForAlliveOrDropPlayer(_CurrentGameEngine, players[0]) &&
                        SilentGameEngine.CheckForAlliveOrDropPlayer(_CurrentGameEngine, players[1]))
                    {
                        return;
                    }

                    turnsLeft--;
                    if (_Aborted)
                    {
                        break;
                    }
                    else
                    {
                        _NumberOfTurnsPlayed++;
                        _CurrentGameEngine.FlushGamePlaybackString();
                        labelTurn.Text = _NumberOfTurnsPlayed.ToString();

                        _CurrentGameEngine.DoTimeStep();
                        if (_Aborted)
                        {
                            break;
                        }
                        #region Rendering and pausing
                        if (cbRender.Checked)
                        {
                            panelRender.Refresh();
                            Application.DoEvents();
                            if (!_PlayAtFullSpeed)
                            {
                                int value = -1 + trackBarRenderDelay.Value;
                                System.Threading.Thread.Sleep(value);
                            }
                        }
                        else
                        {
                            Application.DoEvents();
                        }
                        #endregion
                    }
                }
            }
            finally
            {
                ForceRender();
                buttonPlay.Enabled = true;
                if (_Restart)
                {
                    _Restart = false;
                    Start(filename);
                }
                else
                {
                    _PlayAtFullSpeed = false;
                }
            }
        }